close

public class Player {
  int number;
  public void guess(){
    number = (int) (Math.random()*10);
    System.out.println("I guess " + number);
  }
}

public class GaessGame {
  Player p1;
  Player p2;
  Player p3;

  public void startGame(){
    p1 = new Player();
    p2 = new Player();
    p3 = new Player();

    int guess1 = 0;
    int guess2 = 0;
    int guess3 = 0;

    boolean p1IsRight = false;
    boolean p2IsRight = false;
    boolean p3IsRight = false;

    int target = (int) (Math.random()*10);
    System.out.println("Target is a number between 0 and 9");
    while(true){
      System.out.println("Number of target is " + target);
      p1.guess();
      p2.guess();
      p3.guess();

      guess1 = p1.number;
      System.out.println("Player one guess is " + guess1);
      guess2 = p2.number;
      System.out.println("Player two guess is " + guess2);
      guess3 = p3.number;
      System.out.println("Player three guess is " + guess3);

      if(guess1 == target){
        p1IsRight = true;
      }
      if(guess2 == target){
        p2IsRight = true;
      }
      if(guess3 == target){
        p3IsRight = true;
      }

      if(p1IsRight || p2IsRight || p3IsRight){
        System.out.println("We have a winner");
        System.out.println("Player one got it right " + p1IsRight);
        System.out.println("Player two got it right " + p2IsRight);
        System.out.println("Player three got it right " + p3IsRight);
        break;
      }
      else{
        System.out.println("Continue the Game");
      }

    }
  }
}

public class GameLauncher {
  public static void main(String[] args) {
    GaessGame game = new GaessGame();
    game.startGame();
  }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 阿洲 的頭像
    阿洲

    阿洲程式天地

    阿洲 發表在 痞客邦 留言(0) 人氣()