Results 1 to 5 of 5
  1. #1
    alara's Avatar
    alara is offline Member
    Join Date
    Nov 2011
    Location
    Florida
    Posts
    2
    Rep Power
    0

    Default program compiles but does not work

    Hello I am new to Java and am taking it this semseter for school I have an assignment to create a tic tac toe program for the most part it worked fine but when I added the user information and the AI for the computer decision I began to have problems I pasted my code below any assistance would be helpful. I have been racking my brains on this program for hours and most likely the issue is staring at me in the face but for some reason I can't find it.

    Java Code:
    import java.io.*;
    import java.util.Random;
    
    public class Tic_Tac_Toe {
    
    	private static boolean count;
    
    	public static <showMoves> void main(String args []) throws IOException{
    	
    		char [] moves = {'0','1','2','3','4','5','6','7','8'};
    		char [] plays = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
    		
    		int pTurn, players = 0, inputMove, rNumber, computer;
    		String pCount;
    		String playerMoves;
    		count = false;
    		boolean numberEntered = false;
    		boolean endGame = false;
    		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    		
    		pTurn = 1;
    		Random randomizer = new Random();
    		
    		
    			System.out.println("How many players are there?\nEnter 1 or 2:\n");
    			
    			pCount = dataIn.readLine(); // read input from the player
    			pTurn = Integer.parseInt(pCount);
    		
    		//Game Movements
    		
    		while (endGame == false) 
    		{
    			if (pTurn == 1 && endGame == false)
    			{
    				inputMove = playerMoves (moves, plays);
    				moves[inputMove] = ' ';
    				plays[inputMove] = 'X';
    				pTurn = 2;
    				endGame = check(plays);
    			}
    			if (pTurn == 2 && endGame == false)
    			{
    				if (players == 2)
    				{
    					inputMove = playerMoves(moves, plays);
    					moves[inputMove] = ' ';
    					plays[inputMove] = 'O';
    					pTurn = 2;
    					endGame = check(plays);
    				}
    
    				else
    				{
    					rNumber = randomizer.nextInt(9);
    					computer = compM(moves, plays, rNumber);
    					moves[computer] = ' ';
    					plays[computer] = 'O';
    					pTurn = 1;
    					endGame = check(plays);
    				}
    			}
    		}
    	}
    	private static boolean check(char[] plays) {
    		return false;
    	}
    	private static int playerMoves(char[] moves, char[] plays) {
    		return 0;
    	}
    	public static int compM(char moves[], char plays[], int rNumber)
    	{
    		int computerMove;
    		Random sRandomizer = new Random();
    		
    		while (plays[rNumber] != ' ')	
    		{
    			rNumber = sRandomizer.nextInt(9);
    		}
    			computerMove = rNumber;
    			return computerMove;
    	}
    		private static void addMove(int pTurn, char[] moves) {	
    	}
    		public static int showMoves(char moves [], char plays[]) throws IOException
    		{
    			int input = 0, place = 0;
    			String inputChoice;
    			int keyPressed = 0;
    	
    			BufferedReader inputData = new BufferedReader(new InputStreamReader(System.in));
    			
    		while (input == 0)
    			{
    			System.out.println("Choose a number where you want to place your X\n");
    			System.out.println(" ");
    			System.out.println(moves [0] + " | " + moves [1] + " | " + moves [2]);
    			System.out.println("- - - - -");
    			System.out.println(moves [3] + " | " + moves [4] + " | " + moves [5]);
    			System.out.println("- - - - -");
    			System.out.println(moves [6] + " | " + moves [7] + " | " + moves [8]);
    			System.out.println("- - - - -");
    			System.out.println(" ");
    			System.out.println(plays [0] + " | " + plays [1] + " | " + plays [2]);
    			System.out.println("- - - - -");
    			System.out.println(plays [3] + " | " + plays [4] + " | " + plays [5]);
    			System.out.println("- - - - -");
    			System.out.println(plays [6] + " | " + plays [7] + " | " + plays [8]);
    			System.out.println("- - - - -");
    			System.out.println(" ");
    			addMove(input,moves); //add the move
    			showMoves(moves, plays); // shows the moves on the board and the spaces played
    			
    			inputChoice = inputData.readLine();
    			place = Integer.parseInt(inputChoice);
    			
    		if (moves[place] == ' ')
    			{
    			 System.out.println("That is not a valid choice.  Please try again.");
    			}
    			else
    			keyPressed = 1;
    		}
    		return place;
    			}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: program compiles but does not work

    I assume from the title of your post that the code compiles. But what does "does not work" mean?

    Specifically, what happens when you run the code? And what did you expect or intend to happen?

  3. #3
    alara's Avatar
    alara is offline Member
    Join Date
    Nov 2011
    Location
    Florida
    Posts
    2
    Rep Power
    0

    Default Re: program compiles but does not work

    Basicaly when compiling the code It asks the initial question of how many players there are then nothing after that you enter the integer and it does not go through the motions of showing the tic tac toe board and asking the next question where do you want to place your X.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: program compiles but does not work

    Try debugging your code to see where it is executing, by adding many println statements to show the values of variables as they change and to show the execution flow. What is printed out should help you understand what your code is doing.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: program compiles but does not work

    it does not go through the motions of showing the tic tac toe board
    The board is displayed in the showMoves() method. But if you look at the code that method is not called anywhere (except in the showMoves() method itself).

    I agree with Norm's point. If you use System.out.println() at the start of each method to trace the execution flow it will reveal the point made above. Likewise printing the value of variables that control what the program is doing will provide valuable information.

    A number of these variables are unused. It would be good to have a clear plan for what each variable you declare is going to be used for and ensure that it is used for that. Removing unused variables will reduce logical clutter.

    -----

    What is "<showMoves>" for in the declaration of main()?

Similar Threads

  1. Help Creating a GUI applet window that compiles my program
    By ReclaimerGold in forum New To Java
    Replies: 3
    Last Post: 04-29-2011, 01:27 AM
  2. Method won't work even though program compiles
    By NixasMuraki in forum New To Java
    Replies: 10
    Last Post: 02-14-2011, 10:39 PM
  3. compiles but dosent work...
    By darkflame5019 in forum New To Java
    Replies: 8
    Last Post: 01-01-2011, 08:22 PM
  4. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 09:44 PM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 04:41 AM

Posting Permissions

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