Skip to main content
added 117 characters in body
Source Link
Rob Audenaerde
  • 3.5k
  • 15
  • 24

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it.

You now can easily implement a TestPlayer or a AIPlayer without having to change anything in the game logic.

As guess() is game logic, it should return something the Game knows, and notify the player. For example a MoveResult, which might contain some code (enum), variables and a message.

   public MoveResult 
   {
      public static enum {TOO_HIGH, TOO_LOW, RIGHT}
      String message;
   }

And in Game

    public void nextMove()
    {
        //or something similar
        MoveResult result = guess(player.getNextInput());
        player.notify(result);
    }

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it.

You now can easily implement a TestPlayer or a AIPlayer without having to change anything in the game logic.

As guess() is game logic, it should return something the Game knows, and notify the player. For example a MoveResult, which might contain some code (enum), variables and a message.

   public MoveResult 
   {
      public static enum {TOO_HIGH, TOO_LOW, RIGHT}
      String message;
   }

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it.

You now can easily implement a TestPlayer or a AIPlayer without having to change anything in the game logic.

As guess() is game logic, it should return something the Game knows, and notify the player. For example a MoveResult, which might contain some code (enum), variables and a message.

   public MoveResult 
   {
      public static enum {TOO_HIGH, TOO_LOW, RIGHT}
      String message;
   }

And in Game

    public void nextMove()
    {
        //or something similar
        MoveResult result = guess(player.getNextInput());
        player.notify(result);
    }
added 117 characters in body
Source Link
Rob Audenaerde
  • 3.5k
  • 15
  • 24

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it.

You now can easily implement a TestPlayer or a AIPlayer without having to change anything in the game logic.

As guess() is game logic, it should return something the Game knows, and notify the player. For example a MoveResult, which might contain some code (enum), variables and a message.

   public MoveResult 
   {
      public static enum {TOO_HIGH, TOO_LOW, RIGHT}
      String message;
   }

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it.

You now can easily implement a TestPlayer or a AIPlayer without having to change anything in the game logic.

As guess() is game logic, it should return something the Game knows, and notify the player. For example a MoveResult, which might contain some code (enum), variables and a message.

   public MoveResult 
   {
      public static enum {TOO_HIGH, TOO_LOW, RIGHT}
      String message;
   }
added 7 characters in body
Source Link
Rob Audenaerde
  • 3.5k
  • 15
  • 24

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        player.getNextInput();
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it

Nice start!

You should move all the game-control logic to Game, so that your main loop will be

while (!game.isGameOver())
{
     game.nextMove()
}

You can code the 'Do you want to play another?' nicely outside this loop.

I would also introduce a Player, which can be asked for a move. So the Game will ask the Player for input:

class Game
{
    public Game (Player p)
    {
        player = p;
    }

    Player player;
    public void nextMove()
    {
        //or something similar
        guess(player.getNextInput());
    }
}

You can move all the IO (System.out etc) to the Player class, because Game does not need to know about it

Source Link
Rob Audenaerde
  • 3.5k
  • 15
  • 24
Loading