Sign in to follow this  
LilAznMVP89

Help With TicTacToe Program

Recommended Posts

LilAznMVP89    126
Could someone help me with my Tic Tac Toe game? If player1 enters say 1, 1 and that is X, it still lets player2 enter 1, 1 and the X is replaced with an O. Also, sometimes it does not determine a winner if there are 3 in a row or if there is no winner, a Cat game.
[source tag]
import cs1.Keyboard;

public class Tic
{
     private int r=3, c=3;
     private char ast='*', j='X', temp;
	
	
     public Tic()
     {
	newgame();
     }
     public void newgame()
     {
	char [][] ticgame = new char [r][c];
	
	for(int row=0; row<ticgame.length; row++)
	{
	     for (int col=0; col<ticgame[row].length; col++)
		ticgame[row][col] = ast ;
			
	} 
		
	for(int row=0; row<ticgame.length; row++)
	{
	     for (int col=0; col<ticgame[row].length; col++)
		System.out.print(ticgame[row][col] + "\t");
		System.out.println();
	} 

	int x,y;
	
	boolean cont = true;
	boolean mike = true;
	while(cont)
	{
	     int name = 0;
	     if(j == 'X')
		name = 1;
	     else
		name = 2;
			
        System.out.print("Player" + name + " enter a row(1-3) : ");
	  x= Keyboard.readInt();

        System.out.print("Player" + name + " enter a column(1-3) : ");
   	  y= Keyboard.readInt();
		
        if(mike)
	  ticgame[(x-1)][(y-1)] = j;
	
	for(int row=0; row<ticgame.length; row++)
	{
	   for (int col=0; col<ticgame[row].length; col++)
	   {
	      System.out.print(ticgame[row][col] + "\t");
	   } 
			
	   System.out.println();
        }
		
        if(j == 'X') 
    	   j = 'O';
	else
	   j = 'X';
		
        String Player1 = "Player 1 wins", Player2 = "Player 2 wins", playervar="No one has won yet", Cat = "Cat Wins";
	
        if (ticgame[0][0] == 'X' && ticgame[1][1] == 'X' && ticgame[2][2] == 'X')
       {cont = false;
        playervar = Player1;
       }
       else 
       if (ticgame[0][0] == 'O' && ticgame[1][1] == 'O' && ticgame[2][2] == 'O')
       {cont = false;
        playervar = Player2;}
       else
       if (ticgame[0][0] == 'X' && ticgame[1][0] == 'X' && ticgame[2][0] == 'X')
       {cont = false;
        playervar = Player1;}
       else 
       if (ticgame[0][0] == 'O' && ticgame[1][0] == 'O' && ticgame[2][0] == 'O')
       {cont = false;
        playervar =  Player2;}
       else 
       if (ticgame[0][1] == 'X' && ticgame[1][1] == 'X' && ticgame[2][1] == 'X')
       {cont = false;
        playervar = Player1;}
       else
       if (ticgame[0][1] == 'O' && ticgame[1][1] == 'O' && ticgame[2][1] == 'O')
       {cont = false;
        playervar =  Player2;}
       else 
       if (ticgame[0][2] == 'X' && ticgame[1][2] == 'X' && ticgame[2][2] == 'X')
       {cont = false;
        playervar =  Player1;}
       else
       if (ticgame[0][2] == 'O' && ticgame[1][2] == 'O' && ticgame[2][2] == 'O')
       {cont = false;
        playervar =  Player2;}
       else
       if (ticgame[0][0] == 'X' && ticgame[0][1] == 'X' && ticgame[0][2] == 'X')
       {cont = false;
        playervar =  Player1;}
       else 
       if (ticgame[0][0] == 'O' && ticgame[0][1] == 'O' && ticgame[0][2] == 'O')
       {cont = false;
        playervar =  Player2;}
       else
       if (ticgame[1][0] == 'X' && ticgame[1][1] == 'X' && ticgame[1][2] == 'X')
       {cont = false;
        playervar =  Player1;}
      else
      if (ticgame[1][0] == 'O' && ticgame[1][1] == 'O' && ticgame[1][2] == 'O')
      {cont = false;
       playervar =  Player2;}
     else
     if (ticgame[2][0] == 'X' && ticgame[2][1] == 'X' && ticgame[2][2] == 'X')
     {cont = false;
      playervar =  Player1;}
     else 
     if (ticgame[2][0] == 'O' && ticgame[2][1] == 'O' && ticgame[2][2] == 'O')
     {cont = false;
      playervar =  Player2;}
     else 
     if (ticgame[0][0] != 'X' && ticgame[1][1] != 'X' && ticgame[2][2] != 'X' && ticgame[0][0] != 'O' && ticgame[1][1] != 'O' && ticgame[2][2] != 'O' && ticgame[0][0] != 'X' && ticgame[1][0] != 'X' && ticgame[2][0] != 'X' && ticgame[0][0] != 'O' && ticgame[1][0] != 'O' && ticgame[2][0] != 'O' && ticgame[0][1] != 'X' && ticgame[1][1] != 'X' && ticgame[2][1] != 'X' && ticgame[0][1] != 'O' && ticgame[1][1] != 'O' && ticgame[2][1] != 'O' && ticgame[0][2] != 'X' && ticgame[1][2] != 'X' && ticgame[2][2] != 'X' && ticgame[0][2] != 'O' && ticgame[1][2] != 'O' && ticgame[2][2] != 'O' && ticgame[0][0] != 'X' && ticgame[0][1] != 'X' && ticgame[0][2] != 'X' && ticgame[0][0] != 'O' && ticgame[0][1] != 'O' && ticgame[0][2] != 'O' && ticgame[1][0] != 'X' && ticgame[1][1] != 'X' && ticgame[1][2] != 'X' && ticgame[1][0] != 'O' && ticgame[1][1] != 'O' && ticgame[1][2] != 'O' && ticgame[2][0] != 'X' && ticgame[2][1] != 'X' && ticgame[2][2] != 'X' && ticgame[2][0] != 'O' && ticgame[2][1] != 'O' && ticgame[2][2] != 'O')
     {cont = false;
      playervar = Cat;}
					
     System.out.println(playervar);
		
     } 
	
   }
	
}[/source tag]

Thanks :)

[Edited by - LilAznMVP89 on November 25, 2004 12:09:30 AM]

Share this post


Link to post
Share on other sites
LilAznMVP89    126
Quote:
Original post by Ekim_Gram
I refuse to read that until you format your code a bit and put in in source tags. It's nothing personal, just preference.


I'm new at this... how do I format my code and put in source tags?

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by LilAznMVP89
Could someone help me with my Tic Tac Toe game?
If player1 enters say 1, 1 and that is X, it still lets player2 enter 1, 1 and the X is replaced with an O.


Well yes, you're not doing any kind of check to prevent that. You need to examine the proposed square for the move first to see if it's been occupied


// This:
//System.out.print("Player" + name + " enter a row(1-3) : ");
//x= Keyboard.readInt();
//System.out.print("Player" + name + " enter a column(1-3) : ");
//y= Keyboard.readInt();
//if(mike)
//ticgame[(x-1)][(y-1)] = j;

// becomes this:
do {
// Ask for input at least once
System.out.print("Player" + name + " enter a row(1-3) : ");
x= Keyboard.readInt();
System.out.print("Player" + name + " enter a column(1-3) : ");
y= Keyboard.readInt();
} while (ticgame[x-1][y-1] != ast);
// That will loop until we get an x and y such that
// ticgame[x-1][y-1] DOES equal the 'ast'; that is, until we have
// a valid input (empty square).
ticgame[x-1][y-1] = j;



Any additional user-friendliness is left as an exercise.

Quote:

Also, sometimes it does not determine a winner if there are 3 in a row or if there is no winner, a Cat game.


Well, let's consider the Cat's game detection first.


// I hope you don't mind if I break up your line a bit.
if (ticgame[0][0] != 'X' && ticgame[1][1] != 'X' && ticgame[2][2] != 'X' &&
ticgame[0][0] != 'O' && ticgame[1][1] != 'O' && ticgame[2][2] != 'O' &&
ticgame[0][0] != 'X' && ticgame[1][0] != 'X' && ticgame[2][0] != 'X' &&
ticgame[0][0] != 'O' && ticgame[1][0] != 'O' && ticgame[2][0] != 'O' &&
ticgame[0][1] != 'X' && ticgame[1][1] != 'X' && ticgame[2][1] != 'X' &&
ticgame[0][1] != 'O' && ticgame[1][1] != 'O' && ticgame[2][1] != 'O' &&
ticgame[0][2] != 'X' && ticgame[1][2] != 'X' && ticgame[2][2] != 'X' &&
ticgame[0][2] != 'O' && ticgame[1][2] != 'O' && ticgame[2][2] != 'O' &&
ticgame[0][0] != 'X' && ticgame[0][1] != 'X' && ticgame[0][2] != 'X' &&
ticgame[0][0] != 'O' && ticgame[0][1] != 'O' && ticgame[0][2] != 'O' &&
ticgame[1][0] != 'X' && ticgame[1][1] != 'X' && ticgame[1][2] != 'X' &&
ticgame[1][0] != 'O' && ticgame[1][1] != 'O' && ticgame[1][2] != 'O' &&
ticgame[2][0] != 'X' && ticgame[2][1] != 'X' && ticgame[2][2] != 'X' &&
ticgame[2][0] != 'O' && ticgame[2][1] != 'O' && ticgame[2][2] != 'O')


You have a lot of redundant cases, but that won't cause the problem. What WILL cause a problem is that this is only true if all these squares that you're checking do NOT contain an X or O. You want for all of them to contain an X or O; equivalently, for none of them to contain an asterisk.

But you can make things much easier than that, once you get the move validity checking working. Hint: How many valid moves are made in a Cat's game?

As for the win checking: You're missing the cases for one of the diagonals: [0][2], [1][1] and [2][0] being filled with the same symbol.

But before you go any further, please stop and think about what you're doing. There is a huge amount of duplication here, because checking for a win by player 1 is the exact same thing as checking for a win by player 2. You need to write yourself a function to check for a win:

private boolean winBy(char player) {
// Write this part. The function should return true if the
// indicated player won, and false otherwise.
}

//In the main code
String Player1 = "Player 1 wins", Player2 = "Player 2 wins", playervar="No one has won yet", Cat = "Cat Wins";
cont = false;
if (winBy('X')) {
playervar = Player1;
} else if (winBy('O')) {
playervar = Player2;
} else if (/* fill in this part with the 'easy' Cat's game detection */) {
playervar = Cat;
} else {
cont = true;
}



Duplication is bad bad bad! Just don't do it. Control-V is not your friend. It will cause strange errors down the line when you forget to make a subtle change after pasting something (or if you make a change you shouldn't have), and it puts huge amounts of extra space between other related bits of code. Your IDE might provide "jump to definition" but it will never be as convenient as having the necessary bit on the same page. The shorter your code is, the more often that happens. ;) Notice how now you'll be writing eight checks for filled lines instead of sixteen (and using them twice each, by calling the function twice), and setting playervar in four places instead of eighteen. (And 'cont' in two places instead of seventeen - although the 'obvious' way of doing it would use four; the code above is a little bit tricky with its handling of that variable ;) )

Oh, and while you're at it:

- Think some more about some of your variable names. While it's a reasonably good idea to have a const variable to store '*' as a symbol for an unoccupied square, it's silly to name it "ast" - first of all that's needlessly short, and secondly it describes the currently-used value (an asterisk) instead of what it's for - and thus you defeat the purpose of having the variable.

- Get rid of 'mike', which is always true and thus has no effect. I assume you were using it for debugging or something? :s

- Don't check "ticgame.length" and "ticgame[row].length", but use the constants you already defined for the dimensions. It's not so much a matter of optimization, but of documentation: writing it the way you have gives the false impression that those values might vary at runtime or need to be recalculated for some reason.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this