I am having trouble with the last three methods of my program in the Board.java. I'm not sure how to go get it to read gameBoard. It keeps giving me errors. I really cant figure out what I'm doing wrong. All the methods were given to me and cannot be taken out or put in different classes.

Board.java

import javax.swing.JOptionPane;

    public class Board {

        private char player1;
        private char player2;
        private int playerTurn;
        private static final String EMPTY = null;
        private static final char BLANK = 0;
        private char[] current = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        public void drawBoard() {
            String message = " ";

            for (int i = 0; i < current.length; i++) {
                char c = current[i];
                message += c + "  |  ";
                if (i == 2) {
                    message += "\n----+----+----\n";
                }
                if (i == 5) {
                    message += "\n----+----+----\n";
                }

            }

            JOptionPane.showMessageDialog(null, message, "Tic Tac Toe",
                    JOptionPane.INFORMATION_MESSAGE);
            // insert code here draw the board to the screen
        }

        public void markPlayerMove(int row, int col) {

            playerTurn = 1;
            // Initialize boolean move
            boolean move;

            // Checks to see if board location is occupied and a move can be made
            if (gameBoard[row][col] == ' ') {
                gameBoard[row][col] = player1;
                return true;
            } else {
                // The specified cell is already occupied.
                move = false;
            }

            return move;

            // insert code here to modified the current board based on the players
            // choice

        }

        public boolean checkWin() {

            // First check rows and columns
            int winner = 0;
            // Check for winner in row 1
            if (gameBoard[0][0] == gameBoard[0][1]
                    && gameBoard[0][0] == gameBoard[0][2] && gameBoard[0][0] != ' ') {
                winner = 1;
            }
            // Check for winner in row 2
            if (gameBoard[1][0] == gameBoard[1][1]
                    && gameBoard[1][0] == gameBoard[1][2] && gameBoard[1][0] != ' ') {
                winner = 1;
            }
            // Check for winner in row 3
            if (gameBoard[2][0] == gameBoard[2][1]
                    && gameBoard[2][0] == gameBoard[2][2] && gameBoard[2][0] != ' ') {
                winner = 1;
            }
            // Check for winner in col 1
            if (gameBoard[0][0] == gameBoard[1][0]
                    && gameBoard[0][0] == gameBoard[2][0] && gameBoard[0][0] != ' ') {
                winner = 1;
            }
            // Check for winner in col 2
            if (gameBoard[0][1] == gameBoard[1][1]
                    && gameBoard[0][1] == gameBoard[2][1] && gameBoard[0][1] != ' ') {
                winner = 1;
            }
            // Check for winner in col 3
            if (gameBoard[0][2] == gameBoard[1][2]
                    && gameBoard[0][2] == gameBoard[2][2] && gameBoard[0][2] != ' ') {
                winner = 1;
            }
            // Check for winner in first diagonal
            if (gameBoard[0][0] == gameBoard[1][1]
                    && gameBoard[0][0] == gameBoard[2][2] && gameBoard[0][0] != ' ') {
                winner = 1;
            }
            // Check for winner in 2nd diagonal
            if (gameBoard[2][0] == gameBoard[1][1]
                    && gameBoard[2][0] == gameBoard[0][2] && gameBoard[2][0] != ' ') {
                winner = 1;
            }

            // insert code here to check after a turn to see if someone has won (or
            // tied)

            return false;

        }

        public void resetBoard() {
            for (int i = 0; i < 9; ++i)
                current[i] = BLANK;

            // insert code here to clear all Xs and Os from the board so a new game
            // can begin
        }
    }

Game.java

import javax.swing.JOptionPane;

public class Game {
    Player player1;
    Player player2;
    Board gameBoard;

    public Game() {
        player1 = new Player('X');
        player2 = new Player('O');
        gameBoard = new Board();
    }

    public void play() {
        boolean playAgain = true;
        boolean won = false;
        // player1.setName
        // insert code here play the game...give each player a turn and when the
        // game is over
        // show how many wins each has and ask if they would like to play again
        player1.collectPlayerData("Player 1");
        player2.collectPlayerData("Player 2");

        while (playAgain) {
            while (!won) {
                gameBoard.drawBoard();
                String message = player1.getName() + " your move.";
                int number = Integer.parseInt(JOptionPane
                        .showInputDialog(message));
                gameBoard.markPlayerMove(number, player1.getSymbol()); // you
                                                                        // need
                                                                        // to
                                                                        // write
                                                                        // the
                                                                        // code
                                                                        // to
                                                                        // make
                                                                        // the
                                                                        // player
                                                                        // move
                won = gameBoard.checkWin(); // you need to write the code to see
                                            // if player1 won.

                if (!won) {
                    gameBoard.drawBoard();
                    message = player2.getName() + " your move.";
                    number = Integer.parseInt(JOptionPane
                            .showInputDialog(message));
                    gameBoard.markPlayerMove(number, player2.getSymbol()); // you
                                                                            // need
                                                                            // to
                                                                            // write
                                                                            // the
                                                                            // code
                                                                            // to
                                                                            // make
                                                                            // the
                                                                            // player
                                                                            // move
                    won = gameBoard.checkWin(); // you need to write the code to
                                                // see if player2 won.
                    if( won)
                        player2.setWinCount();
                } else {
                    player1.setWinCount();
                }

            }

             String playAgainMessage = "Do you wish to play again?";
        }
    }

}

Player.java

import javax.swing.JOptionPane;


public class Player {
    private char symbol;  //X or O only
    private String name;
    private int winCount;

    public Player(char letter) {
        setSymbol(letter);
    }



    public void collectPlayerData(String player)
    {

        //insert code here get the players information
        name = JOptionPane.showInputDialog(player + " enter your name");
        symbol = JOptionPane.showInputDialog("Enter X or O").charAt(0);

    }



    /**
     * @return the symbol
     */
    public char getSymbol() {
        return symbol;
    }



    /**
     * @param symbol the symbol to set
     */
    public void setSymbol(char symbol) {
        this.symbol = symbol;
    }



    /**
     * @return the name
     */
    public String getName() {
        return name;
    }



    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }



    /**
     * @return the winCount
     */
    public int getWinCount() {
        return winCount;
    }



    /**
     * @param winCount the winCount to set
     */
    public void setWinCount() {
        this.winCount++;
    }
    }

TicTacToe.java

public class TicTacToe {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
           Game ttt = new Game();
           ttt.play();



    }

}

Recommended Answers

All 9 Replies

also 'It keeps giving me errors.' is a pretty vague description.
if what Yetzderixx mentioned doens't solve all your problems, you may want to specify the 'errors' part.

remember your board can have 3 values for each field, not 2. X, O, and blank.
Best make that an enum, something like

public enum FieldState {
  STATE_X('x'),
  STATE_O('o'),
  STATE_BLANK(' ');

  private value;
  private FieldState(char val) {
    value = val;
  }

  public char getValue() {
    return value;
  }
}

just noticed: in your Board class, in about every line you have a reference to the 2D array of chars gameBoard, which doesn't exist.

you do have a variable gameBoard in your Game class, but even there it isn't a 2D array. lot of re-thinking and refactoring to do before you'll get that to work.

Nice enum jwenting! I ended up using an object for this when I had to do this for homework, but I would add a setter method or everytime the state changes you'll have instantiate a new enum.

you'll have instantiate a new enum.

?
You can't instantiate enums. They are created some time before they are first needed and that's it.

ahh, well I don't use them in Java at all, in that case I'd probably go with an object just to reduce confusion myself.

or learn something new rather than mindlessly rehashing the few tricks you already know...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.