How do you add the winner the cards that were compared. I have concurrentmodification error. I am trying to add the array pile to the winners arraylist but I am getting an error.
import java.util.ArrayList;
public class Deck {
ArrayList<Card> deck = new ArrayList<Card>();
ArrayList<Card> player1 = new ArrayList<Card>();
ArrayList<Card> player2 = new ArrayList<Card>();
ArrayList<Card> pile = new ArrayList<Card>();
boolean winner;
public Deck(){
}
public void createDeck(){
for(int i =0; i<4; i++){
for(int j =1; j<15;j++){
if(i==0) deck.add(new Card("Spades", j));
if(i==1) deck.add(new Card("Hearts", j));
if(i==2) deck.add(new Card("Clubs", j));
if(i==3) deck.add(new Card("Diamonds",j));
}
}
}
public void splitDeck(){
for(int i = 0; i< 52;i++){
int r = (int) (Math.random()*(51-i));
if(i%2==0){
player1.add(deck.get(r));
}
else{
player2.add(deck.get(r));
}
deck.remove(r);
}
}
public int [] war(){
int[]score = {0,0};
for(Card c : player1){
int x = player1.indexOf(c);
if(c.getValue() > player2.get(x).getValue()){
winner = true;
score[0]++;
pile.add(player1.get(x));
pile.add(player2.get(x));
distributeCards();
}
else if(c.getValue() <player2.get(x).getValue()) score[1]++;
distributeCards();
}
return score;
}
public void distributeCards(){
if(winner==true){
for(int i = 0; i<pile.size(); i++){
player1.add(pile.get(i));
}
}
else{
for(int i = 0; i<pile.size(); i++){
player2.add(pile.get(i));
}
}
}
public String warGame(){
String print= "";
for(Card c : player1){
int x = player1.indexOf(c);
if(c.getValue() > player2.get(x).getValue()){
print+= "\n"+"Player1 won";
}
else if(c.getValue() <player2.get(x).getValue())
print+="\n"+"Player2 won";
else if(c.getValue() ==player2.get(x).getValue())
print+= "\n"+"Its a tie";
}
return print;
}
}
Error message is...
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Deck.war(Deck.java:41)
at War.main(War.java:7)
From inside war() where you iterate the ArrayList player1 you're calling distributeCards() which modifies player1. You can't change a list while iterating it - that's why you're getting ConcurrentModificationException (read the docs for further explanation).
Solution:
Copy player1 to a tmp list - and modify that tmp list and only after you're done traversing player1 - assign tmp to player1. (Same logic applies for player2)
Related
I'm trying to make a battleship game, and this is the part where the players set their boards. When the second player to set the board makes theirs, the first player's board becomes the same as the second player's (when playing the game guessing the locations of that board are misses though, but that's another problem that will hopefully be fixed once this is). For example if I set the first player's ships as being at A1 A2 A3, B1 B2 B3, and C1 C2 C3, then set the second player's ships as D1 D2 D3, E1 E2 E3, and F1 F2 F3, when both lists of ships are printed out I get D1 D2 D3, E1 E2 E3, and F1 F2 F3 for both ships. I've found other questions on here with the same problem, but they always had the problem because they didn't make new lists every time, which I do (at least I'm pretty sure I do), so I can't find where my problem is. Here is the entire main game class, the GetShipLocations, SetShipLocations, and PlayersMakeTheirBoards, functions are what are giving me problems.
import java.util.*;
import java.lang.*;
public class MainGame {
InputReader read = new InputReader();
Grid p1Board = new Grid();
Grid p2Board = new Grid();
Grid p1ShipsBoard = new Grid();
Grid p2ShipsBoard = new Grid();
ArrayList<Ship> p1Ships = new ArrayList<Ship>();
ArrayList<Ship> p2Ships = new ArrayList<Ship>();
String activePlayer = "P1";
public void SetUp() {
p1Board.PrepPrintGrid();
p2Board.PrepPrintGrid();
Ship ship1 = new Ship();
Ship ship2 = new Ship();
Ship ship3 = new Ship();
p1Ships.add(ship1);
p1Ships.add(ship2);
p1Ships.add(ship3);
p2Ships.add(ship1);
p2Ships.add(ship2);
p2Ships.add(ship3);
}
public void GameIntro() {
System.out.println("Welcome to battleship!");
String rulesOption = read.getUserInput("Do you need to see the rules?");
if(rulesOption.equals("Yes") || rulesOption.equals("yes"))
{
System.out.println("Put rules here");
}
System.out.println("Randomly determining which player goes first");
int random = (int) (Math.random() * 2);
if(random == 1)
{
activePlayer = "P2";
}
System.out.println(activePlayer + " starts!");
}
public ArrayList<ArrayList<String>> GetShipLocations(Grid board) {
ArrayList<ArrayList<String>> ships = new ArrayList<ArrayList<String>>();
ArrayList<String> ship1 = new ArrayList<String>();
ArrayList<String> ship2 = new ArrayList<String>();
ArrayList<String> ship3 = new ArrayList<String>();
ships.add(ship1);
ships.add(ship2);
ships.add(ship3);
String[] numbers = {"first", "second", "third"};
board.PrepPrintGrid();
board.PrintGrid();
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
String coordinate = read.getUserInput("Enter the " + numbers[j] + " coordinate of your " + numbers[i] + " ship (3 long):");
ships.get(i).add(coordinate);
board.SetGridDisplay(coordinate, "hit");
board.PrintGrid();
}
}
return ships;
}
public void SetShipLocations(ArrayList<Ship> ship, Grid activeBoard) {
ArrayList<ArrayList<String>> shipLocations = GetShipLocations(activeBoard);
for(int i = 0; i < 3; i++) {
ship.get(i).SetCells(shipLocations.get(i));
}
}
public void PlayersMakeTheirBoards() {
if(activePlayer.equals("P1"))
{
System.out.println("Hand the computer to player one.");
System.out.println("Player one, time to set your board.");
SetShipLocations(p1Ships, p1ShipsBoard);//the effects of this seem to maybe be overriden when the second board is set
for(int i = 0; i < /*100*/3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(p1Ships.get(i).cells.get(j));
}
}
System.out.println("Hand the computer to player two.");
System.out.println("Player two, time to set your board.");
SetShipLocations(p2Ships, p2ShipsBoard);
for(int i = 0; i < /*100*/3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(p2Ships.get(i).cells.get(j));
}
}
for(int i = 0; i < /*100*/3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(p1Ships.get(i).cells.get(j));
}
}
}
else
{
System.out.println("Hand the computer to player two.");
System.out.println("Player two, time to set your board.");
SetShipLocations(p2Ships, p2ShipsBoard);
/*for(int i = 0; i < 100; i++)
{
System.out.println("");
}*/
for(int i = 0; i < /*100*/3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(p2Ships.get(i).cells.get(j));
}
}
System.out.println("Hand the computer to player one.");
System.out.println("Player one, time to set your board.");
SetShipLocations(p1Ships, p1ShipsBoard);
for(int i = 0; i < /*100*/3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(p1Ships.get(i).cells.get(j));
}
}
for(int i = 0; i < /*100*/3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(p2Ships.get(i).cells.get(j));
}
}
}
}
public void PlayTheGame() {
String guess = new String();
String result = new String();
for(int i = 0; i < 100; i++)
{
System.out.println("");
}
while(!p1Ships.isEmpty() && !p2Ships.isEmpty())
{
if(activePlayer.equals("P1"))
{
//print the grid
p1Board.PrintGrid();
//ask user for their guess
guess = read.getUserInput("Player one, enter your guess:");
for(Ship boat:p2Ships)
{
result = boat.CheckYourself(guess);
if(result.equals("hit"))
{
System.out.println(result + "!");
break;
}
if(result.equals("kill"))
{
System.out.println(result + "!");
p2Ships.remove(boat);
break;
}
}
if(result.equals("miss"))
{
System.out.println(result);
}
p1Board.SetGridDisplay(guess, result);
activePlayer = "P2";
}
else
{
p2Board.PrintGrid();
//ask user for their guess
guess = read.getUserInput("Player two, enter your guess:");
for(Ship boat:p1Ships)
{
result = boat.CheckYourself(guess);
if(result.equals("hit"))
{
System.out.println(result + "!");
break;
}
if(result.equals("kill"))
{
System.out.println(result + "!");
p1Ships.remove(boat);
break;
}
}
if(result.equals("miss"))
{
System.out.println(result);
}
p2Board.SetGridDisplay(guess, result);
activePlayer = "P1";
}
}
}
public void EndTheGame() {
String winner = new String();
if(p1Ships.isEmpty())
{
winner = "Player two";
p2Board.PrintGrid();
}
else
{
winner = "Player one";
p1Board.PrintGrid();
}
System.out.println("The game is over!");
System.out.println(winner + " wins! Congratulations!");
}
public static void main(String[] args) {
MainGame game = new MainGame();
game.SetUp();
game.GameIntro();
game.PlayersMakeTheirBoards();
game.PlayTheGame();
game.EndTheGame();
}
}
and here is the Ship class
import java.util.*;
public class Ship {
ArrayList<String> cells = new ArrayList<String>();
//String name = new String();
public void SetCells(ArrayList<String> locations) {
cells = locations;
}
/*public void SetName(String word) {
name = word;
}*/
public String CheckYourself(String guess) {
if(cells.contains(guess))
{
cells.remove(guess);
if(cells.isEmpty())
{
return "kill";
}
else
{
return "hit";
}
}
return "miss";
}
}
The grid and reader classes are working perfectly so I didn't include them.
(This is all based off the dotcom battleship game in headfirst java)
In your setup function:
public void SetUp() {
p1Board.PrepPrintGrid();
p2Board.PrepPrintGrid();
Ship ship1 = new Ship();
Ship ship2 = new Ship();
Ship ship3 = new Ship();
p1Ships.add(ship1);
p1Ships.add(ship2);
p1Ships.add(ship3);
p2Ships.add(ship1);
p2Ships.add(ship2);
p2Ships.add(ship3);
}
You are adding the same instances of ship1-3 to both p1Ships and p2Ships. So when you change the ships of player 2, the p1Ships ArrayList is still pointing to the same ships as p2Ships, and thus will always be the same.
I am hoping to get some help even though I know this is probably a very simple bug that I cannot seem to find an answer to.
What I am trying to accomplish is to create a deck of cards, and I keep running into an out of bounds error.
Here is my code:
Card Class:
public class Card {
private String suit;
private int face;
public Card(int face, String suit){
face = this.face;
suit = this.suit;
}
public int getFace(){
return face;
}
public String getFaceAsString(int face){
int faceName = face;
String faceString = "";
if(faceName == 11){
faceString = "J";
} else if(faceName == 12){
faceString = "Q";
} else if(faceName == 13){
faceString = "K";
} else if(faceName == 14){
faceString = "A";
} else {
faceString = Integer.toString(faceName);
}
return faceString;
}
public String getSuit(){
return suit;
}
public void setSuit(String suit){
this.suit = suit;
}
}
This is my main class:
import java.util.Random;
import java.util.Scanner;
public class Hero_Game {
public static void main(String[] args) {
String[] suits = {"Spades","Clubs","Hearts","Diamonds"};
int[] faces = {2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int index = 0;
Card[] deck = new Card[52];
for(int i = 0; i<suits.length;i++){
for(int j = 0; j<faces.length;j++){
deck[index] = new Card(faces[i],suits[j]);
index++;
}
}
You switched the indexes for the faces and suits arrays in your for loops. It should be:
for(int i = 0; i<suits.length;i++){
for(int j = 0; j<faces.length;j++){
deck[index] = new Card(faces[j],suits[i]);
index++;
}
}
You also have an extra number in your "faces" array - you're trying to create 14 * 4 = 56 cards instead of 13 * 4 = 52.
Why do you have 14 faces instead of 13?
In any case, to more safely allocate the correct number of items in your deck array, use int numCardsInDeck = suits.length * faces.length; to calculate the total items in the deck.
Try this instead:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static class Card {
final int face;
final String suit;
public Card(int face, String suit) {
this.face = face;
this.suit = suit;
}
#Override
public String toString() {
return face + " of " + suit;
}
}
public static void main(String[] args) {
String[] suits = {"Spades","Clubs","Hearts","Diamonds"};
int[] faces = {2,3,4,5,6,7,8,9,10,11,12,13,14};
int index = 0;
int numCardsInDeck = suits.length * faces.length;
Card[] deck = new Card[numCardsInDeck];
for(int i = 0; i < suits.length; i++) {
for(int j = 0; j < faces.length; j++) {
deck[index] = new Card(faces[j],suits[i]);
index++;
}
}
System.out.println(Arrays.toString(deck));
}
}
int[] faces = {2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Get rid of 15 and do the loop as suggested by #Geshode
I keep getting an IndexOutOfBoundsException when I run my code. I am using an ArrayList so I'm not sure why this is happening.
My ArrayList is
ArrayList<Card> cards = new ArrayList<Card>();
This is where the error occurs
public static void printCard(){
System.out.printf("%-20s %-20s\n", player1.name, player2.name);
for(int i = 0; i < 24; i++){
System.out.printf("%-20s %-20s\n", player1.getCard(i), player2.getCard(i));
} System.out.println();
}
Player class
public class Player {
public Deck mainDeck;
public Deck sideDeck;
public String name;
public int duelsWon;
public int totalCompares;
public Player(String name){
this.name=name;
mainDeck = new Deck();
sideDeck = new Deck();
duelsWon = 0;
totalCompares = 0;
}
public void addCard(Card newCard){
sideDeck.addCard(newCard);
}
public Card drawCard() throws OutOfCardsException{
if(mainDeck.numCards() == 0) {
addSideDeck();
}
if(mainDeck.numCards() == 0){
throw new OutOfCardsException();
}
Card c = mainDeck.drawCard();
return c;
}
public Card getCard(int i){
return mainDeck.cards.get(i);
}
/*public Card getCard(int i){
if(i < mainDeck.cards.size()) {
return mainDeck.cards.get(i);
}else{
}
return null;
}*/
public void addSideDeck(){
sideDeck.shuffle();
System.out.println("sideDeck: " + sideDeck.numCards());
for(int i = 0; i < sideDeck.numCards(); i++){
Card c = sideDeck.drawCard();
mainDeck.addCard(c);
}
}
}
Deck class
import java.util.ArrayList;
import java.util.Random;
public class Deck {
ArrayList<Card> cards = new ArrayList<Card>();
public Deck() {
}
public void addCard(Card c){
cards.add(c);
}
public Card drawCard(){
Card c = cards.remove(cards.size() - 1);
return c;
}
public Card getCard(){
return cards.get(cards.size() - 1);
}
public int numCards(){
return cards.size();
}
public void shuffle()
{
int index;
Card temp;
Random random = new Random();
for (int i = cards.size() - 1; i > 0; i--)
{
index = random.nextInt(i + 1);
temp = cards.get(index);
cards.set(index, cards.get(i));
cards.set(i, temp);
}
}
}
The answer is that you don't have 24 cards in your deck for one of your players. Nothing else will cause this with this code.
What is 24 ?
i think you need to check card size.
public static void printCard(){
System.out.printf("%-20s %-20s\n", player1.name, player2.name);
for(int i = 0; i < cards.size(); i++){
System.out.printf("%-20s %-20s\n", player1.getCard(i), player2.getCard(i));
} System.out.println();
}
I hope it help.
IndexOutOfBoundsException happens when you are trying to access an item which index is beyond the items stored in the ArrayList. Ex, if an ArrayList contains 5 items (index from 0 to 4) then if you try to access the ArrayList with index 5 then IndexOutOfBoundsException would be thrown.
Best approach to iterate though ArrayList/List are like:
for (Card c: cards) {
}
Or looping to the size
for (int i=0; i<cards.size(); i++) {
}
I think there is some issue with Random number you are getting
index = random.nextInt(i + 1);
temp = cards.get(index);
Just log the index and check if it is between 0 to cards.size();
In my game's code, I am trying to add a card to hand. As soon as I do, my array is out of bounds. Everything looks right, but maybe I'm missing something.
FYI, one and two are Player instances. Relevant code from Main class (sorry about the formatting. i suck at transferring it to Stack Overflow):
import java.util.*;
public class Program {
public static void main(String args[]) {
String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "jack", "queen", "king", "ace"};
String[] suit = {"hearts", "diamonds", "spades", "clubs"};
Scanner scan = new Scanner(System.in);
String something = "yes", something2 = "yes"; //Use with while loop
String winner = "yes"; //Use for while loop
String temp; //Use with setting names
Card[] deck = new Card[52]; //Deck array
int playercount = 0;
Player one = new Player("temp");
Player two = new Player("temp");
Player three = new Player("temp");
Player four = new Player("temp");
while (something2.equals("yes") || playercount < 2) { //Add players to game
System.out.println("Would a(nother) player like to join?");
something2 = scan.nextLine();
System.out.println();
if (something2.equals("yes")) {
if (playercount <= 4) {
if (playercount == 0) {
System.out.println("What is your name: ");
Player one1 = new Player(scan.nextLine());
one = one1;
playercount++;
System.out.println();
}
else if (playercount == 1) {
System.out.println("What is your name: ");
Player two2 = new Player(scan.nextLine());
two = two2;
playercount++;
System.out.println();
}
else if (playercount == 2) {
System.out.println("What is your name: ");
Player three3 = new Player(scan.nextLine());
three = three3;
playercount++;
System.out.println();
}
else if (playercount == 3) {
System.out.println("What is your name: ");
Player four4 = new Player(scan.nextLine());
four = four4;
playercount++;
System.out.println();
}
else {System.out.println("Only four players are allowed.");
something2 = "no";}
}
}
else if (playercount < 2) {
System.out.println("You need at least two players...");
System.out.println();
}
else something2 = "no";
}
//Start game
while (something.equals("yes")) {
//Prepare game
Card.makeDeck(deck, rank, suit);
deck = Card.getDeck();
Card.shuffle(deck);
deck = Card.getDeck();
//Deal cards
if (playercount == 2) {
for (int i = 1; i < 8; i++) {
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else if (playercount == 3) {
for (int i = 1; i < 8; i++) {
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else {
for (int i = 1; i < 8; i++) {
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
four.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
}
}
}
Card class:
import java.util.*;
public class Card {
private String suit;
private String rank;
private static int temp = 0, temp2 = 0; //Use for reseting rank and suit
private static Card temp3; //Use for draw method
private static int temp4; //Use for shuffle method
private static Card[] deck = new Card[52];
//Constructors
public Card() {
this.rank = "two";
this.suit = "hearts";
}
public Card(String r, String s) {
this.rank = r;
this.suit = s;
}
//Mutators
//Make deck
public static void makeDeck(Card[] c, String[] r, String[] s) {
for (int i = 0; i < c.length; i++) {
c[i] = new Card(r[temp], s[temp2]);
temp++; temp2++;
//Reset rank and suit
if (temp > 12)
temp = 0;
if (temp2 > 3)
temp2 = 0;
}
deck = c;
}
//Accessors
//Return deck
public static Card[] getDeck() {
return deck;
}
//Shuffle
public static Card[] shuffle(Card[] c) {
for (int i = 0; i < c.length; i++) {
int rand = (int)(Math.random()*(i + 1));
//Don't let anything be in a slot that doesn't exist
while (rand > c.length) {
temp4 = (int)Math.random();
rand -= temp4;
}
if (rand < 0)
rand += temp4;
Card temp = c[i];
c[i] = c[rand];
c[rand] = temp;
}
deck = c;
return deck;
}
//Draw
public static Card draw(Card[] c) {
if (c != null) {
for (int i = 0; i < c.length; i++) {
if (c[i] != null) {
try {
return c[i];
} finally {
c[i] = null;} //Remove i from c
}
}
}
return null;
}
}
Player class:
import java.util.*;
public class Player {
private String name;
private Card[] hand = new Card[52];
private int handsize = 0;
//Constructor
public Player(String n) {
name = n;
}
//Mutators
public void addCard(Card c) {
hand[handsize] = c;
handsize++;
}
//Accessors
public String getName() {
return name;
}
public Card[] getHand() {
return hand;
}
}
Your draw method is broken.
// get the first non-null Card from the cards "c".
public static Card draw(Card[] c) {
if (c != null) {
for (int i = 0; i < c.length; i++) {
if (c[i] != null) {
try {
return c[i];
} finally {
// now remove element i from the `c` array.
c[i] = null;
}
}
}
}
return null;
}
The problem is with your loop
while (something.equals("yes"))
There's nothing that sets something to any other value, so this loop just goes around endlessly, until all the players have more than 52 cards. Once someone has more than 52 cards, adding a new card causes the exception.
I think you need to remove this while. The code inside it should only be run once.
I think the order of your code is incorrect (hard to tell with this code)
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
}
maybe should be
for (int i = 1; i < 8; i++)
{
deck = Card.getDeck();
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
}
Update
Also
public void addCard(Card c) {
hand[handsize] = c;
handsize++;
}
handsize is never incremented - it is always 0
I am trying to draw a Hand of Cards (in the Hand class) but have it shuffled. My problem is if I do shuffleDeck() in the initialDeal() method (where I need to draw a Hand of Cards but shuffled) it gives me an ArrayIndexOutOfBounds exception.
And I draw two narfs OF Clubs... Note, the narf is basically 0 and just a placeholder. It will not be used.
class Card {
int suit, rank;
public Card () {
this.suit = 0; this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
}
public int getSuit() {
return suit;
}
public int getRank() {
return rank;
}
public void printCard () {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[rank] + " of " + suits[suit]);
} //end printCard
} //end class
Card class(this is basically doing basic stuff) ^
import java.util.Random;
class Hand {
static Card[] deck = new Card[52];
Card[] hand = new Card[10];
static int index;
public static void createDeck () {
int m = 0;
for (int j = 0; j < 4; j++) {
for (int k = 1; k < 14; k++) {
deck[m] = new Card(j,k);
m++;
}
}
index = 0;
} // end createDeck
public static Card deal () {
Hand.index++;
return deck[index-1];
} // end deal
public static int compareCard (Card c1, Card c2) {
if (c1.getSuit() > c2.getSuit()) return 1;
if (c1.getSuit() < c2.getSuit()) return -1;
if (c1.getRank() > c2.getRank()) return 1;
if (c1.getRank() < c2.getRank()) return -1;
return 0;
} // end compareCard
public void printDeck (int size) {
int j = 0;
while (j < size) {
deck[j].printCard();
j++;
}
}// end printDeck
public static void shuffleDeck () {
Card tempCard;
Random rd = new Random();
for (index = 0; index < deck.length; index++) {
int r = rd.nextInt(deck.length);
tempCard = deck[index];
deck[index] = deck[r];
deck[r] = tempCard;
}
} // end shuffleDeck
public void initialDeal() {
hand[0] = null;
hand[1] = null;
hand[0] = deal();
hand[1] = deal();
}
public void printHand() {
initialDeal();
index = 0;
for(Card outputCard = new Card(); hand[index] != null; index++) {
outputCard.printCard();
}
}
}
Hand class^ (this is where I need to draw two cards from a shuffled deck)
class Dealer {
Hand dealer = new Hand();
public Dealer () {
dealer.createDeck();
dealer.shuffleDeck();
System.out.println("Dealer's Hand");
System.out.println("");
initDeal();
}
public void initDeal() {
dealer.initialDeal();
dealer.printHand();
}
} //end class
Dealer class.^ Calling the methods of Hand
class Driver {
public static void main (String[] args) {
Dealer dealer = new Dealer();
} //end main method
} //end class
^ Running everything basically
You're modifying the index in shuffleDeck, so after the shuffleDeck finishes, the index is shuffleDeck.length.
In shuffleDeck, do for (int index = 0; index < deck.length; index++)
(note the int index -> declare local variable, and not use the static one.)
Or you can (in initialDeal) set the index to 0.