Created
December 28, 2018 19:07
-
-
Save Gzoref/d8b126605138cad58cfb061159dce3b2 to your computer and use it in GitHub Desktop.
Cho_Han
This file contains hidden or 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
import java.util.Scanner; | |
public class ChoHanMain { | |
public static void main(String[] args) { | |
final int MAX = 5; | |
String player1Name; | |
String player2Name; | |
Scanner input = new Scanner(System.in); | |
System.out.println("Player one name:"); | |
player1Name = input.nextLine(); | |
System.out.println("Player two name:"); | |
player2Name = input.nextLine(); | |
Dealer dealer = new Dealer(); | |
Player player1 = new Player(player1Name); | |
Player player2 = new Player(player2Name); | |
for (int round = 0; round < MAX; round++) { | |
System.out.println("--------------------"); | |
System.out.printf("Round %d\n", (round + 1)); | |
dealer.rollDice(); | |
player1.makeGuess(); | |
player2.makeGuess(); | |
roundResults(dealer, player1, player2); | |
} | |
displayWinner(player1, player2); | |
} | |
public static void roundResults(Dealer dealer, Player player1, Player player2) { | |
System.out.printf("Dealer rolled %d and %d.\n", dealer.getDie1Value(), dealer.getDie2Value()); | |
System.out.printf("Result: %s\n", dealer.getChoOrHan()); | |
checkGuess(player1, dealer); | |
checkGuess(player2, dealer); | |
} | |
private static void checkGuess(Player player, Dealer dealer) { | |
final int POINTS_TO_ADD = 1; | |
String guess = player.getGuess(); | |
String choHanResults = dealer.getChoOrHan(); | |
System.out.printf("Player %s guessed %s\n", player.getName(), player.getGuess()); | |
if(guess.equalsIgnoreCase(choHanResults)) { | |
player.addPoints(POINTS_TO_ADD); | |
System.out.printf("Awarding %d point(s) to %s.\n", POINTS_TO_ADD, player.getName()); | |
} | |
} | |
private static void displayWinner(Player player1, Player player2) { | |
System.out.println("---------------------------------"); | |
System.out.println("Game Over. Results:"); | |
System.out.printf("%s: %d points\n", player1.getName(), player1.getPoints()); | |
System.out.printf("%s: %d points\n", player2.getName(), player2.getPoints()); | |
if(player1.getPoints() > player2.getPoints()){ | |
System.out.println(player1.getName() + "is the winner!"); | |
}else if(player2.getPoints() > player1.getPoints()) { | |
System.out.println(player2.getName() + "is the winner!"); | |
}else{ | |
System.out.println("It's a tie!"); | |
} | |
} | |
} | |
/** | |
*/ |
This file contains hidden or 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
/* | |
* Name: Geoffrey Zoref | |
* Date: 11/28/2018 | |
* Project: Cho_Han | |
*/ | |
public class Dealer { | |
private int die1Value; | |
private int die2Value; | |
public Dealer() { | |
die1Value = 0; | |
die2Value = 0; | |
} | |
public void rollDice() { | |
final int SIDES = 6; | |
Die die1 = new Die(SIDES); | |
Die die2 = new Die(SIDES); | |
die1Value = die1.getValue(); | |
die1Value = die2.getValue(); | |
} | |
//even = Cho | |
//Odd = Han | |
public String getChoOrHan(){ | |
String result; | |
int sum = die1Value + die2Value; | |
if(sum % 2 == 0){ | |
result = "Cho (Even)"; | |
}else{ | |
result = "Han(Odd)"; | |
} | |
return result; | |
} | |
public int getDie1Value() { | |
return die1Value; | |
} | |
public int getDie2Value() { | |
return die2Value; | |
} | |
} | |
/** | |
*/ |
This file contains hidden or 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
import java.util.Random; | |
/* | |
* Name: Geoffrey Zoref | |
* Date: 11/10/2018 | |
* Project: First to one Game | |
*/ | |
class Die { | |
private int sides; | |
private int value; | |
public Die(int numSides) | |
{ | |
sides = numSides; | |
roll(); | |
} | |
public void roll() | |
{ | |
// Create a Random object. | |
Random rand = new Random(); | |
// Get a random value for the die. | |
value = rand.nextInt(sides) + 1; | |
} | |
public int getSides() | |
{ | |
return sides; | |
} | |
public int getValue() | |
{ | |
return value; | |
} | |
} | |
/** | |
*/ |
This file contains hidden or 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
import java.util.Random; | |
/* | |
* Name: Geoffrey Zoref | |
* Date: 11/28/2018 | |
* Project: Cho_Han | |
*/ | |
public class Player { | |
private String name, guess; | |
private int points; | |
public Player(String playerName) { | |
name = playerName; | |
guess = ""; | |
points = 0; | |
} | |
public void makeGuess() { | |
Random random = new Random(); | |
int guessNumber = random.nextInt(2); | |
if(guessNumber == 0) { | |
guess = "Cho(Even)"; | |
}else { | |
guess = "Han(Odd)"; | |
} | |
} | |
public void addPoints(int newPoints) { | |
points += newPoints; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getGuess() { | |
return guess; | |
} | |
public int getPoints() { | |
return points; | |
} | |
} | |
/** | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment