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();
}
}