Я делаю простую программу для блэкджека. Проблемы, с которыми я сталкиваюсь, состоят в том, что мои массивы всегда пустые при передаче в метод, и в методе cardSuits я получаю ошибку выхода за границы этого для l oop, которую я пытаюсь использовать, чтобы предотвратить дубликаты
for (int count2 = 0; count2 < cardSuits.length; count2++) {
if (cardValues[count] == cardValues[count2] && count != count2 && cardSuits[count] == cardSuits[count]) {
count--;
}
} //end of for (int count2 = 0; count2 < cardSuits.length; count2++)
Вот остальная часть программы (извините, что это довольно долго для сообщения на форуме)
//scanny boi
public static Scanner scanner = new Scanner(System.in);
//fills an array with card values
public static int [] drawCard(int [] cardValues, int [] otherUserCardValues) {
int duplicates = 0;
//fills array with cards
for (int count = 0; count < cardValues.length; count++) {
duplicates = 0;
cardValues [count] = (int)(Math.random() * 12) + 1;
//Checks for duplicate cards in one of the hands
for (int count2 = 0; count2 < cardValues.length; count2++) {
if (cardValues [count] == cardValues [count2]) {
duplicates++;
} //end of if (cardValues [count] == cardValues [count2])
} //end of for (int count2 = 0; count != 7; count++)
//Checks for duplicate cards in the other hand
for (int count2 = 0; count2 < cardValues.length; count2++) {
if (cardValues [count] == otherUserCardValues [count2]) {
duplicates++;
} //end of if (cardValues [count] == cardValues [count2])
} //end of for (int count2 = 0; count != 7; count++)
//makes it loop again if there are duplicates
if (duplicates > 4) {
count--;
} //end of if (duplicates > 4)
} //end of for (int count = 0; count != 7; count++)
return cardValues;
} //end of cardDraw
//lower card values
public static int [] lowerCardValue(int [] cardValues, int [] userCard) {
for (int count = 0; count < cardValues.length; count++) {
if (cardValues [count] > 10) {
cardValues [count] = 10;
} //end of for (int count = 0; count != 7; count++)
userCard [count] = cardValues [count];
} //end of for (int count = 0; count != 7; count++)
return cardValues;
} //end of cardValueLower
//higher card values
public static int [] higherCardValue(int [] cardValues, int [] userCard) {
boolean ace = false;
for (int count = 0; count < cardValues.length; count++) {
if (cardValues [count] > 10) {
cardValues [count] = 10;
} //end of for (int count = 0; count != 7; count++)
// turns the first ace into an 11
if (cardValues [count] == 1 && ace == false) {
cardValues [count] = 11;
ace = true;
} //if (cardValues [count] == 1)
userCard [count] = cardValues [count];
} //end of for (int count = 0; count != 7; count++)
return cardValues;
} //end of higherCardValue
//fills an array with the names of the cards
public static String [] cardNames(String [] cardNames, int [] cardValues) {
int cardValuesInt = 0; // holds cardValues because switches don't like objects
for (int count = 0; count < cardValues.length; count++) {
cardValuesInt = cardValues [count];
switch (cardValuesInt) {
case 1: cardNames [count] = "Ace of ";
break;
case 2: cardNames [count] = "Two of ";
break;
case 3: cardNames [count] = "Three of ";
break;
case 4: cardNames [count] = "Four of ";
break;
case 5: cardNames [count] = "Five of ";
break;
case 6: cardNames [count] = "Six of ";
break;
case 7: cardNames [count] = "Seven of ";
break;
case 8: cardNames [count] = "Eight of ";
break;
case 9: cardNames [count] = "Nine of ";
break;
case 10: cardNames [count] = "Ten of ";
break;
case 11: cardNames [count] = "Jack of ";
break;
case 12: cardNames [count] = "Queen of ";
break;
case 13: cardNames [count] = "King of ";
break;
} //end of switch (cardValuesInt)
} //end of for (int count = 0; count == 7; count++)
return cardNames;
} //end of cardNames
//fills an array with the suits of the cards
public static String [] cardSuits(String [] cardSuits, String [] otherCardSuits, int [] cardValues, int [] otherCardValues) {
int rand = 0; // random number used to choose suits
//gives suit names
for (int count = 0; count < cardSuits.length; count++) {
rand = (int)(Math.random() * 3) + 1;
switch (rand) {
case 1: cardSuits[count] = "Hearts"; break;
case 2: cardSuits[count] = "Spades"; break;
case 3: cardSuits[count] = "Diamonds"; break;
case 4: cardSuits[count] = "Clubs"; break;
} //end of switch (rand)
//duplicate checking current hand
for (int count2 = 0; count2 < cardSuits.length; count2++) {
if (cardValues[count] == cardValues[count2] && count != count2 && cardSuits[count] == cardSuits[count]) {
count--;
}
} //end of for (int count2 = 0; count2 < cardSuits.length; count2++)
//duplicate checking other hand
for (int count2 = 0; count2 < cardSuits.length; count2++) {
if (cardValues[count] == cardValues[count2] && count != count2 && cardSuits[count] == cardSuits[count]) {
count--;
}
} //end of for (int count2 = 0; count2 < cardSuits.length; count2++)
} //end of for (int count = 1; count == 6; count++)
return cardSuits;
} //end of cardSuits
public static void main(String[] args) {
String input = null;
boolean endGame = false;
//user variables
double money = 0;
int userCardTotal = 0;
int userCardCount = 0;
boolean userAce = false;
int [] lowerUserCards = {0, 0, 0, 0, 0, 0};
int [] higherUserCards = {0, 0, 0, 0, 0, 0};
int [] userCardValues = {0, 0, 0, 0, 0, 0};
String [] userCardNames = {"a", "a", "a", "a", "a", "a"};
String [] userCardSuits = {"a", "a", "a", "a", "a", "a"};
//dealer variables
int dealerCardTotal = 0;
int dealerCardCount = 0;
boolean dealerAce = false;
int [] lowerDealerCards = {0, 0, 0, 0, 0, 0};
int [] higherDealerCards = {0, 0, 0, 0, 0, 0};
int [] dealerCardValues = {0, 0, 0, 0, 0, 0};
String [] dealerCardNames = {"a", "a", "a", "a", "a", "a"};
String [] dealerCardSuits = {"a", "a", "a", "a", "a", "a"};
//title
System.out.println("----------------------=================={#}==================----------------------");
System.out.println("" +
",-----. ,--. ,--. ,--. ,--. \r\n" +
"| |) /_ | | ,--,--. ,---.| |,-. `--' ,--,--. ,---.| |,-. \r\n" +
"| .-. \\| |' ,-. || .--'| / ,--.' ,-. || .--'| / \r\n" +
"| '--' /| |\\ '-' |\\ `--.| \\ \\ | |\\ '-' |\\ `--.| \\ \\ \r\n" +
"`------' `--' `--`--' `---'`--'`--'.-' / `--`--' `---'`--'`--' \r\n" +
" '---' ");
System.out.println("----------------------=================={#}==================----------------------\r\n");
do {
System.out.print("Would you like to see the rules? (Y/N) : ");
input = scanner.next();
System.out.println();
if (input.charAt(0) == 'Y' || input.charAt(0) == 'y') {
printRules();
break;
} else if (input.charAt(0) == 'N' || input.charAt(0) == 'n') {
break;
} else {
System.out.println("Please enter a valid option\r\n");
System.out.println("----------------------==================[+]==================----------------------\r\n");
continue;
} //end of else
} while (1 == 1);
System.out.println("----------------------==================[+]==================----------------------");
do {
//calculations and variable resets
userCardTotal = 0;
dealerCardTotal = 0;
userCardCount = 0;
dealerCardCount = 0;
userAce = false;
dealerAce = false;
userCardValues = drawCard(userCardValues, dealerCardValues);
dealerCardValues = drawCard(dealerCardValues, userCardValues);
lowerUserCards = lowerCardValue(lowerUserCards, userCardValues);
lowerDealerCards = lowerCardValue(lowerDealerCards, dealerCardValues);
higherUserCards = higherCardValue(higherUserCards, userCardValues);
higherDealerCards = higherCardValue(higherDealerCards, dealerCardValues);
userCardNames = cardNames(userCardNames, userCardValues);
dealerCardNames = cardNames(dealerCardNames, dealerCardValues);
userCardSuits = cardSuits(userCardSuits, dealerCardSuits, userCardValues, dealerCardValues);
userCardSuits = cardSuits(dealerCardSuits, userCardSuits, dealerCardValues, userCardValues);
//checks for aces
for (int count : higherDealerCards) {
if (lowerDealerCards[count] == 1)
dealerAce = true;
} //end of for (int count : higherDealerCards)
for (int count = 0; count < 2; count++)
userCardCount++;
userCardTotal += lowerUserCards[userCardCount];
//beginning of hand
do {
System.out.println("The dealer has a card face down and a " + (String)dealerCardNames[1] + (String)dealerCardSuits[1]);
System.out.println("\r\n\r\n\r\n\r\n");
System.out.println("You have a " + (String)userCardNames[0] + (String)userCardSuits [0] + " and a " + (String)userCardNames[1] + (String)userCardSuits [1] );
} while (1 == 1);
} while (endGame == true);
} //end of main