Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.uchechilaka.examples;
- import java.util.Random;
- import java.util.Scanner;
- class Player {
- public String name;
- public String choice;
- public int numberOfFingers;
- public boolean wonLastGame = false;
- Player(String name) {
- this.name = name;
- }
- Player() {}
- }
- public class OddsAndEvens {
- //String playerName;
- //int numberOfFingers = 0;
- Scanner input;
- Player player1;
- Player player2;
- OddsAndEvens() {
- input = new Scanner(System.in);
- }
- public static void sectionBreak() {
- System.out.println("-----------------------------------------------------");
- }
- public void intro() {
- System.out.println("Let's play a game called \"Odds nad Evens\"");
- System.out.print("What is your name? ");
- player1 = new Player(); player2 = new Player("The computer");
- player1.name = input.nextLine();
- System.out.println("Hi " + player1.name + ", which do you choose? (O)dds or (E)vens? ");
- String choice = input.next();
- if (choice.matches("(?i:^o)") || choice.equalsIgnoreCase("odds")) {
- player1.choice = "odds";
- System.out.println(player1.name + " has picked odds! " + player2.name + " will be evens");
- player2.choice = "evens";
- } else {
- player1.choice = "evens";
- System.out.println(player1.name + " has picked evens! The computer will be odds");
- player2.choice = "odds";
- }
- }
- public void decide() {
- // calc result of the hand
- int sum = player1.numberOfFingers + player2.numberOfFingers;
- System.out.println(player1.numberOfFingers + " + " + player2.numberOfFingers + " = " + sum);
- // decide the game
- boolean numberIsEven = sum % 2 == 0;
- }
- public void play() {
- System.out.print("How many \"fingers\" do you put out? ");
- player1.numberOfFingers = input.nextInt();
- // choose the computer's number of "fingers"
- player2.numberOfFingers = (new Random()).nextInt(6);
- System.out.println(player2.name + " plays " + player2.numberOfFingers + " number \"fingers\"");
- }
- public static void main(String[] args) {
- OddsAndEvens game = new OddsAndEvens();
- game.intro();
- sectionBreak();
- game.play();
- sectionBreak();
- game.decide();
- }
- }
Add Comment
Please, Sign In to add comment