5 Replies - 1173 Views - Last Post: 27 June 2009 - 05:02 PM Rate Topic: -----

#1 alexnguyen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 17-March 08

TicTacToe Helper Class

Posted 24 June 2009 - 04:03 PM

This is the Helper Class...

public class TTTBoard
{
	// Declare variables
	private int _board[][];
	private int _player;

	public TTTBoard()
	{
		_player = 1;

		_board = new int [3][3];
		for( int row = 0; row < 3; row++ )
			for( int col = 0; col < 3; col++ )
				_board[row][col] = 0;
	}
	
	public boolean makeMove ( int row, int col )
	{
		boolean valid = false;
		for( row = 0; row < 3; row++ )
			for( col = 0; col < 3; col++ )
				if( _board[row][col] == 0 )
				{
					valid = true;
					_board[row][col] = _player;

					if( _player == 1 )
						_player = 2;
					else
						_player = 1;
				}

		return valid;
	}

	public boolean isBoardComplete()
	{
		boolean complete = true;

		for( int row = 0; row < 3; row++ )
			for( int col = 0; col < 3; col++ )
				if( _board[row][col] == 0 )
					complete = false;

		return complete;
	}

	public int determineWinner()
	{
		int winner = 0;

		if( _board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] &&
				_board[1][0] != 0 )
			winner = _board[1][0];

		if( _board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] &&
				_board[2][0] != 0 )
			winner = _board[2][0];

		if( _board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] &&
				_board[0][1] != 0 )
			winner = _board[0][1];

		if( _board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] &&
				_board[0][2] != 0 )
			winner = _board[0][2];

		if( _board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[0][2] == _board[1][1] && _board[0][2] == _board[2][0] &&
				_board[0][2] != 0 )
			winner = _board[0][2];
					
		return winner;
	}

	public void displayBoard()
	{
		for( int row = 0; row < 3; row++ )
			for( int col = 0; col < 3; col++ )
				System.out.printf( " %d", _board[row][col] );
	}

	public int getCurrentPlayer()
	{
		return _player;
	}

}



This is the Main class...

import java.util.Scanner;
/**
 *
 * @author profwilson
 */
public class TTTGame
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		// declare variables including our TTT board
		TTTBoard myGame = new TTTBoard();
		Scanner input = new Scanner(System.in);
		int row;
		int col;
		
		// As long as there is no winner and there are still moves that can
		// be made, then play on
		while(myGame.determineWinner() == 0 && !myGame.isBoardComplete())
		{
			// dispaly the current board state
			myGame.displayBoard();

			// get the current move from the current player
			System.out.println("Player " + myGame.getCurrentPlayer());
			System.out.println("Make your move.");
			System.out.print("Row please (1-3):");
			row = input.nextInt();

			// error trap for valid row
			while(row < 1 || row > 3)
			{
				System.out.println("Invalid Row.");
				System.out.print("Try again (1-3):");
				row = input.nextInt();
			}

			System.out.print("Col please (1-3):");
			col = input.nextInt();

			// error trap for valid col
			while(col < 1 || col > 3)
			{
				System.out.println("Invalid Col.");
				System.out.print("Try again (1-3):");
				col = input.nextInt();
			}

			// while the move is invalid make them make another move
			while(!myGame.makeMove(row, col))
			{
				System.out.println("Invalid Move... Try Again.");
				System.out.print("Row please (1-3):");
				row = input.nextInt();

				// error trap for valid row
				while(row < 1 || row > 3)
				{
					System.out.println("Invalid Row.");
					System.out.print("Try again (1-3):");
					row = input.nextInt();
				}

				System.out.print("Col please (1-3):");
				col = input.nextInt();

				// error trap for valid col
				while(col < 1 || col > 3)
				{
					System.out.println("Invalid Col.");
					System.out.print("Try again (1-3):");
					col = input.nextInt();
				}
			}
		}

		// if we left the loop because the boards full and there's no winner
		// it must be a cats game
		if (myGame.determineWinner() == 0)
		{
			System.out.println("Sorry - Cat's Game");
		}
		else
		{
			// the last player to move won but the board has already advanced
			// the turn, therefore the winner is opposite of whose turn it is
			System.out.print("The Winner is Player ");
			if (myGame.getCurrentPlayer() == 1)
			{
				System.out.println("2");
			}
			else
			{
				System.out.println("1");
			}
		}
	}

}



When I run the program, it declares player 1 as the winner after player 1 makes his/her first move. The instructor gave us the code for the main application and we have to come up with the helper class. What do I need to fix to get my program running? It has to be my code because the instructor's code makes sense.

Is This A Good Question/Topic? 0
  • +

Replies To: TicTacToe Helper Class

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: TicTacToe Helper Class

Posted 24 June 2009 - 05:31 PM

when you check for the winner, you check to see if 3 squares in a row have the same value. They have the same value because they're all zero, so it thinks player 1 won. :D Just add alonger condition that checks if they're either all 1 or all 2 (or preferably maybe a negating condition, if they're all equal and one of them isn't zero,)

Hope this helps, nice job! :D

This post has been edited by crazyjugglerdrummer: 24 June 2009 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

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

Re: TicTacToe Helper Class

Posted 24 June 2009 - 06:21 PM

Hope you will never write a 16 X 16 tic tac toe game because your method determineWinner() will last forever :)
this can be done in a lot less code using for( loops
Was This Post Helpful? 0
  • +
  • -

#4 alexnguyen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 17-March 08

Re: TicTacToe Helper Class

Posted 24 June 2009 - 06:32 PM

View Postpbl, on 24 Jun, 2009 - 05:21 PM, said:

Hope you will never write a 16 X 16 tic tac toe game because your method determineWinner() will last forever :)
this can be done in a lot less code using for( loops


I came across the for loop code that will truncate what I have there, but for now I want to make it simple and understand what I'm doing. The instructor didn't seem to enthusiastic about the for loop and would rather show us something on a more basic level.
Was This Post Helpful? 0
  • +
  • -

#5 alexnguyen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 17-March 08

Re: TicTacToe Helper Class

Posted 24 June 2009 - 06:51 PM

View Postcrazyjugglerdrummer, on 24 Jun, 2009 - 04:31 PM, said:

when you check for the winner, you check to see if 3 squares in a row have the same value. They have the same value because they're all zero, so it thinks player 1 won. :D Just add alonger condition that checks if they're either all 1 or all 2 (or preferably maybe a negating condition, if they're all equal and one of them isn't zero,)

Hope this helps, nice job! :D


	public int determineWinner()
	{
		int winner = 0;

		if( _board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] &&
				_board[1][0] != 0 )
			winner = _board[1][0];

		if( _board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] &&
				_board[2][0] != 0 )
			winner = _board[2][0];

		if( _board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] &&
				_board[0][1] != 0 )
			winner = _board[0][1];

		if( _board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] &&
				_board[0][2] != 0 )
			winner = _board[0][2];

		if( _board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[0][2] == _board[1][1] && _board[0][2] == _board[2][0] &&
				_board[0][2] != 0 )
			winner = _board[0][2];

		return winner;
	}



I thought I had done so with the 8 arguments above. For example, the last one checks diagonally (top right corner to bottom left corner). It checks the value in the top right corner with the middle and then checks the top right corner with the bottom left corner. Also, it checks whether or not the value in the top right corner is a 0. Each argument checks for the 8 possible ways a person can win. I'm not sure how else to implement it with the main class...
Was This Post Helpful? 0
  • +
  • -

#6 ceppler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 27-June 09

Re: TicTacToe Helper Class

Posted 27 June 2009 - 05:02 PM

Alex,

I'm not sure that the determineWinner method is the souce of the problem. I copied your helper and main program code into Eclipse, added some println statements and ran the program a few times. What I find is that something is filling in the tic tac toe board all for one move by Player 1 (i.e., player = 1). It fills it in the same way each time. Consequently, starting with player = 1 means that player = 1 is the winner. It must be the for loop in the makeMove method. Once invoked, it doesn't terminate until it achieves completes all 9 iterations on the board (!!!).

If the main program accepts the user input from the current user, the for loop in the makeMove method may be overwriting the input. What you have is a computer playing tic tac toe with itself, only the loop in makeMove just fills the board in alternating fashion with either 1 or 2. Here is the result of invoking the displayBoard method after main program prints the winner to the screen: 1 2 1 2 1 2 1 2 1. It is the same result no matter what I enter for row and column for player 1.

I think you need to find another way to hand off the game from player = 1 to player = 2 and back. The main program creates a complex logical dependency. It starts with the while statement that checks both the winner and the completion of the board while invoking makeMove on each loop. The trouble is that the for loop in makeMove is 'taking over' the game on each run of the program. It completes the board prematurely. Then that means that the program leaves the while loop after only one turn. This is why you are seeing the winner determined so early.

(By the way, this problem may be 'dumping' on determineWinner's results. I show a number '1' entered for both of the diagonals being tested by the seventh and eighth 'if' statements in that method).

Your job now is to find out how to make the makeMove method work -- passing the game from 1 to 2 and back -- but without jumping out of the while loop in the main program (the one that checks both the winner and completion of the board). Hint: with the aforementioned while loop checking for both a winner and a completed board, is it really necessary to know the number of iterations needed for tic tac toe?

Good luck!

Chuck





View Postalexnguyen, on 24 Jun, 2009 - 05:51 PM, said:

View Postcrazyjugglerdrummer, on 24 Jun, 2009 - 04:31 PM, said:

when you check for the winner, you check to see if 3 squares in a row have the same value. They have the same value because they're all zero, so it thinks player 1 won. :D Just add alonger condition that checks if they're either all 1 or all 2 (or preferably maybe a negating condition, if they're all equal and one of them isn't zero,)

Hope this helps, nice job! :D


	public int determineWinner()
	{
		int winner = 0;

		if( _board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] &&
				_board[1][0] != 0 )
			winner = _board[1][0];

		if( _board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] &&
				_board[2][0] != 0 )
			winner = _board[2][0];

		if( _board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] &&
				_board[0][1] != 0 )
			winner = _board[0][1];

		if( _board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] &&
				_board[0][2] != 0 )
			winner = _board[0][2];

		if( _board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] &&
				_board[0][0] != 0 )
			winner = _board[0][0];

		if( _board[0][2] == _board[1][1] && _board[0][2] == _board[2][0] &&
				_board[0][2] != 0 )
			winner = _board[0][2];

		return winner;
	}



I thought I had done so with the 8 arguments above. For example, the last one checks diagonally (top right corner to bottom left corner). It checks the value in the top right corner with the middle and then checks the top right corner with the bottom left corner. Also, it checks whether or not the value in the top right corner is a 0. Each argument checks for the 8 possible ways a person can win. I'm not sure how else to implement it with the main class...

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1