HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Guessing game - am I using the concept of objects in Java properly?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theobjectsproperlyconceptjavagameusingguessing

Problem

I'm new to Java and find it hard to grasp the concept of objects. I wrote a simple guessing game to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game.

Player

public class Player {

    private String playerName;
    private int number;

    public int getNumber() {
        return number;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public void guessNumber(){
        number = Integer.parseInt(JOptionPane.showInputDialog(playerName 
            + "'s Turn\nGuess the Number"));
        JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
    }
}


GuessGame

```
public class GuessGame {

private int numberToGuess;
private Player p1;
private Player p2;
private Player p3;

public void startGame(){
numberToGuess = (int) (Math.random()*10);
p1 = new Player();
p2 = new Player();
p3 = new Player();

p1.setPlayerName(JOptionPane.showInputDialog("Enter Player 1 Name: "));
p2.setPlayerName(JOptionPane.showInputDialog("Enter Player 2 Name: "));
p3.setPlayerName(JOptionPane.showInputDialog("Enter Player 3 Name: "));

int flagWinner = 0;
while(0==flagWinner){
p1.guessNumber();
if (p1.getNumber()==numberToGuess){
flagWinner = 1;
break;
}
JOptionPane.showMessageDialog(null, p1.getPlayerName()
+ "'s Guess is Wrong!");
p2.guessNumber();
if (p2.getNumber()==numberToGuess){
flagWinner = 2;
break;
}
JOptionPane.showMessageDialog(null, p2.getPlayerName()
+ "'s Guess is Wrong!");

Solution

To start with, spot the inconsistency. For the name you create it in your main loop and then use a setter; for the number you have a method in the Player object to acquire the guess. The latter is more in line with the message-passing philosophy of OO. (Many people seem to think that using getters and setters is being OO. They're often - not always - a sign of non-OO thinking). However, it doesn't take it as far as it could. The number guessed isn't a property of the player - it's a property of the guess.

Guessing a number isn't the same thing as displaying it - either rename the method or, probably better - a method should do one thing - move that out.

Next up, the flagWinner is a big red flag. Its sole purpose is to identify one of the objects p1, p2, p3, or none of the above. What's the OO way to do that? Use the object to identify itself.

I'd be inclined to factor out the IO as an object too, to be a bit less procedural. So without going into a full over-patterned architecture we have

public class IO {
public void output(String msg) {
JOptionPane.showMessageDialog(null, msg);
}

public String input(String prompt) {
return JOptionPane.showInputDialog(prompt)
}

public int inputInt(String prompt) {
return Integer.parseInt(input(prompt));
}
}

public class Player {
private String playerName;

private Player(String name) {
playerName = name;
}

public static Player createPlayer(IO io, String prompt) {
String name = io.input("Enter " + prompt + " Name: ");
return new Player(name);
}

public String getPlayerName() {
return playerName;
}

public int guessNumber(IO io) {
return io.inputInt(playerName +"'s Turn\nGuess the Number"));
}
}

public class GuessGame {

private final IO io;
private final int numPlayers;
private int numberToGuess;
private List players = new LinkedList();

public GuessGame(IO io, int numPlayers) {
this.io = io;
this.numPlayers = numPlayers;
}

public void init() {
numberToGuess = (int) (Math.random()*10);
for (int i = 0; i it = players.iterator();
while (winner == null) {
if (!it.hasNext()) {
it = players.iterator();
}

Player turnPlayer = it.next();
String playerName = turnPlayer.getPlayerName();

int guess = turnPlayer.guessNumber(io);
io.output(playerName+ "'s GUESS is " + number);

if (guess == numberToGuess) {
winner = turnPlayer;
}
else {
io.output(playerName + "'s Guess is Wrong!");
}
}

io.output(winner.getPlayerName()+ " Wins!");
}
}

Context

StackExchange Code Review Q#850, answer score: 11

Revisions (0)

No revisions yet.