Saturday 15 March 2014

Adding "cards" to an ArrayList in Java -



Adding "cards" to an ArrayList<Integer> in Java -

i'm trying create game of gofish. i'm in process of creating deck. know how* set number* "cards" in arraylist. since there 4 different types of cards aren't integers (king, queen, jack, ace), causes speedbumps.

what best way add together 4 cards arraylist?

arraylist<integer> cards = null;

also, how go making sure there 4 of each card in deck?

@user3189142 how search , take guessed card computer's hand , have added player's hand?

public static void playonegame(arraylist<integer> cards, scanner input) { arraylist<integer> computer = new arraylist<integer>(); arraylist<integer> person = new arraylist<integer>(); arraylist<integer> computerpile = new arraylist<integer> (); arraylist<integer> personpile = new arraylist<integer>(); // todo: deal cards (int p = 0; p < 7; p++) { person.add(cards.get(0)); cards.remove(0); } (int c = 0; c < 7; c++) { computer.add(cards.get(0)); cards.remove(0); } // todo: show person starting hand system.out.println("your cards are: " + person); // next line testing, not apart of final programme system.out.println("computer's cards are: " + computer); //todo: play 1 turn person doing choosing //todo: allow player draw deck while (computerpile.size() + personpile.size() < 52 || !cards.isempty()) { if (!person.isempty()) { //todo: play 1 turn person doing choosing system.out.println("what card want?"); int card = input.nextint(); playpersonturn(card, person, computer, personpile, computerpile, cards); } else { //todo: allow player draw deck person.add(cards.get(0)); cards.remove(0); } showgamestate(person, computerpile, personpile); //todo: play 1 turn computer doing choosing if (!computer.isempty()) { int card = computer.get((int)(math.random()*computer.size())); system.out.println("do have " + card + "'s ?"); playcomputerturn(card, person, computer, personpile, computerpile, cards); } else if (!cards.isempty()) { //todo: allow computer draw deck computer.add(cards.get(0)); cards.remove(0); } //todo: create piles hands if applicable checkpiles(person, personpile, computer, computerpiles); showgamestate(person, computerpile, personpile); }

pile check methods:

public static void checkpiles(arraylist<integer> person, arraylist<integer> personpile, arraylist<integer> computer, arraylist<integer> computerpile) { (int = 1; < 14; i++) { if (collections.frequency(person,i) == 4) { (int j = 0; j < 4; j++) { personpile.add(person.remove(person.indexof(i))); } else if (collections.frequency(computer,i) == 4) { (int k = 0; k < 4; k++) { computerpile.add(computer.remove(computer.indexof(i))); } } } }

codes works now, except when pile formed, it's showing 4 same-number cards, shown below number 1. there way can have show 1 1?

what card want? 1 took computer's 1. here cards: 2 1 13 12 12 1 6 1 6 10 9 1 don't have piles yet. computer doesn't have piles yet. have 7's? computer went fishing. computer drew 1 card deck. here cards: 2 13 12 12 6 6 10 9 here pile: 1 1 1 1 computer doesn't have piles yet. card want?

if doing this, i'd add together ace 1, jack 11, queen 12, , king 13. making sure there 4 of each card in deck pretty easy, utilize nested loops:

for (int = 0; < 4; i++) { (int j = 1; j < 14; j++) cards.add(j); }

that add together 13 cards deck 4 times. i'm assuming need output cards too, why you're having trouble. when outputting, can utilize simple if's check ace, king, queen, , jack cards, , output names instead:

outputcard(int cardnumber) { if (cardnumber == 1) system.out.println("ace"); else if (cardnumber == 11) system.out.println("jack"); else if (cardnumber == 12) system.out.println("queen"); else if (cardnumber == 13) system.out.println("king"); else system.out.println(cardnumber); }

to add together top 7 cards array list, we'll phone call hand, seek following:

for (int k = 0; k < 7; k++) hand.add(cards.remove(0));

then hand should have 7 cards @ top of cards list

to inquire computer card, , set in player's hand if computer has it, seek following:

//get card selection user int cardchoice = input.nextint(); if (computer.contains(cardchoice)) player.add(computer.remove(computer.indexof(cardchoice)));

what first check the computer hand has @ to the lowest degree 1 of cards inquire for, , if does, finds index of card (computer.indexof), removes (computer.remove), returns integer stored @ position, , adds players hand player.add. if need on more 1 line, allow me know , i'll alter slightly.

alright, check if hand has 4 of same card, we'll utilize couple loops, so:

//for players hand (int = 1; < 14; i++) { if (collections.frequency(player,i) == 4) { (int j = 0; j < 4; j++) { playerpile.add(player.remove(i)) } } }

for computers hand, copy-paste , alter playerpile computerpile , player computer

the error getting because pilecheck functions have homecoming value, value isn't assigned anything. doesn't need 2 functions, should able merge them together:

public static void checkpiles() { (int = 1; < 14; i++) { if (collections.frequency(player,i) == 4) { (int j = 0; j < 4; j++) { playerpile.add(player.remove(player.indexof(i))) //updated } } else if (collections.frequency(computer,i) == 4) { (int k = 0; k < 4; k++) { computerpile.add(computer.remove(computer.indexof(i))) //updated } } } }

you can create pile show 1 of type of card in it, changing loop to:

if (collections.frequency(player,i) == 4) { (int j = 0; j < 4; j++) { player.remove(player.indexof(i)); } playerpile.add(i); }

note if way instead, while loop need changed to:

while (computerpile.size() + personpile.size() < 13 || !cards.isempty())

because total in both piles add together 13 instead of 52 when matches found

java arraylist

No comments:

Post a Comment