I am learning java and was looking at this program I don't understand what is wrong with it, I am getting an error that says something about the compareTo cannot be found and variable cannot be found.

class Card implements Comparable
{
Card(int r, int s)
{
rank = r;
suit = s;
}
int getRank()
{
return rank;
}
int getSuit()
{
return suit;
}
public int compareTo(Object ob)
{
Card other = (Card)ob;
int thisRank = this.getRank();
int otherRank = other.getRank();
if (thisRank == 1) thisRank = 14; // make aces high
if (otherRank == 1) otherRank = 14;
return thisRank - otherRank;
}
public boolean equals(Object ob)
{
if (ob instanceof Card)
{
Card other = (Card)ob;
return value==other.value && suit==other.suit;
}
else return false;
}
public String toString()
{
String val;
String [] suitList =
{ "", "Clubs", "Diamonds", "Hearts", "Spades" };
if (rank == 1) val = "Ace";
else if (rank == 11) val = "Jack";
else if (rank == 12) val = "Queen";
else if (rank == 13) val = "King";
else val = String.valueOf(rank); // int to String
String s = val + " of " + suitList[suit];
for (int k=s.length()+1; k<=17; k++) s = s + " ";
return s;
}
private int rank;
private int suit;
}
class CardDeck
{
CardDeck()
{
deck = new Card [52];
fill();
}
void shuffle()
{
for (int next = 0; next < numCards-1; next++)
{
int r = myRandom(next, numCards-1);
Card temp = deck[next];
deck[next] = deck[r];
deck[r] = temp;
}
}
Card deal()
{
if (numCards == 0) return null;
numCards--;
return deck[numCards];
}
int getSize()
{
return numCards;
}
private void fill()
{
int index = 0;
for (int r = 1; r <= 13; r++)
for (int s = 1; s <= 4; s++)
{
deck[index] = new Card(r, s);
index++;
}
numCards = 52;
}
private static int myRandom(int low, int high)
{
return (int)((high+1-low)*Math.random()+low);
}
private Card [] deck;
private int numCards;
}

class Pile
{
Pile()
{
pile = new Card[52];
front = 0; end = 0;
}
int getSize()
{
return end - front;
}
void clear()
{
front = 0; end = 0;
}
void addCard(Card c)
{
pile[end] = c;
end++;
}
void addCards(Pile p)
{
while (p.getSize() > 0)
addCard(p.nextCard());
}
Card nextCard()
{
if (front == end)
return null; // should not happen
Card c = pile[front];
front++;
return c;
}
private Card [] pile;
private int front, end; // front ≤ end
}
class Player
{
Player(String n)
{
name = n;
playPile = new Pile();
wonPile = new Pile();
}
Card playCard()
{
if (playPile.getSize() == 0)
useWonPile();
if (playPile.getSize() > 0)
return playPile.nextCard();
return null;
}
String getName()
{
return name;
}
void collectCard(Card c)
{
wonPile.addCard©;
}
void collectCards(Pile p)
{
wonPile.addCards(p);
}
void useWonPile()
{
playPile.clear(); // reset front and end to 0
playPile.addCards(wonPile);
wonPile.clear(); // reset front and end to 0
}
int numCards()
{
return playPile.getSize() + wonPile.getSize();
}
private Pile playPile, wonPile;
private String name;
}
class Game
{
void play()
{
CardDeck cd = new CardDeck();
cd.shuffle();
p1 = new Player("Ernie");
p2 = new Player("Burt");
while (cd.getSize() >= 2)
{
p1.collectCard(cd.deal());
p2.collectCard(cd.deal());
}
p1.useWonPile();
p2.useWonPile();
Pile down = new Pile(); // Pile for cards in a war
loop: for (int t=1; t<=100; t++)
{
if (!enoughCards(1)) break loop;
Card c1 = p1.playCard();
Card c2 = p2.playCard();
System.out.println("\nTurn " + t + ": ");
System.out.print(p1.getName() + ": " + c1 + " ");
System.out.print(p2.getName() + ": " + c2 + " ");
if (c1.compareTo(c2) > 0)
{
p1.collectCard(c1); p1.collectCard(c2);
}
else if (c1.compareTo(c2) < 0)
{
p2.collectCard(c1); p2.collectCard(c2);
}
else // War
{
down.clear();
down.addCard(c1); down.addCard(c2);
boolean done = false;
do
{ int num = c1.getRank();
if (!enoughCards(num)) break loop;
System.out.print("\nWar! Players put down ");
System.out.println(num + " card(s).");
for (int m=1; m<=num; m++)
{
c1 = p1.playCard(); c2 = p2.playCard();
down.addCard(c1);
down.addCard(c2);
}
System.out.print(p1.getName()+": "+ c1 + " ");
System.out.print(p2.getName()+": " + c2 + " ");
if (c1.compareTo(c2) > 0)
{ p1.collectCards(down);
done = true;
}
else if (c1.compareTo(c2) < 0)
{ p2.collectCards(down);
done = true;
}
}
while (!done);
} // end of for t=1 to 100
System.out.println(p1.numCards() + " to "
+ p2.numCards());
}
}
boolean enoughCards(int n)
{
if (p1.numCards() < n || p2.numCards() < n)
return false;
return true;
}
Player getWinner()
{
if (p1.numCards() > p2.numCards())
return p1;
else if (p2.numCards() > p1.numCards())
return p2;
else
return null;
}
private Player p1, p2;
}
public class War
{
public static void main(String [] args)
{
Game g = new Game();
g.play();
Player winner = g.getWinner();
if (winner == null) System.out.println("Tie game.");
else System.out.println("\nWinner = "
+ winner.getName());
}
}

ok here is the exact messages
cannot find variable value - line 34
cannot find symbol method CompareTo(Card) - line 209,243,248

I looked at your program, and found only two problems.

First off, something simple, and probably not your fault. In your collectCard method:

void collectCard(Card c)
{
wonPile.addCard©;
}

Whatever compiler or word processor you used to type this in has changed the instance of (c) in wonPile.addCard(c); into a copyright symbol. Something you should fix.

The other issue is just as easily solved, once you realize that there is no instance of "value" in the program. The variable instead should be "rank". That is, this:

return value==other.value && suit==other.suit;

...should instead say this:

return rank==other.rank && suit==other.suit;

I've tested the code with these changes, and it compiles and runs fine. It's a clever program. -DC

This article has been dead for over six months. Start a new discussion instead.