Gobang Foundation algorithm and Victory judgment algorithm (no AI) __ algorithm

Source: Internet
Author: User
Tags readline

This article simply draws a chessboard, through the input coordinates (format example: 3,4) to play chess, then is the Victory judgment algorithm. My first algorithm will be the array of cross-border problems, that is, when the chess pieces on the side of the most side of the array will appear on the side of the exception, and then changed the big change, solve the problem. The following is the correct code:

Import Java.io.BufferedReader;

Import Java.io.InputStreamReader;

	public class Five {//defines the size of the chessboard private static int board_size=15;
	Define a two-dimensional array to display the chessboard private string[][] board;
	Defines a two-dimensional array for recording coordinates private int[][]xy=new int[225][2];
	Judge whether has won the private int[][]idx=new int[15][15];
	Count is the number of steps to win, and each type of victory should reset the count int count=0;
	X,y is the coordinates, Q is the Victory type, the ID to determine the point that has been experienced, each victory switch should also reset the Count Boolean win=false; public void issuccess (int x,int y,int q,int ID) {//The following entire condition is judged to determine whether the horizontal direction of the five sons wins//is crossed if (x<0| | x>=14| | y<0| |
				Y&GT;=14) return; Not a piece or visited grid if (idx[x][y]!=0| |
				Board[x][y]!= "") return;
				Idx[x][y]=id;
				count++;
				System.out.println ("Count" +count); if (count==5) {System.out.println ("You have won.")
					");
					Win=true;
				Return Switch (q) {//Horizontal Victory judgment case 0:for (int i=-1;i<2;i++) {if (i!=0) issuccess (X+i,y,
					0,ID);
				} break; Longitudinal victory judgment Case 1:for (int i=-1;i<2;i++) {if (i!=0)
							Issuccess (X,y+i,1,id);
				} break;
					From top left to bottom right case 2:for (int i=-1;i<2;i++) {if (i!=0) issuccess (X+i,y+i,2,id);
				} break;
					Case 3://below to determine the victory for (int i=-1;i<2;i++) {if (i!=0) issuccess (X-i,y+i,3,id) from the upper right to the left;
				
			} break;
		}///Initialize board public void Initboard () {board=new string[board_size][board_size];
				for (int i=0;i<board_size;i++) {for (int j=0;j<board_size;j++) {board[i][j]= "+";
			Board[i][j]= "➕"; The console output Checkerboard method public void Printboard () {for (int i=0;i<board_size;i++) {for int j=0;j<board_size;j+
			+) {System.out.print (board[i][j]);
		} System.out.println ();
		} public static void Main (String [] args) throws Exception {five gb=new five ();
		Gb.initboard ();
		Gb.printboard ();
		int id=1;
		int step=0;
		A method for obtaining keyboard input BufferedReader br=new BufferedReader (New InputStreamReader (system.in)); String Inputstr=nulL
			while ((Inputstr=br.readline ())!=null) {string[] Posstrarr=inputstr.split (",");
			int Xpos=integer.parseint (posstrarr[0]);
			int Ypos=integer.parseint (posstrarr[1]);
				if (gb.board[xpos-1][ypos-1]== "") {System.out.println ("The point already has a piece, please choose another landing");
				Gb.printboard ();
			Continue
			} gb.xy[step][0]=xpos-1;
			gb.xy[step][1]=ypos-1;
			step++;
			
			Gb.board[xpos-1][ypos-1]= "";
			Gb.printboard ();
			for (int i=0;i<step;i++) {to (int j=0;j<4;j++) {if (Gb.win) return;
		Gb.issuccess (Gb.xy[i][0], gb.xy[i][1], J, id++);  System.out.println ("i=" +i+);
			J= "+j);
			for (int a=0;a<15;a++) {for (int b=0;b<15;b++) gb.idx[a][b]=0;
			} gb.count=0;

				} System.out.println ("Please input your chess coordinates, should be in the form of X,y");

 }
			
			
			
		
		
		
	}	
	}
The following is the array of errors before the error of the code, although there are problems, but the idea is correct, also put it up.

Import Java.io.BufferedReader;

Import Java.io.InputStreamReader;

	public class Gobang {//Defines the size of the chessboard private static int board_size=15;
	Define a two-dimensional array to display the chessboard private string[][] board;
		Initialize board public void Initboard () {board=new string[board_size][board_size];
				for (int i=0;i<board_size;i++) {for (int j=0;j<board_size;j++) {board[i][j]= "+";
			Board[i][j]= "➕"; The console output Checkerboard method public void Printboard () {for (int i=0;i<board_size;i++) {for int j=0;j<board_size;j+
			+) {System.out.print (board[i][j]);
		} System.out.println ();
		} public static void Main (String [] args) throws Exception {Gobang gb=new gobang ();
		Gb.initboard ();
		Gb.printboard ();
		int [] px=new int[225];
		int [] Py=new int [225];
		int count=0;
		A method for obtaining keyboard input BufferedReader br=new BufferedReader (New InputStreamReader (system.in));
		String Inputstr=null;
			while ((Inputstr=br.readline ())!=null) {string[] Posstrarr=inputstr.split (","); int xpos=integer.parseint (Posstrarr[0]);
			int Ypos=integer.parseint (posstrarr[1]);
				if (gb.board[xpos-1][ypos-1]== "") {System.out.println ("The point already has a piece, please choose another landing");
				Gb.printboard ();
			Continue
			} px[count]=xpos-1;
			py[count]=ypos-1;
			Gb.board[xpos-1][ypos-1]= "";
			count++;
			
			 Gb.printboard ();
					if (count>=5) {for (int i=0;i<count;i++) {int x=px[i];
					
					int y=py[i]; The following entire condition is judged to determine the horizontal direction of the five-child victory if (x!=0&&x!=14&&y!=0&&y!=14) {if (gb.board[x-1][y]== "" | |
						gb.board[x+1][y]== "") {//For recording there are several points connected int s=1;
						int a,b;
						A=x-1;
						b=x+1;
							This condition is judged in order to avoid array bounds if (a!=0) {while (gb.board[a][y]== "") {s++;
							a--;
							if (a==0) {break;
								} if (s==5) {System.out.println ("You have won");
							Return
						Also to avoid array bounds if (b!=14) {while (gb.board[b][y]== "") {		s++;
								b++;
								if (a==14) {break;
									} if (s==5) {System.out.println ("You have won");
								Return The following judgment in order to judge the longitudinal victory if (gb.board[x][y-1]== "" | |
						gb.board[x][y+1]== "") {//For recording there are several points connected int s=1;
						int a=y-1;
						int b=y+1;
								if (a!=0) {while (gb.board[x][a]== "") {s++;
								a--;
								if (a==0) {break;
									} if (s==5) {System.out.println ("You have won");
								Return
								}} if (b!=14) {while (gb.board[x][b]== "") {s++;
								b++;
								if (a==14) {break;
									} if (s==5) {System.out.println ("You have won");
								Return }}//Below is used to determine the victory from top left to bottom right if (gb.board[x-1][y-1]== "" | |
						gb.board[x+1][y+1]== "") {//For recording there are several points connected int s=1;
						int a=x-1;
			int b=y-1;			if (a!=0&&b!=0) {while (gb.board[a][b]== "") {s++;
								a--;
								b--; if (a==0| |
								b==0) {break;
									} if (s==5) {System.out.println ("You have won");
								Return
						}} a=x+1;
						b=y+1;
								if (b!=14&&a!=14) {while (gb.board[x][b]== "") {s++;
								a++;
								b++; if (a==14| |
								B==14) {break;
									} if (s==5) {System.out.println ("You have won");
								Return }}//Below is used to determine the victory from right to left if (gb.board[x-1][y+1]== "" | |
						gb.board[x+1][y-1]== "") {//For recording there are several points connected int s=1;
						int a=x-1;
						int b=y+1;
								if (a!=0&&b!=14) {while (gb.board[a][b]== "") {s++;
								a--;
								b++; if (a==0| |
								B==14) {break;
} if (s==5) {System.out.println ("You have won");									Return
						}} a=x+1;
						B=y-1;
								if (b!=14&&a!=14) {while (gb.board[x][b]== "") {s++;
								a++;
								b--; if (a==14| |
								b==0) {break;
									} if (s==5) {System.out.println ("You have won");
								Return
			
			}else if (x==0) {if (y==0) {}}}}}
			
		SYSTEM.OUT.PRINTLN ("Please input your chess coordinates, should be in the form of X,y");
 }
		
		
	}

}



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.