mardi 29 avril 2014

Java - carte c au vidéo poker - Stack Overflow


I'm working on a video poker assignment in java. We are given class templates that we are supposed to use in our program. I'm confused as to what Card c represents. Like in the method addCard(Card c), does that just mean it will be executed on the Card object that's currently in use? Here are the class files I've been working on so far. They are far from finished.


Player Class:


import java.util.ArrayList;
public class Player {


private ArrayList<Card> hand; // the player's cards
// you will likely need more instance variables

public Player(){
ArrayList<Card> hand = new ArrayList;
}

public void addCard(Card c){
// add the card c to the player's hand
}

public void removeCard(Card c){
// remove the card c from the player's hand
}

// you will likely need more methods here
}

Deck class:


import java.util.Random
public class Deck {

private Card[] theDeck;
private int top;
// add more instance variables if needed

public Deck(){
top = 0
Card[] theDeck = new Card[52];
for(int s = 1; s <= 4; s++)
{
for (int v = 1; v <= 13; v++)
{
for (int i = 0; i < theDeck.length; i++)
theDeck[i] = new Card(s,v);
}

public void shuffle()
{
// shuffle the deck here
Random generator = new Random();
int i;
int j;
int temp = i;
int i = generator.nextInt(51) + 1;
int j = generator.nextInt(51) + 1;
for(int k = 1; k <100; k++)
{
theDeck[i] = theDeck[j];
theDeck[j] = temp;
}
top = 0;
}

public Card deal(){
// deal the top card in the deck
if (top < 40)
{
theDeck.shuffle;
}
return theDeck[top];
top++;
} //this method should add cards to array hand? no addCard will do that.





// add more methods here if needed
//DB getter methods here?

}

Card Class:


public class Card implements Comparable<Card>{

private int suit;
private int value;

public Card(int s, int v){ //constructor of an object Card
s = suit;
v = value;
//make a card with suit s and value v
}

public int compareTo(Card c){
// use this method to compare cards so they
// may be easily sorted

}

public String toString() //to tell the user what card/s they have
{
myCard.getSuit();
myCard.getValue();
if(s == 1)
{
if(v == 11)
{
return "Jack of Clubs";
}
if(v == 12)
{
return "Queen of Clubs";
}
if(v == 13)
{
return "King of Clubs";
}
if(v == 1)
{
return "Ace of Clubs";
}
else{
return v + " of Clubs";
}
}
if(s == 2)
{
return v + "Diamonds";
}
if(s == 3)
}
return v + "Hearts";
}
if(s == 4)
}
return v + "Spades";
}


//DB method to set 1, 2, 3, and 4 to card suits


//now here create string representation for the Card
// use this method to easily print a Card object

public int getSuit()
{
return s;
}
public int getValue()
{
return v;
}

//DB right now have cards in theDeck like Card(2, 10), need Card(d, 10)
//need to convert that to a String d10
}
// add some more methods here if needed

}



In public void addCard(Card c), c is a parameter.


This means that within the body of the function addCard, you can refer to the value passed to the function by the name c. You call the function addCard with some value (e.g., theDeck[0]), and that value will be used in addCard().



I'm working on a video poker assignment in java. We are given class templates that we are supposed to use in our program. I'm confused as to what Card c represents. Like in the method addCard(Card c), does that just mean it will be executed on the Card object that's currently in use? Here are the class files I've been working on so far. They are far from finished.


Player Class:


import java.util.ArrayList;
public class Player {


private ArrayList<Card> hand; // the player's cards
// you will likely need more instance variables

public Player(){
ArrayList<Card> hand = new ArrayList;
}

public void addCard(Card c){
// add the card c to the player's hand
}

public void removeCard(Card c){
// remove the card c from the player's hand
}

// you will likely need more methods here
}

Deck class:


import java.util.Random
public class Deck {

private Card[] theDeck;
private int top;
// add more instance variables if needed

public Deck(){
top = 0
Card[] theDeck = new Card[52];
for(int s = 1; s <= 4; s++)
{
for (int v = 1; v <= 13; v++)
{
for (int i = 0; i < theDeck.length; i++)
theDeck[i] = new Card(s,v);
}

public void shuffle()
{
// shuffle the deck here
Random generator = new Random();
int i;
int j;
int temp = i;
int i = generator.nextInt(51) + 1;
int j = generator.nextInt(51) + 1;
for(int k = 1; k <100; k++)
{
theDeck[i] = theDeck[j];
theDeck[j] = temp;
}
top = 0;
}

public Card deal(){
// deal the top card in the deck
if (top < 40)
{
theDeck.shuffle;
}
return theDeck[top];
top++;
} //this method should add cards to array hand? no addCard will do that.





// add more methods here if needed
//DB getter methods here?

}

Card Class:


public class Card implements Comparable<Card>{

private int suit;
private int value;

public Card(int s, int v){ //constructor of an object Card
s = suit;
v = value;
//make a card with suit s and value v
}

public int compareTo(Card c){
// use this method to compare cards so they
// may be easily sorted

}

public String toString() //to tell the user what card/s they have
{
myCard.getSuit();
myCard.getValue();
if(s == 1)
{
if(v == 11)
{
return "Jack of Clubs";
}
if(v == 12)
{
return "Queen of Clubs";
}
if(v == 13)
{
return "King of Clubs";
}
if(v == 1)
{
return "Ace of Clubs";
}
else{
return v + " of Clubs";
}
}
if(s == 2)
{
return v + "Diamonds";
}
if(s == 3)
}
return v + "Hearts";
}
if(s == 4)
}
return v + "Spades";
}


//DB method to set 1, 2, 3, and 4 to card suits


//now here create string representation for the Card
// use this method to easily print a Card object

public int getSuit()
{
return s;
}
public int getValue()
{
return v;
}

//DB right now have cards in theDeck like Card(2, 10), need Card(d, 10)
//need to convert that to a String d10
}
// add some more methods here if needed

}


In public void addCard(Card c), c is a parameter.


This means that within the body of the function addCard, you can refer to the value passed to the function by the name c. You call the function addCard with some value (e.g., theDeck[0]), and that value will be used in addCard().


0 commentaires:

Enregistrer un commentaire