anosTekbot
asked on
help on divide cards into 4 hands and display!
hello there;
I wrote a code to divide 52 cards into 4 hands and try to display them on screen but i have problems. can somebody look at the code and see whats wrong
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
Card [] d = new Card[52];
public Card(){
for (int j = 1; j <= 4; j++){
for (int i = 2;i <= 14;i++){
d[(13*(j-1))+(i-2)] = new Card(i,j,1,1);
}
}
for (int i = 0;i < 52; i++){
Random r = new Random();
int x = (r.nextInt() * i / (52-i)) % 51;
if (x<0)
x = -x;
int t = d[x].getValue();
int s = d[x].getSuit();
d[x].setValue(d[i].getValu e());
d[x].setSuit(d[i].getSuit( ));
d[i].setValue(t);
d[i].setSuit(s);
}
}
public Card getCard(int c) {
return d[c];
}
public Card[] deal(int c) {
Card [] hand = new Card[c];
return hand;
}
/*public class Node
{
private Node next;
}
public class LinkList
{
private Node head = null;
private Node prev = null;
private Node cur = null;
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList list3 = new LinkedList();
LinkedList list4 = new LinkedList();
}
*/
public static void main(String[] args)
{
System.Out.println(""+hand )
}
}//END OF CARD CLASS
thanks for your time
I wrote a code to divide 52 cards into 4 hands and try to display them on screen but i have problems. can somebody look at the code and see whats wrong
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
Card [] d = new Card[52];
public Card(){
for (int j = 1; j <= 4; j++){
for (int i = 2;i <= 14;i++){
d[(13*(j-1))+(i-2)] = new Card(i,j,1,1);
}
}
for (int i = 0;i < 52; i++){
Random r = new Random();
int x = (r.nextInt() * i / (52-i)) % 51;
if (x<0)
x = -x;
int t = d[x].getValue();
int s = d[x].getSuit();
d[x].setValue(d[i].getValu
d[x].setSuit(d[i].getSuit(
d[i].setValue(t);
d[i].setSuit(s);
}
}
public Card getCard(int c) {
return d[c];
}
public Card[] deal(int c) {
Card [] hand = new Card[c];
return hand;
}
/*public class Node
{
private Node next;
}
public class LinkList
{
private Node head = null;
private Node prev = null;
private Node cur = null;
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList list3 = new LinkedList();
LinkedList list4 = new LinkedList();
}
*/
public static void main(String[] args)
{
System.Out.println(""+hand
}
}//END OF CARD CLASS
thanks for your time
ASKER
d[(13*(j-1))+(i-2)] = new Card(i,j);
I dont get any output
am I missing some arguments ?
Don't understand why you have a Card constructor like this
public Card(){
for (int j = 1; j <= 4; j++){
for (int i = 2;i <= 14;i++){
d[(13*(j-1))+(i-2)] = new Card(i,j); // This is filling your deck d[]. Why do you do that in the constructor of one Card???
}
}
for (int i = 0;i < 52; i++){ // What do you try here?
Random r = new Random();
int x = (r.nextInt() * i / (52-i)) % 51;
if (x<0)
x = -x;
int t = d[x].getValue();
int s = d[x].getSuit();
d[x].setValue(d[i].getValu e());
d[x].setSuit(d[i].getSuit( ));
d[i].setValue(t);
d[i].setSuit(s);
}
}
public Card(){
for (int j = 1; j <= 4; j++){
for (int i = 2;i <= 14;i++){
d[(13*(j-1))+(i-2)] = new Card(i,j); // This is filling your deck d[]. Why do you do that in the constructor of one Card???
}
}
for (int i = 0;i < 52; i++){ // What do you try here?
Random r = new Random();
int x = (r.nextInt() * i / (52-i)) % 51;
if (x<0)
x = -x;
int t = d[x].getValue();
int s = d[x].getSuit();
d[x].setValue(d[i].getValu
d[x].setSuit(d[i].getSuit(
d[i].setValue(t);
d[i].setSuit(s);
}
}
I would expect this as Card class:
Card.java:
-------------
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
}
Card.java:
-------------
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
}
Yeah, I would have the Card class just as much as zzynx has posted, and make the other stuff in another class. Create an array of Card objects in the main () method and store values randomly.
ASKER
First I edited the code from//*******//
Second, I want to start the program with 52 cards then randomly divides them into 4 hands. Thats all right now what i am trying to accomplish.
the Card constructor should be the Deck in Deck class,
you were right, there shouldnt be a card constructor.
Second, I want to start the program with 52 cards then randomly divides them into 4 hands. Thats all right now what i am trying to accomplish.
the Card constructor should be the Deck in Deck class,
you were right, there shouldnt be a card constructor.
ASKER
i missed the code.. here is it
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
//*******************//
public class Deck {
Card [] d = new Card[52];
public Deck(){
for (int j = 1; j <= 4; j++){
for (int i = 2;i <= 14;i++){
d[(13*(j-1))+(i-2)] = new Card(i,j,1,1);
}
}
for (int i = 0;i < 52; i++){
Random r = new Random();
int x = (r.nextInt() * i / (52-i)) % 51;
if (x<0)
x = -x;
int t = d[x].getValue();
int s = d[x].getSuit();
d[x].setValue(d[i].getValu e());
d[x].setSuit(d[i].getSuit( ));
d[i].setValue(t);
d[i].setSuit(s);
}
}
public Card getCard(int c){
return d[c];
}
public Card[] deal(int c){
Card [] hand = new Card[c];
return hand;
}
}
/* public class Node
{
private Node next;
}
public class LinkList
{
private Node head = null;
private Node prev = null;
private Node cur = null;
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList list3 = new LinkedList();
LinkedList list4 = new LinkedList();
}
*/
public static void main(String[] args)
{
System.Out.println(""+hand );
}
}//END OF CARD CLASS
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
//*******************//
public class Deck {
Card [] d = new Card[52];
public Deck(){
for (int j = 1; j <= 4; j++){
for (int i = 2;i <= 14;i++){
d[(13*(j-1))+(i-2)] = new Card(i,j,1,1);
}
}
for (int i = 0;i < 52; i++){
Random r = new Random();
int x = (r.nextInt() * i / (52-i)) % 51;
if (x<0)
x = -x;
int t = d[x].getValue();
int s = d[x].getSuit();
d[x].setValue(d[i].getValu
d[x].setSuit(d[i].getSuit(
d[i].setValue(t);
d[i].setSuit(s);
}
}
public Card getCard(int c){
return d[c];
}
public Card[] deal(int c){
Card [] hand = new Card[c];
return hand;
}
}
/* public class Node
{
private Node next;
}
public class LinkList
{
private Node head = null;
private Node prev = null;
private Node cur = null;
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList list3 = new LinkedList();
LinkedList list4 = new LinkedList();
}
*/
public static void main(String[] args)
{
System.Out.println(""+hand
}
}//END OF CARD CLASS
ASKER
wrong arguemnt is this code d[(13*(j-1))+(i-2)] = new Card(i,j,1,1);
it should be d[(13*(j-1))+(i-2)] = new Card(i,j);
it should be d[(13*(j-1))+(i-2)] = new Card(i,j);
This how I would do it (no Deck class needed):
1) Card.java
-----------------
/*
* Card.java
*
*/
/**
*
* @author zzynx
*/
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
public String toString() {
return "Suit = " + suit + " Value = " + value;
}
}
2) CardGame.java
-----------------------
/*
* CardGame.java
*
*/
import java.util.*;
/**
*
* @author zzynx
*/
public class CardGame {
List theDeck = new ArrayList(52);
Random r = new Random();
public CardGame() {
// Fill the deck
for (int j = 1; j <= 4; j++)
for (int i = 1;i <= 13;i++)
theDeck.add( new Card(i,j) );
}
public List deal(int number) {
List hand = new ArrayList();
while (hand.size()<number) {
int index = r.nextInt( theDeck.size() );
hand.add( theDeck.get(index) );
theDeck.remove(index);
}
return hand;
}
public void listHand(List hand) {
for (int i=0; i<hand.size(); i++)
System.out.println( ((Card) hand.get(i)) );
}
public static void main(String args[]) {
CardGame cg = new CardGame();
List hand1 = cg.deal(13);
System.out.println("Hand 1 contains:");
cg.listHand(hand1);
List hand2 = cg.deal(13);
System.out.println("Hand 2 contains:");
cg.listHand(hand2);
List hand3 = cg.deal(13);
System.out.println("Hand 3 contains:");
cg.listHand(hand3);
List hand4 = cg.deal(13);
System.out.println("Hand 4 contains:");
cg.listHand(hand4);
}
}
When running the last one you get e.g.:
Hand 1 contains:
Suit = 2 Value = 2
Suit = 4 Value = 9
Suit = 1 Value = 12
Suit = 3 Value = 5
Suit = 1 Value = 4
Suit = 2 Value = 1
Suit = 3 Value = 13
Suit = 3 Value = 12
Suit = 4 Value = 10
Suit = 2 Value = 3
Suit = 2 Value = 10
Suit = 1 Value = 13
Suit = 1 Value = 2
Hand 2 contains:
Suit = 4 Value = 13
Suit = 4 Value = 1
Suit = 1 Value = 8
Suit = 4 Value = 8
Suit = 3 Value = 2
Suit = 3 Value = 10
Suit = 4 Value = 12
Suit = 3 Value = 3
Suit = 1 Value = 5
Suit = 2 Value = 6
Suit = 3 Value = 11
Suit = 2 Value = 12
Suit = 1 Value = 6
Hand 3 contains:
Suit = 4 Value = 4
Suit = 1 Value = 3
Suit = 3 Value = 8
Suit = 4 Value = 3
Suit = 3 Value = 1
Suit = 4 Value = 11
Suit = 4 Value = 6
Suit = 4 Value = 2
Suit = 3 Value = 7
Suit = 2 Value = 11
Suit = 2 Value = 13
Suit = 1 Value = 1
Suit = 1 Value = 9
Hand 4 contains:
Suit = 4 Value = 5
Suit = 4 Value = 7
Suit = 2 Value = 5
Suit = 1 Value = 11
Suit = 2 Value = 8
Suit = 3 Value = 6
Suit = 2 Value = 7
Suit = 2 Value = 4
Suit = 2 Value = 9
Suit = 3 Value = 4
Suit = 3 Value = 9
Suit = 1 Value = 7
Suit = 1 Value = 10
1) Card.java
-----------------
/*
* Card.java
*
*/
/**
*
* @author zzynx
*/
public class Card{
private int value, suit;
public Card(int val, int s){
value = val;
suit = s;
}
public int getValue(){
return value;
}
public int getSuit(){
return suit;
}
public void setValue(int v){
value = v;
}
public void setSuit(int s){
suit = s;
}
public String toString() {
return "Suit = " + suit + " Value = " + value;
}
}
2) CardGame.java
-----------------------
/*
* CardGame.java
*
*/
import java.util.*;
/**
*
* @author zzynx
*/
public class CardGame {
List theDeck = new ArrayList(52);
Random r = new Random();
public CardGame() {
// Fill the deck
for (int j = 1; j <= 4; j++)
for (int i = 1;i <= 13;i++)
theDeck.add( new Card(i,j) );
}
public List deal(int number) {
List hand = new ArrayList();
while (hand.size()<number) {
int index = r.nextInt( theDeck.size() );
hand.add( theDeck.get(index) );
theDeck.remove(index);
}
return hand;
}
public void listHand(List hand) {
for (int i=0; i<hand.size(); i++)
System.out.println( ((Card) hand.get(i)) );
}
public static void main(String args[]) {
CardGame cg = new CardGame();
List hand1 = cg.deal(13);
System.out.println("Hand 1 contains:");
cg.listHand(hand1);
List hand2 = cg.deal(13);
System.out.println("Hand 2 contains:");
cg.listHand(hand2);
List hand3 = cg.deal(13);
System.out.println("Hand 3 contains:");
cg.listHand(hand3);
List hand4 = cg.deal(13);
System.out.println("Hand 4 contains:");
cg.listHand(hand4);
}
}
When running the last one you get e.g.:
Hand 1 contains:
Suit = 2 Value = 2
Suit = 4 Value = 9
Suit = 1 Value = 12
Suit = 3 Value = 5
Suit = 1 Value = 4
Suit = 2 Value = 1
Suit = 3 Value = 13
Suit = 3 Value = 12
Suit = 4 Value = 10
Suit = 2 Value = 3
Suit = 2 Value = 10
Suit = 1 Value = 13
Suit = 1 Value = 2
Hand 2 contains:
Suit = 4 Value = 13
Suit = 4 Value = 1
Suit = 1 Value = 8
Suit = 4 Value = 8
Suit = 3 Value = 2
Suit = 3 Value = 10
Suit = 4 Value = 12
Suit = 3 Value = 3
Suit = 1 Value = 5
Suit = 2 Value = 6
Suit = 3 Value = 11
Suit = 2 Value = 12
Suit = 1 Value = 6
Hand 3 contains:
Suit = 4 Value = 4
Suit = 1 Value = 3
Suit = 3 Value = 8
Suit = 4 Value = 3
Suit = 3 Value = 1
Suit = 4 Value = 11
Suit = 4 Value = 6
Suit = 4 Value = 2
Suit = 3 Value = 7
Suit = 2 Value = 11
Suit = 2 Value = 13
Suit = 1 Value = 1
Suit = 1 Value = 9
Hand 4 contains:
Suit = 4 Value = 5
Suit = 4 Value = 7
Suit = 2 Value = 5
Suit = 1 Value = 11
Suit = 2 Value = 8
Suit = 3 Value = 6
Suit = 2 Value = 7
Suit = 2 Value = 4
Suit = 2 Value = 9
Suit = 3 Value = 4
Suit = 3 Value = 9
Suit = 1 Value = 7
Suit = 1 Value = 10
ASKER
what I am trying to do is sort of HEARTS GAME but I am not going to code how to score and how to pass left then right then no pass. by the first step what I am doin is to divide 52 cards into 4 players then I will you a class "Node" to stor them, then I will use linklist to structure those nodes. : )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also I used array lists instead of arrays. Hope you don't mind ;°)
I think you don't since Node contains 4 lists.
I think you don't since Node contains 4 lists.
So, instead of writing
List hand1 = cg.deal(13);
you could write
node.setList1( cg.deal(13) );
with node an instance of your Node class having a setList1() function like
public void setList1(List lst) { list1 = lst; }
List hand1 = cg.deal(13);
you could write
node.setList1( cg.deal(13) );
with node an instance of your Node class having a setList1() function like
public void setList1(List lst) { list1 = lst; }
ASKER
your code is much clear to me and it gave me much exlpanation. now my job is to implement linklists and try to use them. such as a linklist of Card for players ( north south west east). : )
thanks zzynx
thanks zzynx
:°)
ASKER
so this would be my Node class?
public class Node{
private Node node1;
private Node node2;
private Node node3;
private Node node4;
public void setList1(List lst) {
list1 = lst;
}
public void setList2(List lst2) {
list2 = lst2;
}
public void setList1(List lst3) {
list3 = lst3;
}public void setList1(List lst4) {
list4 = lst4;
}
}
how is that ?
public class Node{
private Node node1;
private Node node2;
private Node node3;
private Node node4;
public void setList1(List lst) {
list1 = lst;
}
public void setList2(List lst2) {
list2 = lst2;
}
public void setList1(List lst3) {
list3 = lst3;
}public void setList1(List lst4) {
list4 = lst4;
}
}
how is that ?
I see that you have Node members in your class and not Lists, but your constructors accept List arguments and try to set them on members (which are also not defined in your class :-) )
setList3() and setList4() you meant I suppose?
and instead of
private Node nodex;
I guess you meant
private List listx;
Right? ;°)
ASKER
I will accept your answer and re ask about the Node class with Linklist relation
thanks again
thanks again
Do you really need *Linked*List's? Or is a "simple" List OK? You decide.
BTW:
If you want a more "human" printout change the Card's class toString() function to this:
public String toString() {
StringBuffer cardDescription = new StringBuffer();
switch(value) {
case 1 : cardDescription.append("Ac e"); break;
case 11 : cardDescription.append("Ja ck"); break;
case 12 : cardDescription.append("?? ??"); break; // <<<<<<<<<<<< replace please. Don't know in English ;°)
case 13 : cardDescription.append("Ki ng"); break;
default :
cardDescription.append( value + "" );
break;
}
cardDescription.append(" of ");
switch(suit) {
case 1 : cardDescription.append("He arts"); break;
case 2 : cardDescription.append("Sp ades"); break;
case 3 : cardDescription.append("Di amonds"); break;
case 4 : cardDescription.append("?? ????"); break; // <<<<<<<<<<<< replace please. Don't know in English ;°)
}
return cardDescription.toString() ;
}
BTW:
If you want a more "human" printout change the Card's class toString() function to this:
public String toString() {
StringBuffer cardDescription = new StringBuffer();
switch(value) {
case 1 : cardDescription.append("Ac
case 11 : cardDescription.append("Ja
case 12 : cardDescription.append("??
case 13 : cardDescription.append("Ki
default :
cardDescription.append( value + "" );
break;
}
cardDescription.append(" of ");
switch(suit) {
case 1 : cardDescription.append("He
case 2 : cardDescription.append("Sp
case 3 : cardDescription.append("Di
case 4 : cardDescription.append("??
}
return cardDescription.toString()
}
Thanks. You're welcome.
ASKER
hellow zzynx ;
I submitted new question because it is unfair to answer two questions in one : )
you already answered what i was looking for.
thanks
I submitted new question because it is unfair to answer two questions in one : )
you already answered what i was looking for.
thanks
>> case 12 : cardDescription.append("?? ??"); break; // <<<<<<<<<<<< replace please. Don't know in English ;°)
Queen :-)
>> case 4 : cardDescription.append("?? ????"); break; // <<<<<<<<<<<< replace please. Don't know in English ;°)
Clubs.
Queen :-)
>> case 4 : cardDescription.append("??
Clubs.
ASKER
u r welcome to answer the second one. : )
First off, I don't see a Card constructor that takes 4 arguments. What is the problem that you're facing, BTW? Compilation or some exception or unexpected output?