Please help. I ask the users to give me a char (either X or O), but when the users play the game, all they see are squares. Thanks!!
//game class
import java.util.Scanner;
public class Game {
Player player1;
Player player2;
Board gameBoard;
public Game()
{
player1 = new Player();
player2 = new Player();
gameBoard = new Board();
}
public void play()
{
Scanner input = new Scanner( System.in );
Player player = new Player();
Board board = new Board();
String choice;
int gameCount=0;
player.collectPlayerData();
do {
board.resetBoard();
System.out.println();
System.out.println();
System.out.println(board.drawBoard());
System.out.println();
String players = null;
while (!board.checkWin() && board.getWinCount() < 9) {
players = board.getCurrentPlayer() == 1 ? player.getName1 () : player.getName2();
boolean validPick = false;
while (!validPick) {
System.out.print("It is " + players + "'s turn. Pick a square: ");
String square = player.getPrompt();
if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) {
int pick = 0;
try {
pick = Integer.parseInt(square);
} catch (NumberFormatException e) {
}
validPick = board.markPlayerMove(pick);
}
if (!validPick) {
System.out.println("Square can not be selected. Retry");
}
}
board.switchPlayers();
System.out.println();
System.out.println(board.drawBoard());
System.out.println();
}
if (board.checkWin()) {
System.out.println("Game Over - " + players + " WINS!!!");
}
else {
System.out.println("Game Over - Draw");
}
System.out.println();
gameCount++;
System.out.print("Play again? (Y/N): ");
choice = input.next();
} while (choice.trim().toUpperCase().equals("Y"));
System.out.printf("You played %d games.\n" , gameCount);
}
}// end game class
// board class
public class Board {
Player player = new Player();
private char[] current = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
private char[][] board = new char[3][3];
private int currentPlayer;
private int winCount;
public String drawBoard() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
builder.append(" " + board[i][i1] + " ");
}
builder.append("\n");
}
return builder.toString();
}
public void resetBoard()
{
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
board[i][i1] = Character.forDigit(++counter, 10);
}
}
currentPlayer = 1;
winCount = 0;
}
public int getCurrentPlayer() {
return currentPlayer;
}
public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
public int getWinCount() {
return winCount;
}
public void setWinCount(int winCount) {
this.winCount = winCount;
}
public void switchPlayers() {
if (getCurrentPlayer() == 1) {
setCurrentPlayer(2);
} else {
setCurrentPlayer(1);
}
setWinCount(getWinCount() + 1);
}
public boolean markPlayerMove(int play) {
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
if (board[i][i1] == Character.forDigit(play, 10)) {
board[i][i1] = (getCurrentPlayer() == 1) ? player.getSymbol1() : player.getSymbol2();
return true;
}
}
}
return false;
}
public boolean checkWin() {
//Checking rows
char current = ' ';
for (int i = 0; i < 3; i++) {
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i][i1])) {
break;
}
if (i1 == 0) {
current = board[i][i1];
} else if (current != board[i][i1]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking columns
for (int i = 0; i < 3; i++) {
current = ' ';
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i1][i])) {
break;
}
if (i1 == 0) {
current = board[i1][i];
} else if (current != board[i1][i]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking diagonals
current = board[0][0];
if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
return true;
}
current = board[2][0];
if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current) {
return true;
}
return false;
}
}// end board class
//player class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Player {
Scanner input = new Scanner( System.in );
public char symbol1; //X or O only
public char symbol2;
private String name1;
private String name2;
private BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
public Player() {
}
public void collectPlayerData()
{
System.out.print("Enter player one's name: ");
setName1(getPrompt());
System.out.print("Enter player two's name: ");
setName2(getPrompt());
boolean symbolOk = false;
while (!symbolOk) {
System.out.print("Select X or O as " + getName2() + "'s symbol: ");
String symbol = getPrompt();
if //(symbol == "O" || symbol == "X" )
(symbol.trim().toUpperCase().equals("O")||symbol.trim().toUpperCase().equals("X"))
{
symbolOk = true;
setSymbol1(symbol.toCharArray()[0]);
if(symbol == "O") {
String symbolB = "X";
setSymbol2(symbolB.toCharArray()[0]);
}
else {
String symbolB = "O";
setSymbol2(symbolB.toCharArray()[0]);
}
}
else {
System.out.println("Invalid symbol, try again");
}
}
}
public String getPrompt() {
String prompt = "";
try {
prompt = reader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return prompt;
}
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public char getSymbol1() {
return symbol1;
}
public void setSymbol1(char symbol1) {
this.symbol1 = symbol1;
}
public char getSymbol2() {
return symbol2;
}
public void setSymbol2(char symbol2) {
this.symbol2 = symbol2;
}
}//end player class
//tic tac toe main
public class TicTacToe {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Game ttt = new Game();
ttt.play();
}
}// end tic tac toe main