patternjavaMinor
Using multiple methods with Blackjack
Viewed 0 times
withmethodsusingmultipleblackjack
Problem
I am very new to Java and programming theory and am desperately trying to improve my knowledge. This is the first program I've made without help and really would appreciate some feedback. I know there must be 1,000,000 better ways to do what I did.
Notes:
Images of console to give you an idea of two runs:
Any and all comments are appreciated. I really want to get to grips with basic Java programming concepts before my bad habits are too engraved to break!
```
import java.util.*;
public class BlackJack {
public static void main(String[] args) {
/*
* Scanner && User Variables
*/
Scanner kb = new Scanner(System.in);
int usersDecision = 0;
/*
* Users && Dealers Value Variables
*/
int usersValue = 0;
int dealersValue = 0;
/*
* Suit && Rank Arrays
*/
String[] card = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"};
/*
* Array Lists Users Cards && Dealers Cards
*/
ArrayList usersCards = new ArrayList();
ArrayList dealersCards = new ArrayList();
/*
* GENERATE DEALERS FIRST CARD
*/
for (int i = 0; i 21) {
System.out.println("You've Bust! You Lose!");
System.exit(0);
}
break;
case 2:
System.out.println("You've Stuck - Your Cards: " + usersCards +"\n");
x = 1;
/*
* For Loop
* Generate Users Card Value
*/
usersValue = 0;
Notes:
- I want to move away from
main()programming. How could I achieve this program using methods and OOP?
- I am aware that the
Aceshould not just be = 11 and should give the user the chance to dictate its value, but implementing that would not of taught me anything new.
- The way in which I calculated the dealers & users card value is atrocious, especially because I had to re-iterate it constantly. This is the biggest problem I want to fix.
Images of console to give you an idea of two runs:
Any and all comments are appreciated. I really want to get to grips with basic Java programming concepts before my bad habits are too engraved to break!
```
import java.util.*;
public class BlackJack {
public static void main(String[] args) {
/*
* Scanner && User Variables
*/
Scanner kb = new Scanner(System.in);
int usersDecision = 0;
/*
* Users && Dealers Value Variables
*/
int usersValue = 0;
int dealersValue = 0;
/*
* Suit && Rank Arrays
*/
String[] card = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"};
/*
* Array Lists Users Cards && Dealers Cards
*/
ArrayList usersCards = new ArrayList();
ArrayList dealersCards = new ArrayList();
/*
* GENERATE DEALERS FIRST CARD
*/
for (int i = 0; i 21) {
System.out.println("You've Bust! You Lose!");
System.exit(0);
}
break;
case 2:
System.out.println("You've Stuck - Your Cards: " + usersCards +"\n");
x = 1;
/*
* For Loop
* Generate Users Card Value
*/
usersValue = 0;
Solution
You seem to be repeating sections like this.
First, it does not make sense to use a
So why not refactor both into a separate method?
And you can use it thus
Also note that it is better to take out numbers such as
--
These sections seem to be repeating again and again. You could make them into a method like above, and call it instead of repeating them.
Using it,
for (int i = 0; i <= 0; i++) {
int randomGenNumber = (int) (Math.random()*13);
dealersCards.add(card[randomGenNumber]);
}First, it does not make sense to use a
for loop if the number of loops are just one. On the other hand, I also notice that you have for (int i = 0; i <= 1; i++) {
int randomGenNumber = (int) (Math.random()*13);
usersCards.add(card[randomGenNumber]);
}So why not refactor both into a separate method?
static final int MaxCard = 13;
void dealCards(ArrayList cards, int num) {
for (int i = 0; i < num; i++) {
cards.add(card[(int) (Math.random()*MaxCard)]);
}
}And you can use it thus
dealCards(dealersCards, 1); // for the first type
dealCards(usersCards, 2); // for the second typeAlso note that it is better to take out numbers such as
13 and declare them to be a constant with a good variable name that denotes their purpose. It helps you to understand your code at a later time.--
usersValue = 0;
for(int i = 0; i < usersCards.size(); i++) {
if(usersCards.get(i).equals("2")) {
usersValue += 2;
} else if(usersCards.get(i).equals("3")) {
usersValue += 3;
} else if(usersCards.get(i).equals("4")) {
usersValue += 4;
} else if(usersCards.get(i).equals("5")) {
usersValue += 5;
} else if(usersCards.get(i).equals("6")) {
usersValue += 6;
} else if(usersCards.get(i).equals("7")) {
usersValue += 7;
} else if(usersCards.get(i).equals("8")) {
usersValue += 8;
} else if(usersCards.get(i).equals("9")) {
usersValue += 9;
} else if(usersCards.get(i).equals("10")) {
usersValue += 10;
} else if(usersCards.get(i).equals("Jack")) {
usersValue += 10;
} else if(usersCards.get(i).equals("Queen")) {
usersValue += 10;
} else if(usersCards.get(i).equals("King")) {
usersValue += 10;
} else if(usersCards.get(i).equals("Ace")) {
usersValue += 11;
}
}These sections seem to be repeating again and again. You could make them into a method like above, and call it instead of repeating them.
class NoParse extends Exception {}
...
String[] tenValcards = {"Jack", "Queen", "King"};
int parseCard(String card) throws NoParse {
char c = card.charAt(0);
int i = Character.digit(c,10);
switch(i) {
case -1: {
for (String c : tenValueCards)
if c.equals(card) return 10;
break;
}
case 1: {
i = Character.digit(card.charAt(1),10);
if (i == 0) return 10;
break;
}
case 0: break;
default: return i;
}
// handle the parse error.
throw new NoParse(card);
}Using it,
usersValue += parseCard(usersCards.get(i));Code Snippets
for (int i = 0; i <= 0; i++) {
int randomGenNumber = (int) (Math.random()*13);
dealersCards.add(card[randomGenNumber]);
}for (int i = 0; i <= 1; i++) {
int randomGenNumber = (int) (Math.random()*13);
usersCards.add(card[randomGenNumber]);
}static final int MaxCard = 13;
void dealCards(ArrayList<String> cards, int num) {
for (int i = 0; i < num; i++) {
cards.add(card[(int) (Math.random()*MaxCard)]);
}
}dealCards(dealersCards, 1); // for the first type
dealCards(usersCards, 2); // for the second typeusersValue = 0;
for(int i = 0; i < usersCards.size(); i++) {
if(usersCards.get(i).equals("2")) {
usersValue += 2;
} else if(usersCards.get(i).equals("3")) {
usersValue += 3;
} else if(usersCards.get(i).equals("4")) {
usersValue += 4;
} else if(usersCards.get(i).equals("5")) {
usersValue += 5;
} else if(usersCards.get(i).equals("6")) {
usersValue += 6;
} else if(usersCards.get(i).equals("7")) {
usersValue += 7;
} else if(usersCards.get(i).equals("8")) {
usersValue += 8;
} else if(usersCards.get(i).equals("9")) {
usersValue += 9;
} else if(usersCards.get(i).equals("10")) {
usersValue += 10;
} else if(usersCards.get(i).equals("Jack")) {
usersValue += 10;
} else if(usersCards.get(i).equals("Queen")) {
usersValue += 10;
} else if(usersCards.get(i).equals("King")) {
usersValue += 10;
} else if(usersCards.get(i).equals("Ace")) {
usersValue += 11;
}
}Context
StackExchange Code Review Q#13004, answer score: 6
Revisions (0)
No revisions yet.