Last active
June 21, 2018 20:55
-
-
Save uchilaka/a352a924d0149281d6f0c56693719642 to your computer and use it in GitHub Desktop.
Java code for OddsAndEvens assignment on **Microsoft: DEV276x**
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment