Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. package ca.sheridancollege.project;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Random;
  7. /**
  8. * @author Rich Smith at ZenOfProgramming.com
  9. */
  10. public class StartTheWar
  11. {
  12. public static void main (String[] args)
  13. {
  14. System.out.println("Let the War begin!!!nnWe will now deal the cards. Best of luck to both playersn ");
  15. List<Card> cardDeck = new ArrayList<>();
  16.  
  17. for (int x = 0; x < 4; x++) { //for loop using the 4 suits
  18. for (int y = 2; y < 15; y++) { // for loop for 13 for cards 2-10 and faced cards1
  19. cardDeck.add(new Card(x, y)); //create new card then use add method to combine to array list
  20. } //end cards for loop
  21. }//end suit for
  22.  
  23. Collections.shuffle(cardDeck, new Random()); //deck shuffler
  24.  
  25. //using our newly learnt knowledge on Linked lists to create 2 - one for each player. player 1/player2
  26. LinkedList<Card> deck1 = new LinkedList<>();
  27. LinkedList<Card> deck2 = new LinkedList<>();
  28.  
  29.  
  30. //found interesting Java operation called List which can be broken down into sublists to store information. Just add the total # of cards needed in each sublist
  31. deck1.addAll(cardDeck.subList(0, 26)); //26 cards for p1
  32.  
  33. deck2.addAll(cardDeck.subList(26, cardDeck.size()));//26 cards for p2
  34.  
  35. //Array for player objects to be held
  36. ArrayList<Player> p3 = new ArrayList<>();
  37. Player p1 = new Player(deck1);
  38. Player p2 = new Player(deck2);
  39. p3.add(p1);
  40. p3.add(p2);
  41.  
  42. War game = new War(p3);
  43. game.play();
  44.  
  45. }
  46. }
  47.  
  48.  
  49. /**
  50. * SYST 17796 Project Winter 2019 Base code.
  51. * Students can modify and extend to implement their game.
  52. * Add your name as a modifier and the date!
  53. */
  54. package ca.sheridancollege.project;
  55. public class Card
  56. {
  57. private int rank; //initialize the rank (2,3,4...King, Ace)
  58. private int suit; //initialize the suit (spades, hearts...)
  59.  
  60. public Card (int suit, int rank)
  61. {
  62. this.rank = rank;
  63. this.suit = suit;
  64. }//end construcor
  65. //getter method
  66.  
  67. public int getCard ()
  68. {
  69. return rank;
  70. }//end getCard
  71.  
  72. //setter method
  73. public void setCard (int rank)
  74. {
  75. this.rank = rank;
  76. }//end setCard
  77.  
  78. @Override
  79. public String toString ()
  80. {
  81. //combine rank and suit together into a single string(ex: Ace of Diamonds)
  82. //suing StringBuilder for modifiability later on
  83. StringBuilder displayCard = new StringBuilder();
  84. //personal choice to use switch
  85. switch (rank) {
  86. //since rank is int type, now match int 11 to String jack...14 to Ace
  87. case 11:
  88. displayCard.append("Jack");
  89. break;
  90. case 12:
  91. displayCard.append("Queen");
  92. break;
  93. case 13:
  94. displayCard.append("King");
  95. break;
  96. case 14:
  97. displayCard.append("Ace");
  98. break;
  99. default:
  100. displayCard.append(rank); //number from 2 to 10 does not need to modify
  101. break;
  102. }//end rank switch
  103. displayCard.append(" of "); //setting the format of the output
  104.  
  105. switch (suit) {
  106. case 0:
  107. displayCard.append("Spades");
  108. break;
  109. case 1:
  110. displayCard.append("Hearts");
  111. break;
  112. case 2:
  113. displayCard.append("Clubs");
  114. break;
  115. case 3:
  116. displayCard.append("Diamonds");
  117. break;
  118. default: //anything else, do nothing
  119. break;
  120. }//end suit switch
  121. //return the result of an entire cmombined string
  122. return displayCard.toString();
  123. }//end toString
  124. }
  125.  
  126.  
  127. package ca.sheridancollege.project;
  128. /**
  129. * @author Rich Smith at ZenOfProgramming.com
  130. */
  131. import java.util.ArrayList;
  132. import java.util.List;
  133. public class War extends Game
  134. {
  135. public War (ArrayList<Player> players)
  136. {
  137. super(players);
  138.  
  139. }
  140.  
  141. public void play ()
  142. {
  143.  
  144. while (true) {
  145.  
  146.  
  147. //use pop to remove card from top of Linked List- thank god for Rich teaching us all this lol
  148. //this is where error is
  149. Card p1Card = this.getPlayers().get(0).getDeck().pop(); //each player place one card face up
  150. Card p2Card = this.getPlayers().get(1).getDeck().pop();
  151.  
  152.  
  153. //display the face up card
  154. System.out.println("Player 1 plays: " + p1Card.toString());
  155. System.out.println("Player 2 plays: " + p2Card.toString());
  156.  
  157. //rank comparation between two cards
  158. //
  159. if (p1Card.getCard() > p2Card.getCard()) {//if player 1 win
  160. this.getPlayers().get(0).getDeck().pop(); //higher rank wins both cards and
  161. this.getPlayers().get(0).getDeck().pop(); //places them at the bottom of his deck.
  162. System.out.println("Player 1 wins the round and Player 2's cards: " + p2Card.toString());
  163. System.out.println();
  164. }//end if
  165.  
  166. else if (p1Card.getCard() < p2Card.getCard()) {//if player 2 win
  167. this.getPlayers().get(1).getDeck().pop();
  168. this.getPlayers().get(1).getDeck().pop();
  169. System.out.println("Player 2 wins the round and Player 1's card : " + p1Card.toString());
  170. System.out.println();
  171. }//end else if
  172. // test
  173.  
  174. else { //war happens while both players draw the same car
  175. System.out.println("War!! Each player places 3 cards face down and flips the 4th! Winner take all!n");
  176.  
  177.  
  178. //creating new temp array for tie
  179. List<Card> war1 = new ArrayList<>();
  180. List<Card> war2 = new ArrayList<>();
  181.  
  182. //checking do players have enough (4)cards to play war / if not game is over
  183. for (int x = 0; x < 4; x++) {
  184. //either one player runs out of card is game over
  185. if (this.getPlayers().get(0).getDeck().isEmpty() || getPlayers().get(1).getDeck().isEmpty()) {
  186. break;
  187. }//end if
  188.  
  189. System.out.println("War card for Player 1 is xxnWar card for Player 2 is xxn");
  190.  
  191. war1.add(this.getPlayers().get(0).getDeck().pop()); //place additional card for war
  192. war2.add(this.getPlayers().get(1).getDeck().pop());
  193. }//end for
  194.  
  195. //only compare result when both players have enough cards for war
  196. if (war1.size() == 4 && war2.size() == 4) {
  197. //display the war cards from each player
  198. System.out.println("War card for Player 1 is " + war1.get(3).toString());
  199. System.out.println("War card for Player 2 is " + war2.get(3).toString());
  200. System.out.println();
  201.  
  202. //if player 1 wins the war round
  203. if (war1.get(3).getCard() > war2.get(3).getCard()) {
  204. this.getPlayers().get(0).getDeck().addAll(war1); //player1 get all 10 cards
  205. this.getPlayers().get(0).getDeck().addAll(war2);
  206. System.out.println("Player 1 wins the battle! But as they say, winning the battle does not guarantee winning the War!n");
  207. }//end if
  208. //otherwise player 2 wins the war round
  209. else {
  210. getPlayers().get(1).getDeck().addAll(war1); //player2 get all 10 cards
  211. getPlayers().get(1).getDeck().addAll(war2);
  212. System.out.println("Player 2 wins the battle! But as they say, winning the battle does not guarantee winning the War!n");
  213. }//end else
  214. }//end if
  215.  
  216. }//end war round else
  217. }
  218.  
  219. }
  220.  
  221. }
  222.  
  223.  
  224. /**
  225. * SYST 17796 Project Winter 2019 Base code.
  226. * Students can modify and extend to implement their game.
  227. * Add your name as a modifier and the date!
  228. */
  229. package ca.sheridancollege.project;
  230. import java.util.LinkedList;
  231.  
  232. public class Player
  233. {
  234. private LinkedList<Card> deck;
  235.  
  236. public Player (LinkedList<Card> deck)
  237. {
  238. this.deck = deck;
  239. }
  240.  
  241. public LinkedList<Card> getDeck ()
  242. {
  243. return this.deck;
  244. }
  245. }
  246.  
  247.  
  248. at java.util.LinkedList.removeFirst(LinkedList.java:270)
  249. at java.util.LinkedList.pop(LinkedList.java:801)
  250. at ca.sheridancollege.project.War.play(War.java:23)
  251. at ca.sheridancollege.project.StartTheWar.main(StartTheWar.java:43)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement