So basically I'm trying to test to see if my hand of 5 cards has a pair in it (two cards have the same value (1-9)) and I'm getting an unknown error and this is my code
Error:
java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86)...
Code:
public static boolean hasPair(Card[] cards) {
Deck theDeck = new Deck();
cards = theDeck.deal(5);
int k=0;
for (int atPos = 0; atPos<5; atPos++){
for (int atPos2 = atPos+1; atPos2<5; atPos2++){
if(cards[atPos].getValue() == cards[atPos2].getValue()){
k++;
}
}
}
if (k==2){
return true;
}
else {
return false;
}
JUnit that is failing
#Test
public void testExampleTest_SinglePairTest() {
Card[] testHand = new Card[5];
testHand[0] = new Card(1,1);
testHand[1] = new Card(2,1);
testHand[2] = new Card(2,1);
testHand[3] = new Card(4,1);
testHand[4] = new Card(5,1);
assertTrue(HandEvaluatorBBXP.hasPair(testHand));
It seems like there's no point in passing in Card[] cards as an argument to the hasPair method because you immediately set the variable to whatever deck.deal() returns. So the incoming value is never used.
This could well be the reason your test is failing, because your test data what the hasPair method is actually using to evaluate the test with.
Related
I am creating a Dice Game as a project in IntelliJ with Java and right now I have a method in the Game class, where I want to reset the Dice Cup in the beginning of the game. Therefore, I have created a method called resetDiceCup() and a method called startGame().
Right now, I am getting the error "Identifier expected" in startGame() and I think it has something to do with the parameters I use in resetDiceCup().
Is there any way to fix this?
public class Game{
public void startGame() {
resetDiceCup( int doublets, int numOfDoublets);
System.out.println("Willkommen zu diesem Würfelspiel. Los geht´s!");
String[] playerNames = Print.multipleAnswers("Please insert the name of a player.");
player = new Player[playerNames.length];
for (int i = 0; i < playerNames.length; i++)
{
player[i] = new Player(playerNames[i]);
}
}
//to "refresh" the DiceCup before every new game
public void resetDiceCup(int doublets, int numOfDoublets) {
int[] result = new int[numOfDices]; //amout of dices in the game as chosen by players
sum = 0;
doublets = 0;
numOfDoublets = 0;
sumWithDoublets = 0;
}
}
Lets say I have a public class called GameBoard that will be a two dimensional array with 4 rows and 5 columns. The spaces in the array are filed with String values from 1 to 20. A card will be drawn that has a name (King of Spades for example) . If the user inputs 15 I will store it in a String variable called userLocation. What would be the most efficient way to create a method that takes the input location and updates the array with the name of the Card? Would a for loop be most efficient?
public GameBoard() {
square = new String[4][5];
square[0][0] = new String("1");
square[0][1] = new String("2");
square[0][2] = new String("3");
square[0][3] = new String("4");
square[0][4] = new String("5");
square[1][0] = new String("6");
square[1][1] = new String("7");
square[1][2] = new String("8");
square[1][3] = new String("9");
square[1][4] = new String("10");
square[2][1] = new String("11");
square[2][2] = new String("12");
square[2][3] = new String("13");
square[3][1] = new String("14");
square[3][2] = new String("15");
square[3][3] = new String("16");
square[2][0] = new String(17);
square[3][0] = new String(18);
square[2][4] = new String(19);
square[3][4] = new String(20);
}
My preferred method as of now would look something like this but it gives me the error code "type mismatch:cannot convert string to boolean" under userLocation = board[i][j]
public String[][] updateBoard(String userLocation, Card card, String[][] board) {
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 5; j++)
{
if(userLocation = board[i][j]) {
board[i][j] = card.name;
}
}
}
return board;
}
So the reason it will not compile is your = does not return a boolean expression. == would, but it's still not what you want, since you want to check if the String contents are the same, not if they're the same object, so use .equals.
But, no, I think you don't want to depend on strings to identify locations. What if you want to replace a card? And why look through everything when you need not?
Rather if i is some number between 1 and 20, identify the corresponding spot in the array by square[(i-1)/5][(i-1)%5]
That should bypass the issue you are having with matching strings.
So for example, your constructor becomes:
public GameBoard() {
square = new String[4][5];
for (int i=1; i<=20;i++){
square[(i-1)/5][(i-1)%5]=""+i;//initialize with 1 to 20 if you like
}
and userLocation is an int.
public static ArrayList<Hand> getPossibleHands(Hand h) {
ArrayList<Hand> allPossible = new ArrayList<Hand>();
addNext(allPossible, h);
return allPossible;
}
public static void addNext(ArrayList<Hand> poss, Hand h) {
if (h.cards.size() == 5)
poss.add(h);
else
for (int i = 0; i < 52; i++) {
Card c = Card.makeCard(i);
if (!h.contains(c))
h.add(c);
addNext(poss,h);
}
}
The code above is supposed to essentially take an incomplete poker board (anywhere from 0-4 cards) and return all possible complete boards (5 cards). The logic that I feel like it should follow is as follows: recurse through every combination of adding valid (not on the board already) cards until the size of the board is equal to 5 in which case it will add the board to the list and skip over the rest of the function.
However upon using a print statement at the beginning of the function I see that hand sizes of greater than 5 or being created. Since the first part of the function should catch all hands at 5 and terminate it there, I don't see how the code executes at the rest of the function.
Your class should receive stack overflow with an empty hand.
You send new Card(0) to the hand. This is added.
Then you call add next again - and the 'for' starts from 0 again. Checks adds 1. Then starts from 0 - it is there, does not add anything and starts over. Where it starts from 0. Does not do anything. Starts from 0. Ad infinum -> StackOverFlow.
You also need to reset to previous state of the hand every time you finish with 5 cards and backtrack.
If you want a recursive solution you can try :
private static ArrayList<Hand> getPossibleHands(Hand h) {
ArrayList<Integer> except;
if (h.cards == null) except = new ArrayList<>();
else
except = h.cards.stream().map(c -> (c.getCard())).collect(Collectors.toCollection(ArrayList::new));
ArrayList<Hand> allPossible = new ArrayList<>();
addNext(allPossible, h, except);
return allPossible;
}
private static void addNext(ArrayList<Hand> poss, Hand h, ArrayList<Integer> except) {
//assuming hands 0-4 - we don't need to check on entry, only when we add
Hand localHand = h.copy();
for (int i = 0; i < 52; i++) {
if (except.contains(i)) continue;
Card c = Card.makeCard(i);
if (!localHand.contains(c)) {
addNext(poss, localHand.copy(), copyExcept(except, i));
localHand.add(c);
if (localHand.cards.size() == 5) {
poss.add(localHand);
break;
}
}
}
}
private static ArrayList<Integer> copyExcept(ArrayList<Integer> except, int i) {
ArrayList<Integer> clonedExcept = new ArrayList<>(except);
clonedExcept.add(i);
return clonedExcept;
}
import java.util.ArrayList;
public class Hand {
ArrayList<Card> cards = new ArrayList<>();
public boolean contains(Card c) {
for (Card card : cards) {
if (card.getCard() == c.getCard())
return true;
}
return false;
}
public void add(Card c) {
cards.add(c);
}
Hand copy() {
Hand temp = new Hand();
for (Card c : cards) {
temp.add(new Card(c.getCard()));
}
return temp;
}
}
class Card {
private int card;
public Card(int card) {
this.card = card;
}
public static Card makeCard(int i) {
return new Card(i);
}
public int getCard() {
return card;
}
}
Initially, h is (presumably) empty. So addNext will loop through all possible cards, and since none of them are in the hand, add each card to the hand, regardless of how many cards are currently in the hand.
It looks to me like your for loop is eventually adding whole deck to hand.
In your loop you run 52 iterations. On each iteration you (conditionally) add a card to the hand and then you call your function recursively. But after that recursive call has returned, you go to the next iteration and again add a card to the hand.
So that '5 card restriction' does not restrict anything here.
I'm making a card game and I'm getting the error that n must be positive. I did some research and it means that (cards.size) is equal or less than 0 I believe, but I don't understand how to make my code work, something must be wrong.
Code:
public class Deck
{
public ArrayList <Card> cards;
Deck()
{
cards = new ArrayList<>();
for (int a = 0; a < 52; a++)
{
cards.add(new Card(a));
}
}
public Card PlayerCardDraw ()
{
Random generator = new Random ();
int index = generator.nextInt (cards.size ());
return cards.remove (index);
}
How can I fix my array list so i don't get this error? it relates back to Card so I'll post that code too, I know something isn't right in my Card class but I don't know if that's the problem or not.
public class Card
{
int c = 52;
int cardpath[] = new int[c];
Card ()
{
}
public Card(int c)
{
this.c = c;
}
public int getCardPath()
{
return cardpath[c];
}
}
Error message:
java.util.Random.nextInt(Unknown Source) at Cards.Deck.PlayerCardDraw(Deck.java:21)
line 21 is int index = generator.nextInt (cards.size ());
EDIT: I did what Nankumar Tekale said and it's saying what you guys predicted: It's drawing more than 52 cards. What I don't understand is the error is popping up at
for (int i = 0 ; i < 4 ; i++)
{
C = deck.P1CardDraw ();
card [cardNum].draw (i*75+100, 400); //Error line
cardNum++;
}
my P1CardDraw() class
public ArrayList < Card > p1Hand;
public ArrayList < Card > P1CardDraw ()
{
p1Hand = new ArrayList < > ();
p1Hand.add (PlayerCardDraw ());
return p1Hand;
}
Well looking at your Deck class, You have intialized cards in constructor so there should not be such an exception(as cards size is 52).
But one thing is possible to get an exception which is cards is declared public and you may have modified it outside class directly. So arraylist cards is of size 0 and you get IllegalArgumentException exception for your method Random.nextInt(int).
Make cards private.
If you have withdrawn all cards then size of arraylist would become 0, which may cause an exception. Add check for 0 as :
public Card PlayerCardDraw ()
{
Random generator = new Random ();
if(cards.size() > 0) {
int index = generator.nextInt (cards.size());
return cards.remove (index);
} else {
return null;
}
}
You might be calling PlayerCardDraw method even before a card is added to the list. Means cards.size() must be returning a zero. So it is illegal to generate a random int using a zero seed. Hence is the error.
Refer to:
Random.nextInt( n )
My guess is that you keep calling PlayerCardDraw more than 52 times.
Each time, a card is removed from the deck, so the 53:d time, you are trying to draw a card from an empty deck (ie. a deck of size 0) which causes generator.nextInt(cards.size()); to throw an exception.
However it is not easy to say.
When you are having problems like this and want help, always show the full stack trace of the exception. Also, since we cannot see line numbers in code on stackoverflow, be sure to indicate in the code exactly at which line the exception occurs.
I'm trying to learn Java and basically my approach has been to take the procedural style I learned with python, and apply it to Java. So I never use classes and just put everything in a single class with many methods(which I just use as python functions). I think I've hit a problem, and need to bite the bullet and use classes, but I'm having trouble wrapping my head around how to do it.
To simplify my problem(ignore the poor design- it's just to illustrate the point), I have a program that takes a list and within a for loop does some math on each item(in this case adds 1 to the value of the list). I only want it to do work on 2 items on the list and then stop(in this example it's the first 2 items but in my real program it could be anywhere in the list). Here's the working code that is similar to how I'm already doing it:
No Classes:
public class LearningClasses {
public static void main(String[] args) {
int[] list = new int[]{1,2,3,4,5,6,7,8,9,10};
int[] data_list = new int[list.length];
for (int current_location = 0; current_location<list.length;current_location++) {
for (int i =0; i<100; i++){
if (check_size(data_list) == false ) {
break;
}
data_list[current_location] = (list[current_location]+1);
}
}
//its done now lets print the results
for (Integer item : data_list) {
System.out.println(item);
}
}
private static boolean check_size(int[] data_list) {
// TODO Auto-generated method stub
int count = 0;
for (int item : data_list) {
if (item != 0) {
count++;
if (count>=2) {
break;
}
}
}
if (count>=2) {
return false;
} else {
return true;
}
}
}
The problem with this code is although it works it's inefficient because it calculates the count on every iteration of the second for loop. In my program I cannot put anything above the first for loop but I can put anything below it, so I thought instead of doing the count every time maybe I could use a class to somehow maintain state and just increment the number as oppose to recalculating every time.
With classes:
public class LearningClassesCounter {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] list = new int[]{1,2,3,4,5,6,7,8,9,10};
int[] data_list = new int[list.length];
for (int current_location = 0; current_location<list.length;current_location++) {
//can only put commands in here. Nothing above.
Counter checker = new Counter(data_list);
System.out.println(checker.check_data());
for (int i =0; i<100; i++){
data_list[current_location] = (list[current_location]+1);
}
}
//its done now lets print the results
for (Integer item : data_list) {
System.out.println(item);
}
}
}
class Counter {
private int count; // current value
private boolean continue_or_not;
private int[] data_list;
// create a new counter with the given parameters
public Counter(int[] data_list) {
data_list = this.data_list;
count = 0;
continue_or_not = true;
}
public boolean check_data() {
// TODO Auto-generated method stub
int count = 0;
for (int item : data_list) {
if (item != 0) {
count++;
if (count>=3) {
break;
}
}
}
if (count>=3) {
return false;
} else {
return true;
}
}
// increment the counter by 1
public void increment() {
count++;
}
// return the current count
public int value() {
return count;
}
}
This doesn't work because it thinks the data_list is a null pointer(I know I'm declaring it null, but if I make it private int[] data_list = data_list it doesn't compile either). My ultimate goal is to have some kind of controls, in this case its limiting it to 2 items but I want to also add other limits like total value of al items cannot exceed X or cannot be lower than X and want to save CPU power by not having to do full calculations every time. So I think I need to be able to increment the values and then need to check that those increments haven't exceeded thresholds.
Can anyone help me understand what I'm doing wrong? Am I only wrong with syntax; or am I designing this wrong?
//can only put commands in here. Nothing above.
Counter checker = new Counter(data_list);
System.out.println(checker.check_data());
When you are calling checker.check_data(), its trying to parse through the data_list, but its empty. So, it throws a NullPointerException. The data_list is empty because inside your constructor, you may need to initialize like this this.data_list = data_list instead of data_list = this.data_list (here this.data_list has no reference so NULL)
If you avoid that call, the output will be 2,3,4,5,6,7,8,9,10,11.