Running Java Poker Game - java

I have to write a program that creates 5 decks of 5 random cards chosen, and evaluate them to return the value of the deck. Etc. 9 is a royal lush and a 2 is a 2 pair.
I have three classes so far. One to represent the Card, One to represent the Deck, and one to represent the Hand evaluation. I do not have a main program yet to call these, and wanted to know how that would be done. For example, I want to have a main program that once ran, disperses out 5 hands of 5 random cards, evaluates them, and returns the value. I have the classes necessary to everything, just not sure how to make a class to make it run.
public class Card{
private short rank, suit;
private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
public static String rankAsString( int __rank ) {
return ranks[__rank];
}
Card(short suit, short rank)
{
this.rank=rank;
this.suit=suit;
}
public #Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}
public short getRank() {
return rank;
}
public short getSuit() {
return suit;
}
}
import java.util.Random;import java.util.ArrayList;
public class Deck {
private ArrayList<Card> cards;
Deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;
for (short a=0; a<=3; a++)
{
for (short b=0; b<=12; b++)
{
cards.add( new Card(a,b) );
}
}
int size = cards.size() -1;
for (short i=0; i<100; i++)
{
index_1 = generator.nextInt( size );
index_2 = generator.nextInt( size );
temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}
public Card drawFromDeck()
{
return cards.remove( cards.size()-1 );
}
public int getTotalCards()
{
return cards.size();
//we could use this method when making
//a complete poker game to see if we needed a new deck
}
}
public class Hand {
private Card[] cards;
private int[] value;
Hand(Deck d)
{
value = new int[6];
cards = new Card[5];
for (int x=0; x<5; x++)
{
cards[x] = d.drawFromDeck();
}
int[] ranks = new int[14];
//miscellaneous cards that are not otherwise significant
int[] orderedRanks = new int[5];
boolean flush=true, straight=false;
int sameCards=1,sameCards2=1;
int largeGroupRank=0,smallGroupRank=0;
int index=0;
int topStraightValue=0;
for (int x=0; x<=13; x++)
{
ranks[x]=0;
}
for (int x=0; x<=4; x++)
{
ranks[ cards[x].getRank() ]++;
}
for (int x=0; x<4; x++) {
if ( cards[x].getSuit() != cards[x+1].getSuit() )
flush=false;
}
for (int x=13; x>=1; x--)
{
if (ranks[x] > sameCards)
{
if (sameCards != 1)
//if sameCards was not the default value
{
sameCards2 = sameCards;
smallGroupRank = largeGroupRank;
}
sameCards = ranks[x];
largeGroupRank = x;
} else if (ranks[x] > sameCards2)
{
sameCards2 = ranks[x];
smallGroupRank = x;
}
}
if (ranks[1]==1) //if ace, run this before because ace is highest card
{
orderedRanks[index]=14;
index++;
}
for (int x=13; x>=2; x--)
{
if (ranks[x]==1)
{
orderedRanks[index]=x; //if ace
index++;
}
}
for (int x=1; x<=9; x++)
//can't have straight with lowest value of more than 10
{
if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 &&
ranks[x+3]==1 && ranks[x+4]==1)
{
straight=true;
topStraightValue=x+4; //4 above bottom value
break;
}
}
if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 &&
ranks[13]==1 && ranks[1]==1) //ace high
{
straight=true;
topStraightValue=14; //higher than king
}
for (int x=0; x<=5; x++)
{
value[x]=0;
}
//start hand evaluation
if ( sameCards==1 ) {
value[0]=1;
value[1]=orderedRanks[0];
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}
if (sameCards==2 && sameCards2==1)
{
value[0]=2;
value[1]=largeGroupRank; //rank of pair
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
value[4]=orderedRanks[2];
}
if (sameCards==2 && sameCards2==2) //two pair
{
value[0]=3;
//rank of greater pair
value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
value[3]=orderedRanks[0]; //extra card
}
if (sameCards==3 && sameCards2!=2)
{
value[0]=4;
value[1]= largeGroupRank;
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
}
if (straight && !flush)
{
value[0]=5;
value[1]=4;
}
if (flush && !straight)
{
value[0]=6;
value[1]=orderedRanks[0]; //tie determined by ranks of cards
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}
if (sameCards==3 && sameCards2==2)
{
value[0]=7;
value[1]=largeGroupRank;
value[2]=smallGroupRank;
}
if (sameCards==4)
{
value[0]=8;
value[1]=largeGroupRank;
value[2]=orderedRanks[0];
}
if (straight && flush)
{
value[0]=9;
value[1]=0;
}
}
void display()
{
String s;
switch( value[0] )
{
case 1:
s="high card";
break;
case 2:
s="pair of " + Card.rankAsString(value[1]) + "\'s";
break;
case 3:
s="two pair " + Card.rankAsString(value[1]) + " " +
Card.rankAsString(value[2]);
break;
case 4:
s="three of a kind " + Card.rankAsString(value[1]) + "\'s";
break;
case 5:
s=Card.rankAsString(value[1]) + " high straight";
break;
case 6:
s="flush";
break;
case 7:
s="full house " + Card.rankAsString(value[1]) + " over " +
Card.rankAsString(value[2]);
break;
case 8:
s="four of a kind " + Card.rankAsString(value[1]);
break;
case 9:
s="straight flush " + Card.rankAsString(value[1]) + " high";
break;
default:
s="error in Hand.display: value[0] contains invalid value";
}
s = " " + s;
System.out.println(s);
}
void displayAll()
{
for (int x=0; x<5; x++)
System.out.println(cards[x]);
}
int compareTo(Hand that)
{
for (int x=0; x<6; x++)
{
if (this.value[x]>that.value[x])
return 1;
else if (this.value[x]<that.value[x])
return -1;
}
return 0; //if hands are equal
}
}

Wouldn't it just be something like this? Not too complex... you wrote all the logic of the program already (I didn't double check that, BTW)
public class MainRunner{
public static void main(String[] args){
Deck deck = new Deck();
Hand myHand = new Hand(deck);
myHand.display();
}
}

i am pretty sure that there's no add function in your Card.java
for (short a=0; a<=3; a++)
{
for (short b=0; b<=12; b++)
{
cards.add( new Card(a,b) ); //here
}
}
So maybe go through your classes first?

Related

Memory game not revealing correct guesses

This program is a Domino memory game where you flip dominos until you make a correct guess where the correct dominos are supposed to stay revealed. However the problem is that while the game does work correctly the dominos do not stay revealed nor does the game end.
This is the code for my Domino Class
`
public class Domino {
private int top, bottom;
private boolean revealed;
public Domino(int x, int y) {
if (x > y) {
top = y;
bottom = x;
} else {
top = x;
bottom = y;
}
}
public int getTop() {
return top;
}
public int getBottom() {
return bottom;
}
public boolean isRevealed() {
if (revealed)
return true;
return false;
}
public void setRevealed(boolean revealed) {
this.revealed = revealed;
}
public boolean equals(Domino other) {
if (top == bottom)
return true;
return false;
}
}
`
Then here is the memory game class (called MemoryLane)
`
import java.util.Arrays;
import java.util.Random;
public class MemoryLane
{
private Domino[] board;
public MemoryLane(int max)
{
board = new Domino[(max * max) + max];
int i = 0;
for(int top = 1; top <= max; top++)
for(int bot = 1; bot <= max; bot++)
{
// make new Domino(2x) +
// save into array
if(top <= bot)
{
board[i] = new Domino(top, bot);
i++;
board[i] = new Domino(top, bot);
i++;
}
}
shuffle();
}
private void shuffle()
{
int index;
Random random = new Random();
for (int i = board.length - 1; i > 0; i--)
{
index = random.nextInt(i + 1);
if (index != i)
{
Domino temp = board[index];
board[index] = board[i];
board[i] = temp;
}
}
}
public boolean guess(int i, int k)
{
if(board[i] == board[k])
{
return true;
}
return false;
}
public String peek(int a, int b)
{
String text = new String();
text += ("[" + board[a].getTop()+ "] [" + board[b].getTop()+ "]\n");
text += ("[" + board[a].getBottom()+ "] [" + board[b].getBottom()+ "]\n");
return text;
}
public boolean gameOver() {
int count = 0;
for(int i=0; i< board.length; i++)
{
if(board[i].isRevealed())
count ++;
}
return (count == board.length);
}
public String toString() {
String text = new String();
for(int i=0; i< board.length; i++)
{
if(board[i].isRevealed())
text += ("[" + board[i].getTop()+ "] ");
else
text += ("[ ] ");
}
text += ('\n');
for(int i=0; i< board.length; i++)
{
if(board[i].isRevealed())
text += ("[" + board[i].getBottom()+ "] ");
else
text += ("[ ] ");
}
return text;
}
}
`
Then here is the driver (the driver was provided to me by a third party so it must work as it is presented and cannot be changed)
`
import java.util.Scanner;
public class MemoryLaneDriver
{
public static void main(String[] args)
{
String message = "Welcome to Memory Lane!" + "\n" +
"Choose two indexes to reveal the corresponding dominoes." + "\n" +
"If the dominoes match, they stay revealed." + "\n" +
"Reveal all the dominoes to win the game!" + "\n";
System.out.println(message);
Scanner input = new Scanner(System.in);
MemoryLane game = new MemoryLane(2);
long start = System.currentTimeMillis();
while(!game.gameOver())
{
System.out.println(game);
System.out.print("First: ");
int first = input.nextInt();
System.out.print("Second: ");
int second = input.nextInt();
game.guess(first, second);
System.out.println(game.peek(first, second) + "\n");
}
long stop = System.currentTimeMillis();
long elapsed = (stop - start) / 1000;
System.out.println(game);
System.out.println("\nYou win!");
System.out.println("Total time: " + elapsed + "s");
}
}
`
I have tried using the methods in Domino like setRevealed and isRevealed in the guess method (for example when i try board.setRevealed = true or board.isRevealed = true), but it wont work and turns up red in IntelliJ. I can also not use any Stringbuilder uses (such as append) because it is outside of what has been covered in class.
When I say the game is working correctly, I mean that it outputs my choices like:
`
Welcome to Memory Lane!
Choose two indexes to reveal the corresponding dominoes.
If the dominoes match, they stay revealed.
Reveal all the dominoes to win the game!
[ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ]
First: 1
Second: 3
[2] [2]
[2] [2]
[ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ]
First:
`
However as you can see it is not revealing the correct guess, and even if I guess all of the Dominos correctly the game does not end.
So, in your original code, you were using board[i] == board[k] which is comparing memory address locations and not the object properties, instead, you should be using board[i].equals(board[k]).
In this case you need to override equals method of the Domino class in order to change how the comparison works, for example...
#Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + this.getTop();
hash = 59 * hash + this.getBottom();
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Domino)) {
return false;
}
final Domino other = (Domino) obj;
if (this.getTop() != other.getTop()) {
return false;
}
if (this.getBottom() != other.getBottom()) {
return false;
}
return true;
}
It's important to remember, if you override equals you should also override hashCode as they have an important relationship to each other.
You also never call setRevealed, which I guess should be done in guess
public boolean guess(int i, int k) {
if (board[i].equals(board[k])) {
board[i].setRevealed(true);
board[k].setRevealed(true);
return true;
}
return false;
}
Runnable example...
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main();
}
Main() {
String message = "Welcome to Memory Lane!" + "\n"
+ "Choose two indexes to reveal the corresponding dominoes." + "\n"
+ "If the dominoes match, they stay revealed." + "\n"
+ "Reveal all the dominoes to win the game!" + "\n";
System.out.println(message);
Scanner input = new Scanner(System.in);
MemoryLane game = new MemoryLane(2);
long start = System.currentTimeMillis();
while (!game.gameOver()) {
// This is just making it easier to cheat.
System.out.println(game.debug());
System.out.println(game);
System.out.print("First: ");
int first = input.nextInt();
System.out.print("Second: ");
int second = input.nextInt();
game.guess(first, second);
System.out.println(game.peek(first, second) + "\n");
}
long stop = System.currentTimeMillis();
long elapsed = (stop - start) / 1000;
System.out.println(game);
System.out.println("\nYou win!");
System.out.println("Total time: " + elapsed + "s");
}
public class Domino {
private int top, bottom;
private boolean revealed;
public Domino(int x, int y) {
if (x > y) {
top = y;
bottom = x;
} else {
top = x;
bottom = y;
}
}
public int getTop() {
return top;
}
public int getBottom() {
return bottom;
}
public boolean isRevealed() {
return revealed;
}
public void setRevealed(boolean revealed) {
this.revealed = revealed;
}
#Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + this.getTop();
hash = 59 * hash + this.getBottom();
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Domino)) {
return false;
}
final Domino other = (Domino) obj;
if (this.getTop() != other.getTop()) {
return false;
}
if (this.getBottom() != other.getBottom()) {
return false;
}
return true;
}
}
public class MemoryLane {
private Domino[] board;
public MemoryLane(int max) {
board = new Domino[(max * max) + max];
int i = 0;
for (int top = 1; top <= max; top++) {
for (int bot = 1; bot <= max; bot++) {
// make new Domino(2x) +
// save into array
if (top <= bot) {
board[i] = new Domino(top, bot);
i++;
board[i] = new Domino(top, bot);
i++;
}
}
}
shuffle();
}
private void shuffle() {
int index;
Random random = new Random();
for (int i = board.length - 1; i > 0; i--) {
index = random.nextInt(i + 1);
if (index != i) {
Domino temp = board[index];
board[index] = board[i];
board[i] = temp;
}
}
}
public boolean guess(int i, int k) {
if (board[i].equals(board[k])) {
board[i].setRevealed(true);
board[k].setRevealed(true);
return true;
}
return false;
}
public String peek(int a, int b) {
String text = new String();
text += ("[" + board[a].getTop() + "] [" + board[b].getTop() + "]\n");
text += ("[" + board[a].getBottom() + "] [" + board[b].getBottom() + "]\n");
return text;
}
public boolean gameOver() {
int count = 0;
for (int i = 0; i < board.length; i++) {
if (board[i].isRevealed()) {
count++;
}
}
return (count == board.length);
}
public String debug() {
String text = new String();
for (int i = 0; i < board.length; i++) {
text += ("[" + board[i].getTop() + "] ");
}
text += ('\n');
for (int i = 0; i < board.length; i++) {
text += ("[" + board[i].getBottom() + "] ");
}
return text;
}
public String toString() {
String text = new String();
for (int i = 0; i < board.length; i++) {
if (board[i].isRevealed()) {
text += ("[" + board[i].getTop() + "] ");
} else {
text += ("[ ] ");
}
}
text += ('\n');
for (int i = 0; i < board.length; i++) {
if (board[i].isRevealed()) {
text += ("[" + board[i].getBottom() + "] ");
} else {
text += ("[ ] ");
}
}
return text;
}
}
}
I figured it out
The problem was my equals method was comparing top to bottom which is why i was getting such weird results. I changed it to
public boolean equals(Domino other){
return this.top == other.top && this.bottom == other.bottom
}
and now it works perfectly fine thanks for all of the help guys!

Cannot get card/deck object program to work

this is my first time posting on the forum and I'm not entirely sure if my question is valid, but I will try to be specific and follow the guideline. In following the guidelines, this is a question based around a class assignment. This assignment is to take code that creates a 'deck' object that represents a deck of cards and add several features. I am currently stuck in the process.
My issue lies within this code:
public class SilasAlmgrenS6L1CardCreate {
public static void main(String args[]) {
Deck d = new Deck();
d.shuffle();
Hand f = new Hand(d); //Returns error 'Hand cannot be resolved to a type'
}
public static class Deck {
Card[] cardArray = new Card[52];
Deck() { //constructor
int suits = 4;
int cardType = 13;
int cardCount = 0;
for (int i = 1; i <= suits; i++)
for (int j = 1; j <= cardType; j++) {
cardArray[cardCount] = new Card(i, j);
cardCount++;
} //End loop
} //End deck() constructor
////////////////////////////////////////////////////////
//My code starts here
public class Hand {
Hand(Deck a) {
Card[] Hand = {a.cardArray[0], a.cardArray[1], a.cardArray[2], a.cardArray[3], a.cardArray[4]};
Card[] playerHand = {Hand[0], Hand[1]};
System.out.println("You have " + playerHand[0] + " and " + playerHand[1] );
} //End hand constructor
} //End hand class
public void shuffle() {
//Runs loop for the length of the deck
for(int i = 0; i < cardArray.length; i++) {
int num = (int) (Math.random() * (51 - 0)) + 0; //Creates a random number between 0 and 51; used to shuffle
Card placeHolder = cardArray[i]; //Picks a place holder card from the deck
cardArray[i] = cardArray[num]; //Picks two random cards and make them equal
cardArray[num] = placeHolder; //Assigns one of the duplicate cards to the placeholder value
} //End for
} //End shuffle
//And ends here
/////////////////////////////////////////////////
public void print() {
for (int i = 0; i < cardArray.length; i++)
System.out.println(cardArray[i]);
} //End print loop
} //End print class
public static class Card {
String suit, name;
int points;
Card(int n1, int n2) {
suit = getSuit(n1);
name = getName(n2);
points = getPoints(name);
} //End card class
public String toString() {
return "The " + name + " of " + suit;
} //End toString
public String getName(int i) {
if (i == 1) return "Ace";
if (i == 2) return "Two";
if (i == 3) return "Three";
if (i == 4) return "Four";
if (i == 5) return "Five";
if (i == 6) return "Six";
if (i == 7) return "Seven";
if (i == 8) return "Eight";
if (i == 9) return "Nine";
if (i == 10) return "Ten";
if (i == 11) return "Jack";
if (i == 12) return "Queen";
if (i == 13) return "King";
return "error";
} //End getName String
public int getPoints(String n) {
if (n == "Jack" || n == "Queen" || n == "King" || n == "Ten")
return 10;
if (n == "Two")
return 2;
if (n == "Three")
return 3;
if (n == "Four")
return 4;
if (n == "Five")
return 5;
if (n == "Six")
return 6;
if (n == "Seven")
return 7;
if (n == "Eight")
return 8;
if (n == "Nine")
return 9;
if (n == "Ace")
return 11;
return -1;
} //End int getPoints
public String getSuit(int i) {
if (i == 1) return "Diamonds";
if (i == 2) return "Clubs";
if (i == 3) return "Spades";
if (i == 4) return "Hearts";
return "error";
} //End getSuit String
} //End Deck class
}
The majority of this program was already provided with the assignment, but we are to add several features. The first of these features was a shuffle method, which I was able to do. In the next feature we are to create a 'Hand' class that deals a hand of cards to the user and calculate how many points they have, as if it were a game of blackjack.
This is the exact wording of this step is:
'Add a Hand Class that contains an array of 5 Card references. Have the program Deal the Hand two cards and display them for the user. Tell the user how many points they have and ask them if they would like another card or not. Continue to allow the player to add cards until they reach 5 cards or the total is greater than 21.'
I have ran through several ways that I felt I could create this class, but none seem to be working. This current iteration is as close as I've gotten. I am currently stumped, however. My issues are; I don't know why I'm getting the type error when I try to use the Hand class and I have no idea how to implement the getPoints() method. There are several steps following the creation of the Hand class, but I am sure I can get through them if I can figure out how to make this class work. I'm on the brink of punching a hole in my wall, so any help with fixing this code would be absolutely appreciated.
The first problem is that your Hand class is a subclass of your static Deck class. This way you can not use it in your main method, because that is not the enclosing class.
EDIT
So, I went a little overboard here. Hope it makes sense to you.
First of all, we have your main class. I renamed it for the sake of not wanting to type that long name. I added your Handand Deck objects as variables, since neither the Hand belongs to the Deck or the other way around, but they both are part of the Main class, let's refer to it as the game.
You start by shuffling the deck. When you have the deck, you can get a random card. This is provided by the deck. (I will come to that method shortly).
As soon as you have a hand, you can show the user the amount of points they have and ask them if they would like an extra card (if numberOfPoints < 22 and numberOfCards < 5). If they do want an extra card, ask the deck for a random card and add it to the hand. Go on until any of the boundaries are met.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CardExample {
private Hand playHand;
private Deck playDeck;
private Scanner reader = new Scanner(System.in); // Reading from System.in
public static void main(String args[]) {
new CardExample().play();
}
public void play(){
playDeck = new Deck();
playDeck.shuffle();
Card firstCard = playDeck.getRandomCard();
Card secondCard = playDeck.getRandomCard();
List<Card> startCards = new ArrayList<>();
startCards.add(firstCard);
startCards.add(secondCard);
playHand = new Hand(startCards);
requestInput();
}
private void requestInput(){
System.out.println("You have " + playHand.getPoints() + " points");
System.out.println("New card? (Y/N)");
String input = reader.next();
if(input.equalsIgnoreCase("y")){
Card newCard = playDeck.getRandomCard();
playHand.addCard(newCard);
if(playHand.getNumberOfCards() < 5 && playHand.getPoints() < 22) {
requestInput();
}else if(playHand.getPoints() >= 22){
System.out.println("You have " + playHand.getPoints() + "points. You're dead, sorry.");
reader.close();
} else{
System.out.println("You have 5 cards, that's the max");
reader.close();
}
}else{
System.out.println("Your score is " + playHand.getPoints() + " points");
reader.close();
}
}
}
Deck class
public class Deck {
private Card[] cardArray = new Card[52];
private int currentIndex = 0;
public Deck() { //constructor
int suits = 4;
int cardType = 13;
int cardCount = 0;
for (int i = 1; i <= suits; i++)
for (int j = 1; j <= cardType; j++) {
cardArray[cardCount] = new Card(i, j);
cardCount++;
} //End loop
}
public void shuffle() {
//Runs loop for the length of the deck
for(int i = 0; i < cardArray.length; i++) {
int num = (int) (Math.random() * (51 - 0)) + 0; //Creates a random number between 0 and 51; used to shuffle
Card placeHolder = cardArray[i]; //Picks a place holder card from the deck
cardArray[i] = cardArray[num]; //Picks two random cards and make them equal
cardArray[num] = placeHolder; //Assigns one of the duplicate cards to the placeholder value
} //End for
} //End shuffle
public Card[] getCardArray() {
return cardArray;
}
public Card getRandomCard(){
Card nextCard = cardArray[currentIndex];
currentIndex += 1;
return nextCard;
}
//And ends here
/////////////////////////////////////////////////
public void print() {
for (int i = 0; i < cardArray.length; i++)
System.out.println(cardArray[i]);
} //End print loop
}
Hand class
addCard updates points so that whenever getting numberOfCards or points, right value is returned
import java.util.ArrayList;
import java.util.List;
public class Hand {
private int points = 0;
private int numberOfCards = 0;
private List<Card> hand = new ArrayList<>();
public Hand(List<Card> cards) {
hand.addAll(cards);
numberOfCards += cards.size();
for(Card card: cards){
points += card.points;
}
} //End hand constructor
public void addCard(Card card){
hand.add(card);
points += card.points;
numberOfCards += 1;
}
public int getNumberOfCards() {
return numberOfCards;
}
public int getPoints() {
return points;
}
}
Card class (unchanged, but seperate file)
public class Card {
String suit, name;
int points;
Card(int n1, int n2) {
suit = getSuit(n1);
name = getName(n2);
points = getPoints(name);
} //End card class
public String toString() {
return "The " + name + " of " + suit;
} //End toString
public String getName(int i) {
if (i == 1) return "Ace";
if (i == 2) return "Two";
if (i == 3) return "Three";
if (i == 4) return "Four";
if (i == 5) return "Five";
if (i == 6) return "Six";
if (i == 7) return "Seven";
if (i == 8) return "Eight";
if (i == 9) return "Nine";
if (i == 10) return "Ten";
if (i == 11) return "Jack";
if (i == 12) return "Queen";
if (i == 13) return "King";
return "error";
} //End getName String
public int getPoints(String n) {
if (n == "Jack" || n == "Queen" || n == "King" || n == "Ten")
return 10;
if (n == "Two")
return 2;
if (n == "Three")
return 3;
if (n == "Four")
return 4;
if (n == "Five")
return 5;
if (n == "Six")
return 6;
if (n == "Seven")
return 7;
if (n == "Eight")
return 8;
if (n == "Nine")
return 9;
if (n == "Ace")
return 11;
return -1;
} //End int getPoints
public String getSuit(int i) {
if (i == 1) return "Diamonds";
if (i == 2) return "Clubs";
if (i == 3) return "Spades";
if (i == 4) return "Hearts";
return "error";
} //End getSuit String
} //End Deck class

JTable object [][] data

I am trying to create a table based on my trial class. When I try to run my table class the table prints with the proper heading, but it does not print anything in the body of the table. How should I fix my for loop so the data enters the table? Do I need to add a parameter to the constructor?
I understand there are more getters and setters than necessary. I was just trying to go about the for loop in different ways.
public class Trial extends JFrame{
static int counter = 0;
int laps = 0;
String lapTime;
double currentTime = 0;
String difference;
int lapsCount = 0;
String[] array1;
double[] array2;
double[] array3;
public int getLapsCount() {
return lapsCount;
}
public void setLapsCount(int lapsCount) {
this.lapsCount = lapsCount;
}
public static int getCounter() {
return counter;
}
public static void setCounter(int counter) {
Trial.counter = counter;
}
public int getLaps() {
return laps;
}
public void setLaps(int laps) {
this.laps = laps;
}
public String getLapTime() {
return lapTime;
}
public void setLapTime(String lapTime) {
this.lapTime = lapTime;
}
public double getCurrentTime() {
return currentTime;
}
public void setCurrentTime(double currentTime) {
this.currentTime = currentTime;
}
public String getDifference() {
return difference;
}
public void setDifference(String difference) {
this.difference = difference;
}
public String[] getArray1() {
return array1;
}
public void setArray1(String[] array1) {
this.array1 = array1;
}
public double[] getArray2() {
return array2;
}
public void setArray2(double[] array2) {
this.array2 = array2;
}
public double[] getArray3() {
return array3;
}
public void setArray3(double[] array3) {
this.array3 = array3;
}
public static void main(String[] args) {
//drop down for number of laps
String lapsString;
String[] selections = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
lapsString = (String) JOptionPane.showInputDialog(null,
"Choose a number of laps:",
"Lap Counter",
JOptionPane.INFORMATION_MESSAGE,
null, /* icon */
selections,
"0");
System.out.printf("Number of laps = \"%s\"\n", lapsString);
int lapsCount = Integer.parseInt(lapsString);
String[] array1 = new String[lapsCount+1];
double[] array2 = new double[lapsCount+1];
double[] array3 = new double[lapsCount+1];
// showInputDialog with for lap time
for (int i = 1; i <= lapsCount; i++){
String lapTime;
lapTime =
JOptionPane.showInputDialog("Enter lap " + i + " total time.");
System.out.printf("Lap " + i + " = \"%s\"\n", lapTime);
array1[i] = lapTime;
double currentTime = inputConvert(array1[i]);
array2[i] = currentTime;
array3[i] = (array2[i] - array2[i-1]);
counter++;
//confirm each time
int result;
String[] results = { "Yes", "No", "Cancel" };
result = JOptionPane.showConfirmDialog(null,
"Please confirm lap " + i + " is " + lapTime);
if (result == -1)
System.out.printf("user closed window\n");
else if (result == 0)
System.out.printf("result = %d, user pressed \"%s\"\n", result, results[result]);
else {
lapTime =
JOptionPane.showInputDialog("Please re-enter lap " + i);
System.out.printf("Lap " + i + " = \"%s\"\n", lapTime);
counter++;
}
}
}
public static double inputConvert(String s) {
double convert = 0.0;
int minutes = Integer.parseInt(s.substring(0, 1));
double seconds = Double.parseDouble(s.substring(2, s.length()));
minutes = minutes * 60;
convert = minutes + seconds;
return convert;
}
private static String getDifference(int j, double sign) {
String difference = "";
if(j == 0) {
difference = "---";
}
else if(sign > 0.0) {
difference = "+";
}
return difference;
}
public static String displayTime(double time) {
String result = null;
int minutes = (int) (time/60);
double seconds = (time % 60);
seconds = Math.round(seconds*100.0)/100.0;
if(seconds < 10){
result = minutes + ":0" + seconds;
}
else{
result = minutes + ":" + seconds;
}
return result;
}
}
This is my table class where the problem arises.
public class table {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new table();
}
});
}
public table() {
JFrame guiFrame = new JFrame();
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Table to record lap times");
guiFrame.setSize(700,200);
guiFrame.setLocationRelativeTo(null);
JTable table = new JTable(new ExampleTableModel());
table.setAutoCreateRowSorter(true);
table.setGridColor(Color.YELLOW);
table.setBackground(Color.CYAN);
JScrollPane tableScrollPane = new JScrollPane(table);
guiFrame.add(tableScrollPane);
guiFrame.setVisible(true);
}
class ExampleTableModel extends AbstractTableModel{
int laps = 0;
String lapTime;
double currentTime = 0;
String difference;
Trial t = new Trial();
//Two arrays used for the table data
private String[] columnNames = {"Lap #", "Total Time", "Lap Time" , "Difference" };
private Object[][] data;
public ExampleTableModel(){
data = new Object[laps][4];
for(int i = 0; i < laps; i++){
data[i][0] = "Lap " + i;
data[i][1] = t.getArray1() [i];
data[i][2] = t.getArray2()[i];
data[i][3] = t.getArray3()[i];
}
}
#Override public int getRowCount() {
return data.length;
}
#Override public int getColumnCount() {
return columnNames.length;
}
#Override public Object getValueAt(int row, int column) {
return data[row][column];
}
#Override public String getColumnName(int column) {
return columnNames[column];
}
#Override public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
#Override public boolean isCellEditable(int row, int column) {
if (column == 0 || column == 1) {
return false;
}
else {
return true;
}
}
}
}
I made those changes but I need my output to look like this
Lap #: Total time Lap time Difference
Lap1: 2:10 2:10 ---
I fixed the lap number so it starts at 1. I made these changes in the hopes that it would fix the lap time and difference cells but it is giving me an error.
public ExampleTableModel(Trial trial){
int laps = trial.getLapsCount();
data = new Object[laps][4];
for(int i = 0; i < laps; i++){
if (i == 0){
data[i][0] = "Lap " + (i + 1);
data[i][1] = trial.getArray1() [i + 1];
data[i][2] = trial.getArray2() [i + 1];
data[i][3] = trial.getDifference(i, (trial.getArray3()[i + 1] - trial.getArray3()[i]));
}
else {
data[i][0] = "Lap " + (i + 1);
data[i][1] = trial.getArray1() [i + 1];
data[i][2] = trial.displayTime(trial.getArray2() [i + 1] - trial.getArray2() [i]);
data[i][3] = trial.getDifference(i, (trial.getArray3()[i + 1] - trial.getArray3()[i])) + (trial.getArray3()[i + 1] - trial.getArray3()[i]);
}
}
}
Edit: combining the code from the Trial.main and the Table.main methods
To make the code from your Trial class easier to use from the Table class, you can make the following changes. This way you can create a Trial object and call the getUserInput to let the user initialize it:
public static void main(String[] args) {
new Trial().getUserInput();
}
public void getUserInput() {
// [Code from the old main method...]
// Use the fields of the class instead of local variables:
lapsCount = Integer.parseInt(lapsString);
array1 = new String[lapsCount+1];
array2 = new double[lapsCount+1];
array3 = new double[lapsCount+1];
// [Code from the old main method...]
}
Now you can modify the Table model like this:
public Table() {
// [...]
Trial trial = new Trial();
trial.getUserInput();
JTable table = new JTable(new ExampleTableModel(trial));
// [...]
}
And let the ExampleTableModel constructor use a trail parameter (the laps and t fields are no longer used):
public ExampleTableModel(Trial trial){
int laps = trial.getLapsCount();
data = new Object[laps][4];
for(int i = 0; i < laps; i++){
data[i][0] = "Lap " + i;
data[i][1] = trial.getArray1()[i];
data[i][2] = trial.getArray2()[i];
data[i][3] = trial.getArray3()[i];
}
}
There was also an issue with the getColumnClass method:
#Override public Class getColumnClass(int column) {
//return getValueAt(0, column).getClass();
return (column <= 1) ? String.class : Double.class;
}
Now the table looks like this:
The output was:
Number of laps = "2"
Lap 1 = "2:10"
result = 0, user pressed "Yes"
Lap 2 = "2:20"
result = 0, user pressed "Yes"
Old answer
There seem to be a few issues that prevent the table from being filled:
In the ExampleTableModel class the laps field should be something higher than zero (for the constructor to fill the data array):
class ExampleTableModel extends AbstractTableModel {
//int laps = 0;
int laps = 6;
In the constructor of the ExampleTableModel class:
public ExampleTableModel(){
data = new Object[laps][4];
for(int i = 0; i < laps; i++){
data[i][0] = "Lap " + i;
data[i][1] = t.getArray1()[i];
data[i][2] = t.getArray2()[i];
data[i][3] = t.getArray3()[i];
}
}
the calls to the t.getArray1, t.getArray2, and t.getArray3 methods return null. The Trial object appears to have no data yet.
Do you want your program to execute both the code of the Trial.main method and the Table.main method?

CardPile Index Out of bounds

I'm currently trying to convert my code to ArrayList and I can't seem to make it work. I'm running the whole program and it tells me Index out bounds. I'm sure I forgot to add the size of the array for the cards, but I don't know how to add it. Thanks for the help!
Edit: The error I get is at the bottom. Also, it tells me to go to the removeTop method. It 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 is full. You cannot add any more Card objects.");
}
this.cards.add(c);
}
public Card removeCard(Card c) {
if (this.cards.contains(c)) {
this.cards.remove(c);
}
return null;
}
public Card removeTop() {
return this.cards.remove(0);
}
public int searchValue(int value) {
int count = 0,
for (int i = 0;i < this.cards.size();i++) {
if (this.cards.get(i).getValue() == value) {
count++;
}
}
//System.out.println("Count = "+count);
return 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 i = 0;
while (deletedCount < count) {
if (this.cards.get(i).getValue() == value) {
removed[deletedCount] = this.cards.remove(i);
deletedCount++;
} else {
i++;
}
}
return removed;
}
public int getNumberCards() {
return this.cards.size();
}
public String toString() {
if (this.cards.isEmpty()) {
return "";
}
String builder = "";
for (int i = 0;i < this.cards.size() - 1;i++) {
builder = builder + this.cards.get(i) + ", ";
}
builder = builder + this.cards.get(this.cards.size() - 1);
return builder;
}
public void shuffle() {
if (this.cards.isEmpty()) {
return;
}
for (int count = 0; count < 100000;count++) {
int i = 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();
for (int suit = 0;suit < 4;suit++) {
for (int value = 1; value <= 13;value++) {
deck.addToBottom(new Card(suit, value));
}
}
deck.shuffle();
return deck;
}
}
**Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.remove(ArrayList.java:492)
at CardPile.removeTop(CardPile.java:40)
at GoFish.dealCards(GoFish.java:112)
at GoFish.main(GoFish.java:13)**
EDIT:
This is the 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 all are false
}
public boolean hasCard(int value)
{
return this.pile.searchValue(value) > 0;
}
public Card[] removeAll(int value)
{
return this.pile.removeAll(value);
}
public void addAll(Card[] cards)
{
for (int i = 0; i < cards.length; i++)
{
this.pile.addToBottom(cards[i]);
}
}
//optional additional method
public void addCard(Card card)
{
this.pile.addToBottom(card);
}
public int getNumberCards()
{
return this.pile.getNumberCards();
}
public int countBooks()
{
int count = 0;
for (int i = 0; i < MAXIMUM_VALUE_CARD; i++)
{
if (books[i])
{
count++;
}
}
return count;
}
public void addBook(int value)
{
this.books[value - 1] = true;
}
public void printHand()
{
System.out.println("Player's hand is " + this.pile);
}
}
And this is the 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 is your hand. What card would you like to ask for?");
players[currentTurn].printHand();
int queryCard = reader.nextInt();
System.out.println("And from whom would you like to get it 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 to make sure this extra card didn't form a book
//Note this could happen even if it doesn't match the card they were asking about
updateBooks(players[currentTurn]);
if (top.getValue() == queryCard)
{
System.out.println("You successfully went fishing!");
}
else
{
currentTurn++;
if (currentTurn == numberPlayers)
{
currentTurn = 0;
}
}
}
}
}
//calculate the winner now
int maxPlayer = 0;
int maxPlayerBooks = players[0].countBooks();
for (int i = 1; i < numberPlayers; i++)
{
int currentBooks = players[i].countBooks();
if (currentBooks > maxPlayerBooks)
{
maxPlayer = i;
maxPlayerBooks = currentBooks;
}
}
System.out.println("Congratulations! Player " + maxPlayer + " you have won the game by accumulating " + maxPlayerBooks + " books!");
}
private static Player[] createPlayersArray(int numPlayers)
{
Player[] players = new Player[numPlayers];
for (int i = 0; i < numPlayers; i++)
{
players[i] = new Player();
}
return players;
}
private static void dealCards(CardPile deck, Player[] players)
{
final int NUMBER_CARDS_PER_PLAYER = 7;
for (int i = 0; i < NUMBER_CARDS_PER_PLAYER * players.length; i++)
{
Card next = deck.removeTop();
players[i % players.length].addCard(next);
}
}
private static void updateBooks(Player player)
{
for (int i = 1; i <= 13; i++)
{
//alternative option would be to modify the hasCard method to return an int instead of boolean. Then we could just count (this is probably better design)
Card[] valued = player.removeAll(i);
if (valued.length == 4)
{
player.addBook(i);
}
else
{
player.addAll(valued);
}
}
}
}

Why do object arrays in my ArrayList fail to retain their values?

I am creating a program in Java to simulate evolution. The way I have it set up, each generation is composed of an array of Organism objects. Each of these arrays is an element in the ArrayList orgGenerations. Each generation, of which there could be any amount before all animals die, can have any amount of Organism objects.
For some reason, in my main loop when the generations are going by, I can have this code without errors, where allOrgs is the Organism array of the current generation and generationNumber is the number generations since the first.
orgGenerations.add(allOrgs);
printOrgs(orgGenerations.get(generationNumber));
printOrgs is a method to display an Organism array, where speed and strength are Organism Field variables:
public void printOrgs(Organism[] list)
{
for (int x=0; x<list.length; x++)
{
System.out.println ("For organism number: " + x + ", speed is: " + list[x].speed + ", and strength is " + list[x].strength + ".");
}
}
Later on, after this loop, when I am trying to retrieve the data to display, I call this very similar code:
printOrgs(orgGenerations.get(0));
This, and every other array in orgGenerations, return a null pointer exception on the print line of the for loop. Why are the Organism objects loosing their values?
Alright, here is all of the code from my main Simulation class. I admit, it might be sort of a mess. The parts that matter are the start and simulator methods. The battle ones are not really applicable to this problem. I think.
import java.awt.FlowLayout;
import java.util.*;
import javax.swing.JFrame;
public class Simulator {
//variables for general keeping track
static Organism[] allOrgs;
static ArrayList<Organism[]> orgGenerations = new ArrayList <Organism[]>();
ArrayList<Integer> battleList = new ArrayList<Integer>();
int deathCount;
boolean done;
boolean runOnce;
//setup
Simulator()
{
done = false;
Scanner asker = new Scanner(System.in);
System.out.println("Input number of organisms for the simulation: ");
int numOfOrgs = asker.nextInt();
asker.close();
Organism[] orgArray = new Organism[numOfOrgs];
for (int i=0; i<numOfOrgs; i++)
{
orgArray[i] = new Organism();
}
allOrgs = orgArray;
}
//graphsOrgs
public void graphOrgs() throws InterruptedException
{
JFrame f = new JFrame("Evolution");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000,500);
f.setVisible(true);
Drawer bars = new Drawer();
//System.out.println(orgGenerations.size());
for (int iterator=0;iterator<(orgGenerations.size()-1); iterator++)
{
printOrgs(orgGenerations.get(0));
//The 0 can be any number, no matter what I do it wont work
//System.out.println("first");
f.repaint();
bars.data = orgGenerations.get(iterator);
f.add(bars);
//System.out.println("before");
Thread.sleep(1000);
//System.out.println("end");
}
}
//prints all Orgs and their statistics
public void printOrgs(Organism[] list)
{
System.out.println("Number Of Organisms: " + list.length);
for (int x=0; x<list.length; x++)
{
System.out.println ("For organism number: " + x + ", speed is: " + list[x].speed + ", and strength is " + list[x].strength + ".");
}
System.out.println();
}
//general loop for the organisms lives
public void start(int reproductionTime) throws InterruptedException
{
int generationNumber = 0;
orgGenerations.add(allOrgs);
printOrgs(orgGenerations.get(0));
generationNumber++;
while(true)
{
deathCount = 0;
for(int j=0; j<reproductionTime; j++)
{
battleList.clear();
for(int m=0; m<allOrgs.length; m++)
{
if (allOrgs[m].alive == true)
oneYearBattleCheck(m);
}
battle();
}
reproduction();
if (done == true)
break;
orgGenerations.add(allOrgs);
printOrgs(orgGenerations.get(generationNumber));
generationNumber++;
}
printOrgs(orgGenerations.get(2));
}
//Checks if they have to fight this year
private void oneYearBattleCheck(int m)
{
Random chaos = new Random();
int speedMod = chaos.nextInt(((int)Math.ceil(allOrgs[m].speed/5.0))+1);
int speedSign = chaos.nextInt(2);
if (speedSign == 0)
speedSign--;
speedMod *= speedSign;
int speed = speedMod + allOrgs[m].speed;
if (speed <= 0)
speed=1;
Random encounter = new Random();
boolean battle = false;
int try1 =(encounter.nextInt(speed));
int try2 =(encounter.nextInt(speed));
int try3 =(encounter.nextInt(speed));
int try4 =(encounter.nextInt(speed));
if (try1 == 0 || try2 == 0 || try3 == 0 || try4 == 0 )
{
battle = true;
}
if(battle == true)
{
battleList.add(m);
}
}
//Creates the matches and runs the battle
private void battle()
{
Random rand = new Random();
if (battleList.size()%2 == 1)
{
int luckyDuck = rand.nextInt(battleList.size());
battleList.remove(luckyDuck);
}
for(int k=0; k<(battleList.size()-1);)
{
int competitor1 = rand.nextInt(battleList.size());
battleList.remove(competitor1);
int competitor2 = rand.nextInt(battleList.size());
battleList.remove(competitor2);
//Competitor 1 strength
int strengthMod = rand.nextInt(((int)Math.ceil(allOrgs[competitor1].strength/5.0))+1);
int strengthSign = rand.nextInt(2);
if (strengthSign == 0)
strengthSign--;
strengthMod *= strengthSign;
int comp1Strength = strengthMod + allOrgs[competitor1].strength;
//Competitor 2 strength
strengthMod = rand.nextInt(((int)Math.ceil(allOrgs[competitor2].strength/5.0))+1);
strengthSign = rand.nextInt(2);
if (strengthSign == 0)
strengthSign--;
strengthMod *= strengthSign;
int comp2Strength = strengthMod + allOrgs[competitor2].strength;
//Fight!
if (comp1Strength>comp2Strength)
{
allOrgs[competitor1].life ++;
allOrgs[competitor2].life --;
}
else if (comp2Strength>comp1Strength)
{
allOrgs[competitor2].life ++;
allOrgs[competitor1].life --;
}
if (allOrgs[competitor1].life == 0)
{
allOrgs[competitor1].alive = false;
deathCount++;
}
if (allOrgs[competitor2].life == 0)
{
allOrgs[competitor2].alive = false;
deathCount ++ ;
}
}
}
//New organisms
private void reproduction()
{
//System.out.println("Number of deaths: " + deathCount + "\n");
if (deathCount>=(allOrgs.length-2))
{
done = true;
return;
}
ArrayList<Organism> tempOrgs = new ArrayList<Organism>();
Random chooser = new Random();
int count = 0;
while(true)
{
int partner1 = 0;
int partner2 = 0;
boolean partnerIsAlive = false;
boolean unluckyDuck = false;
//choose partner1
while (partnerIsAlive == false)
{
partner1 = chooser.nextInt(allOrgs.length);
if (allOrgs[partner1] != null)
{
if (allOrgs[partner1].alive == true)
{
partnerIsAlive = true;
}
}
}
count++;
//System.out.println("Count 2: " + count);
partnerIsAlive = false;
//choose partner2
while (partnerIsAlive == false)
{
if (count+deathCount == (allOrgs.length))
{
unluckyDuck=true;
break;
}
partner2 = chooser.nextInt(allOrgs.length);
if (allOrgs[partner2] != null)
{
if (allOrgs[partner2].alive == true)
{
partnerIsAlive = true;
}
}
}
if (unluckyDuck == false)
count++;
//System.out.println("count 2: " + count);
if (unluckyDuck == false)
{
int numOfChildren = (chooser.nextInt(4)+1);
for (int d=0; d<numOfChildren; d++)
{
tempOrgs.add(new Organism(allOrgs[partner1].speed, allOrgs[partner2].speed, allOrgs[partner1].strength, allOrgs[partner2].strength ));
}
allOrgs[partner1] = null;
allOrgs[partner2] = null;
}
if (count+deathCount == (allOrgs.length))
{
Arrays.fill(allOrgs, null);
allOrgs = tempOrgs.toArray(new Organism[tempOrgs.size()-1]);
break;
}
//System.out.println(count);
}
}
}
Main method:
public class Runner {
public static void main(String[] args) throws InterruptedException {
Simulator sim = new Simulator();
int lifeSpan = 20;
sim.start(lifeSpan);
sim.graphOrgs();
}
}
Organism class:
import java.util.Random;
public class Organism {
static Random traitGenerator = new Random();
int life;
int speed;
int strength;
boolean alive;
Organism()
{
speed = (traitGenerator.nextInt(49)+1);
strength = (50-speed);
life = 5;
alive = true;
}
Organism(int strength1, int strength2, int speed1, int speed2)
{
Random gen = new Random();
int speedMod = gen.nextInt(((int)Math.ceil((speed1+speed2)/10.0))+1);
int speedSign = gen.nextInt(2);
if (speedSign == 0)
speedSign--;
speedMod *= speedSign;
//System.out.println(speedMod);
int strengthMod = gen.nextInt(((int)Math.ceil((strength1+strength2)/10.0))+1);
int strengthSign = gen.nextInt(2);
if (strengthSign == 0)
strengthSign--;
strengthMod *= strengthSign;
//System.out.println(strengthMod);
strength = (((int)((strength1+strength2)/2.0))+ strengthMod);
speed = (((int)((speed1+speed2)/2.0))+ speedMod);
alive = true;
life = 5;
}
}
The problem lies in the graphOrgs class when I try to print to check if it is working in preparation for graphing the results. This is when it returns the error. When I try placing the print code in other places in the Simulator class the same thing occurs, a null pointer error. This happens even if it is just after the for loop where the element has been established.
You have code that sets to null elements in your allOrgs array.
allOrgs[partner1] = null;
allOrgs[partner2] = null;
Your orgGenerations list contains the same allOrgs instance multiple times.
Therefore, when you write allOrgs[partner1] = null, the partner1'th element becomes null in all the list elements of orgGenerations, which is why the print method fails.
You should create a copy of the array (you can use Arrays.copy) each time you add a new generation to the list (and consider also creating copies of the Organism instances, if you want each generation to record the past state of the Organisms and not their final state).

Categories