试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议。
一、棋盘类
1 package 井字棋游戏; 2 3 public class ChessBoard { 4 private int number; 5 Person player = new Person(); // 创建棋手 6 String[][] board = new String[3][3]; // 创建棋盘 7 // 设置棋子个数 8 public void setNumber(int number) { 9 this.number = number; 10 } 11 // 获得棋子数 12 public int getNumber() { 13 return this.number; 14 } 15 // 打印棋盘 16 public void printBoard() { 17 for (int i = 0; i < 3; i++) { 18 System.out.println("-------------"); 19 for (int j = 0; j < 3; j++) { 20 if (board[i][j] == null) 21 System.out.printf("| "); 22 else 23 System.out.printf("| %s ", board[i][j]); 24 } 25 System.out.println("|"); 26 } 27 System.out.println("-------------"); 28 } 29 // 判断位置是否合法 30 public boolean judgement(int row, int column) { 31 if (board[row][column] == null) //该位置无棋子 32 return true; 33 else if (row > 2 || row < 0 || column > 2 || column < 0) //越界 34 return false; 35 else //该位置有棋子 36 return false; 37 } 38 // 放置棋子 39 public boolean putChess(String chess) { 40 player.chessPlace(chess); 41 // 若棋子位置合法,则存入数组 42 if (judgement(player.row, player.column)) { 43 board[player.row][player.column] = player.chesspiece; 44 return true; 45 } 46 else { 47 System.out.println("This site has been taken up, please choose another!"); 48 return false; 49 } 50 } 51 // 胜利的条件 52 public boolean winCondition() { 53 int i, j; 54 // 判断行 55 for (i = 0; i < 3; i++) { 56 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != null) { 57 System.out.printf("%s player won! ", board[i][0]); 58 return true; 59 } 60 } 61 //判断列 62 for (j = 0; j < 3; j++) { 63 if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != null) { 64 System.out.printf("%s player won! ", board[0][j]); 65 return true; 66 } 67 } 68 //判断对角线 69 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != null) { 70 System.out.printf("%s player won! ", board[0][0]); 71 return true; 72 } 73 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != null) { 74 System.out.printf("%s player won! ", board[0][2]); 75 return true; 76 } 77 return false; 78 } 79 }
二、棋手类
1 package 井字棋游戏; 2 3 import java.util.Scanner; 4 public class Person { 5 Scanner s = new Scanner(System.in); 6 int row, column; 7 String chesspiece; // 棋子类型 8 // 选择棋子 9 public void chooseChess() { 10 String playerone, playertwo; 11 do { 12 System.out.printf("The first player chooses chess(X or O):"); 13 playerone = s.nextLine(); 14 }while(!(playerone.equals("X") || playerone.equals("x") || playerone.equals("O") || playerone.equals("o"))); 15 if (playerone.equals("X") || playerone.equals("x")){ 16 playertwo = "O"; 17 System.out.printf("The first player is %s, the second player is %s ", playerone,playertwo); 18 } 19 else { 20 playertwo = "X"; 21 System.out.printf("The first player is %s, the second player is %s ", playerone,playertwo); 22 } 23 } 24 // 选择棋子的位置 25 public void chessPlace(String chesspiece) { 26 do { 27 System.out.printf("Enter a row (1, 2 or 3) for player %s:", chesspiece); 28 row = s.nextInt() - 1; 29 s.nextLine(); 30 }while(row < 0 || row > 2); 31 do { 32 System.out.printf("Enter a column (1, 2 or 3) for player %s:", chesspiece); 33 column = s.nextInt() - 1; 34 s.nextLine(); 35 }while(column < 0 || column > 2); 36 this.chesspiece = chesspiece; 37 } 38 // 选择是否开始下一局 39 public boolean reStart() { 40 Scanner s = new Scanner(System.in); 41 String flag; 42 System.out.printf("Do you want a new game?(Y or N):"); 43 flag = s.nextLine(); 44 s.close(); 45 if (flag.equals("Y") || flag.equals("y")) 46 return true; 47 else 48 return false; 49 } 50 }
三、测试
package 井字棋游戏; public class Test { public static void main(String[] args) { while (true) { int i = 0; ChessBoard p = new ChessBoard(); p.player.chooseChess(); p.printBoard(); while(!p.winCondition()) { if (p.getNumber() % 2 == 0) { boolean judge = p.putChess("X"); if (!judge) continue; // 如果位置不合法,则重新下 } else { boolean judge = p.putChess("O"); if (!judge) continue; // 如果位置不合法,则重新下 } i++; // 棋子数加一 p.setNumber(i); // 设置棋子数 p.printBoard(); if(p.getNumber() == 9) { System.out.println("This is a draw!"); break; } } if (!p.player.reStart()) { System.out.println("Game Over!"); break; } } } }
效果如下: