Results 1 to 4 of 4
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Tic Tac Toe, not being able to print the X's and O's in the table.

    Could somebody point me in the right direction how I would go about making the X's and O's appear in the table?

    My code seems to run well besides that.

    Java Code:
    import java.util.*;
    
    class TicTacToe
    {
    	char ttt[][] = new char[3][3];
    	static final char player1 = 'O';
    	static final char player2 = 'X';
    	static final int totalRow = 3, totalColumn = 3;
    	Scanner scan  =new Scanner(System.in);
    
    
    	String playerID(char player)
    	{
    		if (player == player1) {
    			return "player1: "+ player;
    		}
    		else {
    			return "player2: "+ player;
    		}
    	}
    
    	void getPlayerInput(char player)
    	{
    		int row, column;
    
    		boolean CorrectInput = false;
    
    		do
    		{
    			if (playerID(player) != null)	
    			{
    				System.out.print("Player " + player + ", enter your move ( Row [1 - 3] )");
    				row = scan.nextInt() - 1;
    				System.out.print("Player " + player + ", enter your move ( Column [1 - 3] )");
    				column = scan.nextInt() - 1;
    			}
    
    			else 
    			{
    				System.out.print("Player " + player + ", enter your move ( Row [1 - 3] )");
    				row = scan.nextInt() - 1;
    				System.out.print("Player " + player + ", enter your move ( Column [1 - 3] )");
    				column = scan.nextInt() - 1;
    			}
    
    			if (row >= 0 && row < totalRow && column >= 0 && column <totalColumn && ttt[row][column] == 0)
    			{
    				ttt[row][column] = player;
    			}
    			else 
    			{
    				System.out.println("Your entry at (" + (row + 1) + "," + (column + 1) + ") was not found... Enter another coordinate");
    
    			} 
    
    		} while (!CorrectInput);
    
    
    	}
    
    	boolean gameIsDraw()
    	{
    		for (int row = 0; row < totalRow; row++) {
    			for (int col = 0; col < totalColumn; col++) {
    				if (ttt[row][col] == 0) {
    					return true;  // an empty cell found, not draw, exit
    				}
    			}
    		}
    		return false;  // no empty cell, it's a draw
    	}
    
    	boolean winner(char player)
    	{
    		 {
    		        boolean win;
    		        
    		        if (ttt [0][1] == ttt [1][1] && ttt [1][1] == ttt [2][1])
    		            win = true;
    		        else
    		        if (ttt [0][2] == ttt [1][2] && ttt [1][2] == ttt[2][2])
    		            win = true;
    		        else
    		        if (ttt [0][0] == ttt [1][1] && ttt[1][1] == ttt [2][2])
    		            win = true;
    		        else
    		        if (ttt [2][0] == ttt [1][1] && ttt [1][1] == ttt [0][2])
    		            win = true;
    		        else  
    		        if (ttt [0][0] == ttt [0][1] && ttt [0][1] == ttt[0][2])
    		            win = true;
    		        else
    		        if (ttt [1][0] ==ttt [1][1] && ttt [1][1] == ttt[1][2])
    		            win = true;
    		        else
    		        if (ttt [2][0] == ttt [2][1] && ttt [2][1] == ttt [2][2])
    		            win = true;
    		        else
    		        if (ttt [0][0] == ttt[1][0] && ttt [1][0] == ttt[2][0])
    		            win = true;
    		        else
    		            win = false;
    		        
    		        return win;
    		    }
    	}
    
    	void displayBoard()
    	{
    		System.out.print(("  1   2   3"));
    		System.out.println();
    		for (int i = 0; i < 3; i++) {
    			System.out.println("  ----------");
    			System.out.print((i + 1));
    
    			for (int j = 0; j < 3; j++) {
    				System.out.print(" |" + ttt[i][j]);
    
    			}
    			System.out.print(" |");
    			System.out.println();
    		}
    		System.out.println("  ----------");
    	}
    
    	void newgame()
    	{
    		char currPlayer = player1;
    		for(int i=0; i<3; i++)
    			for(int j=0; j<3; j++)
    				ttt[i][j] =' ';
    
    		boolean continueFlag = true;        
    		while (continueFlag)
    		{
    			displayBoard();
    			if (gameIsDraw())
    			{
    				System.out.println("Game Ends in Draw");
    				continueFlag = false;
    			}
    			else
    			{
    				getPlayerInput(currPlayer);
    				if (winner(currPlayer))
    				{
    					System.out.println("We have a winner: " + playerID(currPlayer));
    					displayBoard();
    					continueFlag = false;
    				}
    				else
    				{ 
    					if (currPlayer == player1) currPlayer = player2;
    					else currPlayer = player1;
    				}
    			}
    		}
    
    	}
    
    
    	public static void main(String[] args)
    	{
    		TicTacToe game = new TicTacToe();
    		String str;
    		do
    		{
    			game.newgame();
    
    			System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
    			str= game.scan.next();
    		} while ("y".equals(str));
    
    		System.out.println("Bye");
    	}    
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    351
    Rep Power
    4

    Default Re: Tic Tac Toe, not being able to print the X's and O's in the table.

    Hi, can you please explain your exact problem. i.e What it does and what it doesn't do.

    Regards.

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Tic Tac Toe, not being able to print the X's and O's in the table.

    Quote Originally Posted by Ronin View Post
    Hi, can you please explain your exact problem. i.e What it does and what it doesn't do.

    Regards.
    When the user inputs coordinates of the Tic Tac Toe board it doesn't read it and place the X's or O's on the board.

    This is a example of my output.


    1 2 3
    ----------
    1 | | | |
    ----------
    2 | | | |
    ----------
    3 | | | |
    ----------
    Player O, enter your move ( Row [1 - 3] )1
    Player O, enter your move ( Column [1 - 3] )2
    Your entry at (1,2) was not found... Enter another coordinate

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    351
    Rep Power
    4

    Default Re: Tic Tac Toe, not being able to print the X's and O's in the table.

    The fact you are seeing the message indicates the condition on line 46 is not being met. There is something with the condition statement which is never going to be met but which one it is?

    Your code on line 30 is also a little off. playerID() return a string which should be compared using the equals() method. In general the lines 30 to 44 are introducing dedunant code. At the moment it says if the condition is met perform the code, but if the condition is not met perform the exact same code.

    Regards.

Similar Threads

  1. Cant print the contents of a table
    By tudorH in forum JDBC
    Replies: 2
    Last Post: 03-20-2010, 06:53 PM
  2. Print the table in struts...
    By makpandian in forum Web Frameworks
    Replies: 1
    Last Post: 09-22-2009, 06:13 PM
  3. Print table
    By Implode in forum New To Java
    Replies: 7
    Last Post: 09-12-2009, 03:43 PM
  4. how to print out lines in a table- like form?
    By helloworld in forum New To Java
    Replies: 5
    Last Post: 02-10-2009, 09:31 PM
  5. How to print selected items in a SWT table
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •