こんにちは私は試してみたいtic tacのつま先のゲームに問題があります。私はまだ初心者ですので、組織とそのようなことについてのヒントを自由にお寄せください。しかし、私のメソッドcheckRowWin、checkColoumnWin、およびE.T.Cは、一緒に追加されたときに動作していないようです。彼らはすべて個々に動作するように見えます。一緒に追加すると、最後に呼ばれるものだけが動作します。私は自分自身でそれを理解するように見えることはできません。Java Tic Tac Toeチェックの勝者方法が一緒に働いていない
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;
}
}
* "気軽にどうぞヒントを与えるために自由に* * - 静的に生きることを学ぶ – MadProgrammer
==を使用して文字列値を比較しないことを学ぶ。この特定のプログラムでは正常に動作しますが、通常はそうではありません。 –
@MadProgrammer - 自分の質問には適しているかもしれませんが、なぜ静的なものではないのですか? –