Game of war!! text version
keep getting out of bounds error and it doesn't really shuffle the cards very well. please give advise new to programming.....Game of war!! text version against computer. I also need to split deck and play till playerA cards or playerB cards are gone.
keep getting out of bounds error and it doesn't really shuffle the cards very well. please give advise new to programming.....Game of war!! text version against computer. I also need to split deck and play till playerA cards or playerB cards are gone.
import java.util.ArrayList;
import java.util.Random;
//import java.util.Vector;
public class cardWars
{
public static void main(String[] args)
{
int cardIndex = 0;
int playerAScore = 0;//computer
int playerBScore = 0;//your
int playerACard;//computer
int playerBCard;//your
int[] myCards = new int[52];
//ArrayList<Integer> myCards = new ArrayList<Integer>();
//ArrayList<Integer> playerADeck = new ArrayList<Integer>();
//ArrayList<Integer> playerBDeck = new ArrayList<Integer>();
//ArrayList<Integer> splitDeck = new ArrayList<Integer>();
myCards = nIntegers(52);
//Collections.shuffle(myCards);
//split the deck 26 cards each playerADeck, playerBDeck....
do{
//display cards
System.out.print("Player A card: ");
playerACard = cardDisplay(myCards[cardIndex]);
System.out.print("Player B card: ");
playerBCard = cardDisplay(myCards[cardIndex] + 1);
if (playerBCard > playerACard)
{
System.out.println("Player B wins!");
playerBScore = playerBScore + 2;
// System.out.println("Player B deck: " + playerBDeck);
}
else if (playerACard > playerBCard)
{
System.out.println("Player A wins!");
playerAScore = playerAScore + 2;
//System.out.println("player a deck: " + playerADeck);
}
else
{
System.out.println("It's a tie.");
playerBScore = playerBScore + 1;
playerAScore = playerAScore + 1;
}
System.out.println("Player A Score: " + playerAScore);
System.out.println("Player B Score: " + playerBScore);
cardIndex = cardIndex + 2;
System.out.println("There are " + (52 - cardIndex) + " cards remaining. ");
}
while((52 - cardIndex)>0);
System.out.println("Game Over!");
}//end main
public static int[] nIntegers(int n) //Shuffles
{
int nArray[] = new int[n];
int temp, s;
Random myRandom = new Random();
//Initialize array
for(int i = 0; i < n; i++)
{
nArray[i] = i;
}
for(int i = n; i >= 1; i--)
{
s = myRandom.nextInt(i);
temp = nArray[s];
nArray[s] = nArray[i - 1];
nArray [i - 1] = temp;
}
return(nArray);
}
//Card Display
public static int cardDisplay(int n)
{
//given card number n(0-51) prints & returns value
String suit;
String[] value = new String[13];//declare & create
value[0] = "Two";
value[1] = "Three";
value[2] = "Four";
value[3] = "Five";
value[4] = "Six";
value[5] = "Seven";
value[6] = "Eight";
value[7] = "Nine";
value[8] = "Ten";
value[9] = "Jack";
value[10] = "Queen";
value[11] = "King";
value[12] = "Ace";
//Determine suit adjust numeric value n
if(n >= 0 && n <= 12)
{
suit = "Hearts";
}
else if (n >= 13 && n <= 25)
{
suit = "Diamonds";
n = n - 13;
}
else if (n >= 26 && n <= 38)
{
suit = "Clubs";
n = n - 26;
}
else
{
suit = "Spades";
n = n - 39;
}
// print
System.out.println(value[n] + " of " + suit);//prints value & suit
// return numeric value
return(n);
}
}