gpt4 book ai didi

Java Tic Tac Toe 检查获胜者方法不能一起工作

转载 作者:行者123 更新时间:2023-12-01 20:21:02 26 4
gpt4 key购买 nike

您好,我想尝试制作一个井字游戏,但遇到问题。我仍然是一个初学者,所以请随意提供有关组织和类似内容的提示,但我的问题是我的方法 checkRowWin、checkColoumnWin 和 E.T.C 添加在一起时似乎不起作用。它们似乎都是单独工作的,当加在一起时,最后一个是唯一有效的。我自己似乎无法弄清楚。

import java.util.*;
public class TicTacToeATREU {
public static final int BOARD_SIZE = 3;
public static final String PLAYER_ONE = "x"; //Sets piece to corresponding player
public static final String PLAYER_TWO = "o";
public static void main (String []args) {
Scanner console = new Scanner(System.in);
String[][] gameBoard = new String[BOARD_SIZE][BOARD_SIZE];
String playerTurn = PLAYER_ONE;
boolean gameWon = false;
intro();
playGame(gameBoard,console,playerTurn,gameWon);
}
public static void fillBoard(String[][] gameBoard) { //Fills board with all underscores
for(int i = 0; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = "_";
}
}
}
public static void printBoard(String[][] gameBoard) { //Prints gameBoard array so that it looks like a tic tac toe board
for(int i = 0; i < gameBoard.length; i++) {
String[] tempArray = gameBoard[i];
for(int j = 0; j < tempArray.length; j++) {
System.out.print(tempArray[j] + " ");
}
}
}
public static void intro(){ //Intro Message to let brief players on rules
System.out.println("This program allows you to play a game of Tic-Tac-Toe.");
System.out.println("Each player will be prompted for the location");
System.out.println("to place their piece. When one player has filled");
System.out.println("an entire row, column, or diagonal the game is won.");
System.out.println();
System.out.println("Player 1, you will be x");
System.out.println("Player 2, you will be o");
System.out.println();
}
public static void takeTurn(String[][] gameBoard,Scanner console, String playerTurn) { //Method for placing piece each turn into board and then reprinting board
if(playerTurn == PLAYER_ONE) { // Tests to see which player is making move
System.out.println();
System.out.println("Make your move player 1:");
}
else {
System.out.println();
System.out.println("Make your move player 2:");
}
System.out.print("What row? ");
int rowMove = console.nextInt();
System.out.print("What column? ");
int columnMove = console.nextInt();
if(playerTurn == PLAYER_ONE) { //test to see which player is making move and places corresponding piece
gameBoard[rowMove-1][columnMove-1] = PLAYER_ONE;
}
else {
gameBoard[rowMove-1][columnMove-1] = PLAYER_TWO;
}
System.out.println();
printBoard(gameBoard);
}
public static String playGame(String[][] gameBoard, Scanner console, String playerTurn, boolean gameWon) { //Method for playing one game of tic tac toe
//boolean gameWon = false;
int currentPlayer = 1; //this currentPlayer variable is just to offset the game reporting the congratulations method for the losing player
fillBoard(gameBoard);
printBoard(gameBoard);
while(gameWon == false) { //while this loop is false, the game will play
takeTurn(gameBoard,console,playerTurn);
if(checkRowWin(gameBoard,playerTurn) = true) {
gameWon = true;
}
gameWon = checkRowWin(gameBoard,playerTurn);
gameWon = checkColoumnWin(gameBoard,playerTurn);
gameWon = checkDiagonalWin(gameBoard,playerTurn);
gameWon = checkTie(gameBoard,playerTurn);
if(playerTurn == PLAYER_ONE) { //for switching players after every call of take turn method
playerTurn = PLAYER_TWO;
currentPlayer = 1;
}
else {
playerTurn = PLAYER_ONE;
currentPlayer = 2;
}
}
System.out.println();
System.out.println("Congratulations Player " + currentPlayer + "! You win!");
return playerTurn;
}
public static boolean checkRowWin(String[][] gameBoard, String playerTurn) { //checks for three pieces in a row to decide if the game has been won
boolean rowWin = true;
for(int i = 0; i < gameBoard.length; i++) {
String[] tempArray = gameBoard[i];
for(int j = 0; j < tempArray.length; j++) {
if(!tempArray[j].equals(playerTurn)) {
rowWin = false;
}
}
if(rowWin == true) {
return true;
}
else {
rowWin = true;
}
}
return false;
}
public static boolean checkColoumnWin(String[][] gameBoard, String playerTurn) { //checks for three pieces in a coloumn to decide if the game has been won
boolean coloumnWin = true;
for(int i = 0; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard.length; j++) {
if(!gameBoard[j][i].equals(playerTurn)) {
coloumnWin = false;
}
}
if(coloumnWin == true) {
return true;
}
else {
coloumnWin = true;
}
}
return false;
}
public static boolean checkDiagonalWin(String[][] gameBoard, String playerTurn) { //checks for three pieces in a diagonal of the board to decide if the game has been won
boolean diagonalWin = true;
for(int i = 0; i < gameBoard.length; i++) {
if(!gameBoard[i][i].equals(playerTurn)) {
diagonalWin = false;
}

}
if(diagonalWin == true) {
return true;
}
else {
diagonalWin = true;
}
for(int j = 0; j < gameBoard.length; j++) {
int row = j;
int coloumn = BOARD_SIZE-1-j;
if(!gameBoard[row][coloumn].equals(playerTurn)) {
diagonalWin = false;
}
}

if(diagonalWin == true) {
return true;
}
else {
diagonalWin = true;
}
return false;
}
public static boolean checkTie(String[][] gameBoard,String playerTurn) {
boolean test = true;
for(int i = 0; i < BOARD_SIZE; i++) {
for(int j = 0; j < BOARD_SIZE; j++) {
if(gameBoard[i][j].equals("_")) {
test = false;
}

}
}
return test;
}
}

最佳答案

在你的代码中

gameWon = checkRowWin(gameBoard,playerTurn);
gameWon = checkColoumnWin(gameBoard,playerTurn);
gameWon = checkDiagonalWin(gameBoard,playerTurn);
gameWon = checkTie(gameBoard,playerTurn);

意味着前三行的结果将被丢弃,并被 checkTie() 返回的任何内容覆盖 - 即,如果 checkRowWin() 返回 True,则不会执行任何操作。

该代码存在其他问题,可能最适合 https://codereview.stackexchange.com/ .

关于Java Tic Tac Toe 检查获胜者方法不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687597/

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