Java Program-Console Gobang

Source: Internet
Author: User

Console Gobang, see code comments for specific ideas.

The code is as follows:


<span style= "FONT-SIZE:18PX;" >package test;import java.io.ioexception;import java.util.scanner;//Console simple Gobang public class Gobang {// Two-dimensional array as the chessboard private static char[][] Board=new char[16][16];//The number of pieces of private static int cnt;//represents the player's own private static char [] qizi={' # ', ' @ '};//eight direction search detection private static int[] dx={-1,0,-1,-1,1,1,0,1};p rivate static int[] dy={0,-1,-1,1,-1,1,1,0 };//input detects private static int checkinput (int x,int y) {if (board[x][y]!= ' + ') return 0;if (x<1| | x>15| | y<1| |        Y&GT;15) return 0;return 1;} Boundary detection private static int Check (int x,int y) {if (x>15| | x<1| | y>15| | Y&LT;1) return 0;return 1;}    Detects if the player wins, needs to pass in the player number, and the current placement coordinates private static int Checkans (int num,int x,int y) {if (cnt==15*15) return 1; Left oblique right oblique four direction to determine if there are five pieces joined together for (int i=0;i<4;i++) {int sum=1;int xx=x+dx[i];int yy=y+dy[i];while (Board[xx][yy]==qizi [Num]&&check (XX,YY) ==1) {sum++;xx=xx+dx[i];yy=yy+dy[i];} Xx=x+dx[7-i];yy=y+dy[7-i];while (Board[xx][yy]==qizi[num]&&check (xx,yy) ==1) {sum++;xx=xx+dx[7-i];yy=yY+dy[7-i];}    if (sum>=5) return 1; } return 0;} Initialize private static void, Init () {cnt=0;for (int i=1;i<=15;i++) for (int j=1;j<=15;j++) board[i][j]= ' + ';}    Output checkerboard private static void Print () {int i,j;    for (i=1;i<=15;i++) {for (j=1;j<=15;j++) {System.out.print (board[i][j]);    } System.out.println (); }}public static int run () {int x,y;init ();//current player number, default No. 0 number first under int num=0;while (TRUE) {Print (); System.out.print (num+1+ "Number of players please enter your chess coordinates, should be in X y form:"); Scanner SC =new Scanner (system.in); while (true) {x=sc.nextint (); Y=sc.nextint (); if (Checkinput (x, y) ==1) break; System.out.print ("Input coordinates are not valid, please re-enter:");} Board[x][y]=qizi[num];if (Checkans (num,x,y) ==1) {return num+1;} Num=1-num;}} public static void Main (string[] args) {System.out.println ("Welcome to LKL Gobang!"); int Ans=run (); System.out.println ("Congratulations to the +ans+" for the final Victory ");}} </span>


Java Program-Console Gobang

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.