Guest User

Untitled

a guest
Jun 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package com.uchechilaka.examples;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. class Player {
  7.  
  8. public String name;
  9. public String choice;
  10. public int numberOfFingers;
  11. public boolean wonLastGame = false;
  12.  
  13. Player(String name) {
  14. this.name = name;
  15. }
  16.  
  17. Player() {}
  18. }
  19.  
  20. public class OddsAndEvens {
  21.  
  22. //String playerName;
  23. //int numberOfFingers = 0;
  24. Scanner input;
  25. Player player1;
  26. Player player2;
  27.  
  28. OddsAndEvens() {
  29. input = new Scanner(System.in);
  30. }
  31.  
  32. public static void sectionBreak() {
  33. System.out.println("-----------------------------------------------------");
  34. }
  35.  
  36. public void intro() {
  37. System.out.println("Let's play a game called \"Odds nad Evens\"");
  38. System.out.print("What is your name? ");
  39. player1 = new Player(); player2 = new Player("The computer");
  40. player1.name = input.nextLine();
  41. System.out.println("Hi " + player1.name + ", which do you choose? (O)dds or (E)vens? ");
  42. String choice = input.next();
  43. if (choice.matches("(?i:^o)") || choice.equalsIgnoreCase("odds")) {
  44. player1.choice = "odds";
  45. System.out.println(player1.name + " has picked odds! " + player2.name + " will be evens");
  46. player2.choice = "evens";
  47. } else {
  48. player1.choice = "evens";
  49. System.out.println(player1.name + " has picked evens! The computer will be odds");
  50. player2.choice = "odds";
  51. }
  52. }
  53.  
  54. public void decide() {
  55. // calc result of the hand
  56. int sum = player1.numberOfFingers + player2.numberOfFingers;
  57. System.out.println(player1.numberOfFingers + " + " + player2.numberOfFingers + " = " + sum);
  58. // decide the game
  59. boolean numberIsEven = sum % 2 == 0;
  60. }
  61.  
  62. public void play() {
  63. System.out.print("How many \"fingers\" do you put out? ");
  64. player1.numberOfFingers = input.nextInt();
  65. // choose the computer's number of "fingers"
  66. player2.numberOfFingers = (new Random()).nextInt(6);
  67. System.out.println(player2.name + " plays " + player2.numberOfFingers + " number \"fingers\"");
  68. }
  69.  
  70.  
  71. public static void main(String[] args) {
  72. OddsAndEvens game = new OddsAndEvens();
  73. game.intro();
  74. sectionBreak();
  75. game.play();
  76. sectionBreak();
  77. game.decide();
  78. }
  79.  
  80. }
Add Comment
Please, Sign In to add comment