Removed package and general cleanup

This commit is contained in:
Tim
2021-02-21 12:15:00 +10:30
parent 27ba49f586
commit cdb34fef3b
4 changed files with 25 additions and 36 deletions

View File

@@ -1,5 +1,3 @@
package aceyducey;
import java.util.Scanner;
/**
@@ -7,7 +5,7 @@ import java.util.Scanner;
*
* 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
* Note: The idea was to create a version of 1970's Basic game in Java, without introducing
* new features - no additional text, error checking, etc has been added.
*
*/
@@ -32,7 +30,7 @@ public class AceyDucey {
private boolean gameOver = false;
// Used for keyboard input
private Scanner kbScanner;
private final Scanner kbScanner;
// Constant value for cards from a deck - 2 lowest, 14 (Ace) highest
public static final int LOW_CARD_RANGE =2;
@@ -71,7 +69,6 @@ public class AceyDucey {
public void play() {
// Keep playing hands until player runs out of cash
// which sets gameover to true
do {
if(firstTimePlaying) {
intro();
@@ -81,17 +78,17 @@ public class AceyDucey {
drawCards();
displayCards();
int betAmount = getBet();
this.playersCard = randomCard();
playersCard = randomCard();
displayPlayerCard();
if(playerWon()) {
System.out.println("YOU WIN!!");
this.playerAmount += betAmount;
playerAmount += betAmount;
} else {
System.out.println("SORRY, YOU LOSE");
this.playerAmount -= betAmount;
playerAmount -= betAmount;
// Player run out of money?
if(this.playerAmount <=0) {
this.gameOver = true;
if(playerAmount <=0) {
gameOver = true;
}
}
@@ -102,18 +99,14 @@ public class AceyDucey {
// 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;
// winner
return (playersCard.getValue() >= firstCard.getValue())
&& playersCard.getValue() <= secondCard.getValue();
}
private void displayPlayerCard() {
System.out.println(this.playersCard.getName());
System.out.println(playersCard.getName());
}
// Get the players bet, and return the amount
@@ -121,16 +114,16 @@ public class AceyDucey {
// method will loop until a valid bet is entered.
private int getBet() {
boolean validBet = false;
int amount = 0;
int amount;
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) {
} else if(amount > playerAmount) {
System.out.println("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.");
System.out.println("YOU HAVE ONLY " + this.playerAmount + " DOLLARS TO BET.");
System.out.println("YOU HAVE ONLY " + playerAmount + " DOLLARS TO BET.");
} else {
validBet = true;
}
@@ -140,13 +133,13 @@ public class AceyDucey {
}
private void displayBalance() {
System.out.println("YOU NOW HAVE " + this.playerAmount + " DOLLARS.");
System.out.println("YOU NOW HAVE " + 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());
System.out.println(firstCard.getName());
System.out.println(secondCard.getName());
}
// Draw two dealer cards, and save them for later use.
@@ -154,9 +147,9 @@ public class AceyDucey {
private void drawCards() {
do {
this.firstCard = randomCard();
this.secondCard = randomCard();
} while(this.firstCard.getValue() >= this.secondCard.getValue());
firstCard = randomCard();
secondCard = randomCard();
} while(firstCard.getValue() >= secondCard.getValue());
}
// Creates a random card

View File

@@ -1,20 +1,16 @@
package aceyducey;
/**
* This class is used to invoke the game.
*
*/
public class Game {
private static AceyDucey game;
public class AceyDuceyGame {
public static void main(String[] args) {
boolean keepPlaying = true;
boolean keepPlaying;
AceyDucey game = new AceyDucey();
// Keep playing game until infinity or the player loses
do {
game = new AceyDucey();
game.play();
System.out.println();
System.out.println();

View File

@@ -1,5 +1,3 @@
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