gpt4 book ai didi

java - Java 基本 Tictactoe 程序中的语义错误

转载 作者:行者123 更新时间:2023-12-01 14:17:03 25 4
gpt4 key购买 nike

首先,我希望我可以在这里寻求一些调试帮助。照这样说,我创建了这个简单的小井字游戏程序,它基本上已经完成,但是这个语义错误一直让我丧命。

显然,我花了一些时间试图自己解决问题,但你可以说我已经放弃了,所以我现在在这里:)

简单概述一下,棋盘由 char ttt[3][3] 类型的数组表示。玩家也是 char 类型的变量,因此它们要么是“X”,要么是“O”,并且棋盘的坐标以字母形式输入:

示例运行如下所示:

********************************
---a------b------c---
| | | |
---d------e------f---
| | | |
---g------h------i---
| | | |
---------------------
player1: O, it is your turn.
Select a cell [a, b, c, ... i]
a
********************************
---a------b------c---
| O | | |
---d------e------f---
| | | |
---g------h------i---
| | | |
---------------------
player2: X, it is your turn.
Select a cell [a, b, c, ... i]

数组 ttt[3][3] 已初始化,每个元素都只是“”。

在大多数情况下,程序运行良好。为了尽量为大家节省一些时间,我相信以下方法可以完美运行

  • boolean 获胜者(角色玩家)
  • boolean 值 gameIsDraw()
  • 无效的displayBoard()
  • 字符串playerID(字符玩家)
  • 和主要方法

我确实发现问题很可能包含在我的getPlayerInput(char player) 方法:

void getPlayerInput(char player)
{
int row = 0;
int col = 0;

System.out.println(playerID(player) + ", it is your turn.");
System.out.println("Select a cell [a, b, c, ... i]");

char answer;
answer = scan.next().charAt(0);

switch(answer)
{
case 'a':
row = 0;
col = 0;
break;
case 'b':
row = 0;
col = 1;
break;
case 'c':
row = 0;
col = 2;
break;
case 'd':
row = 1;
col = 0;
break;
case 'e':
row = 1;
col = 1;
break;
case 'f':
row = 1;
col = 2;
break;
case 'g':
row = 2;
col = 0;
break;
case 'h':
row = 2;
col = 1;
break;
case 'i':
row = 2;
col = 2;
break;

default:
System.out.println("Invalid location, try again.");
getPlayerInput(player);
}

if(ttt[row][col] != ' ')
{
System.out.println("This square is taken. Try again.");
getPlayerInput(player);
}
else
{
ttt[row][col] = player;
}
}

对我来说,它看起来不错,但我的输出表明情况并非如此。该方法包括两个故障保护,

  1. 如果用户输入了棋盘范围之外的内容(“a”到“i”之外的字符),

  2. 或者如果用户在板上选择的字母/位置已被另一个“X”或“O”占据。

在这两种情况下,该方法都会打印出输入了一些错误的输入,然后再次调用 getPlayerInput()。

通过调试我注意到,如果仅输入有效的输入,程序似乎运行良好。但是,如果输入了错误的输入(任一类型),然后输入了有效的输入,有时该方法会打印出仍然输入了错误的输入。

例如,

********************************
---a------b------c---
| | | |
---d------e------f---
| | | |
---g------h------i---
| | | |
---------------------
player1: O, it is your turn.
Select a cell [a, b, c, ... i]
a
********************************
---a------b------c---
| O | | |
---d------e------f---
| | | |
---g------h------i---
| | | |
---------------------
player2: X, it is your turn.
Select a cell [a, b, c, ... i]
z
Invalid location, try again.
player2: X, it is your turn.
Select a cell [a, b, c, ... i]
e
This square is taken. Try again.
player2: X, it is your turn.
Select a cell [a, b, c, ... i]
f
********************************
---a------b------c---
| O | | |
---d------e------f---
| | X | X |
---g------h------i---
| | | |
---------------------
player1: O, it is your turn.
Select a cell [a, b, c, ... i]

请注意,我输入了字符的 a-z-e-f。 'z' 显然是一个无效字符,因此该方法按预期工作(到目前为止),打印出它是无效输入,然后该方法再次运行,要求输入。然后输入“e”,显然是一个有效的位置,但该方法打印出“方 block 已被占用”,而显然不是。但是,输入不同的字符“f”允许我退出它。

最终结果是玩家“X”有两个回合并填满了“e”和“f”两个方格。

应该注意的是,如果用户不断地输入错误的输入,他应该被困在该方法中,直到输入有效的输入,但这显然是良好的输入在某种程度上被误解为错误的输入的情况,并且除非输入良好输入的不同实例,否则无法退出循环。

所以,说了这么多,帮帮我吧?无论如何,我非常感谢所有有耐心阅读本文的人......

如果您想自己运行代码,这里是源代码:

import java.util.*;

class TicTacToe
{
char ttt[][] = new char[3][3];
static final char player1 = 'O';
static final char player2 = 'X';
Scanner scan =new Scanner(System.in);


String playerID(char player)
{
if (player == player1)
return "player1: "+player;
else
return "player2: "+ player;
}

void getPlayerInput(char player)
{
int row = 0;
int col = 0;

System.out.println(playerID(player) + ", it is your turn.");
System.out.println("Select a cell [a, b, c, ... i]");

char answer;
answer = scan.next().charAt(0);

switch(answer)
{
case 'a':
row = 0;
col = 0;
break;
case 'b':
row = 0;
col = 1;
break;
case 'c':
row = 0;
col = 2;
break;
case 'd':
row = 1;
col = 0;
break;
case 'e':
row = 1;
col = 1;
break;
case 'f':
row = 1;
col = 2;
break;
case 'g':
row = 2;
col = 0;
break;
case 'h':
row = 2;
col = 1;
break;
case 'i':
row = 2;
col = 2;
break;

default:
System.out.println("Invalid location, try again.");
getPlayerInput(player);
}

if(ttt[row][col] != ' ')
{
System.out.println("This square is taken. Try again.");
getPlayerInput(player);
}
else
{
ttt[row][col] = player;
}
}

boolean gameIsDraw()
{
boolean isDraw = true;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(ttt[i][j] == ' ')
{
isDraw = false;
}
}
}

return isDraw;
}

boolean winner(char player)
{
boolean hasWon = false;

// possible horizontal wins
for(int i = 0; i < 3; i++)
{
if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
{
hasWon = true;
}
}

// possible vertical wins
for(int i = 0; i < 3; i++)
{
if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
{
hasWon = true;
}
}

// one diagonal win
if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
{
hasWon = true;
}

// other diagonal win
if(ttt[0][2] == player && ttt[1][1] == player && ttt[2][0] == player)
{
hasWon = true;
}

return hasWon;
}


void displayBoard()
{
System.out.println("********************************");
System.out.println(" ---a------b------c---");

for (int i=0; i<3; i++)
{
for (int j=0; j< 3; j++)
{
if (j == 0) System.out.print(" | ");
System.out.print(ttt[i][j]);
if (j < 2) System.out.print( " | ");
if (j==2) System.out.print(" |");
}
System.out.println();
switch (i)
{
case 0:
System.out.println(" ---d------e------f---");
break;
case 1:
System.out.println(" ---g------h------i---");
break;
case 2:
System.out.println(" ---------------------");
break;
}
}
}


void newgame()
{
char currPlayer = player1;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
ttt[i][j] =' ';

boolean continueFlag = true;
while (continueFlag)
{
displayBoard();
if (gameIsDraw())
{
System.out.println("Game Ends in Draw");
continueFlag = false;
}
else
{
getPlayerInput(currPlayer);
if (winner(currPlayer))
{
System.out.println("We have a winner: " + playerID(currPlayer));
displayBoard();
continueFlag = false;
}
else
{
if (currPlayer == player1) currPlayer = player2;
else currPlayer = player1;
}
}
}

}


public static void main(String[] args)
{
TicTacToe game = new TicTacToe();
String str;
do
{
game.newgame();

System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
str= game.scan.next();
} while ("y".equals(str));

System.out.println("Bye");
}
}

最佳答案

真正的问题是 getPlayerInput 正在递归地调用自身。如果将 rowcolumn 初始化为错误值而不是实际值,例如int row = -1;。递归通常非常适合大型问题,当将其分解为较小的相同问题时可以更轻松地处理。在这种情况下,问题只是获取单个有效输入,该任务无法分解为更简单的任务。

该方法应该使用迭代来代替递归来进行输入验证。例如:

void getPlayerInput(char player) {       
int row = -1;
int col = -1;

System.out.println(playerID(player) + ", it is your turn.");

while(row==-1) {
System.out.println("Select a cell [a, b, c, ... i]");

char answer;
answer = scan.next().charAt(0);

switch(answer) {
case 'a':
row = 0;
col = 0;
break;
// <snip>
default:
System.out.println("Invalid location, try again.");
}
if(row !- -1 && ttt[row][col] != ' ') {
System.out.println("This square is taken. Try again.");
row = -1;
}
}
ttt[row][col] = player;
}

关于java - Java 基本 Tictactoe 程序中的语义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18039922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com