mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
180
01 Acey Ducey/java/src/aceyducey/AceyDucey.java
Normal file
180
01 Acey Ducey/java/src/aceyducey/AceyDucey.java
Normal file
@@ -0,0 +1,180 @@
|
||||
package aceyducey;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of AceyDucey
|
||||
*
|
||||
* Based on the Basic game of AceyDucey here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/01%20Acey%20Ducey/aceyducey.bas
|
||||
* Note: The idea was to create a version of this 1970's Basic game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*
|
||||
*/
|
||||
public class AceyDucey {
|
||||
|
||||
// Current amount of players cash
|
||||
private int playerAmount;
|
||||
|
||||
// First drawn dealer card
|
||||
private Card firstCard;
|
||||
|
||||
// Second drawn dealer card
|
||||
private Card secondCard;
|
||||
|
||||
// Players drawn card
|
||||
private Card playersCard;
|
||||
|
||||
// User to display game intro/instructions
|
||||
private boolean firstTimePlaying = true;
|
||||
|
||||
// game state to determine if game over
|
||||
private boolean gameOver = false;
|
||||
|
||||
// Used for keyboard input
|
||||
private Scanner kbScanner;
|
||||
|
||||
// Constant value for cards from a deck - 2 lowest, 14 (Ace) highest
|
||||
public static final int LOW_CARD_RANGE =2;
|
||||
public static final int HIGH_CARD_RANGE = 14;
|
||||
|
||||
public AceyDucey() {
|
||||
// Initialise players cash
|
||||
playerAmount = 100;
|
||||
|
||||
// Initialise kb scanner
|
||||
kbScanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
// Play again method - public method called from class invoking game
|
||||
// If player enters YES then the game can be played again (true returned)
|
||||
// otherwise not (false)
|
||||
public boolean playAgain() {
|
||||
System.out.println();
|
||||
System.out.println("SORRY, FRIEND, BUT YOU BLEW YOUR WAD.");
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.print("TRY AGAIN (YES OR NO) ");
|
||||
String playAgain = kbScanner.next().toUpperCase();
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
if(playAgain.equals("YES")) {
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("O.K., HOPE YOU HAD FUN!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// game loop method
|
||||
|
||||
public void play() {
|
||||
|
||||
// Keep playing hands until player runs out of cash
|
||||
// which sets gameover to true
|
||||
do {
|
||||
if(firstTimePlaying) {
|
||||
intro();
|
||||
firstTimePlaying = false;
|
||||
}
|
||||
displayBalance();
|
||||
drawCards();
|
||||
displayCards();
|
||||
int betAmount = getBet();
|
||||
this.playersCard = randomCard();
|
||||
displayPlayerCard();
|
||||
if(playerWon()) {
|
||||
System.out.println("YOU WIN!!");
|
||||
this.playerAmount += betAmount;
|
||||
} else {
|
||||
System.out.println("SORRY, YOU LOSE");
|
||||
this.playerAmount -= betAmount;
|
||||
// Player run out of money?
|
||||
if(this.playerAmount <=0) {
|
||||
this.gameOver = true;
|
||||
}
|
||||
}
|
||||
|
||||
} while (!gameOver); // Keep playing until player runs out of cash
|
||||
}
|
||||
|
||||
// Method to determine if player won (true returned) or lost (false returned)
|
||||
// to win a players card has to be in the range of the first and second dealer
|
||||
// drawn cards inclusive of first and second cards.
|
||||
private boolean playerWon() {
|
||||
if((this.playersCard.getValue() >= this.firstCard.getValue())
|
||||
&& this.playersCard.getValue() <= this.secondCard.getValue()) {
|
||||
// winner
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private void displayPlayerCard() {
|
||||
System.out.println(this.playersCard.getName());
|
||||
}
|
||||
|
||||
// Get the players bet, and return the amount
|
||||
// 0 is considered a valid bet, but better more than the player has available is not
|
||||
// method will loop until a valid bet is entered.
|
||||
private int getBet() {
|
||||
boolean validBet = false;
|
||||
int amount = 0;
|
||||
do {
|
||||
System.out.print("WHAT IS YOUR BET ");
|
||||
amount = kbScanner.nextInt();
|
||||
if(amount == 0) {
|
||||
System.out.println("CHICKEN!!");
|
||||
validBet = true;
|
||||
} else if(amount > this.playerAmount) {
|
||||
System.out.println("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.");
|
||||
System.out.println("YOU HAVE ONLY " + this.playerAmount + " DOLLARS TO BET.");
|
||||
} else {
|
||||
validBet = true;
|
||||
}
|
||||
} while (!validBet);
|
||||
|
||||
return amount;
|
||||
}
|
||||
|
||||
private void displayBalance() {
|
||||
System.out.println("YOU NOW HAVE " + this.playerAmount + " DOLLARS.");
|
||||
}
|
||||
|
||||
private void displayCards() {
|
||||
System.out.println("HERE ARE YOUR NEXT TWO CARDS: ");
|
||||
System.out.println(this.firstCard.getName());
|
||||
System.out.println(this.secondCard.getName());
|
||||
}
|
||||
|
||||
// Draw two dealer cards, and save them for later use.
|
||||
// ensure that the first card is a smaller value card than the second one
|
||||
private void drawCards() {
|
||||
|
||||
do {
|
||||
this.firstCard = randomCard();
|
||||
this.secondCard = randomCard();
|
||||
} while(this.firstCard.getValue() >= this.secondCard.getValue());
|
||||
}
|
||||
|
||||
// Creates a random card
|
||||
private Card randomCard() {
|
||||
return new Card((int) (Math.random()
|
||||
* (HIGH_CARD_RANGE - LOW_CARD_RANGE + 1) + LOW_CARD_RANGE));
|
||||
}
|
||||
|
||||
public void intro() {
|
||||
System.out.println("ACEY DUCEY CARD GAME");
|
||||
System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.println("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER");
|
||||
System.out.println("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP");
|
||||
System.out.println("YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING");
|
||||
System.out.println("ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE");
|
||||
System.out.println("A VALUE BETWEEN THE FIRST TWO.");
|
||||
System.out.println("IF YOU DO NOT WANT TO BET, INPUT A 0");
|
||||
}
|
||||
}
|
||||
47
01 Acey Ducey/java/src/aceyducey/Card.java
Normal file
47
01 Acey Ducey/java/src/aceyducey/Card.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package aceyducey;
|
||||
|
||||
/**
|
||||
* A card from a deck - the value is between 2-14 to cover
|
||||
* cards with a face value of 2-9 and then a Jack, Queen, King, and Ace
|
||||
*/
|
||||
public class Card {
|
||||
private int value;
|
||||
private String name;
|
||||
|
||||
Card(int value) {
|
||||
init(value);
|
||||
}
|
||||
|
||||
private void init(int value) {
|
||||
this.value = value;
|
||||
if(value <11) {
|
||||
this.name = String.valueOf(value);
|
||||
} else {
|
||||
switch (value) {
|
||||
case 11:
|
||||
this.name = "Jack";
|
||||
break;
|
||||
case 12:
|
||||
this.name = "Queen";
|
||||
break;
|
||||
case 13:
|
||||
this.name = "King";
|
||||
break;
|
||||
case 14:
|
||||
this.name = "Ace";
|
||||
break;
|
||||
|
||||
default:
|
||||
this.name = "Unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
25
01 Acey Ducey/java/src/aceyducey/Game.java
Normal file
25
01 Acey Ducey/java/src/aceyducey/Game.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package aceyducey;
|
||||
|
||||
/**
|
||||
* This class is used to invoke the game.
|
||||
*
|
||||
*/
|
||||
public class Game {
|
||||
|
||||
private static AceyDucey game;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
boolean keepPlaying = true;
|
||||
|
||||
// Keep playing game until infinity or the player loses
|
||||
do {
|
||||
game = new AceyDucey();
|
||||
game.play();
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
keepPlaying = game.playAgain();
|
||||
} while (keepPlaying);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user