From cdb34fef3bfcba24185e3789998bc4d645dd5026 Mon Sep 17 00:00:00 2001 From: Tim <70119791+journich@users.noreply.github.com> Date: Sun, 21 Feb 2021 12:15:00 +1030 Subject: [PATCH] Removed package and general cleanup --- .gitignore | 2 + .../java/src/{aceyducey => }/AceyDucey.java | 47 ++++++++----------- .../Game.java => AceyDuceyGame.java} | 10 ++-- .../java/src/{aceyducey => }/Card.java | 2 - 4 files changed, 25 insertions(+), 36 deletions(-) create mode 100644 .gitignore rename 01 Acey Ducey/java/src/{aceyducey => }/AceyDucey.java (79%) rename 01 Acey Ducey/java/src/{aceyducey/Game.java => AceyDuceyGame.java} (72%) rename 01 Acey Ducey/java/src/{aceyducey => }/Card.java (98%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9bea4330 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/01 Acey Ducey/java/src/aceyducey/AceyDucey.java b/01 Acey Ducey/java/src/AceyDucey.java similarity index 79% rename from 01 Acey Ducey/java/src/aceyducey/AceyDucey.java rename to 01 Acey Ducey/java/src/AceyDucey.java index 37dcc57c..5320c8e0 100644 --- a/01 Acey Ducey/java/src/aceyducey/AceyDucey.java +++ b/01 Acey Ducey/java/src/AceyDucey.java @@ -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 diff --git a/01 Acey Ducey/java/src/aceyducey/Game.java b/01 Acey Ducey/java/src/AceyDuceyGame.java similarity index 72% rename from 01 Acey Ducey/java/src/aceyducey/Game.java rename to 01 Acey Ducey/java/src/AceyDuceyGame.java index 54582f5f..1ce08c28 100644 --- a/01 Acey Ducey/java/src/aceyducey/Game.java +++ b/01 Acey Ducey/java/src/AceyDuceyGame.java @@ -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(); diff --git a/01 Acey Ducey/java/src/aceyducey/Card.java b/01 Acey Ducey/java/src/Card.java similarity index 98% rename from 01 Acey Ducey/java/src/aceyducey/Card.java rename to 01 Acey Ducey/java/src/Card.java index ad15cced..d36f7301 100644 --- a/01 Acey Ducey/java/src/aceyducey/Card.java +++ b/01 Acey Ducey/java/src/Card.java @@ -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