java - CardPile Index Out of bounds -



java - CardPile Index Out of bounds -

i'm trying convert code arraylist , can't seem create work. i'm running whole programme , tells me index out bounds. i'm sure forgot add together size of array cards, don't know how add together it. help!

edit: error @ bottom. also, tells me go removetop method. looks fine there.

import java.util.random; import java.util.list; import java.util.arraylist; public class cardpile { private arraylist<card> cards = new arraylist<card>(); private static random r = new random(1); public void addtobottom(card c) { if (this.cards.size() == 52) { system.out.println("the cardpile full. cannot add together more card objects."); } this.cards.add(c); } public card removecard(card c) { if (this.cards.contains(c)) { this.cards.remove(c); } homecoming null; } public card removetop() { homecoming this.cards.remove(0); } public int searchvalue(int value) { int count = 0, (int = 0;i < this.cards.size();i++) { if (this.cards.get(i).getvalue() == value) { count++; } } //system.out.println("count = "+count); homecoming count; } public card[] removeall(int value) //system.out.println("(removeall) cards ="+ cards); int count = searchvalue(value); card[] removed = new card[count]; int deletedcount = 0; int = 0; while (deletedcount < count) { if (this.cards.get(i).getvalue() == value) { removed[deletedcount] = this.cards.remove(i); deletedcount++; } else { i++; } } homecoming removed; } public int getnumbercards() { homecoming this.cards.size(); } public string tostring() { if (this.cards.isempty()) { homecoming ""; } string builder = ""; (int = 0;i < this.cards.size() - 1;i++) { builder = builder + this.cards.get(i) + ", "; } builder = builder + this.cards.get(this.cards.size() - 1); homecoming builder; } public void shuffle() { if (this.cards.isempty()) { return; } (int count = 0; count < 100000;count++) { int = r.nextint(this.cards.size()); int j = r.nextint(this.cards.size()); card temp = this.cards.get(i); this.cards.set(i, this.cards.get(j)); this.cards.set(j, temp); } } public static cardpile makefulldeck() { cardpile deck = new cardpile(); (int suit = 0;suit < 4;suit++) { (int value = 1; value <= 13;value++) { deck.addtobottom(new card(suit, value)); } } deck.shuffle(); homecoming deck; } } **exception in thread "main" java.lang.indexoutofboundsexception: index: 0, size: 0 @ java.util.arraylist.rangecheck(arraylist.java:653) @ java.util.arraylist.remove(arraylist.java:492) @ cardpile.removetop(cardpile.java:40) @ gofish.dealcards(gofish.java:112) @ gofish.main(gofish.java:13)**

edit: player class:

public class player { private boolean[] books; private cardpile pile; private static int maximum_value_card = 13; public player() { this.pile = new cardpile(); this.books = new boolean[13]; //by default false } public boolean hascard(int value) { homecoming this.pile.searchvalue(value) > 0; } public card[] removeall(int value) { homecoming this.pile.removeall(value); } public void addall(card[] cards) { (int = 0; < cards.length; i++) { this.pile.addtobottom(cards[i]); } } //optional additional method public void addcard(card card) { this.pile.addtobottom(card); } public int getnumbercards() { homecoming this.pile.getnumbercards(); } public int countbooks() { int count = 0; (int = 0; < maximum_value_card; i++) { if (books[i]) { count++; } } homecoming count; } public void addbook(int value) { this.books[value - 1] = true; } public void printhand() { system.out.println("player's hand " + this.pile); } }

and gofish class:

import java.util.scanner; public class gofish { private static scanner reader; public static void main(string[] args) { system.out.println("how many players?"); reader = new scanner(system.in); int numberplayers = reader.nextint(); player[] players = createplayersarray(numberplayers); int currentturn = 0; cardpile deck = cardpile.makefulldeck(); dealcards(deck, players); int maximumretries = 2; int numretries = 0; while(deck.getnumbercards() > 0 && players[currentturn].getnumbercards() > 0) { updatebooks(players[currentturn]); if (numretries == maximumretries) { numretries = 0; currentturn++; if (currentturn == numberplayers) { currentturn = 0; } } system.out.println("player " + currentturn + ", here hand. card inquire for?"); players[currentturn].printhand(); int querycard = reader.nextint(); system.out.println("and whom from?"); int queryplayer = reader.nextint(); if (querycard < 1 || querycard > 13 || queryplayer < 0 || queryplayer >= numberplayers || queryplayer == currentturn) { system.out.println("invalid entries. please retry"); numretries++; } else { numretries = 0; boolean hascard = players[queryplayer].hascard(querycard); if (hascard) { system.out.println("cards found!"); card[] removed = players[queryplayer].removeall(querycard); players[currentturn].addall(removed); } else { system.out.println("go fish!"); card top = deck.removetop(); system.out.println("you drew " + top); players[currentturn].addcard(top); //check create sure card didn't form book //note happen if doesn't match card asking updatebooks(players[currentturn]); if (top.getvalue() == querycard) { system.out.println("you went fishing!"); } else { currentturn++; if (currentturn == numberplayers) { currentturn = 0; } } } } } //calculate winner int maxplayer = 0; int maxplayerbooks = players[0].countbooks(); (int = 1; < numberplayers; i++) { int currentbooks = players[i].countbooks(); if (currentbooks > maxplayerbooks) { maxplayer = i; maxplayerbooks = currentbooks; } } system.out.println("congratulations! player " + maxplayer + " have won game accumulating " + maxplayerbooks + " books!"); } private static player[] createplayersarray(int numplayers) { player[] players = new player[numplayers]; (int = 0; < numplayers; i++) { players[i] = new player(); } homecoming players; } private static void dealcards(cardpile deck, player[] players) { final int number_cards_per_player = 7; (int = 0; < number_cards_per_player * players.length; i++) { card next = deck.removetop(); players[i % players.length].addcard(next); } } private static void updatebooks(player player) { (int = 1; <= 13; i++) { //alternative alternative modify hascard method homecoming int instead of boolean. count (this improve design) card[] valued = player.removeall(i); if (valued.length == 4) { player.addbook(i); } else { player.addall(valued); } } } }

java indexoutofboundsexception

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -