在井字游戏中,两个玩家使用各自的标志(一方用 X 则另一方就用O),轮流填写3x3的网格中的某个空格。当一个玩家在网格的水平方向、垂直方向或者对角线方向上出现了三个相同的 X 或三个相同的 o时,游戏结束,该玩家获胜。平局(没有贏家)是指当网格中所有的空格都被填满时没有任何一方的玩家获胜的情况。创建一个玩井字游戏的程序。
while (true) {// X player round System.out.println("Enter a row (0, 1, 2) for player X: "); intx1= scanner.nextInt(); if (x1 > strings.length || x1 < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
System.out.println("Enter a column (0, 1, 2) for player X: "); inty1= scanner.nextInt(); if (y1 > strings.length || y1 < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
// If it is right if (strings[x1][y1] == " ") { strings[x1][y1] = "X"; break; } else { System.out.println("There was a piece, change another position"); continue; } }
/** * Decide if the game is over and print result * * @param strings The chess board decided * @return The result for the game, and print the result */ publicstaticbooleanisGameOver(String[][] strings) { // Row same for (inti=0; i < strings.length; i++) { if (strings[i][0].equals(strings[i][1]) && strings[i][1].equals(strings[i][2]) && strings[i][0] != " ") { System.out.println(strings[i][0] + " player won"); returntrue; } } // Column same for (inti=0; i < strings[0].length; i++) { if (strings[0][i].equals(strings[1][i]) && strings[1][i].equals(strings[2][i]) && strings[0][i] != " ") { System.out.println(strings[0][i] + " player won"); returntrue; } }
// / same the left angle. if (strings[0][0].equals(strings[1][1]) && strings[1][1].equals(strings[2][2]) && strings[2][2] != " ") { System.out.println(strings[0][0] + " player won"); returntrue; } // \ same the right angle if (strings[0][2].equals(strings[1][1]) && strings[1][1].equals(strings[2][0]) && strings[1][1] != " ") { System.out.println(strings[1][1] + " player won"); returntrue; }
// Full for (inti=0; i < strings.length; i++) { for (intj=0; j < strings[i].length; j++) { if (strings[i][j] == " ") returnfalse; } }
// Draw System.out.println("There is no space, Draw"); returntrue; }
// Create a Scanner, get users' input Scannerscanner=newScanner(System.in);
// Game running while (true) { while (true) {// X player round System.out.println("Enter a row (0, 1, 2) for player X: "); intx1= scanner.nextInt(); if (x1 > 2 || x1 < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
System.out.println("Enter a column (0, 1, 2) for player X: "); inty1= scanner.nextInt(); if (y1 > 2 || y1 < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
// If it is right if (strings[x1][y1] == " ") { strings[x1][y1] = "X"; break; } else { System.out.println("There was a piece, change another position"); continue; } } showPage(strings);
// Verify if game over if (isGameOver(strings)) { break; }
while (true) {// O player round System.out.println("Enter a row (0, 1, 2) for player O: "); intx2= scanner.nextInt(); if (x2 > 2 || x2 < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
System.out.println("Enter a column (0, 1, 2) for player O: "); inty2= scanner.nextInt(); if (y2 > 2 || y2 < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
// If it is right if (strings[x2][y2] == " ") { strings[x2][y2] = "O"; break; } else { System.out.println("There was a piece, change another position"); continue; } } showPage(strings);
// Verify if game over if (isGameOver(strings)) { break; } } scanner.close(); }
/** * Decide if the game is over and print result * * @param strings The chess board decided * @return The result for the game, and print the result */ publicstaticbooleanisGameOver(String[][] strings) { // Row same for (inti=0; i < strings.length; i++) { if (strings[i][0].equals(strings[i][1]) && strings[i][1].equals(strings[i][2]) && strings[i][0] != " ") { System.out.println(strings[i][0] + " player won"); returntrue; } } // Column same for (inti=0; i < strings[0].length; i++) { if (strings[0][i].equals(strings[1][i]) && strings[1][i].equals(strings[2][i]) && strings[0][i] != " ") { System.out.println(strings[0][i] + " player won"); returntrue; } }
// / same the left angle. if (strings[0][0].equals(strings[1][1]) && strings[1][1].equals(strings[2][2]) && strings[2][2] != " ") { System.out.println(strings[0][0] + " player won"); returntrue; } // \ same The right angle if (strings[0][2].equals(strings[1][1]) && strings[1][1].equals(strings[2][0]) && strings[1][1] != " ") { System.out.println(strings[1][1] + " player won"); returntrue; }
// Full for (inti=0; i < strings.length; i++) { for (intj=0; j < strings[i].length; j++) { if (strings[i][j] == " ") returnfalse; } }
// Draw System.out.println("There is no space, Draw"); returntrue; } }
/** * Decide if the game is over and print result * * @param strings The chess board decided * @return The result for the game, and print the result */ publicstaticbooleanisGameOver(String[][] strings) { // Row same for (inti=0; i < strings.length; i++) { if (strings[i][0].equals(strings[i][1]) && strings[i][1].equals(strings[i][2]) && strings[i][0] != " ") { System.out.println(strings[i][0] + " player won"); returntrue; } } // Column same for (inti=0; i < strings[0].length; i++) { if (strings[0][i].equals(strings[1][i]) && strings[1][i].equals(strings[2][i]) && strings[0][i] != " ") { System.out.println(strings[0][i] + " player won"); returntrue; } }
// / same the left angle. if (strings[0][0].equals(strings[1][1]) && strings[1][1].equals(strings[2][2]) && strings[2][2] != " ") { System.out.println(strings[0][0] + " player won"); returntrue; } // \ same The right angle if (strings[0][2].equals(strings[1][1]) && strings[1][1].equals(strings[2][0]) && strings[1][1] != " ") { System.out.println(strings[1][1] + " player won"); returntrue; }
// Full for (inti=0; i < strings.length; i++) { for (intj=0; j < strings[i].length; j++) { if (strings[i][j] == " ") returnfalse; } }
// Draw System.out.println("There is no space, Draw"); returntrue; } /** * A Round * @return */ publicstaticvoidround(String[][] strings, String user) { Scannerscanner=newScanner(System.in); while (true) { System.out.println("Enter a row (0, 1, 2) for player "+ user + ": "); int x= scanner.nextInt(); if (x > strings.length || x < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
System.out.println("Enter a column (0, 1, 2) for player "+ user + ": "); inty= scanner.nextInt(); if (y > strings.length || y < 0) { System.out.println("Input Error, please input the number between 0-2 (0 and 2 inclusive) "); continue; }
// If it is right if (strings[x][y] == " ") { strings[x][y] = user; break; } else { System.out.println("There was a piece, change another position"); continue; } } // scanner.close(); } }