Results 1 to 9 of 9
  1. #1
    skeeter1255 is offline Member
    Join Date
    Aug 2011
    Location
    Miami FL
    Posts
    5
    Rep Power
    0

    Default Help with code (tic-tac-toe)

    Hello all I am still in the process of learning Java. In the mean time I have tried to create a tic-tac-toe game.

    Yet, I am encountering one problem that keeps bugging me. I know there may be others, but in the mean time I am trying to fix this one. The problem is located in the "validMove" Boolean. When the player chooses a location on the board, if the location is already taken, I want the player to have to choose again. Instead what is happening. The program switches players and never gives the other player a chance to go again. The code works the way I want it to in C++, yet doesn't want to work for me in Java.

    I've tried changing the Boolean almost everywhere.

    Please forgive me if the code is still messy, again I am still new to learning this.
    Any help would be greatly appreciated.


    Java Code:
    import java.util.*;
    	public class pageone {
    	
     // static boolean gameOver = false;
    static boolean gameOver;
    	public static void main(String[] args){
    		
    		 char space1 = '1';
    		 char space2 = '2';
    		 char space3 = '3';
    		 char space4 = '4';
    		 char space5 = '5';
    		 char space6 = '6';
    		 char space7 = '7';
    		 char space8 = '8';
    		 char space9 = '9';	
    		
    				
    		int playerChoice; // Lets player choose a number
    		int playerturn = 1; // Declare player1 or player2
    		char playermove; // Give player X or O
    		
    		
    		do{
    			
    				// Give players X or O
    			if(playerturn == 1)
    				{
    				playermove = 'x';
    				}
    			else
    				{
    				playermove = 'o';
    				}
    			
    			System.out.println(space1);
    			
    		System.out.println(space1+"|"+space2+"|"+space3);
    		System.out.println("-"+"+"+"-"+"+"+"-");
    		System.out.println(space4+"|"+space5+"|"+space6);
    		System.out.println("-"+"+"+"-"+"+"+"-");
    		System.out.println(space7+"|"+space8+"|"+space9);
    		
    			// Ask player for input
    		
    	
    		System.out.println( "Player "+ playerturn + " Please choose a space");
    		Scanner scan = new Scanner(System.in);
    		playerChoice = scan.nextInt();
    		
    		boolean validMove = true;
    		
    		
     do{
    		
    		
    		
    			// Take Player choice
    			// Check for Valid move
    			// 		if not player chooses again
    	if(playerChoice == 1 & space1 == '1'){			//
    		space1 = playermove;						//
    		validMove = true;							//
    	}												//
    	else if(playerChoice == 2 & space2 == '2'){    //
    		space2 = playermove;					   //
    												   //   PROBLEM IS HERE!!!!!
    	}											   //
    	else if(playerChoice == 3 & space3 == '3'){	   //
    		space3 = playermove;					   //
    	}											   //
    	else if(playerChoice == 4 & space4 == '4'){
    		space4 = playermove;
    	}
    	else if(playerChoice == 5 & space5 == '5'){
    		space5 = playermove;
    	}	
    	else if(playerChoice == 6 & space6 == '6'){
    		space6 = playermove;
    	}	
    	else if(playerChoice == 7 & space7 == '7'){
    		space7 = playermove;
    	}
    	else if(playerChoice == 8 & space8 == '8'){
    		space8 = playermove;
    	}
    	else if(playerChoice == 9 & space9 == '9'){
    		space9 = playermove;
    	}
    	else{
    		System.out.println("Please choose again");
    		validMove = false;
    		
    	}
    
    	}while(!validMove);							// My validMove Boolean seems to not work when user chooses an already chosen block
    	
    	
    		// Check for game winning condition
    		// If there is a valid move check for won game.
    			
     	gameOver = false;	
    	
    	if(space1 != '1')
    		if(space2 == space1 && space3 == space1){
    				gameOver = true;
    		}
    			if(space4 == space1 && space7 == space1){
    				gameOver = true;
    		}
    		
    	
    	if(space5 != '5'){
    			if(space1 == space5 && space9 == space5){
    				
    				gameOver = true;
    				}
    			if(space4 == space5 && space6 == space5){
    				gameOver = true;
    				}
    			if(space2 == space5 && space8 == space5){
    				gameOver = true;
    				}
    			if(space3 == space5 && space7 == space5){
    				gameOver = true;
    				}
    	}
    	 if(space9 != '9')
    			if(space3 == space9 && space6 == space9){
    				gameOver = true;
    				}
    			if(space7 == space9 && space8 == space9){
    				gameOver = true;
    				}
    	
    			// Alternate Players
    			else{		
    			
    				if(playerturn == 1){
    				playerturn = 2;
    				}
    				else {
    					  playerturn = 1;
    					 }
    		
    			}
    	
    		}while(!gameOver); 
    	
    		
    			// Game winning board
    	System.out.println("Winner");
    		
    	System.out.println(space1+"|"+space2+"|"+space3);
    	System.out.println("-"+"+"+"-"+"+"+"-");
    	System.out.println(space4+"|"+space5+"|"+space6);
    	System.out.println("-"+"+"+"-"+"+"+"-");
    	System.out.println(space7+"|"+space8+"|"+space9);
    	}
    }
    // needed ..
    	// No win condition
    Last edited by skeeter1255; 08-01-2011 at 02:41 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default

    Could you copy and paste here the console contents that shows the problem.
    Add comments to show what the problem is.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  3. #3
    skeeter1255 is offline Member
    Join Date
    Aug 2011
    Location
    Miami FL
    Posts
    5
    Rep Power
    0

    Default

    The program runs fine in the console window. I do not get an "Error" Message. Its more of a bug.

    I am trying to get the player to choose again if the space is already chosen, but instead it just continues unto the other player.

    I will though try and edit the code so show where the problem I am getting is located. (Please full screen)

    My bug starts on my second "Do Loop". It is suppose to initiate a "Valid Move". If the player has a validMove, it then continues. If otherwise it is suppose to ask the player to go again.
    Instead the program either switches the player and ask the opposing player for a input, OR has a continues loop on my else statement.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default

    The program runs fine in the console window
    yes I need to see what your input is and what happens. Copy the console contents here.

    One thing I noticed near the end of the code, there are some if statements and beneath them some indented if statements that are NOT in {}s

  5. #5
    skeeter1255 is offline Member
    Join Date
    Aug 2011
    Location
    Miami FL
    Posts
    5
    Rep Power
    0

    Default

    1|2|3
    -+-+-
    4|5|6
    -+-+-
    7|8|9
    Player 1 Please choose a space
    1
    x|2|3
    -+-+-
    4|5|6
    -+-+-
    7|8|9
    Player 2 Please choose a space
    2
    x|o|3
    -+-+-
    4|5|6
    -+-+-
    7|8|9
    Player 1 Please choose a space
    1

    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again
    Please choose again ..........

    "I just want it to ask me once, then let the player input another space"

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default

    Did you fix the {}s? see post #4

  7. #7
    skeeter1255 is offline Member
    Join Date
    Aug 2011
    Location
    Miami FL
    Posts
    5
    Rep Power
    0

    Default

    Yes I did. Thats the part that alternates between both of my players. Still runs the same.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default

    If the statements are as you want them, then you need to do some debugging by adding some println statements to show the values of variables as then are changed and to show the execution flow.
    Add lots of them to show when ifs are true or false. Add ids to the printlns so you know which one printed what:
    System.out.println("playerChoice=" + playerChoice + ", pmove=" + playermove); //<<<<<

  9. #9
    skeeter1255 is offline Member
    Join Date
    Aug 2011
    Location
    Miami FL
    Posts
    5
    Rep Power
    0

    Default

    Wow I figured it out. I was never asking for the player to Input anything inside of the while loop. So when it came back False, it just looped forever as false.

    Thanks for the help!

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  3. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Tags for this Thread

Posting Permissions

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