试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议。
一、棋盘类
package 井字棋游戏; public class ChessBoard {
private int number;
Person player = new Person(); // 创建棋手
String[][] board = new String[3][3]; // 创建棋盘
// 设置棋子个数
public void setNumber(int number) {
this.number = number;
}
// 获得棋子数
public int getNumber() {
return this.number;
}
// 打印棋盘
public void printBoard() {
for (int i = 0; i < 3; i++) {
System.out.println("-------------");
for (int j = 0; j < 3; j++) {
if (board[i][j] == null)
System.out.printf("| ");
else
System.out.printf("| %s ", board[i][j]);
}
System.out.println("|");
}
System.out.println("-------------");
}
// 判断位置是否合法
public boolean judgement(int row, int column) {
if (board[row][column] == null) //该位置无棋子
return true;
else if (row > 2 || row < 0 || column > 2 || column < 0) //越界
return false;
else //该位置有棋子
return false;
}
// 放置棋子
public boolean putChess(String chess) {
player.chessPlace(chess);
// 若棋子位置合法,则存入数组
if (judgement(player.row, player.column)) {
board[player.row][player.column] = player.chesspiece;
return true;
}
else {
System.out.println("This site has been taken up, please choose another!");
return false;
}
}
// 胜利的条件
public boolean winCondition() {
int i, j;
// 判断行
for (i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != null) {
System.out.printf("%s player won!\n", board[i][0]);
return true;
}
}
//判断列
for (j = 0; j < 3; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != null) {
System.out.printf("%s player won!\n", board[0][j]);
return true;
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != null) {
System.out.printf("%s player won!\n", board[0][0]);
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != null) {
System.out.printf("%s player won!\n", board[0][2]);
return true;
}
return false;
}
}
二、棋手类
package 井字棋游戏; import java.util.Scanner;
public class Person {
Scanner s = new Scanner(System.in);
int row, column;
String chesspiece; // 棋子类型
// 选择棋子
public void chooseChess() {
String playerone, playertwo;
do {
System.out.printf("The first player chooses chess(X or O):");
playerone = s.nextLine();
}while(!(playerone.equals("X") || playerone.equals("x") || playerone.equals("O") || playerone.equals("o")));
if (playerone.equals("X") || playerone.equals("x")){
playertwo = "O";
System.out.printf("The first player is %s, the second player is %s\n", playerone,playertwo);
}
else {
playertwo = "X";
System.out.printf("The first player is %s, the second player is %s\n", playerone,playertwo);
}
}
// 选择棋子的位置
public void chessPlace(String chesspiece) {
do {
System.out.printf("Enter a row (1, 2 or 3) for player %s:", chesspiece);
row = s.nextInt() - 1;
s.nextLine();
}while(row < 0 || row > 2);
do {
System.out.printf("Enter a column (1, 2 or 3) for player %s:", chesspiece);
column = s.nextInt() - 1;
s.nextLine();
}while(column < 0 || column > 2);
this.chesspiece = chesspiece;
}
// 选择是否开始下一局
public boolean reStart() {
Scanner s = new Scanner(System.in);
String flag;
System.out.printf("Do you want a new game?(Y or N):");
flag = s.nextLine();
s.close();
if (flag.equals("Y") || flag.equals("y"))
return true;
else
return false;
}
}
三、测试
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;
}
}
}
}
效果如下: