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

Took too long to edit the previous one...

OK, let me comment on your design a bit, but you do not need to change anything as long as you feel comfortable with yours.

Your Board class has 1 2D-array and 1 1D-array which contain the display and the current valued marked by players respectively. You could simply use only 1 2D-array to do both jobs. The only thing you need to work on is how to convert a position integer to a 2D-array index. Anyway, it could be one of your improvement after you fix everything.

Now, let's get down to your problem.

In your "Board" class line 64, you allow another player to overwrite the mark value. That will create another problem in the future.

In your "Player" class line 40, you should use equals() method instead.

Your "setCurrentPlayer()" does nothing... You have "player" and "currentPlayer" variables, but you do NOT swap players while you do the "switch" player. You have not even do anything with variable "player" but initiate it as an empty object and call it...

What you need to do is to create 2 players object, not 1 in your board. Then you need to properly correct the player data and assign them to each player correctly. You could use the currentPlayer variable to return a correct player. That's all you need right now.

Thanks,
are you refering to this here:

public boolean markPlayerMove(int play) {
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
if (board[i1] == Character.forDigit(play, 10)) {
board[i1] = (getCurrentPlayer() == 1) ? player.getSymbol1() : player.getSymbol2();
return true;
}
}
}
return false;
}

so do I need to have two of these instead of combining them? Thanks again.

Yes. I tried your code and the symbol got lost after you finish the assignment. Also, if you start with "X" the second player symbol will disappear as well.

I really don't know how to fix this part. I tried copying the whole thing and changing the name, but I don't know how to divide the two up without breaking the code. Any pointers will be beter than throwing my computer across the room.

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.