Basic Tic Tac Toe

Design and Build a Basic Tic Tac toe

Page 1 of 1

7 Replies - 25069 Views - Last Post: 27 December 2008 - 01:35 AM Rate Topic: -----

#1 mysticraven2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-December 08

Basic Tic Tac Toe

Posted 26 December 2008 - 12:01 PM

In my Java programming class we were asked to code a basic tic tac toe board. I've coded most of the project, but its not working. We have two classes one being the board, and one being the game. I'm looking for help to make the two communicate as they should increment the turns between the two people. Its like I am missing a few key steps. Another issue I am having is printing the board in a table pattern. I tried using two different codes for printing each row of the table, but it either prints as one line, or 9 lines. Please assist, code that I have is as follows:

Board:

public class TTTBoard 
{
	private int[][] _theBoard;
	private int _currentTurn;
	
	public TTTBoard(int initialTurn)
	{
		_currentTurn = initialTurn;
		_theBoard = new int[3][3];
		
		//Initialize Board
		for (int j = 0; j < 3; j++)
			for(int i =0; i < 3; i++)
				_theBoard[i][j] = 0;
	}
	public void displayBoard()
	{
		for (int j = 0; j < 3; j++)
			System.out.print(_theBoard[0][j] + "\n");
		for (int j = 0; j < 3; j++)
			System.out.print(_theBoard[1][j] + "\n");
		for (int j = 0; j < 3; j++)
			System.out.print(_theBoard[2][j] + "\n");
	}
	public void changeTurn()
	{
		if (_currentTurn == 1)
		{
			_currentTurn = 2;
		}
		if (_currentTurn == 2)
		{
			_currentTurn = 1;
		}
	}
	public int getCurrentTurn()
	{
		return _currentTurn;
	}
	public boolean isComplete()
	{
		boolean complete = true;
		for (int j = 0; j < 3; j++)
			for(int i =0; i < 3; i++)
				_theBoard[i][j] = 0;
					complete = false;
		return complete;
	}
	public void makeMove(int row, int col)
	{
	int move = _theBoard[row][col];
		for (int i =0; i < 3; i++)
			for (int j =0; j < 3; j++)
				if (_theBoard[i][j] == 1 | _theBoard[i][j] == 2)
					System.out.print("Space is currently occuied, place make another selection.");
					move = _theBoard[row][col];
	}
	public int isWinner()
	{
		int winner = 0;
		
		//check to see if there is a winner
		if (_theBoard[0][0] == _theBoard [0][1] && _theBoard[0][0] == _theBoard[0][2])
		{
			if (_theBoard[0][0] != 0)
				winner = _theBoard[0][0];
		}
		if (_theBoard[1][0] == _theBoard [1][1] && _theBoard [1][0] == _theBoard[1][2])
		{	
			if (_theBoard[1][0] != 0)
				winner = _theBoard[1][0];
		}
		if (_theBoard[2][0] == _theBoard[2][1] && _theBoard[2][0] == _theBoard[2][2])
		{
			if (_theBoard[2][0] != 0)
				winner = _theBoard[2][0];
		}
		
		if (_theBoard[0][1] == _theBoard [1][1] && _theBoard [0][1] == _theBoard[2][1])
		{	
			if (_theBoard[0][1] != 0)
				winner = _theBoard[0][1];
		}	   
		if (_theBoard[0][2] == _theBoard [1][2] && _theBoard [0][2] == _theBoard[2][2])
		{	
			if (_theBoard[0][2] != 0)
				winner = _theBoard[0][2];
		}
		if (_theBoard[0][1] == _theBoard [1][1] && _theBoard [0][1] == _theBoard[2][1])
		{	
			if (_theBoard[0][1] != 0)
				winner = _theBoard[0][1];
		}
		if (_theBoard[0][0] == _theBoard [1][1] && _theBoard [0][0] == _theBoard[2][2])
		{	
			if (_theBoard[0][0] != 0)
				winner = _theBoard[0][0];
		}
		if (_theBoard[2][0] == _theBoard [1][1] && _theBoard [2][0] == _theBoard[0][2])
		{	
			if (_theBoard[2][0] != 0)
				winner = _theBoard[2][0];
		}
		return winner;
	}
}



Game:
import java.util.Scanner;
public class TTTGame 
{
	public static void main(String[] args)
	{
		TTTBoard myBoard = new TTTBoard(1);
		Scanner input = new Scanner(System.in);
		int turn;
		int moveRow;
		int moveCol;
		int number;
	
		//Begin Game with statements below
		while (myBoard.isWinner() == 0 && myBoard.isComplete() == false)
		{
			myBoard.displayBoard();
			turn = myBoard.getCurrentTurn();
			System.out.println("Player " + turn + " Make your move.");
			
			//Input of numbers - 1 to get Row and Col moves
			System.out.println("Please enter row (1-3) of your move:");
			number = input.nextInt();
			moveRow = number - 1;
			System.out.println("Please enter column (1-3) of your move:");
			number = input.nextInt();
			moveCol = number - 1;
			myBoard.makeMove(moveRow, moveCol);
			myBoard.changeTurn();
		}
	
		//Make Moves
		turn = myBoard.getCurrentTurn();
		System.out.println("Player " + turn + " Make a valid move.");
		System.out.println("Please enter row (1-3) of your move:");
		number = input.nextInt();
		moveRow = number - 1;
		System.out.println("Please enter column (1-3) of your move:");
		number = input.nextInt();
		moveCol = number -1;
		myBoard.makeMove(moveRow, moveCol);
		
		System.out.println("The winner is " + myBoard.isWinner());
		if (myBoard.isWinner() != 0)
		{
			System.out.println("CATS GAME");
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Basic Tic Tac Toe

#2 mysticraven2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-December 08

Re: Basic Tic Tac Toe

Posted 26 December 2008 - 12:18 PM

In my Java programming class we were asked to code a basic tic tac toe board. I've coded most of the project, but its not working. We have two classes one being the board, and one being the game. I'm looking for help to make the two communicate as they should increment the turns between the two people. Its like I am missing a few key steps. Another issue I am having is printing the board in a table pattern. I tried using two different codes for printing each row of the table, but it either prints as one line, or 9 lines. The other catch to this is that it has to be beginner code, nothing advanced like buttons and tokens. Please assist, code that I have is as follows:

Board:


public class TTTBoard 
{
	private int[][] _theBoard;
	private int _currentTurn;
	
	public TTTBoard(int initialTurn)
	{
		_currentTurn = initialTurn;
		_theBoard = new int[3][3];
		
		//Initialize Board
		for (int j = 0; j < 3; j++)
			for(int i =0; i < 3; i++)
				_theBoard[i][j] = 0;
	}
	public void displayBoard()
	{
		for (int j = 0; j < 3; j++)
			System.out.print(_theBoard[0][j] + "\n");
		for (int j = 0; j < 3; j++)
			System.out.print(_theBoard[1][j] + "\n");
		for (int j = 0; j < 3; j++)
			System.out.print(_theBoard[2][j] + "\n");
	}
	public void changeTurn()
	{
		if (_currentTurn == 1)
		{
			_currentTurn = 2;
		}
		if (_currentTurn == 2)
		{
			_currentTurn = 1;
		}
	}
	public int getCurrentTurn()
	{
		return _currentTurn;
	}
	public boolean isComplete()
	{
		boolean complete = true;
		for (int j = 0; j < 3; j++)
			for(int i =0; i < 3; i++)
				_theBoard[i][j] = 0;
					complete = false;
		return complete;
	}
	public void makeMove(int row, int col)
	{
	int move = _theBoard[row][col];
		for (int i =0; i < 3; i++)
			for (int j =0; j < 3; j++)
				if (_theBoard[i][j] == 1 | _theBoard[i][j] == 2)
					System.out.print("Space is currently occuied, place make another selection.");
					move = _theBoard[row][col];
	}
	public int isWinner()
	{
		int winner = 0;
		
		//check to see if there is a winner
		if (_theBoard[0][0] == _theBoard [0][1] && _theBoard[0][0] == _theBoard[0][2])
		{
			if (_theBoard[0][0] != 0)
				winner = _theBoard[0][0];
		}
		if (_theBoard[1][0] == _theBoard [1][1] && _theBoard [1][0] == _theBoard[1][2])
		{	
			if (_theBoard[1][0] != 0)
				winner = _theBoard[1][0];
		}
		if (_theBoard[2][0] == _theBoard[2][1] && _theBoard[2][0] == _theBoard[2][2])
		{
			if (_theBoard[2][0] != 0)
				winner = _theBoard[2][0];
		}
		
		if (_theBoard[0][1] == _theBoard [1][1] && _theBoard [0][1] == _theBoard[2][1])
		{	
			if (_theBoard[0][1] != 0)
				winner = _theBoard[0][1];
		}	   
		if (_theBoard[0][2] == _theBoard [1][2] && _theBoard [0][2] == _theBoard[2][2])
		{	
			if (_theBoard[0][2] != 0)
				winner = _theBoard[0][2];
		}
		if (_theBoard[0][1] == _theBoard [1][1] && _theBoard [0][1] == _theBoard[2][1])
		{	
			if (_theBoard[0][1] != 0)
				winner = _theBoard[0][1];
		}
		if (_theBoard[0][0] == _theBoard [1][1] && _theBoard [0][0] == _theBoard[2][2])
		{	
			if (_theBoard[0][0] != 0)
				winner = _theBoard[0][0];
		}
		if (_theBoard[2][0] == _theBoard [1][1] && _theBoard [2][0] == _theBoard[0][2])
		{	
			if (_theBoard[2][0] != 0)
				winner = _theBoard[2][0];
		}
		return winner;
	}
}



Game:
import java.util.Scanner;
public class TTTGame 
{
	public static void main(String[] args)
	{
		TTTBoard myBoard = new TTTBoard(1);
		Scanner input = new Scanner(System.in);
		int turn;
		int moveRow;
		int moveCol;
		int number;
	
		//Begin Game with statements below
		while (myBoard.isWinner() == 0 && myBoard.isComplete() == false)
		{
			myBoard.displayBoard();
			turn = myBoard.getCurrentTurn();
			System.out.println("Player " + turn + " Make your move.");
			
			//Input of numbers - 1 to get Row and Col moves
			System.out.println("Please enter row (1-3) of your move:");
			number = input.nextInt();
			moveRow = number - 1;
			System.out.println("Please enter column (1-3) of your move:");
			number = input.nextInt();
			moveCol = number - 1;
			myBoard.makeMove(moveRow, moveCol);
			myBoard.changeTurn();
		}
	
		//Make Moves
		turn = myBoard.getCurrentTurn();
		System.out.println("Player " + turn + " Make a valid move.");
		System.out.println("Please enter row (1-3) of your move:");
		number = input.nextInt();
		moveRow = number - 1;
		System.out.println("Please enter column (1-3) of your move:");
		number = input.nextInt();
		moveCol = number -1;
		myBoard.makeMove(moveRow, moveCol);
		
		System.out.println("The winner is " + myBoard.isWinner());
		if (myBoard.isWinner() != 0)
		{
			System.out.println("CATS GAME");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Basic Tic Tac Toe

Posted 26 December 2008 - 02:35 PM

Topics merged. Please avoid double postings
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Basic Tic Tac Toe

Posted 26 December 2008 - 03:28 PM

Do you have to use 2 dimensional arrays? It's much simpler to think of the TTT board as a one dimensional array with 9 locations.
Was This Post Helpful? 0
  • +
  • -

#5 mysticraven2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-December 08

Re: Basic Tic Tac Toe

Posted 26 December 2008 - 03:45 PM

It has to use a 2 dimensional array, that is the one thing that was required. The reason being is that its a 3x3 table, and we aren't using x's and o's we are using the player values: 1 and 2.

This post has been edited by mysticraven2k4: 26 December 2008 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Basic Tic Tac Toe

Posted 26 December 2008 - 04:00 PM

Ok. :)

Change displayBoard() to use spaces instead of newlines.
...
for (int j = 0; j < 3; j++)
	 System.out.print(_theBoard[0][j] + " ");

System.out.println();
...



A couple more things. :)
In makeMove() why are you scanning the board array? Just check to see if the user's selected board position is already occupied.
	public void makeMove(int row, int col)
	{
		if (_theBoard[row][col] != 0){
			System.out.print("The square is currently occupied, please make another selection.");
		} else {
			_theBoard[row][col] = _currentTurn;
		}
...		


You probably want to change makeMove() to return a value indicating that the move was good or bad. If bad, you'll want to loop on makeMove() until the user makes a good move. :)

ChangeTurn() has a problem because you are checking it twice. Use this instead
public void changeTurn()	{
	 if (_currentTurn == 1)  {
		  _currentTurn = 2;
	 } else {
		  _currentTurn = 1;
	 }
}


This post has been edited by n8wxs: 26 December 2008 - 04:24 PM

Was This Post Helpful? 1
  • +
  • -

#7 mysticraven2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-December 08

Re: Basic Tic Tac Toe

Posted 26 December 2008 - 04:33 PM

So the display of a 3x3 is now working, were there other issues with my code? Did it increment between 2 players from what you see between the two sets of code?
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Basic Tic Tac Toe

Posted 27 December 2008 - 01:35 AM

View Postmysticraven2k4, on 26 Dec, 2008 - 03:33 PM, said:

So the display of a 3x3 is now working, were there other issues with my code? Did it increment between 2 players from what you see between the two sets of code?


If you have questions about the state of your code now please post the latest version of your code (with the fixes that made the 3x3 display work).

My request would be that before you repost your code you go through your code and ensure that you have correctly applied braces { } and indentation as and when appropriate. You may well find that the process of 'scrubbing' your code will allow you to find and correct errors all by yourself and it will definitely help others help you to fix your code.

This discussion may help reinforce the value of good code style:
http://www.dreaminco...wtopic76884.htm

As an example, this jumps out at me as being a possible problem.
	public boolean isComplete()
	{
		boolean complete = true;
		for (int j = 0; j < 3; j++)
			for(int i =0; i < 3; i++)
				_theBoard[i][j] = 0;
					complete = false;
		return complete;
	}


What is this
complete = false;
doing indented over there but not inside any braces?
I haven't thought about this deeply but I think you meant this to work like this:
public boolean isComplete()
{
	boolean complete = true;
	for (int j = 0; j < 3; j++) {
		for(int i =0; i < 3; i++) {
			_theBoard[i][j] = 0;
			complete = false;
		}
	}
	return complete;
}


But as you have written it the compiler will treat it like this:
public boolean isComplete()
{
	boolean complete = true;
	for (int j = 0; j < 3; j++) {
		for(int i =0; i < 3; i++) {
			_theBoard[i][j] = 0;
		}
	}
	complete = false;
	return complete;
}



I have no idea what you thought either of those would achieve within the overall logic of your program but that's something for you to be comfortable with not for me to question.

This is clear example of the point Martyr2 makes at the link I referred you to above in the value of always wrapping code blocks in braces rather than the dangerous practice of leaving one line code block unwrapped.

Please also make a decision about whether you will go with "for(...)" or "for (...)" and try and be consistent. It's a tiny point but it's that sort of consistency that sets the truly serious apart from the rest. When code is consistent on points like that "grepping" and "find and replace" is so much more powerful than when it is all over the place.

This post has been edited by janotte: 27 December 2008 - 01:38 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1