HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Acey Ducey game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gameduceyacey

Problem

I'm a beginner in Java and want to improve and learn.

This is a game called "Acey Ducey":

The dealer shows you two cards. You decide whether you want the dealer to pick a third card or not. If the third card is between the two cards, then you win, otherwise you lose.

GitHub

This is the main file:

import model.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // load
        Player player = new Player();
        List deck = new ArrayList<>();
        for (CardColor color : CardColor.values()) {
            for (CardNumber number : CardNumber.values()) {
                Card card = new Card(number, color);
                deck.add(card);
            }
        }

        while(true) {
            System.out.println("---------------------");
            System.out.println("remaining cards " + deck.size());
            if (deck.size() = minValue && compareThis <= maxValue) {
                    System.out.println("You won!");
                    try{
                        player.depositMoney(2 * playerBet);
                    } catch (Exception e) {
                        System.out.println("Invalid amount of money");
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("though luck!");
                }
            }
        }

    }

    private static String getUserInput() {
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }

}


In the folder "model" I got these classes:

Card

```
package model;

public class Card {
private CardNumber number;
private CardColor color;

public Card() {};
public Card(CardNumber number, CardColor color) {
this.number = number;
this.color = color;
}

public CardNumber getNumber() {
return number;
}

public String getCardNumber() {
return num

Solution

I'll just go over some aspect of your code, the one that I noticed first.

Bare exceptions

You use bare Exceptions for different types of errors. Better is to create specialized exceptions or re-user exsisting ones, and add useful information.

For example:

public void setMoney(int money) throws IllegalArgumentException {
        if (money > 0) {
            this.money = money;
        } else {
            throw new IllegalArgumentException("'money' needs to be positive");
        }

    }


Printing stackstraces

You also show the user the stacktrace if that happens. I would not show the user this.

Possible misuse of methods

  • withdrawMoney allows for negative amounts.



  • pickRandomCardFrom fails if the Deck is empty (you currently have a check in the main() method, but it is safer to also check here)

Code Snippets

public void setMoney(int money) throws IllegalArgumentException {
        if (money > 0) {
            this.money = money;
        } else {
            throw new IllegalArgumentException("'money' needs to be positive");
        }

    }

Context

StackExchange Code Review Q#162438, answer score: 2

Revisions (0)

No revisions yet.