Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HighLowGame {
  4. public void run() {
  5. Player human = new Player();
  6. Player computer = new Player();
  7. boolean done = false;
  8. Scanner input = new Scanner(System.in);
  9.  
  10. System.out.println("Game Begins!\n");
  11. // Run unless interrupted or someone has no money to play
  12. while (!done && human.getMoney() > 0 && computer.getMoney() > 0) {
  13. System.out.println("You: $" + human.getMoney() + ", Computer: $" + computer.getMoney());
  14. System.out.println();
  15. System.out.print("What is your bet? $");
  16. int bet = input.nextInt();
  17.  
  18. int computerBet = Player.getRnd().nextInt(computer.getMoney()) + 1;
  19. System.out.println("The computer bets: $" + computerBet);
  20. System.out.println();
  21.  
  22. System.out.println("You both roll your dice....");
  23. human.roll();
  24. computer.roll();
  25.  
  26. System.out.println("The computer rolled a " + computer.getDie1() + " and a " + computer.getDie2());
  27. System.out.println("You rolled a " + human.getDie1() + " and a " + human.getDie2());
  28. System.out.println();
  29.  
  30. if (human.total() > computer.total()) {
  31. System.out.println("You WIN!");
  32. human.setMoney(human.getMoney() + bet);
  33. computer.setMoney(computer.getMoney() - computerBet);
  34. } else if (human.total() < computer.total()) {
  35. System.out.println("You LOST!");
  36. human.setMoney(human.getMoney() - bet);
  37. computer.setMoney(computer.getMoney() + computerBet);
  38. } else {
  39. System.out.println("Tie!");
  40. }
  41.  
  42. if (human.getMoney() != 0) {
  43. System.out.print("Play again? (y/n)");
  44. String playAgain = input.next();
  45. if ("n".equals(playAgain)) {
  46. done = true;
  47. }
  48. }
  49. }
  50.  
  51. // Display result of game
  52. if (human.getMoney() == 0) {
  53. System.out.print("You lost all your money.");
  54. } else if (computer.getMoney() == 0) {
  55. System.out.print("Computer lost all it's money");
  56. }
  57.  
  58. System.out.println("You left with $" + human.getMoney());
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement