I'm trying to make a card game, and have my card class and my deck class sort of ready, it compiles ok, but when I try to run deck's method makeDeckFull, i get the output: invalidnumberinvalidnumber...
when I use the showDeck method I then see this instead of "hearts", 1
Cards#597f13c5 (i do not know what it means, or how to fix it)
Any help would be kindly appreciated: code below.
Deck Class:
import java.util.ArrayList;
public class Deck
{
private ArrayList<Cards> deck;
private int index;
public Deck()
{
deck = new ArrayList<Cards>();
}
public void makeDeckFull()
{
Cards h1 = new Cards("Hearts", 1);
Cards h2 = new Cards("Hearts", 2);
Cards h3 = new Cards("Hearts", 3);
deck.add(h1);
index ++;
deck.add(h2);
index ++;
deck.add(h3);
index ++;
//Rest of these is left out to conserve space
}
public void showDeck()
{
System.out.println(deck);
}
Card class:
public class Cards
{
private String HEARTS = "Hearts";
private String CLUBS = "Clubs";
private String DIAMONDS = "Diamonds";
private String SPADES = "Spades";
public int number;
public String suit;
public Cards()
{
suit = "unknown suit";
number = 0;
}
public Cards(String suit, int number)
{
setSuit(suit);
setNumber(number);
}
public void setCard(String suit, int number2)
{
setSuit(suit);
setNumber(number2);
}
public void setSuit(String newSuit)
{
if(
(newSuit.equalsIgnoreCase(HEARTS)) ||
(newSuit.equalsIgnoreCase(DIAMONDS)) ||
(newSuit.equalsIgnoreCase(CLUBS)) ||
(newSuit.equalsIgnoreCase(SPADES)))
{
suit = newSuit;
}
else
{
newSuit = "invalid";
System.out.print("Invalid");
}
}
public int getNumber()
{
return number;
}
public String getSuit()
{
return suit;
}
public void setNumber(int newNumber)
{
if(newNumber >0 && newNumber <=10)
{
number = newNumber;
}
else
{
number = 0;
System.out.print("invalid number");
}
}
}
1) You need to override toString() in the Cards class. As is, you are printing out the reference of the object(the gibberish) instead of the "data." You should also override the toString() method of Deck to only print out the list.
2) I'm stepping through your code snippet of makeDeckFull(), and it seems to work fine. Are you sure those three inserts are where you are getting the invalid print statements?
Related
I am creating a dice rolling application with java. I have a "Die" class that rolls a single die, and a "Dice" class that uses multiple instance variable of "die". However, it only returns 0 for my values. The Die class on its own works and will roll a random number, but I can not figure out how to get multiple rolls in my "Dice" class. Any help is appreciated.
Dice Class
public class Dice {
Die die1=new Die();
Die die2=new Die();
private int die1Value;
private int die2Value;
private int sum;
public Dice() {
die1Value=0;
die2Value=0;
}
public int getDie1Value() {
return die1Value;
}
public int getDie2Value() {
return die2Value;
}
public int getSum() {
return sum;
}
public void roll() {
die1Value=die1.getValue();
die2Value=die2.getValue();
sum=die1Value+die2Value;
}
public void printRoll() {
System.out.println("Die 1: "+die1Value);
System.out.println("Die 2: "+die2Value);
System.out.println("Total: "+sum);
if (sum==7) {
System.out.println("Craps!");
} else if (die1Value==1 && die2Value==1) {
System.out.println("Snake Eyes!");
} else if (die1Value==6 && die2Value==6) {
System.out.println("Box Cars!");
} else {
System.out.println();
}
}
}
Die Class
package a3.ben;
public class Die {
private int value;
public Die() {
}
public void roll() {
value=(int) (Math.random()*6)+1;
}
public int getValue() {
return value;
}
}
You never call die.roll. Try changing the roll method in Dice to include rolling both dice before getting their values.
public void roll() {
die1.roll(); // change the value of both dice
die2.roll();
die1Value = die1.getValue();
die2Value = die2.getValue();
sum = die1Value + die2Value;
}
Also added some spaces around operators like = and + for readability
I have a Card Class which contains an enum for both suit and rank of the cards, although I've been told by my lecturers that the value of the ACE rank must be 11, but then in the hand class when I create and store the list of potential hand values I'm not sure how to do it.
For example if I had a hand of <10 Diamonds, Ace Spades, Ace Clubs> it has total value of 12, 22 or 32.
And if I had a more complex hand with more cards and more aces then it makes it quite difficult for me to think of an algorithm I could use to store these values.
Here is the code for my Card class:
public class Card implements Comparable, Serializable{
static final long serialVersionUID = 100;
private final Rank rank;
private final Suit suit;
public static enum Suit{CLUBS, DIAMONDS, HEARTS, SPADES}
public static enum Rank{
TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6),SEVEN(7), EIGHT(8),
NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);
final int cardValue;
/**
* Creating an array of the rank enums used
* to get the next rank in the list
*/
private final static Rank[] cardRanks = values();
Rank(int x) {
cardValue = x;
}
public int getValue(){return cardValue;}
//Getting the next enum in the list
public Rank getNext(){return cardRanks[(this.ordinal()+1)
%cardRanks.length];}
}
public Card(Rank r, Suit s) {
this.rank = r;
this.suit = s;
}
#Override
public int compareTo(Object card) {
if(this.getRank().getValue()==
((Card)card).getRank().getValue()) {
//If there ranks are the same sort them in suit order
return this.getSuit().ordinal()-
((Card)card).getSuit().ordinal();
}else
/**
* If the ranks are different sort
* them in ascending rank order
**/
return (this.getRank().getValue())
-(((Card)card).getRank().getValue());
}
public Rank getRank() {
return this.rank;
}
public Suit getSuit() {
return this.suit;
}
public static int sum(Card a, Card b) {
return (a.getRank().getValue()) + (b.getRank().getValue());
}
public static boolean isBlackjack(Card a, Card b) {
return sum(a, b) == 21;
}
#Override
public String toString() {
String s = this.getRank().name()+ " of "
+ this.getSuit().name();
return s;
}
public static class CompareDescending
implements Comparator<Card> {
#Override
public int compare(Card o1, Card o2) {
if(o1.getRank()==o2.getRank()) {
//If there ranks are the same sort them in suit order
return o1.getSuit().ordinal()-o2.getSuit().ordinal();
}else
//For decending order
return (o2.getRank().getValue())-(o1.getRank().getValue());
}
}
public static class CompareSuit
implements Comparator<Card> {
private final int highestCardValue = 11;
#Override
public int compare(Card o1, Card o2) {
int cardOneValue =
((o1.getSuit().ordinal()*highestCardValue)
+o1.getRank().ordinal());
int cardTwoValue =
((o2.getSuit().ordinal()*highestCardValue)
+o2.getRank().ordinal());
return cardOneValue-cardTwoValue;
}
}
}
And below is my current Hand class
package programming2coursework;
import java.util.ArrayList;
import java.util.Iterator;
public class Hand implements Iterable<Card> {
private ArrayList<Card> hand;
private ArrayList<Integer> handValues = new ArrayList();
private int numOfSpades = 0;
private int numOfHearts = 0;
private int numOfDiamonds = 0;
private int numOfClubs =0;
public Hand() {
hand = new ArrayList();
}
public Hand(Card[] cards) {
hand = new ArrayList();
for (Card card : cards) {
hand.add(card);
switch(card.getSuit()){
case CLUBS:
numOfClubs++;
break;
case DIAMONDS:
numOfDiamonds++;
break;
case HEARTS:
numOfHearts++;
break;
case SPADES:
numOfSpades++;
break;
}
}
}
public Hand(Hand h) {
hand = new ArrayList();
for (Card card : h.hand) {
hand.add(card);
switch(card.getSuit()){
case CLUBS:
numOfClubs++;
break;
case DIAMONDS:
numOfDiamonds++;
break;
case HEARTS:
numOfHearts++;
break;
case SPADES:
numOfSpades++;
break;
}
}
}
public void handValues() {
int numOfAces = 0;
int handValue = 0;
for (Card hand1 : hand) {
if(hand1.getRank() == Card.Rank.ACE)
numOfAces++;
handValue =+ hand1.getRank().getValue();
}
//Store highest possible hand value
handValues.add(handValue);
while(numOfAces != 0) {
//Work out hand values?
numOfAces--;
}
}
#Override
public Iterator<Card> iterator() {
Iterator defaultIterator = hand.iterator();
return defaultIterator;
}
}
Any help would be greatly appreciated.
To generate all counts:
Count the value of non ace cards (k)
Count the number of ace cards (a).
The value of Aces (aValue) ranges from a (all count as 1) to 11*a (all count as 11)
int aValue = a;
while (aValue < 11*a) {
handValues.add(k + aValue);
aValue = aValue + 10; // an 1 becomes an 11,
}
handValues.add(k + aValue); // last case, or if a is 0
This is my code, my questions is with my deal method, how do I get it to inclement the to a different number every time I call it and also how to create a Boolean method. This is my code, my questions is with my deal method, how do I get it to inclement the to a different number every time I call it and also how to create a Boolean method.
package Card;
import java.util.Random;
/**
*
* #author Mr. Pierre
*/
public class Card {
private int SuitRank;
private int CardRank;
private String cardValue;
//My constructor
public Card()
{
SuitRank=1;
CardRank=2;
}
//My deal method
void dealCard()
{
SuitRank++;
Random randomGenerator = new Random();
int SuitRank = randomGenerator.nextInt(4)+1;
CardRank++;
Random randomGenerator1 = new Random();
int CardRank= randomGenerator1.nextInt(13)+2;
}
//My compare method
public int compare(Card otherCard)
{
if (otherCard.getCardRank() > CardRank)
return 1;
if (otherCard.getCardRank() == CardRank)
{
if (otherCard.getSuitRank() > SuitRank)
return 1;
if (otherCard.getSuitRank()< SuitRank)
return -1;
if (otherCard.getSuitRank()==SuitRank)
return 0;
}
if (otherCard.getCardRank() < CardRank)
return -1;
return CardRank;
}
//my Get suitrank method
public int getSuitRank()
{
SuitRank++;
return SuitRank;
}
public String getSuitName ()
{
String SuitName="";
if( SuitRank == 1){
SuitName = "Clubs";
}
else if(SuitRank == 2){
SuitName = "Diamonds";
}
else if(SuitRank == 3){
SuitName = "Hearts";
}
else if(SuitRank == 4){
SuitName = "Spades";
}
return SuitName;
}
public int getCardRank ()
{
return CardRank;
}
public String getCardName ()
{
String CardName="";
if(CardRank==2){
CardName="Duce";
}
else if(CardRank==3){
CardName="Three";
}
else if(CardRank==3){
CardName="Three";
}
else if(CardRank==4){
CardName="Four";
}
else if(CardRank==5){
CardName="Five";
}
else if(CardRank==6){
CardName="Six";
}
else if(CardRank==7){
CardName="Seven";
}
else if(CardRank==8){
CardName="Eight";
}
else if(CardRank==9){
CardName="Nine";
}
else if(CardRank==10){
CardName="Ten";
}
else if(CardRank==11){
CardName="Jack";
}
else if(CardRank==12){
CardName="Queen";
}
else if(CardRank==13){
CardName="King";
}
else if(CardRank==14){
CardName="Ace";
}
return CardName;
}
public String toString()
{
return getCardName()+ " of " +getSuitName();
}
}
int SuitRank = randomGenerator.nextInt(4)+1;
The int means you're creating a local variable instead of modyfying a class member. Also, why are you calling SuitRank++ if you're planning to set SuitRank to a random value right away? Same applies to CardRank.
As for the Boolean method - it's just public Boolean method(...).
I am trying to test my player class properly, I have almost done it but I am having issues with my p1.setPlayerHand method. This is the following code I have used for my player class:
Player Class:
package model;
public class Player
{
private String PlayerName;
private Hand PlayerHand;
private boolean Dealer;
public Player(String name)
{
PlayerName = name;
PlayerHand = new Hand();
Dealer = false;
}
public void setName (String name)
{
this.PlayerName = name;
}
public String getName()
{
return PlayerName;
}
public void setDealer (Boolean dealer)
{
this.Dealer = dealer;
}
public boolean getDealer()
{
return Dealer;
}
public void setPlayerHand (Hand hand)
{
this.PlayerHand = hand;
}
public void getHand()
{
PlayerHand.displayCardsinHand();
}
public static void main (String [] args)
{
Player p1 = new Player("player1");
Hand h = new Hand();
//System.out.println(p1);
p1.setName("BARRY");
System.out.println(p1.getName());
p1.setDealer(false);
System.out.println(p1.getDealer());
//this is the error that is preventing my program to run
p1.setPlayerHand(h.addCard(new Card(Suit.CLUBS, CardRank.ACE)));
p1.getHand();
}
}
The following error I receive (after testing the Player Class) is this:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setPlayerHand(Hand) in the type Player is not applicable for the arguments (void)
at model.Player.main(Player.java:57)
This is the Hand Class underneath (that is linked to the Player Class):
Hand Class:
package model;
import java.util.Vector;
import java.util.Random;
public class Hand
{
private Vector<Card> hand;
public Hand()
{
hand = new Vector<Card>();
}
public void addCard(Card c)
{
hand.add(c);
}
public void displayCardsinHand()
{
for (int card = 0; card < hand.size(); card++)
{
System.out.println(hand.elementAt(card));
}
}
public int getCardsinHand()
{
return hand.size();
}
public Card getCard(int position)
{
if(position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}
public int getScore()
{
int value = 0;
boolean ace = false;
for (int i = 0; i < hand.size(); i++)
{
Card c;
c = getCard(i);
value = value + c.getRankValue();
if(c.getRankValue() == 1)
{
ace = true;
}
}
if(ace == true && value + 10 <= 21)
{
value = value + 10;
}
return value;
}
public static void main (String [] args)
{
Hand h = new Hand();
System.out.println(h);
h.displayCardsinHand();
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.HEARTS, CardRank.ACE));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.SPADES, CardRank.JACK));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.DIAMONDS, CardRank.QUEEN));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.CLUBS, CardRank.KING));
System.out.println(h.getCardsinHand());
System.out.println(h.getCardsinHand());
h.displayCardsinHand();
h.getCard(1);
System.out.println(h.getScore());
}
}
I have tried modifying the p1.setPlayerHand testing numerous times. I appreciate any advice and tips on how to solve this issue, thank you.
If my code is too long for this post then I will gladly accept any advice on what I should do to cut it short (for future reference).
If anyone here required to see any other classes that I wrote (that may help them help me solve this error) then please notify me on here, thank you.
The method addCard doesn't return anything (void). So you can't pass the result of this method to setPlayerHand(Hand). That's what you're doing.
The code should compile and run if you change
p1.setPlayerHand(h.addCard(new Card(Suit.CLUBS, CardRank.ACE)));
to
h.addCard(new Card(Suit.CLUBS, CardRank.ACE));
p1.setPlayerHand(h);
This is because the setPlayerHand method needs to be passed an object of type Hand, but the addCard method doesn't return anything (it's declared as void).
I have 2 class files. One has the get/set statements and I need to write the code to use these get/set statements on another class file in the same project.
How can I do that?
Also, I want 2 random numbers as an output but I get only 1.
Card class:
public class Card{
//variables
public int spades = 0;
public int hearts = 1;
public int diamonds = 2;
public int clubs = 3;
public int ace = 1;
public int jack = 11;
public int queen = 12;
public int king = 13;
private String rank;
private String suit;
//set methods
public void setSpades(String s){
spades = 0;
}
public void setHearts(int h){
hearts = 1;
}
public void setDiamonds(int d){
diamonds = 2;
}
public void setClubs(int c){
clubs = 3;
}
//get methods
public int getSpades(){
return spades;
}
public int getHearts(){
return hearts;
}
public int getDiamonds(){
return diamonds;
}
public int getClubs(){
return clubs;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
}//end class
The second file:
public class PickTwoCards {
public static void main(String[] args){
Card Object = new Card();
Card Object2 = new Card();
int ranSuit = (int)(Math.random()*10);
System.out.println(ranSuit);
}
public void Card (int theValue, int theSuit, int suit, int value){
value = theValue;
suit= theSuit;
}
}//End class PickTwocCards
Let's say that you have the following setter and getter methods in your first class:
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
from the other class, you would call setName with yourObject.getName() once you've instantiated.
Keep in mind, however, that beyond basic functions, getters and setters are considered bad:
Are getters and setters poor design? Contradictory advice seen
Allen Holub wrote "You should never use get/set functions", is he correct?