dimanche 27 juin 2021

Create a competitive guessing game between two players in OOP

I'm new to Java and I create a number guessing game to practice the concept of object-oriented programming, but I am not sure if I am doing it correctly.

The objective of the game is a competitive guessing game between two players where each player will get 3 turns each and I want to set on 5 rounds of the game will have. The one with the most correct guesses wins. Players will need to guess a number between 1 to 10. I give some hints for the guesses by indicating if they are getting too high or getting too low. Player 1 will always be the first to go, whoever wins in the first round will be the first player in the next round. Keep track of the number of wins for each player as well as their name and display which player is the winner for each round and who is the grand winner or draw.

I bet there are some errors especially in the Player class and TwoPlayer class? How can I improve my code?

import java.util.Random;
import java.util.Scanner;

public class Game {
    private Scanner input;
    private Player player;
    private GuessNumberGame guessNumberGame;
    private boolean running;
    private String command;
    private TwoPlayer twoplayer;

    Game() {
        this.player = new Player();
        this.input = new Scanner(System.in);
        this.guessNumberGame = new GuessNumberGame(10);
        this.running = true;
        this.twoplayer = new TwoPlayer();
    }

    public void init() {
        while (running) {
            System.out.println("Commands: Play or Stop?");
            System.out.print(">>");
            this.command = this.input.next();

            if (this.command.equals("stop")) {
                this.running = false;
            } else if (command.equals("play")) {
                this.player.play(this.input, this.guessNumberGame);
                this.guessNumberGame.setSecretNumber(10);

            } else {
                System.out.println("unknown command");
            }
        }
    }
    public class GuessNumberGame {
      private int secretNumber;
      private Random random;

    GuessNumberGame(int limit) {
        this.random = new Random();
        this.setSecretNumber(limit);
    }

    public void setSecretNumber(int limit) {
        this.secretNumber = this.random.nextInt(limit);
    }

    public int getSecretNumber() {
        return this.secretNumber;
    }
}
    public class TwoPlayer {
      private String name;
      private int playerGuess;
      
      public void setName(String name){
        this.name = name;
      }
      public String getName(){
        return this.name;
      }
      public void setplayerGuess(int playerGuess){
        this.playerGuess = playerGuess;
      }
      public int getplayerGuess(){
        return this.playerGuess;
      }
    }
      
    public class Player {
      public void play(Scanner input, GuessNumberGame game) {
        //create 2 players to play
        Player p1 = new Player();
        Player p2 = new Player();
        
        int tries = 3;
        int number = 0;
        int round = 5;
        int draws = 0;
        int playerOneRound = 0;
        int playerTwoRound = 0;
        
       
        System.out.print("Enter Player 1 name:");
        String p1Name = input.next();
        twoplayer.setName(p1Name);
        
        System.out.print("Enter Player 2 name:");
        String p2Name = input.next();
        twoplayer.setName(p2Name);
        
        
        for (int j = 0; j < round; j++){
          for (int i = 0; i < tries; i++) {
            System.out.println("Enter your guess number Player 1: ");
            number1 = Integer.parseInt(input.next());
            System.out.println("Enter your guess number Player 2: ");
            number2 = Integer.parseInt(input.next());
            if (number1 > game.getSecretNumber()) {
              System.out.println("Player 1 guess is too high.");
              tries--;
              System.out.print(" Player 1 has "+tries+" left.");
            }
            else if (number1 < game.getSecretNumber()) {
              System.out.println("Player 1 guess is too low.");
              tries--;
              System.out.print(" Player 1 has "+tries+" left.");
            }
            if (number2 > game.getSecretNumber()) {
              System.out.println("Player 2 guess is too high.");
              tries--;
              System.out.print(" Player 2 has "+tries+" left.");
            }
            else if (number2 < game.getSecretNumber()) {
              System.out.println("Player 2 guess is too low.");
              tries--;
              System.out.print(" Player 2 has "+tries+" left.");
            }
            else {
              break;
            }
          }
        
        if (number1 == game.getSecretNumber()) {
          playerOneRound++;
          System.out.println("Congratulations Player 1! You got it right.");
          round--;
            System.out.println("You have "+round+" game(s) left.");
        }
        else if (number2 == game.getSecretNumber()){
          playerTwoRound++;
          System.out.println("Congratulations Player 2! You got it right.");
          round--;
          System.out.println("You have "+round+" game(s) left.");
        }
        else if (number1 == game.getSecretNumber() && number2 == game.getSecretNumber()){
          System.out.println("Congratulations Player 1 and Player 2! You both got it draw.");
          round--;
          draws++;
          System.out.println("You have "+round+" game(s) left.");
        }
        else{
          round--;
          System.out.println("The number I was thinking of is "+game.getSecretNumber());
        }
    }
        System.out.println("--------RESULT----------");
        
        System.out.println(p1Name+" WON "+playerOneRound+" GAMES!");
        System.out.println(p2Name+" WON "+playerTwoRound" GAMES!");
        System.outprintln("DRAWS - "+draws);
        
        System.out.println();
  
        if (playerOneCounter > playerTwoCounter) {
          System.out.println("Congratulatons to you "+p1Name+"! You Win");
        } 
        else if(playerOneCounter < playerTwoCounter) {
          System.out.println("Congratulatons to you "+p2Name+"! You Win");
        }
        else {
          System.out.println("It was a Draw!");
        }
}
    }
}

And I have a TestGame where I have the main() method which starts the game.

  public static void main(String[] args){
    Game game = new Game();
    game.init();
  }
}



Aucun commentaire:

Enregistrer un commentaire