0

この宿題はほとんど終わったところですが、出力にいくつかの印刷エラーがあります。printOBoardゲームは機能しますが、メソッドに問題がありprintXBoard、移動時にゲーム ボードが台無しになります。ゲームが長く続くほど、問題は悪化します。ご協力いただきありがとうございます。

import java.util.Scanner;

public class TicTacToe {

private enum Tiles {
    X, O, EMPTY;
}
public static void main(String[] args) {

    int i;
    int j;
    //initialize 2d array of enum types called "board"
    Tiles[][] board = new Tiles[3][3];
    for (i=0; i<board.length; i++) 
        for (j=0; j<board.length; j++)
            board[i][j] = Tiles.EMPTY;

    //print out an empty board as a 2d array, with each tile set as "EMPTY"
    printBoard(board);
    int row, col;
    int countEmpty=0;

    //initial countEmpty count, if it's less than 1, the board is full and the game is over.
    for (i=0; i<board.length; i++)
        for (j=0; j<board.length; j++)
            if (board[i][j] == Tiles.EMPTY)
                countEmpty++;

    while (countEmpty > 0) {

        //Player O enters the row coordinate
        System.out.println("Player O's turn.");
        System.out.println("Player O:  Enter row (0, 1, or 2):");
        Scanner stdin1 = new Scanner(System.in);
        row = stdin1.nextInt();

        //Player O enters the column coordinate
        System.out.println("Player O:  Enter column (0, 1, or 2):");
        Scanner stdin2 = new Scanner(System.in);
        col = stdin2.nextInt();
        //If the tile is empty, it was a valid move, and an 'O' is placed on the spot.  
        if (board[row][col] == Tiles.EMPTY) {
            board[row][col] = Tiles.O;
            //MOVE FOR O ********************
            printOBoard(board, row, col);
            checkWin(board);
            if (checkWin(board) == 2)
                ;
            else
                break;


        }
        //If the tile is not empty, it was not a valid move, and Player O is prompted to try again.  
        else {
            System.out.println("Space already occupied.  Please choose another.");
            System.out.println("Player O's turn.");
            //Player 0 enters the row coordinate
            System.out.println("Player O:  Enter row (0, 1, or 2):");
            stdin1 = new Scanner(System.in);
            row = stdin1.nextInt();

            //Player O enters the column coordinate
            System.out.println("Player O:  Enter column (0, 1, or 2):");
            stdin2 = new Scanner(System.in);
            col = stdin2.nextInt();
            //ERROR MOVE FOR O********************
            board[row][col] = Tiles.O;
            printOBoard(board, row, col);



            checkWin(board);
            if (checkWin(board) == 2)
                ;
            else
                break;
        }



        //Player X enters the row coordinate
        System.out.println("Player X's turn.");
        System.out.println("Player X:  Enter row (0, 1, or 2):");
        Scanner stdin3 = new Scanner(System.in);
        row = stdin3.nextInt();

        //Player X enters the column coordinate
        System.out.println("Player X:  Enter column (0, 1, or 2):");
        Scanner stdin4 = new Scanner(System.in);
        col = stdin4.nextInt();

        if (board[row][col] == Tiles.EMPTY) {
            board[row][col] = Tiles.X;
        printXBoard(board, row, col);
            //MOVE FOR X *************************************************
        checkWin(board);
        if (checkWin(board) == 2)
            ;
        else
            break;
        }

        else {
            System.out.println("Space already occupied.  Please choose another.");
            System.out.println("Player O's turn.");
            System.out.println("Player O:  Enter row (0, 1, or 2):");
            stdin3 = new Scanner(System.in);
            row = stdin3.nextInt();

            //Player O enters the column coordinate
            System.out.println("Player O:  Enter column (0, 1, or 2):");
            stdin4 = new Scanner(System.in);
            col = stdin4.nextInt();
            board[row][col] = Tiles.O;
            //ERROR MOVE FOR X ****************************************
            printXBoard(board, row, col);
            checkWin(board);
            if (checkWin(board) == 2)
                ;
            else
                break;
        }

        //After both players move, we check to see if the board is full.
        countEmpty = 0;
        for (i=0; i<board.length; i++)
            for (j=0; j<board.length; j++)
                if (board[i][j] == Tiles.EMPTY)
                    countEmpty++;       

    }

}

//method printBoard prints out a grid of EMPTY's and returns nothing
private static void printBoard(Tiles board[][]) {
    int i, j;

    System.out.println(" -----------------------------");
    System.out.println("|         |         |         |");
    for (i=0; i<board.length; i++){
        for (j=0; j<board.length; j++){
            System.out.printf("|  " + board[i][j] + "  ");
        }
        System.out.println("|");
        System.out.println("|         |         |         |");
        System.out.println(" -----------------------------");
        if (i<2)
            System.out.println("|         |         |         |");
    }
    return;
}

//method printXBoard prints out the grid modified with the addition of an X after Player X's turn
private static void printXBoard(Tiles board[][], int curRow, int curCol) {
    int i, j;

    System.out.println(" -----------------------------");
    System.out.println("|         |         |         |");
    for (i=0; i<board.length; i++){
        for (j=0; j<board.length; j++){
            if (i == curRow && j == curCol)
                board[i][j] = Tiles.X;
            else
                ;
            System.out.printf("|  " + board[i][j] + "  ");
        }
        System.out.println("|");
        System.out.println("|         |         |         |");
        System.out.println(" -----------------------------");
        if (i<2)
            System.out.println("|         |         |         |");
    }
    return;
}

//method printOBoard prints out the grid modified with the addition of an X after Player X's turn
private static void printOBoard(Tiles board[][], int curRow, int curCol) {
    int i, j;

    System.out.println(" -----------------------------");
    System.out.println("|         |         |         |");
    for (i=0; i<board.length; i++){
        for (j=0; j<board.length; j++){
            if (i == curRow && j == curCol)
                board[i][j] = Tiles.O;
            else
                ;
            System.out.printf("|  " + board[i][j] + "  ");
        }
        System.out.println("|");
        System.out.println("|         |         |         |");
        System.out.println(" -----------------------------");
        if (i<2)
            System.out.println("|         |         |         |");
    }
    return;
}


//method checkWin checks all possible winning combinations for both players.
private static int checkWin(Tiles board[][]) {
    if (board[0][0] == Tiles.X && board[0][1] == Tiles.X && board[0][2] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[0][0] == Tiles.X && board[1][1] == Tiles.X && board[2][2] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[0][0] == Tiles.X && board[1][0] == Tiles.X && board[2][0] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[1][0] == Tiles.X && board[1][1] == Tiles.X && board[1][2] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[2][0] == Tiles.X && board[2][1] == Tiles.X && board[2][2] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[0][1] == Tiles.X && board[1][1] == Tiles.X && board[2][1] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[0][2] == Tiles.X && board[1][2] == Tiles.X && board[2][2] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }
    else if (board[2][0] == Tiles.X && board[1][1] == Tiles.X && board[0][2] == Tiles.X){
        System.out.println("Player X wins!");
        return 1;
    }

    //check if player O wins
    else if (board[0][0] == Tiles.O && board[0][1] == Tiles.O && board[0][2] == Tiles.O){
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[0][0] == Tiles.O && board[1][1] == Tiles.O && board[2][2] == Tiles.O){
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[0][0] == Tiles.O && board[1][0] == Tiles.O && board[2][0] == Tiles.O){
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[1][0] == Tiles.O && board[1][1] == Tiles.O && board[1][2] == Tiles.O){
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[2][0] == Tiles.O && board[2][1] == Tiles.O && board[2][2] == Tiles.O) {
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[0][1] == Tiles.O && board[1][1] == Tiles.O && board[2][1] == Tiles.O) {
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[0][2] == Tiles.O && board[1][2] == Tiles.O && board[2][2] == Tiles.O){
        System.out.println("Player O wins!");
        return 0;
    }
    else if (board[2][0] == Tiles.O && board[1][1] == Tiles.O && board[0][2] == Tiles.O) {
        System.out.println("Player O wins!");
        return 0;
    }
    else 
        return 2;
}
}
4

2 に答える 2

0

X を配置する 2 回目の試行で問題が発生します (スペースが O で占められている場合)。貼り付けたコードを O からコピーし、プレーヤーとタイルを変更していません。

System.out.println("Player O's turn.");
board[row][col] = Tiles.O;

このコードを読むのは大変でした - _ - しかし、いずれは良くなるでしょう ;) .

于 2013-11-09T19:28:12.657 に答える
0

レターをボードに印刷するときにこれが発生する理由(Macで実行しました)は、レターを印刷するときにprintlnを使用しているためです... printfを使用する必要があるブロックごとの一定量のスペース...たとえば:

System.out.printf("%10s", LetterVariableForTurn);

これにより、LetterVariableForTurn に 10 スペースのテキストを書き込みます。

ここにあなたを助けるためのリンクがあります:printf

お役に立てれば!

于 2013-11-09T19:29:12.197 に答える