Java中的井字游戏
在 Tic-Tac-Toe 游戏中,您将看到游戏方法的实现。在这个游戏中,将扮演两个玩家,您在屏幕上有一个印刷板,其中将显示 1 到 9 的数字,或者您可以说出它的编号。现在,您必须为特定的盒子编号选择 X 或 O。例如,如果您必须选择任何数字,那么 X 或 O 将显示在印制板上,然后将出现下一个。任务是创建一个Java程序来为两个玩家实现一个 3×3 的井字游戏。
游戏板 :
|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
样本输入:
输入要放置 X 的插槽编号:3
示例输出:
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
样本输入:
现在,轮到 O,输入一个槽号来放置 O:5
示例输出:
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
所以,喜欢这样的游戏才会继续下去。
如何玩游戏:
- 两个玩家都选择X或O来标记他们的单元格。
- 将有一个 3×3 的网格,9 个单元格中的每个单元格都分配有编号。
- 选择X的玩家首先开始游戏。
- 他输入要放置X的单元格编号。
- 现在, O和X交替进行,直到两者中的任何一个获胜。
- 获胜标准:只要两个玩家中的任何一个用他的符号(X/O)填满一行/列/对角线,他就获胜,游戏结束。
- 如果两个玩家都没有赢,则说游戏以平局结束。
下面是游戏在Java中的实现:
Java
// A simple program to demonstrate
// Tic-Tac-Toe Game.
import java.util.*;
public class GFG {
static String[] board;
static String turn;
// CheckWinner method will
// decide the combination
// of three box given below.
static String checkWinner()
{
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}
// For O winner
else if (line.equals("OOO")) {
return "O";
}
}
for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(
String.valueOf(a + 1))) {
break;
}
else if (a == 8) {
return "draw";
}
}
// To enter the X Or O at the exact place on board.
System.out.println(
turn + "'s turn; enter a slot number to place "
+ turn + " in:");
return null;
}
// To print out the board.
/* |---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|*/
static void printBoard()
{
System.out.println("|---|---|---|");
System.out.println("| " + board[0] + " | "
+ board[1] + " | " + board[2]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[3] + " | "
+ board[4] + " | " + board[5]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[6] + " | "
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
board = new String[9];
turn = "X";
String winner = null;
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}
System.out.println("Welcome to 3x3 Tic Tac Toe.");
printBoard();
System.out.println(
"X will play first. Enter a slot number to place X in:");
while (winner == null) {
int numInput;
// Exception handling.
// numInput will take input from user like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error "Invalid input."
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
}
catch (InputMismatchException e) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
// This game has two player x and O.
// Here is the logic to decide the turn.
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;
if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
}
printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
}
}
// If no one win or lose from both player x and O.
// then here is the logic to print "draw".
if (winner.equalsIgnoreCase("draw")) {
System.out.println(
"It's a draw! Thanks for playing.");
}
// For winner -to display Congratulations! message.
else {
System.out.println(
"Congratulations! " + winner
+ "'s have won! Thanks for playing.");
}
}
}
输出:
Below is the output of the above program :
Welcome to 3x3 Tic Tac Toe.
|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X will play first. Enter a slot number to place X in:
3
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
5
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
6
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | O | X |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
1
|---|---|---|
| O | 2 | X |
|-----------|
| 4 | O | X |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
9
|---|---|---|
| O | 2 | X |
|-----------|
| 4 | O | X |
|-----------|
| 7 | 8 | X |
|---|---|---|
Congratulations! X's have won! Thanks for playing.
运行 Eclipse IDE:
- 打开 Eclipse IDE。
- 创建一个新的Java项目。
- 右键单击 src 文件夹并创建一个新类,如类名 -GFG。
- 现在,编写您的源代码和 ctrl+s 以保存它。
- 现在,要执行程序,请右键单击 src 文件夹,然后单击作为Java应用程序运行。
您可以查看下面给出的屏幕截图以供参考。