public class Gobang {
Define a two-dimensional array to act as a chessboard
Private String [] board;
Define Checkerboard Size
private static int board_size = 15;
public void Initborad () {
Board=new String[board_size][board_size];
Assign each element "+" to draw a chessboard in the console
for (int i =0;i<board_size;i++) {
for (int j=0;j<board_size;j++) {
board[i][j]= "+";
}
}
}
How to output a checkerboard in the console
public void Printborad () {
Assign each element "+" to draw a chessboard in the console
for (int i =0;i<board_size;i++) {
for (int j=0;j<board_size;j++) {
Print array elements without wrapping
System.out.print (Board[i][j]);
}
Outputs a newline character after each line of array elements is printed
System.out.println ();
}
}
public static void Main (string[] args) throws IOException {
Gobang GB = new Gobang ();
Gb.initborad ();
Gb.printborad ();
This is the method for getting keyboard input
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
String Inputstr=null;
Whenever you enter a line of content on the keyboard and press ENTER, the content you just entered will be read by BR to
String text =br.readline ();
System.out.println (text);
while ((Inputstr=br.readline ())!=null) {
Separates the user-entered string into 2 strings with a comma (,) as a delimiter
String [] Possstrarr =inputstr.split (",");
Converts 2 strings to the coordinates of a user's chess game
int XPos =integer.parseint (possstrarr[0]);
int YPos =integer.parseint (possstrarr[1]);
The corresponding array element is assigned the ""
Gb.board[xpos][ypos]= "";
Gb.printborad ();
System.out.println ("Please enter the coordinates of your chess, should be in X, y format:");
}
}
}
Print five-word chess