compiler errors wargame

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 18
Reputation: thunderbbolt is an unknown quantity at this point 
Solved Threads: 0
thunderbbolt thunderbbolt is offline Offline
Newbie Poster

compiler errors wargame

 
0
  #1
Mar 7th, 2007
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.

  1. class Card implements Comparable
  2. {
  3. Card(int r, int s)
  4. {
  5. rank = r;
  6. suit = s;
  7. }
  8. int getRank()
  9. {
  10. return rank;
  11. }
  12. int getSuit()
  13. {
  14. return suit;
  15. }
  16. public int compareTo(Object ob)
  17. {
  18. Card other = (Card)ob;
  19. int thisRank = this.getRank();
  20. int otherRank = other.getRank();
  21. if (thisRank == 1) thisRank = 14; // make aces high
  22. if (otherRank == 1) otherRank = 14;
  23. return thisRank - otherRank;
  24. }
  25. public boolean equals(Object ob)
  26. {
  27. if (ob instanceof Card)
  28. {
  29. Card other = (Card)ob;
  30. return value==other.value && suit==other.suit;
  31. }
  32. else return false;
  33. }
  34. public String toString()
  35. {
  36. String val;
  37. String [] suitList =
  38. { "", "Clubs", "Diamonds", "Hearts", "Spades" };
  39. if (rank == 1) val = "Ace";
  40. else if (rank == 11) val = "Jack";
  41. else if (rank == 12) val = "Queen";
  42. else if (rank == 13) val = "King";
  43. else val = String.valueOf(rank); // int to String
  44. String s = val + " of " + suitList[suit];
  45. for (int k=s.length()+1; k<=17; k++) s = s + " ";
  46. return s;
  47. }
  48. private int rank;
  49. private int suit;
  50. }
  51. class CardDeck
  52. {
  53. CardDeck()
  54. {
  55. deck = new Card [52];
  56. fill();
  57. }
  58. void shuffle()
  59. {
  60. for (int next = 0; next < numCards-1; next++)
  61. {
  62. int r = myRandom(next, numCards-1);
  63. Card temp = deck[next];
  64. deck[next] = deck[r];
  65. deck[r] = temp;
  66. }
  67. }
  68. Card deal()
  69. {
  70. if (numCards == 0) return null;
  71. numCards--;
  72. return deck[numCards];
  73. }
  74. int getSize()
  75. {
  76. return numCards;
  77. }
  78. private void fill()
  79. {
  80. int index = 0;
  81. for (int r = 1; r <= 13; r++)
  82. for (int s = 1; s <= 4; s++)
  83. {
  84. deck[index] = new Card(r, s);
  85. index++;
  86. }
  87. numCards = 52;
  88. }
  89. private static int myRandom(int low, int high)
  90. {
  91. return (int)((high+1-low)*Math.random()+low);
  92. }
  93. private Card [] deck;
  94. private int numCards;
  95. }
  96.  
  97. class Pile
  98. {
  99. Pile()
  100. {
  101. pile = new Card[52];
  102. front = 0; end = 0;
  103. }
  104. int getSize()
  105. {
  106. return end - front;
  107. }
  108. void clear()
  109. {
  110. front = 0; end = 0;
  111. }
  112. void addCard(Card c)
  113. {
  114. pile[end] = c;
  115. end++;
  116. }
  117. void addCards(Pile p)
  118. {
  119. while (p.getSize() > 0)
  120. addCard(p.nextCard());
  121. }
  122. Card nextCard()
  123. {
  124. if (front == end)
  125. return null; // should not happen
  126. Card c = pile[front];
  127. front++;
  128. return c;
  129. }
  130. private Card [] pile;
  131. private int front, end; // front ≤ end
  132. }
  133. class Player
  134. {
  135. Player(String n)
  136. {
  137. name = n;
  138. playPile = new Pile();
  139. wonPile = new Pile();
  140. }
  141. Card playCard()
  142. {
  143. if (playPile.getSize() == 0)
  144. useWonPile();
  145. if (playPile.getSize() > 0)
  146. return playPile.nextCard();
  147. return null;
  148. }
  149. String getName()
  150. {
  151. return name;
  152. }
  153. void collectCard(Card c)
  154. {
  155. wonPile.addCard©;
  156. }
  157. void collectCards(Pile p)
  158. {
  159. wonPile.addCards(p);
  160. }
  161. void useWonPile()
  162. {
  163. playPile.clear(); // reset front and end to 0
  164. playPile.addCards(wonPile);
  165. wonPile.clear(); // reset front and end to 0
  166. }
  167. int numCards()
  168. {
  169. return playPile.getSize() + wonPile.getSize();
  170. }
  171. private Pile playPile, wonPile;
  172. private String name;
  173. }
  174. class Game
  175. {
  176. void play()
  177. {
  178. CardDeck cd = new CardDeck();
  179. cd.shuffle();
  180. p1 = new Player("Ernie");
  181. p2 = new Player("Burt");
  182. while (cd.getSize() >= 2)
  183. {
  184. p1.collectCard(cd.deal());
  185. p2.collectCard(cd.deal());
  186. }
  187. p1.useWonPile();
  188. p2.useWonPile();
  189. Pile down = new Pile(); // Pile for cards in a war
  190. loop: for (int t=1; t<=100; t++)
  191. {
  192. if (!enoughCards(1)) break loop;
  193. Card c1 = p1.playCard();
  194. Card c2 = p2.playCard();
  195. System.out.println("\nTurn " + t + ": ");
  196. System.out.print(p1.getName() + ": " + c1 + " ");
  197. System.out.print(p2.getName() + ": " + c2 + " ");
  198. if (c1.compareTo(c2) > 0)
  199. {
  200. p1.collectCard(c1); p1.collectCard(c2);
  201. }
  202. else if (c1.compareTo(c2) < 0)
  203. {
  204. p2.collectCard(c1); p2.collectCard(c2);
  205. }
  206. else // War
  207. {
  208. down.clear();
  209. down.addCard(c1); down.addCard(c2);
  210. boolean done = false;
  211. do
  212. { int num = c1.getRank();
  213. if (!enoughCards(num)) break loop;
  214. System.out.print("\nWar! Players put down ");
  215. System.out.println(num + " card(s).");
  216. for (int m=1; m<=num; m++)
  217. {
  218. c1 = p1.playCard(); c2 = p2.playCard();
  219. down.addCard(c1);
  220. down.addCard(c2);
  221. }
  222. System.out.print(p1.getName()+": "+ c1 + " ");
  223. System.out.print(p2.getName()+": " + c2 + " ");
  224. if (c1.compareTo(c2) > 0)
  225. { p1.collectCards(down);
  226. done = true;
  227. }
  228. else if (c1.compareTo(c2) < 0)
  229. { p2.collectCards(down);
  230. done = true;
  231. }
  232. }
  233. while (!done);
  234. } // end of for t=1 to 100
  235. System.out.println(p1.numCards() + " to "
  236. + p2.numCards());
  237. }
  238. }
  239. boolean enoughCards(int n)
  240. {
  241. if (p1.numCards() < n || p2.numCards() < n)
  242. return false;
  243. return true;
  244. }
  245. Player getWinner()
  246. {
  247. if (p1.numCards() > p2.numCards())
  248. return p1;
  249. else if (p2.numCards() > p1.numCards())
  250. return p2;
  251. else
  252. return null;
  253. }
  254. private Player p1, p2;
  255. }
  256. public class War
  257. {
  258. public static void main(String [] args)
  259. {
  260. Game g = new Game();
  261. g.play();
  262. Player winner = g.getWinner();
  263. if (winner == null) System.out.println("Tie game.");
  264. else System.out.println("\nWinner = "
  265. + winner.getName());
  266. }
  267. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 18
Reputation: thunderbbolt is an unknown quantity at this point 
Solved Threads: 0
thunderbbolt thunderbbolt is offline Offline
Newbie Poster

Re: compiler errors wargame

 
0
  #2
Mar 7th, 2007
ok here is the exact messages
cannot find variable value - line 34
cannot find symbol method CompareTo(Card) - line 209,243,248
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: compiler errors wargame

 
0
  #3
Mar 7th, 2007
says it all, doesn't it.
Typos maybe?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: DrCruel is an unknown quantity at this point 
Solved Threads: 0
DrCruel DrCruel is offline Offline
Newbie Poster

Re: compiler errors wargame

 
0
  #4
Oct 18th, 2007
I looked at your program, and found only two problems.

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

  1. void collectCard(Card c)
  2. {
  3. wonPile.addCard©;
  4. }

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:

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

...should instead say this:

  1.  
  2. 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
Last edited by DrCruel; Oct 18th, 2007 at 1:51 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1207 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC