Need help with a basic Tic Tac Toe 2 Players (no AI)

Step by step.

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4195 Views - Last Post: 25 March 2009 - 05:08 PM Rate Topic: -----

#1 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 03:24 PM

I'm a newcomer so I want to say hi to everyone.

I am having a homework for making a TicTacToe game for 2 human players which contains 1 array 3x3.

1) I want to be able to print the board like this:
I :crazy:I :crazy:I :crazy:I
I----I----I----I
I :crazy:I :crazy:I :crazy:I
I----I----I----I
I :crazy:I :crazy:I :crazy:I

And here is the source code I wrote but it did not print out anything in Ellipse:
public class Board
{
	public static void main (String [] args)
	{
		char [][] gameBoard = new char [3][3];
	}	
	public void printBoard ()
	{
		// Should I declare array gameBoard in main or in this printBoard method. If I put it in main, I get the error below.
		
		for (int x = 0; x < 3; x++)
		{
			for (int y = 0; y < 3; y++)
			{
				System.out.println ("I " + gameBoard [x][y] + " I"); // Error: gameBoard cannot be resolved???
				System.out.println ("I---");
			}
		}
		System.out.println();
	}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a basic Tic Tac Toe 2 Players (no AI)

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 03:34 PM

gameBoard has a local scope, so to resolve this problem
public static void main (String [] args)
	{
		char [][] gameBoard = new char [3][3];
	}	
	public static void printBoard (char [][] gameBoard)
	{
		// Should I declare array gameBoard in main or in this printBoard method. If I put it in main, I get the error below.
	   
		for (int x = 0; x < 3; x++)
		{
			for (int y = 0; y < 3; y++)
			{
				System.out.println ("I " + gameBoard [x][y] + " I"); // Error: gameBoard cannot be resolved???
				System.out.println ("I---");
			}
		}
		System.out.println();
	}	
}


try this
Was This Post Helpful? 0
  • +
  • -

#3 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 03:58 PM

View Postmostyfriedman, on 10 Mar, 2009 - 02:34 PM, said:

gameBoard has a local scope, so to resolve this problem
public static void main (String [] args)
	{
		char [][] gameBoard = new char [3][3];
	}	
	public static void printBoard (char [][] gameBoard)
	{
		
	   
		for (int x = 0; x < 3; x++)
		{
			for (int y = 0; y < 3; y++)
			{
				System.out.println ("I " + gameBoard [x][y] + " I"); 
				System.out.println ("I---");
			}
		}
		System.out.println();
	}	
}


try this


Thank you for the quick reply.
It still does not print the board. Nothing shows in Eclipse when I run it.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 04:04 PM

that's because you need to call the method in the main
public static void main (String [] args)
{
		char [][] gameBoard = new char [3][3];
		printBoard(gameBoard);
} 


Was This Post Helpful? 0
  • +
  • -

#5 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 04:17 PM

View Postmostyfriedman, on 10 Mar, 2009 - 03:04 PM, said:

that's because you need to call the method in the main


Yes, you're genius :^: . "need to call the method in the main!" I will remember this forever.
I got it printed out now. However, it only print out 1 column of 9 indices. How can I make it into a grid of 3x3?
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 04:51 PM

check this out
public static void printBoard (char[][]gameBoard)
	{
		// Should I declare array gameBoard in main or in this printBoard method. If I put it in main, I get the error below.	   
		for (int x = 0; x <= gameBoard.length; x++)
		{
			for(int y = 0; y < gameBoard.length; y++)
			{
				System.out.print ("I " + "---" ); // Error: gameBoard cannot be resolved???
			}
			System.out.print("I");
			System.out.println();
			if(x < gameBoard.length)
			{
				for(int j = 0; j < gameBoard.length; j++)
					System.out.print("   " + gameBoard[x][j] + " ");
			}
			System.out.println();
		}
	}	


however it looks like a piece of crap :P, you may want to think of a better way to do it

This post has been edited by mostyfriedman: 10 March 2009 - 05:01 PM

Was This Post Helpful? 1
  • +
  • -

#7 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 10 March 2009 - 10:31 PM

View Postmostyfriedman, on 10 Mar, 2009 - 03:51 PM, said:

check this out
public static void printBoard (char[][]gameBoard)
	{
		// Should I declare array gameBoard in main or in this printBoard method. If I put it in main, I get the error below.	   
		for (int x = 0; x <= gameBoard.length; x++)
		{
			for(int y = 0; y < gameBoard.length; y++)
			{
				System.out.print ("I " + "---" ); // Error: gameBoard cannot be resolved???
			}
			System.out.print("I");
			System.out.println();
			if(x < gameBoard.length)
			{
				for(int j = 0; j < gameBoard.length; j++)
					System.out.print("   " + gameBoard[x][j] + " ");
			}
			System.out.println();
		}
	}	


however it looks like a piece of crap :P, you may want to think of a better way to do it

:w00t: It actually printed out a nice looking board in 2 dimensions. I have a question: why do we need this "if condition and the for loop" to make it 2 dimensions?
if(x < gameBoard.length)
			{
				for(int j = 0; j < gameBoard.length; j++)
				{	
					System.out.print("   " + gameBoard[x][j] + " ");
				}	
			}
			System.out.println();


So what are the other ways? I cannot use UI now because my teacher has not taught it yet.
I have work and Calculus 3 test tomorrow so I have to put this aside until Thursday. Thank you for all those quick replies. I am very appreciated it. When I am able to print the board, I think I can handle the X/O player move and check win methods.
Have a good day tomorrow man. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 11 March 2009 - 11:18 AM

the loop prints the X's and O's in each row of the array.. i used the if statement to avoid an out of bounds exception since the row number i am printing corresponds to the row number of the board..but if you notice there are 4 horizontal lines in the board containing 3 rows of X's and O's so i just used that if statement to not to print when i go past the 3rd row in the rows containing the X's and O's..i hope that made sense to you, to understand it better, it would be better if you trace it
Was This Post Helpful? 0
  • +
  • -

#9 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 12 March 2009 - 09:58 PM

View Postmostyfriedman, on 11 Mar, 2009 - 10:18 AM, said:

the loop prints the X's and O's in each row of the array.. i used the if statement to avoid an out of bounds exception since the row number i am printing corresponds to the row number of the board..but if you notice there are 4 horizontal lines in the board containing 3 rows of X's and O's so i just used that if statement to not to print when i go past the 3rd row in the rows containing the X's and O's..i hope that made sense to you, to understand it better, it would be better if you trace it

After playing with some "System.out.println"s for a while. I can now print an array of 9 indices.
/* Package Imports */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TicTacToe 
{	
	public static final int SIZE = 3;
	private static String [][] gameBoard = new String [3][3];
	
	public static void main (String [] args)
	{	
		boolean checkMove = false;
		while (!checkMove)
		{
			System.out.println(); // Terminate the current line by writing the line separator string (like "go to next paragraph!")
			System.out.println ("Enter Row!");
			int row = readInt ();
			System.out.println ("Enter Column!");
			int col = readInt ();
			xMove(row,col);
		}
	}
	
	public static void xMove (int row, int col)
	{
		gameBoard [row][col] = "X";
		
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println();
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				if (gameBoard[x][y] != "X")
				{
					gameBoard[x][y] = " ";
				}
				System.out.print("I"+gameBoard[x][y]+"I");
			}
		}
	}
	public static void oMove (int row, int col)
	{
		gameBoard [row][col] = "O";
		
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println();
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				System.out.print(gameBoard[x][y]);
			}
		}
	}


	public static String readString()
	{
		String input = null;
			
		// Do a buffered read for efficiency
		try
		{
			BufferedReader reader = new BufferedReader ( new InputStreamReader( System.in ) );
			input = reader.readLine();			
		}
		catch ( Exception ex )
		{
			System.out.println( "Error reading command-line input: " + ex.getMessage() );
			return null;
		}
		
		return input;
	}	
	
	public static int readInt ()
	{
		String input = readString();
		
		if ( input == null )
		{
			return Integer.MAX_VALUE;
		}
			
		//convert to integer
		return Integer.parseInt( input );
		
	}

}


I dont think I need the "[x][j] for loop" to make it 2 dimensional in printing. Computer knows the array 3x3 2D, but it does not show it to me as 2D. So I figure out that "System.out.println" has a use here:
public static void xMove (int row, int col)
	{
		gameBoard [row][col] = "X";
		
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println(); // Terminate ("enter") to next line every 3 indices !!!
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				if (gameBoard[x][y] != "X")
				{
					gameBoard[x][y] = " "; // Replace anything not X with a space
				}
				System.out.print("I"+gameBoard[x][y]+"I"); // Print all indices onto the screen.
			}
		}
	}


I'm so happy to learn something new. Next step, switching between X and O move.

This post has been edited by Orggrim: 12 March 2009 - 10:00 PM

Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 13 March 2009 - 06:03 AM

Quote

I dont think I need the "[x][j] for loop" to make it 2 dimensional in printing.


actually you do need this, this is how you print a 2nd array..you need a nested loop..the outer loop to go through the rows and the inner loop goes through the columns
Was This Post Helpful? 0
  • +
  • -

#11 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 14 March 2009 - 03:37 PM

I'm working on the method to check for invalid move and ask to re-enter the move.
Here is what I think of:
if ((gameBoard[x][y] != "X") && (gameBoard [x][y] != "O"))
				{
					gameBoard[x][y] = " ";
				}
				else if (((gameBoard[x][y].equals ("X")) || (gameBoard [x][y].equals("O"))))
				{
					System.out.println ("Invalid move! Re-enter your move!");
					// Don't know what should I put in here to make the player re-enter... still thinking (30 minutes so far).
				}


Any hint? :)
/* Package Imports */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TicTacToe 
{	
	public static final int SIZE = 3;
	private static String [][] gameBoard = new String [3][3];
	private static int turnCount = 1;
	private static boolean gameOver = false;
	private static boolean checkMove = false;
	public static void main (String [] args)
	{	
		while (!gameOver)
		{
			while (!checkMove)
			{
				System.out.println(); // Terminate the current line by writing the line separator string (like "go to next paragraph!")
				System.out.println ("Player turn!");
				System.out.println ("Enter Row!");
				int row = readInt ();
				System.out.println ("Enter Column!");
				int col = readInt ();
				move(row,col);
				// put checkWin method here
			}
		}
	}
	
	/* Method that:
	 * - allows 2 players to enter move in row and column
	 * - check for invalid move and ask for re-enter row and column*/
	public static void move (int row, int col) 
	{
		turnCount++;
		
		if (turnCount % 2 == 0) // Determine either X player's or O player's move.
		{
			gameBoard [row][col] = "X";
		}
		else
		{
			gameBoard [row][col] = "O";
		}
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println();
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				if ((gameBoard[x][y] != "X") && (gameBoard [x][y] != "O"))
				{
					gameBoard[x][y] = " ";
				}
				else if (((gameBoard[x][y].equals ("X")) && (gameBoard [x][y].equals("O"))))
				{
					// Don't know what should I put in here to make the player re-enter... still thinking (30 minutes so far).
				}
				System.out.print("|"+gameBoard[x][y]+"|");
			}
		}
	}
	
	public static String readString()
	{
		String input = null;
			
		// Do a buffered read for efficiency
		try
		{
			BufferedReader reader = new BufferedReader ( new InputStreamReader( System.in ) );
			input = reader.readLine();			
		}
		catch ( Exception ex )
		{
			System.out.println( "Error reading command-line input: " + ex.getMessage() );
			return null;
		}
		return input;
	}	
	
	public static int readInt ()
	{
		String input = readString();
		
		if ( input == null )
		{
			return Integer.MAX_VALUE;
		}
			
		//convert to integer
		return Integer.parseInt( input );
	}
}


This post has been edited by Orggrim: 14 March 2009 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 14 March 2009 - 03:50 PM

to check if the cell is empty
if(gameBoard[x][y] != null)
	//print invalid whatever and ask him to renter



btw which university are you studying at?
Was This Post Helpful? 0
  • +
  • -

#13 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 14 March 2009 - 03:56 PM

View Postmostyfriedman, on 14 Mar, 2009 - 02:50 PM, said:

to check if the cell is empty
if(gameBoard[x][y] != null)
	//print invalid whatever and ask him to renter



btw which university are you studying at?

Hi again! You're so helpful and kind. I'm Davy.
Currently, I'm in the second year at Golden West Community College in California. I will be transferred to Cal State Fullerton in next Spring. My major is Computer programmer.
Was This Post Helpful? 0
  • +
  • -

#14 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 15 March 2009 - 09:42 AM

I'm still unable to make checkMove method right.
/* Package Imports */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TicTacToe 
{	
	public static final int SIZE = 3;
	private static final String [][] gameBoard = new String [3][3];
	private static int playerTurn;
	private static boolean gameOver = false;
	private static boolean win = false;
	private static boolean doneMove = false;
	public static void main (String [] args)
	{	
		while (!win)
		{
			getPlayerTurn();
			System.out.println();
			System.out.println ("Player turn!");
			System.out.println ("Enter Row!");
			int row = readInt ();
			System.out.println ("Enter Column!");
			int col = readInt ();
			move (row,col);
			checkMove(row,col);
			
		// put checkWin method here
		}
	}
	public static void getPlayerTurn ()
	{
		if (playerTurn == 1)
		{
		playerTurn = 2;
		}
		else 
		{
		playerTurn = 1;
		}
	}
	/* Method that:
	 * - allows 2 players to enter move in row and column
	 * - check for invalid move and ask for re-enter row and column*/
	public static void move (int row, int col) 
	{
		if (playerTurn == 1) // Determine either X player's or O player's move.
		{
			gameBoard [row][col] = "X";
		}
		else
		{
			gameBoard [row][col] = "O";
		}
		
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println();
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				if ((gameBoard[x][y] != "X") && (gameBoard [x][y] != "O"))
				{
					gameBoard[x][y] = " ";
				}
				System.out.print("|"+gameBoard[x][y]+"|");
			}
		}
	}
	
	public static int checkMove (int row, int col)
	{
		if ((playerTurn == 1) && (gameBoard[row][col] != null))
		{
			playerTurn = 2; // to make playerTurn become 1 again in getPlayerTurn method.
		}
		else if ((playerTurn == 2) && (gameBoard[row][col] != null))
		{
			playerTurn = 1; // to make playerTurn become 2 again in getPlayerTurn method.
		}
		return playerTurn;
	}
	public static String readString()
	{
		String input = null;
			
		// Do a buffered read for efficiency
		try
		{
			BufferedReader reader = new BufferedReader ( new InputStreamReader( System.in ) );
			input = reader.readLine();			
		}
		catch ( Exception ex )
		{
			System.out.println( "Error reading command-line input: " + ex.getMessage() );
			return null;
		}
		return input;
	}	
	
	public static int readInt ()
	{
		String input = readString();
		
		if ( input == null )
		{
			return Integer.MAX_VALUE;
		}
			
		//convert to integer
		return Integer.parseInt( input );
	}
}


Is this checking for invalid move so complicated, or is there another simpler way?
Was This Post Helpful? 0
  • +
  • -

#15 Orggrim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-March 09

Re: Need help with a basic Tic Tac Toe 2 Players (no AI)

Posted 15 March 2009 - 11:56 AM

Ok, so I try to use turn counting again because it looks simplier than the changing turn.
/* Package Imports */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TicTacToe 
{	
	public static final int SIZE = 3;
	private static final String [][] gameBoard = new String [3][3];
	private static int turnCount = 1;
	private static int newTurnCount;
	private static boolean gameOver = false;
	private static boolean win = false;
	private static boolean doneMove = false;
	public static void main (String [] args)
	{	
		while (!win)
		{
			System.out.println();
			System.out.println ("Player turn!");
			System.out.println ("Enter Row!");
			int row = readInt ();
			System.out.println ("Enter Column!");
			int col = readInt ();
			checkMove (row,col);
			move (row,col);
			// put checkWin method here
		}
	}
	
	/* Method that:
	 * - allows 2 players to enter move in row and column
	 * */
	public static int move (int row, int col) 
	{
		if (turnCount % 2 == 0) // Determine either X player's or O player's move.
		{
			gameBoard [row][col] = "X";
		}
		else
		{
			gameBoard [row][col] = "O";
		}
		
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println();
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				if ((gameBoard[x][y] != "X") && (gameBoard [x][y] != "O"))
				{
					gameBoard[x][y] = " ";
				}
				System.out.print("|"+gameBoard[x][y]+"|");
			}
		}
		return turnCount;
	}
	
	public static int checkMove (int row, int col)
	{
		if ((turnCount >= 2) && (gameBoard[row][col] != " "))
		{
			System.out.println ("Invalid Move!");
			return turnCount;
		}
		else
		{
			turnCount++;
			return turnCount;
		}
	}
	public static String readString()
	{
		String input = null;
			
		// Do a buffered read for efficiency
		try
		{
			BufferedReader reader = new BufferedReader ( new InputStreamReader( System.in ) );
			input = reader.readLine();			
		}
		catch ( Exception ex )
		{
			System.out.println( "Error reading command-line input: " + ex.getMessage() );
			return null;
		}
		return input;
	}	
	
	public static int readInt ()
	{
		String input = readString();
		
		if ( input == null )
		{
			return Integer.MAX_VALUE;
		}
			
		//convert to integer
		return Integer.parseInt( input );
	}
}


So the checkMove method is used to add turn for the move method. So checkMove only applied on the second move and higher since first move is turnCount = 1.
The problem here is when I test to enter a value into an occupied cell X or O, the value of the cell is changed according to the turnCount. How do I make the X, or O permanent which cannot be changed?
Please give me some advices.

Here is the checkMove method:
public static int checkMove (int row, int col) // This method is responsible for adding turn to turnCount.
	{
		if ((turnCount >= 2) && (gameBoard[row][col] != " "))
		{
			System.out.println ("Invalid Move!");
			return turnCount;
		}
		else
		{
			turnCount++;
			return turnCount;
		}
	}



And here is the move method:
public static int move (int row, int col) 
	{
		if (turnCount % 2 == 0) // Use turnCount to determine either X player's or O player's move.
		{
			gameBoard [row][col] = "X";
		}
		else
		{
			gameBoard [row][col] = "O";
		}
		
		for (int x = 0; x < gameBoard.length; x++)
		{
			System.out.println();
			
			for (int y = 0; y < gameBoard.length; y++)
			{
				if ((gameBoard[x][y] != "X") && (gameBoard [x][y] != "O"))
				{
					gameBoard[x][y] = " ";
				}
				System.out.print("|"+gameBoard[x][y]+"|");
			}
		}
		return turnCount;
	}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2