Originally Posted by
jediassassin37
Hello! I'm in an intro to CS class and our teacher is giving us pretty difficult hw. It may be simple to lots of people but as a person that has never seen programming until now, it's pretty difficult. Anyways, I have to program a simplified version of blackjack where I don't need to worry about aces being 1 or 11 (extra credit), splits, doubling down, surrenders, or blackjacks. The only knowledge we are suppose to use include arrays, loops, if statements, and everything less advanced than that. The catch is that we have to program it using 1 deck, 2 decks, and 3 decks. I did the game with 1 deck and it seems to work fine. I thought that the other 2 would be a simple copy paste, then change some variables and also extend the array from 52 to 104, and also while gathering the rank and suits of the cards by using a modulus of 26 rather than 13. Same with the 3 decks but 156, and modulus of 39 instead. Here is the code I did for the game with 1 deck. I've tried doing what I said above but it doesn't work. Am I doing it wrong, or am I forgetting something? I can't seem to find out how to do this so I would greatly appreciate some help! Thanks!
Here's the code (and yes my teacher makes me comment):
public static void main(String[] args) {
// Phase 1: Choose amount of decks
System.out.println("Welcome to blackjack!");
System.out.println("How many decks would you like to use? (Maximum 3 decks)");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if (a == 1) {
int[] deck1;
deck1 = new int[52];
String[] suits = {" of Spades", " of Hearts", " of Clubs", " of Diamonds"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int[] cardValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
for (int i = 0; i < deck1.length; i++) {
deck1[i] = i;
}
for (int i = 0; i < deck1.length; i++) {
int index = (int) (Math.random() * deck1.length);
int temp = deck1[i];
deck1[i] = deck1[index];
deck1[index] = temp;
}
int[] playersHand;
playersHand = new int[12];
int[] dealersHand;
dealersHand = new int[12];
int cardIndex = 0;
int playersHandIndex = 0;
int dealersHandIndex = 0;
int playersCurrentCardValue;
String playersCurrentSuit;
String playersCurrentRank;
int dealersCurrentCardValue;
String dealersCurrentSuit;
String dealersCurrentRank;
int totalPlayerCardValue = 0;
int totalDealerCardValue = 0;
// Phase 2: Setup game
// Player draws 1st card from the deck
playersHand[playersHandIndex] = cardIndex;
cardIndex++;
playersHandIndex++;
// Get and display the value and suit and rank of the players current card
playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);
// Get the players total card value
totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;
// Player draws 2nd card from the deck
playersHandIndex++;
playersHand[playersHandIndex] = cardIndex;
cardIndex++;
// Get the values for the players current card
playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);
totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;
System.out.println("Players total card value is " + totalPlayerCardValue);
// Dealer draws card from the deck
dealersHand[dealersHandIndex] = cardIndex;
cardIndex++;
// Get and display the value and suit and rank of the dealers current card
dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);
// Get the dealers total card value
totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;
// Dealer draws his 2nd card
dealersHandIndex++;
dealersHand[dealersHandIndex] = cardIndex;
cardIndex++;
// Get the values for the dealers current card
dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);
dealersHandIndex++;
totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;
System.out.println("The dealer's total card value is " + totalDealerCardValue);
//Phase 3: Begin actual game
while (true) {
System.out.println("Would you like to hit [1] or stand [2]?");
int hitStand = input.nextInt();
if (hitStand == 1) {
playersHand[playersHandIndex] = cardIndex;
cardIndex++;
playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);
totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;
System.out.println("Players total card value is " + totalPlayerCardValue);
if (totalPlayerCardValue > 21) {
System.out.println("You bust!");
System.exit(0);
}
}
if (hitStand == 2) {
System.out.println("Your final card value is: " + totalPlayerCardValue);
while (true) {
dealersHand[dealersHandIndex] = cardIndex;
cardIndex++;
dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);
totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;
System.out.println("Dealer's total card value is " + totalDealerCardValue);
if (totalDealerCardValue >= 17) {
break;
}
}
break;
}
}
if (totalDealerCardValue > 21) {
System.out.println("The dealer bust! You win!");
} else if (totalPlayerCardValue > totalDealerCardValue) {
System.out.println("You win!");
} else if (totalPlayerCardValue == totalDealerCardValue) {
System.out.println("It's a push!");
} else {
System.out.println("You lose!");
}
}