Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
Created February 14, 2020 01:07
Show Gist options
  • Select an option

  • Save bkyrlach/f1f15f1f0485a4c5ea8d341032d81e15 to your computer and use it in GitHub Desktop.

Select an option

Save bkyrlach/f1f15f1f0485a4c5ea8d341032d81e15 to your computer and use it in GitHub Desktop.
package com.kyrlach.hello;
public class BetAction {
private int _amount;
private Face _face;
public BetAction(Face face, int amount) {
_face = face;
_amount = amount;
}
public int getAmount() {
return _amount;
}
public Face getFace() {
return _face;
}
@Override
public String toString() {
String faceString = "";
if(_face.equals(Face.HEADS)) {
faceString = "heads";
} else if (_face.equals(Face.TAILS)) {
faceString = "tails";
}
switch(_face) {
case HEADS:
faceString = "heads";
break;
case TAILS:
faceString = "tails";
break;
}
return "Betting $" + _amount + " on " + faceString + ".";
}
}
package com.kyrlach.hello;
public enum Face {
HEADS,
TAILS,
UNKNOWN
}
package com.kyrlach.hello;
import java.util.Random;
public class Game {
private final InputOutput _inputOutput;
private final Player _player;
private final Random _random;
public Game(InputOutput inputOutput, Player player) {
_inputOutput = inputOutput;
_player = player;
_random = new Random();
}
private Face flip() {
int rng = _random.nextInt(2) + 1;
Face result;
switch(rng) {
case 1:
result = Face.HEADS;
break;
case 2:
result = Face.TAILS;
break;
default:
result = Face.UNKNOWN;
}
return result;
}
private String wagerPrompt() {
return _player.getName() + ", you have " + _player.getBalance() + ". How much would you like to wager? ";
}
private int getWager() {
boolean done = false;
int wager = 0;
while(!done) {
try {
String wagerStr = _inputOutput.prompt(wagerPrompt());
wager = Integer.parseInt(wagerStr);
if(wager <= _player.getBalance()) {
if(wager > 0) {
done = true;
} else {
_inputOutput.println("You have to wager more than " + wager + ".");
}
} else {
_inputOutput.println("You don't have that much money to wager!");
}
} catch (Exception ex) {
_inputOutput.println("That doesn't look like a valid bet.");
}
}
return wager;
}
private Face getFace() {
boolean done = false;
Face face = Face.UNKNOWN;
while(!done) {
String faceStr = _inputOutput.prompt("Heads or tails? ");
if("heads".equals(faceStr)) {
face = Face.HEADS;
} else if ("tails".equals(faceStr)) {
face = Face.TAILS;
} else {
_inputOutput.println("I'm not sure what you mean by " + faceStr + ".");
}
done = !face.equals(Face.UNKNOWN);
}
return face;
}
private BetAction getBet() {
int wager = getWager();
Face face = getFace();
return new BetAction(face, wager);
}
private boolean donePlaying() {
boolean done = false;
boolean result = false;
while(!done) {
if(_player.getBalance() <= 0) {
_inputOutput.println("Looks like you're outta money, kid. Better luck next time!");
result = true;
done = true;
} else {
String keepPlayingStr = _inputOutput.prompt("You wanna have another go? ");
if("yes".equals(keepPlayingStr)) {
result = false;
done = true;
} else if ("no".equals(keepPlayingStr)) {
result = true;
done = true;
} else {
_inputOutput.println("I didn't understand you.");
}
}
}
return result;
}
private String faceToString(Face face) {
String result = "";
switch(face) {
case HEADS:
result = "heads";
break;
case TAILS:
result = "tails";
break;
}
return result;
}
public void start() {
boolean done = false;
_inputOutput.println("Hello, " + _player.getName() + ".");
while(!done) {
BetAction bet = getBet();
_inputOutput.println(bet.toString());
_inputOutput.println("Flipping the coin.");
Face flipResult = flip();
String flipResultStr = faceToString(flipResult);
_inputOutput.println("The coin landed on " + flipResultStr + "!");
if(flipResult.equals(bet.getFace())) {
_inputOutput.println("Looks like you won... this time!");
_player.updateBalance(bet.getAmount());
} else {
_inputOutput.println("Luck wasn't with you... this time!");
_player.updateBalance(bet.getAmount() * -1);
}
done = donePlaying();
}
}
public static void foo() {
}
}
package com.kyrlach.hello;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class InputOutput {
private Scanner _scanner;
private PrintStream _out;
public InputOutput(InputStream in, PrintStream out) {
_scanner = new Scanner(in);
_out = out;
}
public void println(String message) {
_out.println(message);
}
public void print(String message) {
_out.print(message);
}
public String prompt(String promptText) {
_out.print(promptText);
return _scanner.next();
}
public void cleanup() {
_scanner.close();
}
}
package com.kyrlach.hello;
public class Player {
private String _name;
private int _balance;
public Player(String name) {
this(name, 100);
}
public Player(String name, int balance) {
_name = name;
_balance = balance;
}
public String getName() {
return _name;
}
public int getBalance() {
return _balance;
}
public void updateBalance(int delta) {
_balance += delta;
}
}
package com.kyrlach.hello;
public class Program {
public static void main(String[] args) {
InputOutput inputOutput = new InputOutput(System.in, System.out);
String playerName = inputOutput.prompt("What is your name? ");
Player player = new Player(playerName);
Game game = new Game(inputOutput, player);
game.start();
inputOutput.println("Goodbye!");
inputOutput.cleanup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment