[id_[][]]
首先分解任务:
1.如何来显示井字棋的棋盘
2.创建一个3X3的矩阵()来实时记录两个棋手的走棋
3.对异常情况的捕获,用户的输入可能存在哪些不合法的情况
4.怎样判断最终是谁赢了,或者平局
嗯,这只是从逻辑方面进行的划分。在编写代码时,有些功能或许能够在同一个方法模块中得以实现,而有些功能则有可能需要继续进行细分,即一个功能需要多个方法相互配合来完成。
如何显示棋盘呢?需要记录落子的那个矩阵来配合。有两个嵌套的循环,每个循环都进行三次。在遍历到最后一次时,需要加上一个额外的横向分隔或纵向分隔。在遍历时,要同时配合来确定棋盘的各个位置上该是“O”还是“X”,或者是空的。
指定一个位置,若此处无人落子,那么该位置对应的下标元素就是 0;若执 X 选手在该位置落子,那么对应元素就是 -1;若执 O 选手在该位置落子,那么对应元素就是 1。
关于异常,我总结出两点:其一,用户在已经落子的位置上再次落子;其二,用户输入的位置不符合规定。
关于裁定输赢的情况如下:如果某一行全为“O”或者全为“X”,那么执 O 选手获胜;如果某一列全为“O”或者全为“X”,那么执 O 选手获胜;如果某一对角线上全为“O”或者全为“X”,那么执 O 选手获胜;如果某一行全为“O”或者全为“X”,那么执 X 选手获胜;如果某一列全为“O”或者全为“X”,那么执 X 选手获胜;如果某一对角线上全为“O”或者全为“X”,那么执 X 选手获胜。当棋盘落满子且未出现上述情况时,为平局。
是贯穿整个程序的一个“线索”。
运行结果(局部):
一些细节的梳理还是难以用自然的语言表达。
完整代码:
import java.util.Scanner;
public class Ticktacktoe {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char answer = 'y';
while(answer == 'y') {
int[][] chessPiece = new int[3][3];
int step = 0;
while(whoWin(chessPiece) == 0 && step != 9) {
chessBoard(chessPiece);
if(step % 2 != 0) {
System.out.print(请输入玩家 X 的行(1、2 或 3):);
int row = input.nextInt() - 1;
System.out.print(请为玩家 X 输入一列(1、2 或 3):);
int column = input.nextInt() - 1;
int judge = isLegal(row, column, chessPiece);
if(judge == -1) {
System.out.println(你输入了一个错误的位置,请输入一个正确的位置。);
continue;
}
else if(judge == 0)
chessPiece[row][column] = -1;
else {
System.out.println(这个站点被占据了。 这个站点被占用了。 这个站点被使用了。 这个站点被启用了。 这个站点被占有了。 这个站点被利用了。 这个站点被应用了。 这个站点被安置了。 这个站点被布置了。 这个站点被安排了。 这个站点被设定了。 这个站点被规划了。 这个站点被筹划了。 这个站点被筹备了。 这个站点被布设了。 这个站点被安置好了。 这个站点被布置妥当。 这个站点被安排到位。 这个站点被设定完毕。 这个站点被规划完毕。 这个站点被筹划完毕。 这个站点被筹备完毕。 这个站点被布设完毕。);
System.out.println(请将你的棋子放到另一个地方。);
continue;
}
}
else {
System.out.print(让玩家 O 输入一行(1、2 或 3):);
int row = input.nextInt() - 1;
System.out.print(输入一个列号(1、2 或 3)给玩家 O。);
int column = input.nextInt() - 1;
int judge = isLegal(row, column, chessPiece);
if(judge == -1) {
System.out.println("You input a wrong place, please input a right one!");
continue;
}
else if(judge == 0)
chessPiece[row][column] = 1;
else {
System.out.println("This site has been taken up!");
System.out.println("Please put your chess at another place!");
continue;
}
}
step++;
}
chessBoard(chessPiece);
if(step == 9)
System.out.println("You were neck and neck!");
else if(whoWin(chessPiece) == -1)
System.out.println("X player won");
else
System.out.println("O player won");
System.out.print();
String answer_string = input.next();
answer = answer_string.charAt(0);
}
System.out.println();
}
public static void chessBoard(int[][] chessPiece) {
for(int i = 0 ; i < 3; i++) {
System.out.printf("-------------------\n");
for(int j = 0; j < 3; j++) {
System.out.printf("| %c ", chessTranslation(chessPiece[i][j]));
if(j == 2)
System.out.printf("|");
}
System.out.printf("\n");
if(i == 2)
System.out.printf("-------------------\n");
}
}
public static char chessTranslation(int location){
if(location == -1)
return 'X';
else if(location == 1)
return 'O';
else
return ' ';
}
public static int whoWin(int[][] cB) {
int result = 0;
if(cB[0][0] + cB[1][1] + cB[2][2] == 3)
result = 1;
else if(cB[0][0] + cB[1][1] + cB[2][2] == -3)
result = -1;

else if(cB[0][2] + cB[1][1] + cB[2][0] == 3)
result = 1;
else if(cB[0][2] + cB[1][1] + cB[2][0] == -3)
result = -1;
else if(cB[0][0] + cB[0][1] + cB[0][2] == 3)
result = 1;
else if(cB[0][0] + cB[0][1] + cB[0][2] == -3)
result = -1;
else if(cB[1][0] + cB[1][1] + cB[1][2] == 3)
result = 1;
else if(cB[1][0] + cB[1][1] + cB[1][2] == -3)
result = -1;
else if(cB[2][0] + cB[2][1] + cB[2][2] == 3)
result = 1;
else if(cB[2][0] + cB[2][1] + cB[2][2] == -3)
result = -1;
else if(cB[0][0] + cB[1][0] + cB[2][0] == 3)
result = 1;
else if(cB[0][0] + cB[1][0] + cB[2][0] == -3)
result = -1;
else if(cB[0][1] + cB[1][1] + cB[2][1] == 3)
result = 1;
else if(cB[0][1] + cB[1][1] + cB[2][1] == -3)
result = -1;
else if(cB[0][2] + cB[1][2] + cB[2][2] == 3)
result = 1;
else if(cB[0][2] + cB[1][2] + cB[2][2] == -3)
result = -1;
else
result = 0;
return result;
}
public static int isLegal(int row, int column, int[][] cB) {
if(row > 2 || row < 0 || column > 2 || column < 0)
return -1;
else if(cB[row][column] == 0)
return 0;
else
return 1;
}
}
没想到会超过100行。。。
用类来重写
最近我学习了类,我打算用类来重新编写这个程序。这个程序在现实中的具体体现包含三个对象:
棋盘,执X选手,执O选手
所谓的下棋者只是对棋盘进行了一系列操作,所以只需要一个棋盘类就足够了。
棋盘类:
public class ChessboardObject {
private int[][] board = new int[3][3];
public ChessboardObject() {
}
/*
这个棋盘类的数据域只有一个二维数组,并且在创建时没有特殊要求。
** 故不设置带参数的构造方法
*/
/**判断当前位置是否还未落子*/
public boolean isEmpty(int row, int column) {
if(this.board[row][column] == 0)
return true;
else
return false;
}
/**落子操作*/
public int move(int row, int column, int step) {
/*
当 step 为偶数时,由 X 选手落子;当 step 为奇数时,由 O 选手落子(step 从 0 开始计数)
当 board 的[row]行[column]列的值为 0 时,意味着当前所处的位置是没有元素的,即当前位置为空。
当 board 的[row]行[column]列的值为 1 时,就在当前位置落下 X 。
当 board 的[row]行[column]列的值为-1时,就在当前位置落下 O。
*/
boolean flag = this.isEmpty(row, column);
if(flag) {
if(step % 2 == 0) {
this.board[row][column] = 1;
}
else {
this.board[row][column] = -1;
}
step++;
}
else
System.out.println("该位置不可落子!");
return step;
}
/**可视化转换器*/
public char translation(int location){
if(location == 1)
return 'X';
else if(location == -1)
return 'O';
else
return ' ';
}
/**打印棋局*/
public void printBoard() {
int chess;
for(int i = 0 ; i < 3; i++) {
System.out.printf("-------------------\n");
for(int j = 0; j < 3; j++) {

chess = this.board[i][j];
System.out.printf("| %c ", translation(chess));
if(j == 2)
System.out.printf("|");
}
System.out.printf("\n");
if(i == 2)
System.out.printf("-------------------\n");
}
}
/**判断输赢*/
public int whoWin() {
/*
当 board 的某一对角线上的元素之和为 3 时,执 X 选手获胜。
当 board 的某一行的元素之和为 -3 时,O 选手获胜;当 board 的某一列的元素之和为 -3 时,O 选手获胜;当 board 的某一对角线上的元素之和为 -3 时,O 选手获胜。
** 否则,还未决出胜负
*/
int result = 0;
if(this.board[0][0] + this.board[1][1] + this.board[2][2] == 3)
result = 1;
else if(this.board[0][0] + this.board[1][1] + this.board[2][2] == -3)
result = -1;
else if(this.board[0][2] + this.board[1][1] + this.board[2][0] == 3)
result = 1;
else if(this.board[0][2] + this.board[1][1] + this.board[2][0] == -3)
result = -1;
else if(this.board[0][0] + this.board[0][1] + this.board[0][2] == 3)
result = 1;
else if(this.board[0][0] + this.board[0][1] + this.board[0][2] == -3)
result = -1;
else if(this.board[1][0] + this.board[1][1] + this.board[1][2] == 3)
result = 1;
else if(this.board[1][0] + this.board[1][1] + this.board[1][2] == -3)
result = -1;
else if(this.board[2][0] + this.board[2][1] + this.board[2][2] == 3)
result = 1;
else if(this.board[2][0] + this.board[2][1] + this.board[2][2] == -3)
result = -1;
else if(this.board[0][0] + this.board[1][0] + this.board[2][0] == 3)
result = 1;
else if(this.board[0][0] + this.board[1][0] + this.board[2][0] == -3)
result = -1;
else if(this.board[0][1] + this.board[1][1] + this.board[2][1] == 3)
result = 1;
else if(this.board[0][1] + this.board[1][1] + this.board[2][1] == -3)
result = -1;
else if(this.board[0][2] + this.board[1][2] + this.board[2][2] == 3)
result = 1;
else if(this.board[0][2] + this.board[1][2] + this.board[2][2] == -3)
result = -1;
else
result = 0;
return result;
}
}
主函数:
import java.util.Scanner;
public class TestTicktacktoe {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char answer = 'y';
while(answer == 'y') {
ChessboardObject ticktacktoe = new ChessboardObject();
int step = 0;
while(ticktacktoe.whoWin() == 0 && step != 9) {
ticktacktoe.printBoard();
if(step %2 == 0)
System.out.println("现在是执X选手的回合:");
else
System.out.println("现在是执O选手的回合:");
System.out.print("请输入行坐标(1,2,3)> ");
int row = input.nextInt() - 1;
System.out.print("请输入列坐标(1,2,3)> ");
int column = input.nextInt() - 1;
if(isLegal(row, column))
step = ticktacktoe.move(row, column, step);
else
System.out.println("指定的位置无效!");
}
ticktacktoe.printBoard();
if(step == 9)
System.out.println("棋逢对手!");
else if(ticktacktoe.whoWin() == 1)
System.out.println("执X选手获胜!");
else
System.out.println("执O选手获胜!");
System.out.print("您还想来一局井字棋游戏吗?\n"
+ "y or n > ");
String answer_string = input.next();
answer = answer_string.charAt(0);
}
}
/**判断输入的位置是否越界*/
public static boolean isLegal(int row, int column) {
if(row > 2 || row < 0 || column > 2 || column < 0)
return false;
else
return true;
}
}
版权声明:本文为 “博览广文网” 原创文章,转载请附上原文出处链接及本声明;
工作时间:8:00-18:00
客服电话
0755-88186625
电子邮件
admin@lanyu.com
扫码二维码
获取最新动态