***8.9(游戏:井字游戏)在井字游戏中,两个玩家使用各自的标志(一方用X则另一方就用O),轮流标记3x3的网格中的某个空格。当一个玩家在网格的水平方向、垂直方向或者对角线方向上表记录三个相同的X或三个相同的O时,游戏结束,该玩家获胜。平局(没有赢家)是指当网格中所有的空格都被填满时没有玩家获胜的情况。创建一个玩井字游戏的程序。
程序题数两个玩家交替输入X和O标记。当输入一个标记时,程序在控制台上重新显示棋盘,然后确定游戏的状态(是获胜、平局还是继续)。
***8.9(Game: tic tacit)In the tic tac toe game, two players use their own flags (one uses an X, the other uses an o) to mark a space in the 3x3 grid in turn. When a player records three identical X’s or three identical O’s in the horizontal, vertical or diagonal direction of the grid, the game ends and the player wins. A draw (no winner) is a situation in which no player wins when all the spaces in the grid are filled. Create a program to play tic tac toe.
Two players input X and O marks alternately. When a marker is entered, the program redisplays the chessboard on the console and determines the state of the game (win, draw, or continue).
参考代码:
package chapter08;
import java.util.Scanner;
public class Code_09 {
static char[][] ch = new char[3][3];
static int flag = 0;
static Scanner cin = new Scanner(System.in);
public static void main(String[] args){
for(int i = 0; i < 3; ++i)
java.util.Arrays.fill(ch[i], ' ');
printMap();
while(true){
goingX();
printMap();
if(flag == 1)
break;
going0();
printMap();
if(flag == 1)
break;
}
}
public static void going0(){
System.out.print("Enter a row(0,1 or 2) for plyaer 0:");
int x = cin.nextInt();
System.out.print("Enter a column(0,1 or 2) for plyaer 0:");
int y = cin.nextInt();
ch[x][y] = '0';
}
public static void goingX(){
System.out.print("Enter a row(0,1 or 2) for plyaer X:");
int x = cin.nextInt();
System.out.print("Enter a column(0,1 or 2) for plyaer X:");
int y = cin.nextInt();
ch[x][y] = 'X';
}
public static void printMap(){
System.out.printf("-------------\n");
System.out.printf("| %c | %c | %c |\n",ch[0][0],ch[0][1],ch[0][2]);
System.out.printf("-------------\n");
System.out.printf("| %c | %c | %c |\n",ch[1][0],ch[1][1],ch[1][2]);
System.out.printf("-------------\n");
System.out.printf("| %c | %c | %c |\n",ch[2][0],ch[2][1],ch[2][2]);
System.out.printf("-------------\n");
if(check() == 1){
System.out.println("X player win");
flag = 1;
}
else if(check() == 2){
System.out.println("0 player win");
flag =1;
}
}
public static int check(){
if(ch[0][0] == 'X' && ch[0][0] == ch[0][1] && ch[0][0] == ch[0][2])
return 1;
if(ch[0][0] == 'X' && ch[0][0] == ch[1][0] && ch[0][0] == ch[2][0])
return 1;
if(ch[0][0] == 'X' && ch[0][0] == ch[1][1] && ch[0][0] == ch[2][2])
return 1;
if(ch[1][1] == 'X' && ch[0][2] == ch[1][1] && ch[0][2] == ch[2][0])
return 1;
if(ch[0][0] == '0' && ch[0][0] == ch[0][1] && ch[0][0] == ch[0][2])
return 2;
if(ch[0][0] == '0' && ch[0][0] == ch[1][0] && ch[0][0] == ch[2][0])
return 2;
if(ch[0][0] == '0' && ch[0][0] == ch[1][1] && ch[0][0] == ch[2][2])
return 2;
if(ch[1][1] == '0' && ch[0][2] == ch[1][1] && ch[0][2] == ch[2][0])
return 2;
return 0;
}
}
结果显示:
-------------
| | | |
-------------
| | | |
-------------
| | | |
-------------
Enter a row(0,1 or 2) for plyaer X:1
Enter a column(0,1 or 2) for plyaer X:1
-------------
| | | |
-------------
| | X | |
-------------
| | | |
-------------
Enter a row(0,1 or 2) for plyaer 0:1
Enter a column(0,1 or 2) for plyaer 0:2
-------------
| | | |
-------------
| | X | 0 |
-------------
| | | |
-------------
Enter a row(0,1 or 2) for plyaer X:0
Enter a column(0,1 or 2) for plyaer X:0
-------------
| X | | |
-------------
| | X | 0 |
-------------
| | | |
-------------
Enter a row(0,1 or 2) for plyaer 0:0
Enter a column(0,1 or 2) for plyaer 0:2
-------------
| X | | 0 |
-------------
| | X | 0 |
-------------
| | | |
-------------
Enter a row(0,1 or 2) for plyaer X:2
Enter a column(0,1 or 2) for plyaer X:2
-------------
| X | | 0 |
-------------
| | X | 0 |
-------------
| | | X |
-------------
X player win
Process finished with exit code 0