commit 99426992522a5657f37c8d241c69e96744e952a2
parent 335a7ef3a73f63ae8fa49fcb05e4d62d8843936e
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date: Thu, 27 Apr 2023 22:23:44 +0200
Format code
- Remove whitespace
- Fix indentation
- Add/remove spaces where appropriate
Diffstat:
10 files changed, 71 insertions(+), 74 deletions(-)
diff --git a/src/controller/Controller.java b/src/controller/Controller.java
@@ -8,7 +8,7 @@ import model.Statistics;
/**
* Handles most of the game logic
- *
+ *
* @author Michael
*/
public class Controller {
@@ -19,11 +19,9 @@ public class Controller {
private Player player;
private double bet, balance;
private Statistics stats = new Statistics();
-
/**
- * Controller Constructor:
- * Initializes score to 0
+ * Controller Constructor: Initializes score to 0
*/
public Controller() {
@@ -31,13 +29,13 @@ public class Controller {
bet = 100.0;
// create new populated deck
- deck = new Deck();
+ deck = new Deck();
deck.populate();
deck.shuffle();
// create new empty deck
- discarded = new Deck();
-
+ discarded = new Deck();
+
// create new dealer and player
dealer = new Dealer();
player = new Player();
@@ -48,7 +46,7 @@ public class Controller {
}
public void setBet(double bet) {
- if(bet < balance) {
+ if (bet < balance) {
this.bet = bet;
} else { // in case they try to make a bet bigger than their balance
JOptionPane.showMessageDialog(null, "You don't have that much money");
@@ -65,16 +63,16 @@ public class Controller {
/**
* Handles logic for each round
- *
+ *
*/
public void startRound() {
// if we run out of cards
dealer.getHand().discardHandToDeck(discarded);
player.getHand().discardHandToDeck(discarded);
-
+
// make sure deck has at least 4 cards left
- if(deck.cardsLeft() < 4) {
+ if (deck.cardsLeft() < 4) {
deck.reloadDeckFromDiscard(discarded);
}
@@ -85,11 +83,11 @@ public class Controller {
// give the player 2 cards
player.getHand().takeCardFromDeck(deck);
player.getHand().takeCardFromDeck(deck);
-
+
}
public String checkWhoWins() {
-
+
// check if dealer has blackjack
if (dealer.hasBlackjack()) {
dealer.printHand(); // show full hand
@@ -106,59 +104,59 @@ public class Controller {
}
// check if player has blackjack
- if(player.hasBlackjack()) {
+ if (player.hasBlackjack()) {
balance += bet * 1.5;
stats.incrementWins();
return "You have BlackJack - You win";
}
// check whether player busted
- if(player.getHand().calculateValue() > 21) {
+ if (player.getHand().calculateValue() > 21) {
balance -= bet;
stats.incrementLosses();
return "Busted";
}
-
+
// dealer's turn
dealer.printHand();
- while(dealer.getHand().calculateValue() < 17) {
+ while (dealer.getHand().calculateValue() < 17) {
dealer.hit(deck, discarded);
}
-
+
// check who wins
- if(dealer.getHand().calculateValue() > 21) {
+ if (dealer.getHand().calculateValue() > 21) {
balance += bet;
stats.incrementWins();
return "Dealer busts - You win";
}
-
- if(dealer.getHand().calculateValue() > player.getHand().calculateValue() && dealer.getHand().calculateValue() <= 21) {
+
+ if (dealer.getHand().calculateValue() > player.getHand().calculateValue() && dealer.getHand().calculateValue() <= 21) {
balance -= bet;
stats.incrementLosses();
return "You lose";
}
- if(player.getHand().calculateValue() > dealer.getHand().calculateValue()) {
+ if (player.getHand().calculateValue() > dealer.getHand().calculateValue()) {
balance += bet;
stats.incrementWins();
return "You win";
}
-
- if(player.getHand().calculateValue() == dealer.getHand().calculateValue()) {
+
+ if (player.getHand().calculateValue() == dealer.getHand().calculateValue()) {
stats.incrementPushes();
return "Push";
}
-
+
return "Something went wrong";
}
-
+
/**
* Hits the player with a card
*/
public void hitPlayer() {
player.hit(deck, discarded);
}
-
+
/**
*
* @return player hand
@@ -166,41 +164,41 @@ public class Controller {
public String displayPlayerHand() {
return player.printHand();
}
-
+
/**
- *
+ *
* @return the value of the player's hand
*/
public String displayPlayerHandValue() {
return Integer.toString(player.getHand().calculateValue());
}
-
+
/**
- *
+ *
* @return dealer hand
*/
public String displayDealerHand() {
return dealer.printHand();
}
-
+
/**
- *
+ *
* @return value of dealer's hand
*/
public String displayDealerHandValue() {
return Integer.toString(dealer.getHand().calculateValue());
}
-
+
/**
- *
+ *
* @return dealer's first card
*/
public String displayDealerFirstCard() {
return dealer.printFirstHand();
}
-
+
/**
- *
+ *
* @return value of the first card
*/
public String displayDealerFirstCardValue() {
diff --git a/src/controller/Hand.java b/src/controller/Hand.java
@@ -6,8 +6,9 @@ import model.Card;
import model.Deck;
/**
- * Contains methods to calculate the value of a hand, take cards from a deck and discard the hand to a deck.
- *
+ * Contains methods to calculate the value of a hand, take cards from a deck and
+ * discard the hand to a deck.
+ *
* @author Michael
*/
public class Hand {
@@ -15,8 +16,7 @@ public class Hand {
private ArrayList<Card> hand;
/**
- * Hand Constructor:
- * Creates empty hand
+ * Hand Constructor: Creates empty hand
*/
public Hand() {
hand = new ArrayList<Card>();
@@ -24,7 +24,7 @@ public class Hand {
/**
* Adds a card from the deck to the hand
- *
+ *
* @param deck deck to take card from
*/
public void takeCardFromDeck(Deck deck) {
@@ -68,7 +68,7 @@ public class Hand {
/**
* Discards hand to deck
- *
+ *
* @param discardDeck
*/
public void discardHandToDeck(Deck discardDeck) {
diff --git a/src/controller/LoginController.java b/src/controller/LoginController.java
@@ -71,7 +71,7 @@ public class LoginController {
*
* @param mobileNumber the mobile number to check
* @return true if the mobile number is a valid South African mobile number,
- * false otherwise
+ * false otherwise
*/
private boolean isValidSouthAfricanMobileNumber(String mobileNumber) {
// Remove any non-digit characters from the mobile number
@@ -106,7 +106,7 @@ public class LoginController {
*
* @param date the date to be checked
* @return true if the date is valid and the person is at least 18 years
- * old, false otherwise
+ * old, false otherwise
*/
private static boolean isValidDateAndAdult(Date date) {
// Check if the date is null
diff --git a/src/model/Card.java b/src/model/Card.java
@@ -2,7 +2,7 @@ package model;
/**
* Represents a card and handles card operations
- *
+ *
* @author Michael
*/
public class Card {
@@ -59,9 +59,8 @@ public class Card {
private Rank rank;
/**
- * Card Constructor:
- * Creates a card given a suit and rank
- *
+ * Card Constructor: Creates a card given a suit and rank
+ *
* @param suit Card suit
* @param rank Card rank
*/
@@ -71,9 +70,8 @@ public class Card {
}
/**
- * Card Constructor:
- * Creates a new card as a copy of another card
- *
+ * Card Constructor: Creates a new card as a copy of another card
+ *
* @param card card to be added
*/
public Card(Card card) {
diff --git a/src/model/Dealer.java b/src/model/Dealer.java
@@ -1,8 +1,9 @@
package model;
/**
- * Handles dealer specific operations, such as drawing cards to 16 and standing on 17 or above.
- *
+ * Handles dealer specific operations, such as drawing cards to 16 and standing
+ * on 17 or above.
+ *
* @author Michael
*/
public class Dealer extends Person {
@@ -17,7 +18,7 @@ public class Dealer extends Person {
public String printFirstHand() {
return "" + super.getHand().getCard(0);
}
-
+
public String printFirstCardValue() {
return "" + super.getHand().getCard(0).getValue();
}
diff --git a/src/model/Deck.java b/src/model/Deck.java
@@ -8,7 +8,7 @@ import model.Card.Suit;
/**
* Holds multiple cards and performs operations such as shuffling the deck
- *
+ *
* @author Michael
*/
public class Deck {
@@ -57,7 +57,7 @@ public class Deck {
/**
* Method to add a card to the deck
- *
+ *
* @param inCard card the be added
*/
public void addCard(Card inCard) {
@@ -66,19 +66,19 @@ public class Deck {
/**
* Takes copy of first card from deck and removes card from deck
- *
+ *
* @return cardToTake card that was removed
*/
public Card takeCard() { // TODO: change take to draw
Card cardToTake = new Card(deck.get(0));
deck.remove(0);
-
+
return cardToTake;
}
/**
* Makes sure the deck we're hitting from has cards in it
- *
+ *
* @return true (if the deck has cards in)
* @return false (if the deck is empty)
*/
@@ -95,7 +95,7 @@ public class Deck {
/**
* Adds arraylist of cards to deck
- *
+ *
* @param cards arraylist of cards to be added to deck
*/
public void addCards(ArrayList<Card> cards) {
diff --git a/src/model/Person.java b/src/model/Person.java
@@ -4,7 +4,7 @@ import controller.Hand;
/**
* Parent class of the Player and Dealer Objects
- *
+ *
* @author Michael
*/
public abstract class Person {
@@ -38,14 +38,14 @@ public abstract class Person {
/**
* Checks if a person has blackjack
- *
+ *
* @return true (if player has a blackjack)
* @return false (if player does not have a blackjack)
*/
public boolean hasBlackjack() {
return this.getHand().calculateValue() == 21;
}
-
+
/**
* Prints a formatted version of the person's hand
*/
@@ -53,10 +53,10 @@ public abstract class Person {
public String printHand() {
return this.hand + "[" + this.hand.calculateValue() + "]";
}
-
+
/**
* Checks if deck has cards and reloads deck if it ran out
- *
+ *
* @param deck deck to draw from
* @param discard deck to discard to
*/
diff --git a/src/model/Player.java b/src/model/Player.java
@@ -9,11 +9,11 @@ import java.util.Scanner;
/**
* Handles player specific operations, such as deciding to hit or stand.
- *
+ *
* @author Michael
*/
public class Player extends Person {
-
+
private double balance; // TODO: make public
Scanner input = new Scanner(System.in);
@@ -23,7 +23,7 @@ public class Player extends Person {
public Player() {
super.setName("Player");
}
-
+
public double getBalance(String inFile) {
try {
Scanner scFile = new Scanner(new File(inFile));
diff --git a/src/view/MainGUI.java b/src/view/MainGUI.java
@@ -11,7 +11,7 @@ import javax.swing.JOptionPane;
* @author Michael
*/
public class MainGUI extends javax.swing.JFrame {
-
+
Controller game = new Controller();
/**
@@ -339,12 +339,12 @@ public class MainGUI extends javax.swing.JFrame {
new MainGUI().setVisible(true);
});
}
-
+
public void printDealerHand() {
jLabelDisplayDealerHand.setText(game.displayDealerHand());
jLabelDealerHandValue.setText(game.displayDealerHandValue());
}
-
+
public void printPlayerHand() {
jLabelDisplayPlayerHand.setText(game.displayPlayerHand());
jLabelPlayerHandValue.setText(game.displayPlayerHandValue());
diff --git a/src/view/StatisticsGUI.java b/src/view/StatisticsGUI.java
@@ -1,4 +1,5 @@
package view;
+
import com.opencsv.exceptions.CsvValidationException;
import java.io.IOException;
import java.util.logging.Level;
@@ -10,10 +11,9 @@ import model.Statistics;
* @author michael
*/
public class StatisticsGUI extends javax.swing.JFrame {
-
+
Statistics stats;
-
/**
* Creates new form StatisticsGUI
*/