13 Replies - 700 Views - Last Post: 21 March 2010 - 07:04 AM Rate Topic: -----

#1 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Why won't this program END?!?!?

Posted 20 March 2010 - 04:56 PM

I cannot get this darn thing to end....
What did I mess up?

import java.util.Scanner;

public class TicTacToeExe
{

    public static void main(String[] args)
    {
        int userInput;
        int player;
        boolean game = true;
        boolean switchPlayers = true;
        char answer = 'n';
        Scanner keyboard; 
        keyboard = new Scanner(System.in); 
        
        
        
        do
        {
        // construct the object of the class
        Scanner input = new Scanner(System.in);
        TicTacToeClass myAssistant = new TicTacToeClass();

        System.out.print("Welcome to the Game of Tic Tac Toe\n");
        System.out.println("The board is set up using the numbers from 0-8\nUse" +
                           " the chart below to plan your moves accordingly\n ");
        System.out.println("              0 1 2");
        System.out.println("              3 4 5");
        System.out.println("              6 7 8\n\n");
        System.out.println("***************************************************");
        myAssistant.displayBoard();

            // switches between the active player
            while(game)
            {
                if(switchPlayers)  
                {  
                    player = 1;
                }  
                else player = 2;

                System.out.print("Player " + player + " Please enter your Move (0 - 8)\n");
                userInput = input.nextInt();

                //validate the input here, after the user inserts an input
                while(userInput < 0 || userInput > 8)
                {
                    System.out.println("Sorry, you have to enter a number betweem 0 - 8\n");
                    System.out.println("Please try Again\n");
                    userInput = input.nextInt();
                }

                myAssistant.playerMove(player, userInput);
                myAssistant.displayBoard();
                myAssistant.determineWinner();

                //switching player
                switchPlayers =! switchPlayers;
                
            }
                System.out.print("\nWould you Like to Play Again? (Y/N)");
                answer = keyboard.next().charAt(0);
                if (answer =='n')
                {
                        System.out.println("Thanks for Playing!!");
                }
                else if (answer =='y')
                {
                        System.out.println("Good Luck");
                }
          } while (answer == 'y');
         
      }
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Why won't this program END?!?!?

#2 MadScientist305  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 26-December 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 05:02 PM

The variable game is never changed to false.
Was This Post Helpful? 1
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 05:03 PM

In the first nested loop, the game variable will never be false, therefore creating an infinite loop. Inside that loop, you need to create some conditional where you will set game = false;
Was This Post Helpful? 1
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 05:07 PM

first, you don't need to create a new Scanner object for each iteration of the do\while loop:
do 
        { 
        // construct the object of the class 
        Scanner input = new Scanner(System.in); 
 

create it before starting the loop as:
Scanner input = new Scanner(System.in);
do{
//...


you initialize game to true, and never change it's value later in the code.
so this loop:
while(game){ 

is actually an infinite loop.
you will have to set it to false if a winner was found, or the board is full.

also, you will have to initialize some variables (as game for example) for every new game.
because if game is set to false somewher in your code, that loop:
while(game){ 

will never be executed again. so you won't be able to play another game.
it should be:
do{ 
    game = true;
    switchPlayer = true;
    answer = 'n';
    //..
    while(game){
       //...

This post has been edited by japanir: 20 March 2010 - 05:07 PM

Was This Post Helpful? 2
  • +
  • -

#5 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 05:10 PM

Scientist is right. You need to add some code in your while(game){} that checks to see if the game is over and, if it is, flip game to false. You can just add it right after you call the determineWinner() method of TicTacToeClass, which I obviously haven't seen, but this would seem to be a method that would only be called if a particular game is over, yeah?

Edit: Actually, everybody is right (when I started writing this, there was only one answer). And japanir makes some excellent points.

This post has been edited by xor-logic: 20 March 2010 - 05:13 PM

Was This Post Helpful? 1
  • +
  • -

#6 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 05:34 PM

View Postxor-logic, on 20 March 2010 - 04:10 PM, said:

Scientist is right. You need to add some code in your while(game){} that checks to see if the game is over and, if it is, flip game to false. You can just add it right after you call the determineWinner() method of TicTacToeClass, which I obviously haven't seen, but this would seem to be a method that would only be called if a particular game is over, yeah?

Edit: Actually, everybody is right (when I started writing this, there was only one answer). And japanir makes some excellent points.


This is my class file...

public class TicTacToeClass
{
    // class variables
    private int [][] _board;
    
    // constructor
    public TicTacToeClass()
    {
        _board = new int [3][3];
        
        for(int row = 0; row < 3; row++)
            for(int column = 0; column < 3; column++)
                _board[row][column] = 0;
    }

    //method playerMove
    public boolean playerMove(int player, int square)
    {
        int row = square / 3;
        int column = square % 3;
        _board[row][column] = player;
   
        // Initialize boolean move
        boolean valid = false;
        if (square == -1 && _board[0][0] == 0 )
        {
            _board[0][0] = player;
            valid = true;
        }

        if (square == -1 && _board[0][1] == 0 )
        {
            _board[0][1] = player;
            valid = true;
        }

        if (square == -1 && _board[0][2] == 0 )
        {
            _board[0][2] = player;
            valid = true;
        }

        if (square == -1 && _board[1][0] == 0 )
        {
            _board[1][0] = player;
            valid = true;
        }

        if (square == -1 && _board[1][1] == 0)
        {
            _board[1][1] = player;
            valid = true;
        }

        if (square == -1 && _board[1][2] == 0 )
        {
            _board[1][2] = player;
            valid = true;
        }

        if (square == -1 && _board[2][0] == 0 )
        {
            _board[2][0] = player;
            valid = true;
        }

        if (square == -1 && _board[2][1] == 0 )
        {
            _board[2][1] = player;
            valid = true;
        }

        if (square == -1 && _board[2][2] == 0 )
        {
            _board[2][2] = player;
            valid = true;
        }
        return valid;

    }

    // displayBoard method
    public void displayBoard()
    {
        for(int row = 0; row< 3; row++)
        {
            for(int column = 0; column < 3; column++)
            {
                if(_board[row][column] == 0)
                    System.out.print("_ ");
                else
                    System.out.print(_board[row][column] + " ");
            }
             System.out.println();
        }
    }


    // method determineWinner
   public int determineWinner()
   {
        int winner = -1;

        //Cats game
        boolean complete = true;

        for(int row = 0; row < 3; row++)
        {
            for(int column = 0; column < 3; column++)
            {
                if(_board[row][column] == 0)
                complete = false;
            }
        }
            //row 1
            if(_board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] && _board[0][0] !=0)
            {
                winner = _board[0][0];
            }

            //row 2
            if (_board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] && _board[1][0] !=0)
            {
                winner = _board [1][0];
            }

            //row 3
            if(_board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] && _board[2][0] !=0)
            {
                winner = _board [2][0];
            }

            //col 1
            if(_board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] && _board[0][0] !=0)
            {
                winner = _board [0][0];
            }

            //col 2
            if(_board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] && _board[0][1] !=0)
            {
                winner = _board[0][1];
            }

            //col 3
            if(_board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] && _board[0][2] !=0)
            {
                winner = _board[0][2];
            }

            //diag 1
            if(_board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] && _board[0][0] !=0)
            {
                winner = _board[0][0];
            }

            //diag 2
            if(_board[2][0] == _board[1][1] && _board[2][0] == _board[0][2] && _board[2][0] !=0)
            {
                winner = _board[2][0];
            }

            if (complete == true && winner == -1)
            {
                winner = 0;
            }

            if(winner == -1)
            {
                System.out.println("There are still moves left");
            }

            if(complete == true && winner == 0)
            {
                System.out.println("This game is a DRAW - Cat's Game");
                System.out.println("Game OVER");
            }

            if(complete == false && winner == 1)
            {
                System.out.println("Player One --- YOU WIN!!!");
                System.out.println("Game OVER");
            }

            if(complete == false && winner == 2)
            {
                System.out.println("Player Two --- YOU WIN!!!");
                System.out.println("Game OVER");

            }

                return winner;

    }
  
}
   



Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 05:46 PM

the playerMove method is not very efficient..
it could be shorten to the follow:
public boolean playerMove(int player, int square) 
    { 
        int row = square / 3; 
        int column = square % 3;
        
        //if the cell is empty(contains 0), insert the player number to that cell,
        //and return true
        if(_board[col][row] == 0){  
            _board[row][column] = player;
             return true;
        }
        //else, the cell is already occupied, cannot insert to that cell,
        //return false
        return false;
} 


then, inside the game loop of TicTacToeExe class, when a player attempt to move use that code:
//if the method returns false (the cell is not valid)
if(!myAssistant.playerMove(player, userInput)){
    //print a messege: cell is occupied,
    //get a new input
}


as for ending the game, use the myAssistant methods as follows:
//if a winner was found, announce the winner and stop the game
if(determinWinner > 0){
    //print the winner
    game = false;
}


also, you will have to use a method to check if the board is full, to stop the game.
there is another way, if you don't want a method, to loop the game 9 iterations(number of turns in a TicTacToe game):
int numberOfTurns = 0;

while(game && numberOfTurns < 9){
//....
numberOfTurns++;
}

Was This Post Helpful? 1
  • +
  • -

#8 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 07:44 PM

I put this in the while loop and it does it, but not correctly, you enter one number then it ends.
I am bad with syntax and I know it is something silly, but I do not have enough experience to figure this one out.

import java.util.Scanner;

public class TicTacToeExe
{

    public static void main(String[] args)
    {
        int userInput;
        int player;
        boolean game = true;
        boolean switchPlayers = true;
        char answer = 'n';
        Scanner keyboard;
        keyboard = new Scanner(System.in);

        do
        {
            game = true;
            switchPlayers = true;
            answer = 'n';
      
        // construct the object of the class
        Scanner input = new Scanner(System.in);
        TicTacToeClass myAssistant = new TicTacToeClass();

        System.out.print("Welcome to the Game of Tic Tac Toe\n");
        System.out.println("The board is set up using the numbers from 0-8\nUse" +
                           " the chart below to plan your moves accordingly\n ");
        System.out.println("              0 1 2");
        System.out.println("              3 4 5");
        System.out.println("              6 7 8\n\n");
        System.out.println("***************************************************");
        myAssistant.displayBoard();

            // switches between the active player
            while(game)
            {
                if(switchPlayers)
                {
                    player = 1;
                }
                else player = 2;

                System.out.print("Player " + player + " Please enter your Move (0 - 8)\n");
                userInput = input.nextInt();

                    if(myAssistant.determineWinner() > 0);
                    {
                    System.out.println(myAssistant.determineWinner());
                    }
                    game = false;

                //validate the input here, after the user inserts an input
                while(userInput < 0 || userInput > 8)
                {
                    System.out.println("Sorry, you have to enter a number betweem 0 - 8\n");
                    System.out.println("Please try Again\n");
                    userInput = input.nextInt();
                }

                myAssistant.playerMove(player, userInput);
                myAssistant.displayBoard();
                myAssistant.determineWinner();
                
                //switching player
                switchPlayers =! switchPlayers;

            }
                System.out.print("\nWould you Like to Play Again? (Y/N)");
                answer = keyboard.next().charAt(0);
                if (answer =='n')
                {
                        System.out.println("Thanks for Playing!!");
                }
                else if (answer =='y')
                {
                        System.out.println("Good Luck");
                }
          } while (answer == 'y');
        
      }
  }



output

run:
Welcome to the Game of Tic Tac Toe
The board is set up using the numbers from 0-8
Use the chart below to plan your moves accordingly
 
              0 1 2
              3 4 5
              6 7 8


***************************************************
_ _ _ 
_ _ _ 
_ _ _ 
Player 1 Please enter your Move (0 - 8)
0
There are still moves left
There are still moves left
-1
1 _ _ 
_ _ _ 
_ _ _ 
There are still moves left

Would you Like to Play Again? (Y/N)




Was This Post Helpful? 0
  • +
  • -

#9 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 07:49 PM

Shouldn't this:
  game = false;


be inside this?
   if(myAssistant.determineWinner() > 0);
   {
     System.out.println(myAssistant.determineWinner());
   }


I assume that this tests whether there is a winner, and displays it if there is. If it does do that, or if the interior of that loop is reached by any other endgame conditions, then you should put game = false inside it.

This post has been edited by xor-logic: 20 March 2010 - 07:49 PM

Was This Post Helpful? 1
  • +
  • -

#10 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 07:51 PM

Tried it already does not work...me and a few other guys are working this and we are all stuck
Maybe just a little tired, been working it all day now
Thoughts?
Was This Post Helpful? 0
  • +
  • -

#11 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 08:07 PM

Make the change I suggested earlier AND... remove this semi-colon. Don't put semi-colons after if statements.
   if(myAssistant.determineWinner() > 0);


Ran it on my comp, runs.

That said, you should really work on the output, it's very confusing and hard to follow.

Also, looking at your determineWinner method, it looks like a tie game returns a 0. If that's the case, you need to change that if statement to be "if it's greater than OR EQUAL TO zero".
Was This Post Helpful? 1
  • +
  • -

#12 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 08:12 PM

DANG SYNTAX!!
Oh geesh...I hate semi colons!!

Thanx a bunch!!

Will work on it some more and do as advised.
I think I would have a concussion by now from banging my head if it were not for you guys and this website!!

Elwood
Was This Post Helpful? 0
  • +
  • -

#13 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Why won't this program END?!?!?

Posted 20 March 2010 - 08:15 PM

In the future, if you run into unexpected behavior like this, you can try throwing distinct println statements into your loops. It makes it really easy to see if any are being activated when they shouldn't. Then once you know which loop is screwing up, it's easier to figure out why.
Was This Post Helpful? 1
  • +
  • -

#14 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Re: Why won't this program END?!?!?

Posted 21 March 2010 - 07:04 AM

After a good nights sleep and all your wise sage advice..I redid it and it works better, is cleaner and I am done with it!!

MANY MANY MANY Kudo's to all those that chimed in to help this poor soul learning the Java way.

Thank You ALL!

Below is the new class and app file for your viewing pleasure...maybe someone else can gain some helpful insight from it.

Class File
public class TicTacToe
{
    //class variables
    private int [][] _board;

    //constructor
    public TicTacToe()
    {
        _board = new int [3][3];

        for(int row = 0; row < 3; row++)
        {
            for(int column = 0; column < 3; column++)
            {
                _board[row][column] = 0;
            }
        }
    }

    //method playerMove
    public boolean playerMove (int player, int square)
    {
        boolean valid = false;
        if (square == 1 && _board[0][0] == 0)
        {
            _board[0][0] = player;
            valid = true;          
        }

        if (square == 2 && _board[0][1] == 0)
        {
            _board[0][1] = player;
            valid = true;
        }

        if (square == 3 && _board[0][2] == 0)
        {
            _board[0][2] = player;
            valid = true;
        }

        if (square == 4 && _board[1][0] == 0)
        {
            _board[1][0] = player;
            valid = true;
        }

        if (square == 5 && _board[1][1] == 0)
        {
            _board[1][1] = player;
            valid = true;
        }

        if (square == 6 && _board[1][2] == 0)
        {
            _board[1][2] = player;
            valid = true;
        }

        if (square == 7 && _board[2][0] == 0)
        {
            _board[2][0] = player;
            valid = true;
        }

        if (square == 8 && _board[2][1] == 0)
        {
            _board[2][1] = player;
            valid = true;
        }

        if (square == 9 && _board[2][2] == 0)
        {
            _board[2][2] = player;
            valid = true;
        }
        return valid;
    }
                
    // displayBoard method
    public void displayBoard()
    {
        for(int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                if(_board[row][col] == 0)
                    System.out.print("_ ");
                else
                    System.out.print(_board[row][col] + " ");
            }
            System.out.println();
        }
    }

    // method determineWinner
    public int determineWinner()
    {

        boolean complete = true;
        int winner = -1;
        
        //row 1
        if(_board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] && _board[0][0] !=0)
        winner = _board[0][0];
        
        //row 2
        if(_board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] && _board[1][0] !=0)
        winner = _board [1][0];
        
        //row 3
        if(_board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] && _board[2][0] !=0)
        winner = _board [2][0];
        
        //col 1
        if(_board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] && _board[0][0] !=0)     
        winner = _board [0][0];
        
        //col 2
        if(_board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] && _board[0][1] !=0)      
        winner = _board[0][1];
        
        //col 3
        if(_board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] && _board[0][2] !=0)
        winner = _board[0][2];
        
        //diag 1
        if(_board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] && _board[0][0] !=0)     
        winner = _board[0][0];
        
        //diag 2
        if(_board[2][0] == _board[1][1] && _board[2][0] == _board[0][2] && _board[2][0] !=0)       
        winner = _board[2][0];

        //determine if board is full
        for(int row = 0; row < 3; row++)
        {
            for( int col = 0; col < 3; col++)
            {
                if (_board [row][col]==0)
                complete = false;
            }
        }

        //determine cats game
        if (complete == true && winner == -1)
        {
            System.out.println("This game is a DRAW - Cat's Game");
            System.out.println("Try Again");
            System.out.println("Game OVER");
            winner = 0;
        }
       
        if(complete == false && winner == 1)
        {
            System.out.println("Player One --- YOU WIN!!!");
            System.out.println("Good Job!!");
            System.out.println("Game OVER");
            winner = 0;
        }

        if(complete == false && winner == 2)
        {
            System.out.println("Player Two --- YOU WIN!!!");
            System.out.println("Good Job!!");
            System.out.println("Game OVER");
            winner = 0;
        }
        return winner;
    }
}





App File

import java.util.Scanner;

public class TicTacToeEXE
{
    public static void main(String[] args)
    {

        int player = 1;
        int move = 0;
        int counter = 0;

        // construct the object of the class
        Scanner input = new Scanner(System.in);
        TicTacToe myAssistant = new TicTacToe();

        System.out.print("Welcome to the Game of Tic Tac Toe\n");
        System.out.println("The board is set up using the numbers from 1-9\nUse" +
                           " the chart below to plan your moves accordingly\n ");
        System.out.println("              1 2 3");
        System.out.println("              4 5 6");
        System.out.println("              7 8 9\n\n");
        System.out.println("***************************************************");
        myAssistant.displayBoard();

        while(counter == 0)
        {
            System.out.print("Player " + player + " Please enter your move\n");
            move = input.nextInt();
            while(move < 1 || move > 9 || myAssistant.playerMove(player, move) != true)
            {
                System.out.println("Sorry... You have to enter a valid number" +
                                   " from 1 to 9\n" + "You cannot enter a number" +
                                   " that has already been taken");
                System.out.println("Player " + player + " Please try again\n");
                move = input.nextInt();
            }
            
            //loop to error check data entered
            if (player == 1)
                player = 2;
            else
                player = 1;

            myAssistant.displayBoard();

            if (myAssistant.determineWinner() != -1)
                counter = 1;
        }
       
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1