Cannot get card/deck object program to work - java

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

Related

Can't seem to get my method (in Java) to compile correctly

I am trying to figure out exactly what is wrong with my winorTie method in this little tictacttoe game I am trying to create. Would anyone be able to help? Thanks
package tictactoegame;
/**
*
* #author Douglas Boulden
*/
public class tictactoegame {
static int [][] gameboard;
static final int EMPTY = 0;
static final int NOUGHT = -1;
static final int CROSS = 1;
static void set (int val, int row) throws IllegalArgumentException {
int col = 0;
if (gameboard[row][col] == EMPTY)
gameboard[row][col] = val;
else throw new
IllegalArgumentException("Player already there!");
}
static void displayBoard () {
for (int[] gameboard1 : gameboard) {
System.out.print("|");
for (int c = 0; c < gameboard1.length; c++) {
switch (gameboard1[c]) {
case NOUGHT:
System.out.print("0");
break;
case CROSS:
System.out.print("X");
break;
default: //Empty
System.out.print(" ");
}
System.out.print("|");
}
System.out.println("\n------\n");
}
}
static void createBoard(int rows, int cols) {
gameboard = new int [rows] [cols];
}
static int winOrTie() {
if (gameboard [0][0] == NOUGHT && gameboard [0][-1])
return NOUGHT;
} else if (gameboard [0][0] == && CROSS) [0][1] {
return CROSS;
} else if (gameboard [0][0]== && " "()) [0][0] {
return 0;
} else {
return false;
}
/**
* #param args the command line arguments
*/ /**
* #param args the command line arguments
*/
public static void main(String[] args) {
createBoard(3,3);
int turn = 0;
int playerVal;
int outcome;
java.util.Scanner scan = new
java.util.Scanner(System.in);
do {
displayBoard();
playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
if (playerVal == NOUGHT) {
System.out.println ("\n-0's turn-");
} else {
System.out.println("\n-X's turn-");
}
System.out.print("Enter row and Column:");
try {
set(playerVal, scan.nextInt());
} catch (IllegalArgumentException ex)
{System.err.println(ex);}
turn ++;
outcome = winOrTie();
} while ( outcome == -2 );
displayBoard();
switch (outcome) {
case NOUGHT:
System.out.println("0 wins!");
break;
case CROSS:
System.out.println("X wins!");
break;
case 0:
System.out.println("Tie.");
break;
}
}
}
Some of this was mentioned in the comments, but these conditions fundamentally don't make sense:
if (gameboard [0][0] == NOUGHT && gameboard [0][-1])
return NOUGHT;
} else if (gameboard [0][0] == && CROSS) [0][1] {
return CROSS;
} else if (gameboard [0][0]== && " "()) [0][0] {
return 0;
For example, what do you think that if (gameboard [0][0] == && CROSS) [0][1] is supposed to do? What exactly is " "() supposed to be? And what do you think that == && does? It's difficult to know exactly what you were actually trying to achieve here.
Also, consider gameboard [0][-1]. There are two problems here. First, you do realize that -1 isn't actually a valid array index in Java, right? (That's allowable in Python, but not Java). Also, gameboard [0][-1] is an integer, not a bool, so && gameboard [0][-1] doesn't make sense. If you have something like A && B, both A and B must evaluate to some kind of boolean value (i.e. true or false).
Also, I'd encourage you not to do indentation like you have here. I'd recommend putting each "else if" on its own line.

I don't understand how to pass variables to function in my code(Java)

I'm relatively new to programming so be patient with me. I can't figure out how to pass the variables p12c, p13c etc. to the method findWinner. I ran into this problem quite often, so if you could provide some help I will appreciate it. Also, any tips on how to simplify any code is welcome too. Thank you.
import java.util.*;
public class StarterPoker {
public static void main(String[] args)
{
do
{
System.out.print("Welcome to Starter Poker!\n\n" +
"To play, two players will be dealt\n"+
"five cards per hand and the hand with\n"+
"the highest cards will win. Then you\n"+
"will be prompted to continue or quit.\n");
dealHand();
findWinner();
}
while(playAgain()=='Y');
}
public static void dealHand()
{
int p12c=0, p13c=0, p14c=0, p15c=0, p16c=0, p17c=0, p18c=0, p19c=0, p1tc=0, p1jc=0, p1qc=0, p1kc=0, p1ac=0;
int p22c=0, p23c=0, p24c=0, p25c=0, p26c=0, p27c=0, p28c=0, p29c=0, p2tc=0, p2jc=0, p2qc=0, p2kc=0, p2ac=0;
int [][] deck = new int[13][4];
int [][] player1hand = new int[5][2];
int [][] player2hand = new int[5][2];
//the p1 = player1, then it has each card value, followed by c for count, for example: p12c = player1 2s count.
String[] suits = {"Spades","Diamonds","Hearts","Clubs"};
String[] cards = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
Random randGen = new Random();
do
{
String hand1 = "";
//dealing hand 1
for(int dex = 0; dex < player1hand.length; dex++)
{
boolean goodCard = false;
while(!goodCard)
{
int suit = randGen.nextInt(4);
int card = randGen.nextInt(13);
if( deck[card][suit] == 0)
{
goodCard = true;
deck[card][suit] = 1;
player1hand[dex][0] = suit;
player1hand[dex][1] = card;
hand1 += "\n "+cards[card]+" of "+suits[suit]+"";
if(card == 0) p12c=p12c+1;
if(card == 1) p13c=p13c+1;
if(card == 2) p14c=p14c+1;
if(card == 3) p15c=p15c+1;
if(card == 4) p16c=p16c+1;
if(card == 5) p17c=p17c+1;
if(card == 6) p18c=p18c+1;
if(card == 7) p19c=p19c+1;
if(card == 8) p1tc=p1tc+1;
if(card == 9) p1jc=p1jc+1;
if(card == 10) p1qc=p1qc+1;
if(card == 11) p1kc=p1kc+1;
if(card == 12) p1ac=p1ac+1;
}
}
}
String hand2 = "";
//dealing hand 1
for(int dex = 0; dex < player2hand.length; dex++)
{
boolean goodCard = false;
while(!goodCard)
{
int suit = randGen.nextInt(4);
int card = randGen.nextInt(13);
if( deck[card][suit] == 0)
{
goodCard = true;
deck[card][suit] = 1;
player2hand[dex][0] = suit;
player2hand[dex][1] = card;
hand2 += "\n "+cards[card]+" of "+suits[suit]+"";
if(card == 0) p22c=p22c+1;
if(card == 1) p23c=p23c+1;
if(card == 2) p24c=p24c+1;
if(card == 3) p25c=p25c+1;
if(card == 4) p26c=p26c+1;
if(card == 5) p27c=p27c+1;
if(card == 6) p28c=p28c+1;
if(card == 7) p29c=p29c+1;
if(card == 8) p2tc=p2tc+1;
if(card == 9) p2jc=p2jc+1;
if(card == 10) p2qc=p2qc+1;
if(card == 11) p2kc=p2kc+1;
if(card == 12) p2ac=p2ac+1;
}
}
}
System.out.print("\nDo you want to deal two hands?\nenter 'Y' to continue or anything else to quit: ");
Scanner input = new Scanner(System.in);
char cont = input.next().charAt(0);
if(cont != 'Y')
{
System.out.println("Program terminated!");
System.exit(0);
}
System.out.printf("Player 1's hand is %s\n", hand1);
System.out.printf("Player 2's hand is %s\n", hand2);
} while(true);
}
public static void findWinner()
{
int p1s = 0, p2s = 0;
int p1p = 0, p2p = 0;
int p1t = 0, p2t = 0;
int p1f = 0, p2f = 0;
if (p12c > 0)
{
if(p12c >1)
{
if(p12c > 2)
{
if(p12c > 3)
{
p1f = 1;
}
else
{
p1t = 1;
}
}
else
{
p1p = p1p+1;
}
}
else
{
p1s = p1s+1;
}
}
if (p13c > 0)
{
if(p13c >1)
{
if(p13c > 2)
{
if(p13c > 3)
{
p1f = 1;
}
else
{
p1t = 1;
}
}
else
{
p1p = p1p+1;
}
}
else
{
p1s = p1s+1;
}
}
System.out.printf("p1s = %d",p1s);
//System.out.printf("Player %d wins ", winner);
//System.out.print("because %d beats %d\n",winCard, loseCard);
}
public static char playAgain(){
System.out.println("Would you like to play again?");
Scanner input = new Scanner(System.in);
char cont = input.next().charAt(0);
return cont;
}
}
To pass variables, you first have to have the variables being passed into the parameter:
public static void findWinner(int a, int b) //Or how many ever variable you need
{
...
}
Then when you call that method, you need to pass in those variables:
int p12c = 1;
int p13c = 2;
findWinner(p12c, p13c);
On a side note, when you just want to increment the variable by one, you can do either of the following:
var x = 1;
x++; // x = 2
x += 1; // x = 3
Just put that variables that you need in to the brackets and remember that it is an local variables. In your method "findWinner" all that you need is hust wrote public static void findWinner(int p12c, int p13c) and all be Ok ;)

Running Java Poker Game

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?

Algorithm to avoid duplicate Cards in Card Game

I made a rookie mistake, and am trying to figure out a fix without recoding the entire program. Here's the scenario:
I am creating 6 cards from a 52 card deck. The cards have 2 fields: int rank and char suit. I am pulling a random rank and suit and creating a new and unnamed instance of each card. This is not accounting for duplicates.
So I am creating the 6 cards successfully, but I do not want the duplicates. This may be a newbish question, but we have all been there :) Thanks for your help!
public void displayCards(List<ImageView> disp) {
int cardNumber = 0;
for (ImageView c : disp) {
cardNumber++;
rank = rand(13) + 2;
int i = rand(4);
if (i == 0) {
suit = 's';
} else if (i == 1) {
suit = 'h';
} else if (i == 2) {
suit = 'd';
} else {
suit = 'c';
}
if (cardNumber == 1) {
dc1 = new Card(rank, suit);
} else if (cardNumber == 2) {
dc2 = new Card(rank, suit);
} else if (cardNumber == 3) {
dc3 = new Card(rank, suit);
} else if (cardNumber == 4) {
pc1 = new Card(rank, suit);
} else if (cardNumber == 5) {
pc2 = new Card(rank, suit);
} else {
pc3 = new Card(rank, suit);
}}
Option 1: Create a full deck (e.g., in a List) of 52 cards using two (nested) loops (all suits and all ranks). Shuffle (using Collections.shuffle()) the deck and take the six cards.
Option 2: Just create the number of cards you want (like you are attempting to do). Here, you will want to put each card into a Set<Card> until the set has the desired number of cards. Essentially, the duplicates will disappear in the set. For this to work, you need to implement equals() and hashcode() for the Card class.
Create a deck of all 52 cards and extract the cards from the deck. Also you should use enumerators to represent ranks and suits.
See my answer in What variables do I have to compare in this java code? for example code of cards and deck.
First, override equals() and hashCode() in your Card class if you haven't done it yet.
Second, add already created cards to a Set (which guarantees that it won't contain duplicates)
Modify your code for something like this:
public void displayCards(List<ImageView> disp) {
int cardNumber = 0;
Set<Card> myCards = new HashMap<Card>();
for (ImageView c : disp) {
cardNumber++;
Card newCard;
do {
rank = rand(13) + 2;
int i = rand(4);
if (i == 0) {
suit = 's';
} else if (i == 1) {
suit = 'h';
} else if (i == 2) {
suit = 'd';
} else {
suit = 'c';
}
newCard = new Card(rank, suit);
} while(myCards.contains(newCard));
myCards.add(newCard);
if (cardNumber == 1) {
dc1 = newCard;
} else if (cardNumber == 2) {
dc2 = newCard;
} else if (cardNumber == 3) {
dc3 = newCard;
} else if (cardNumber == 4) {
pc1 = newCard;
} else if (cardNumber == 5) {
pc2 = newCard;
} else {
pc3 = newCard;
}
}

How to create a hand class for BlackJack java [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Can someone please please help. I have created a card class and Deck class but I just dont know how to create the Hand class.
This is my Card class below.
package blackjack;
public class Card {
private int rank;
private int suit;
#Override
public String tostring() {
String result = "";
if (rank == 1) result = "Ace";
if (rank == 2) result = "Two";
if (rank == 3) result = "Three";
if (rank == 4) result = "Four";
if (rank == 5) result = "Five";
if (rank == 6) result = "Six";
if (rank == 7) result = "Seven";
if (rank == 8) result = "Eight";
if (rank == 9) result = "Nine";
if (rank == 10) result = "Ten";
if (rank == 11) result = "Jack";
if (rank == 12) result = "Queen";
if (rank == 13) result = "King";
if (suit == 1) result = result + " of Clubs ";
if (suit == 2) result = result + " of Diamonds ";
if (suit == 3) result = result + " of Hearts ";
if (suit == 4) result = result + " of Spades ";
return result;
}
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
}
This is my Deck Class
package blackjack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Deck {
private Random shuffles = new Random();
public ArrayList<Card> Deck = new ArrayList<Card>();
Random rand = new Random();
// private int numberOfCards = 52;
public Deck() {
for (int ranks = 1; ranks <= 13; ranks++) {
for (int suits =1 ; suits <= 4; suits++) {
Deck.add(new Card(ranks, suits));
//System.out.println(Deck.get(ranks) + "" +Deck.get(suits));
}
}
shuffle();
for (int i = 1; i < Deck.size(); i++) {
//int cardPosition2 = shuffles.nextInt(52);
//shuffle.nextInt(Deck.get(i);
System.out.println(Deck.get(i));
//System.out.println(cardPosition2);
//i++;
}
}
public void shuffle() {
Collections.shuffle(Deck);
}
public Card DrawCard() {
int cardPosition = shuffles.nextInt(Deck.size());
return Deck.remove(cardPosition);
}
public int TotalCardsLeft() {
return Deck.size();
}
public Card dealCard() {
// Deals one card from the deck and returns it.
if (Deck.size() == 52) {
shuffle();
}
Card temp;
temp = Deck.get(0);
Deck.remove(0);
return temp;
}
public Card getCard(int i) {
return Deck.get(i);
}
public Card remove(int i) {
Card remo = Deck.get(i);
Deck.remove(i);
return remo;
}
}
If you can help me with my Hand call I would really appreciate it.
Create Class, which will contain for example ArrayList hand.
public class Hand {
private ArrayList<Card> hand
.
.
Then make methods to add(draw) Card :
public void addCard(Card c){
this.hand.add(c);
}
You will also need method to check whether card of specific type is present:
public boolean checkPresence(int rank){
for(int i=0;i<hand.size();i++){
//note you will need to implement getters to the Card class first
if (hand.get(i).getRank==rank)
return true;
}
return false;
}
Similarly for suit.
Of course you will most probably need other methods, but I am sure you can figure them out yourself.

Categories