井字游戏

首先分解任务:
1.如何来显示井字棋的棋盘
2.创建一个3X3的矩阵(chessPiece)来实时记录两个棋手的走棋
3.对异常情况的捕获,用户的输入可能存在哪些不合法的情况
4.怎样判断最终是谁赢了,或者平局
嗯,这只是一个从逻辑上的划分,在写代码时有些功能可能可以在同一个方法模块里实现,也有可能要继续细分——一个功能需要多个方法协同完成。
1.关于如何来显示棋盘,需要记录落子的那个矩阵chessPiece来配合。两个嵌套的循环三次的循环(在遍历到最后一次时需加上一个额外的横向分隔/纵向分隔),在遍历时,同时配合chessPiece来确定棋盘的各个位置上该是‘O’还是‘X’,或者就是空的。
【java】井字游戏
2.关于chessPiece。指定一个位置,若无人落子,则对应下标的元素为0,;若执X选手落子其上,则对应元素为-1;若执O选手落子其上,对应元素为1
3.关于异常。我总结有二:1)用户在已经落子的位置上再落一子;2)用户输入的位置不合法。
4.关于裁定输赢。若某一行或某一列或某一对角线上全为‘O’或者全为‘X’,则为执O选手获胜或执X选手获胜。当棋盘落满子也未出现上面的情况时,平局。
chessPiece是贯穿整个程序的一个“线索”。
运行结果(局部):
【java】井字游戏

一些细节的梳理还是难以用自然的语言表达。
完整代码:

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("Enter a row (1, 2, or 3) for player X:");
					int row = input.nextInt() - 1;
					System.out.print("Enter a column (1, 2, or 3) for player X:");
					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;
					}
				}
				else {
					System.out.print("Enter a row (1, 2, or 3) for player O:");
					int row = input.nextInt() - 1;
					System.out.print("Enter a column (1, 2, or 3) for player 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("Do you want another ticktacktoe? Enter y or n:");
			String answer_string = input.next();
			answer = answer_string.charAt(0);
		}
		System.out.println("Why not have another ticktacktoe?");
	}

	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行。。。

相关文章: