Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. //////////////////// ALL ASSIGNMENTS INCLUDE THIS SECTION /////////////////////
  2. //
  3. // Title: Matching Game(Project 2-CS300)
  4. // Files: MatchingGame.java
  5. // Course: CS 300, Fall, 2019
  6. //
  7. // Author: Parker Swanson
  8. // Email: pjswanson2@wisc.edu
  9. // Lecturer's Name: Mouna Kacem
  10. //
  11. //////////////////// PAIR PROGRAMMERS COMPLETE THIS SECTION ///////////////////
  12. //
  13. // Partner Name: Cameron Hake
  14. // Partner Email: chake@wisc.edu
  15. // Partner Lecturer's Name: Mouna Kacem
  16. //
  17. // VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
  18. // __X_ Write-up states that pair programming is allowed for this assignment.
  19. // __X_ We have both read and understand the course Pair Programming Policy.
  20. // __X_ We have registered our team prior to the team registration deadline.
  21. //
  22. ///////////////////////////// CREDIT OUTSIDE HELP /////////////////////////////
  23. //
  24. // Students who get help from sources other than their partner must fully
  25. // acknowledge and credit those sources of help here. Instructors and TAs do
  26. // not need to be credited here, but tutors, friends, relatives, room mates,
  27. // strangers, and others do. If you received no outside help from either type
  28. // of source, then please explicitly indicate NONE.
  29. //
  30. // Persons: NONE
  31. // Online Sources: NONE
  32. //
  33. /////////////////////////////// 80 COLUMNS WIDE ///////////////////////////////
  34.  
  35. import java.io.File;
  36. import java.util.Random;
  37. import processing.core.PApplet;
  38. import processing.core.PImage;
  39.  
  40. /**
  41. * @author Parker Swanson
  42. *This class creates and allows a user to play a matching card game.
  43. */
  44. public class MatchingGame {
  45. //Congratulations message
  46. private final static String CONGRA_MSG = "CONGRATULATIONS! YOU WON!";
  47. //Cards not matched message
  48. private final static String NOT_MATCHED = "CARDS NOT MATCHED. Try again!";
  49. //Cards matched message
  50. private final static String MATCHED = "CARDS MATCHED! Good Job!";
  51. //2D-array which stores cards coordinates on the window display
  52. private final static float[][] CARDS_COORDINATES =
  53. new float[][] {{170, 170}, {324, 170}, {478, 170}, {632, 170},
  54. {170, 324}, {324, 324}, {478, 324}, {632, 324},
  55. {170, 478}, {324, 478}, {478, 478}, {632, 478}};
  56. //Array that stores the card images filenames
  57. private final static String[] CARD_IMAGES_NAMES = new String[] {"apple.png",
  58. "ball.png", "peach.png", "redFlower.png", "shark.png", "yellowFlower.png"};
  59.  
  60. private static PApplet processing; // PApplet object that represents
  61. //the graphic display window
  62. private static Card[] cards; // one dimensional array of cards
  63. private static PImage[] images; // array of images of the different cards
  64. private static Random randGen; // generator of random numbers
  65. private static Card selectedCard1; // First selected card
  66. private static Card selectedCard2; // Second selected card
  67. private static boolean winner; // boolean evaluated true if the game is won,
  68. //and false otherwise
  69. private static int matchedCardsCount; // number of cards matched so far
  70. //in one session of the game
  71. private static String message; // Displayed message to the display window
  72.  
  73. private static int count = 0; //tracker to help with the mousePressed method
  74.  
  75. /**
  76. * Initializes the Game
  77. */
  78. public static void initGame() {
  79. randGen = new Random(Utility.getSeed());
  80. selectedCard1 = null;
  81. selectedCard2 = null;
  82. matchedCardsCount = 0;
  83. winner = false;
  84. message = "";
  85.  
  86. cards = new Card[CARD_IMAGES_NAMES.length*2];
  87. int[] positions = new int[12];
  88. int runner = 0;
  89.  
  90. //runs until the board has been filled with 12 cards
  91. while (runner < 12){
  92. int randInt = randGen.nextInt(6);//creates a random number between 0 and 5
  93.  
  94. if (positions[randInt] < 2){//sees if the random number has already been used twice
  95. positions[randInt]++;
  96. cards[runner] = new Card(images[randInt], CARDS_COORDINATES[runner][0],
  97. CARDS_COORDINATES[runner][1]);//creates a card and assigns it to a spot on the board
  98. runner++;
  99. }
  100. }
  101. }
  102.  
  103. /**
  104. * Displays a given message to the display window
  105. * @param message to be displayed to the display window
  106. */
  107. public static void displayMessage(String message) {
  108. processing.fill(0);
  109. processing.textSize(20);
  110. processing.text(message, processing.width / 2, 50);
  111. processing.textSize(12);
  112. }
  113.  
  114. /**
  115. * Callback method called each time the user presses a key
  116. */
  117. public static void keyPressed() {
  118. //checks to see if the key pressed is an n or N
  119. if (processing.key == 'n' || processing.key == 'N') {
  120. initGame();
  121. count = 0;
  122. }
  123. }
  124.  
  125. /**
  126. * Callback method draws continuously this application window display
  127. */
  128. public static void draw() {
  129. // Set the color used for the background of the Processing window
  130. processing.background(245, 255, 250); // Mint cream color
  131.  
  132. //draws all of the cards
  133. for (int i = 0; i < cards.length; i ++) {
  134. cards[i].draw();
  135. }
  136.  
  137. displayMessage(message);
  138. }
  139.  
  140. /**
  141. * Checks whether the mouse is over a given Card
  142. * @return true if the mouse is over the storage list, false otherwise
  143. */
  144. public static boolean isMouseOver(Card card) {
  145. int xPosition = processing.mouseX;
  146. int yPosition = processing.mouseY;
  147. int height = card.getHeight();
  148. int width = card.getWidth();
  149.  
  150. //checks to see if the mouse is over a specified card
  151. if (((yPosition >= card.getY() - height/2) && (yPosition <= card.getY() + height/2)) &&
  152. ((xPosition >= card.getX() - width/2) && (xPosition <= card.getX() + width/2))){
  153. return true;
  154. } else {
  155. return false;
  156. }
  157.  
  158. }
  159.  
  160. /**
  161. * Callback method called each time the user presses the mouse
  162. */
  163. public static void mousePressed() {
  164. if ((count == 2) && (matchingCards(selectedCard1, selectedCard2) != true)) {
  165. selectedCard1.setVisible(false);
  166. selectedCard2.setVisible(false);
  167. selectedCard1.deselect();
  168. selectedCard2.deselect();
  169. selectedCard1 = null;
  170. selectedCard2 = null;
  171. count = 0;
  172. message = NOT_MATCHED;
  173. displayMessage(message);
  174. } else if ((count == 2) && (matchingCards(selectedCard1, selectedCard2))) {
  175. selectedCard1.setVisible(true);
  176. selectedCard2.setVisible(true);
  177. count = 0;
  178. matchedCardsCount ++;
  179. if (matchedCardsCount == 6) {
  180. message = CONGRA_MSG;
  181. } else {
  182. message = MATCHED;
  183. }
  184. displayMessage(message);
  185. }
  186.  
  187. //sets selectedCard1 and selectedCard2 to the images the user clicked
  188. for (int i = 0; i < cards.length; i ++) {
  189. if ((isMouseOver(cards[i])) && (count == 0)) {
  190. selectedCard1 = cards[i];
  191. selectedCard1.setVisible(true);
  192. selectedCard1.select();
  193. count ++;
  194. return;
  195. } else if (isMouseOver(cards[i])) {
  196. selectedCard2 = cards[i];
  197. selectedCard2.setVisible(true);
  198. selectedCard2.select();
  199. count = 2;
  200. break;
  201. }
  202. }
  203. }
  204.  
  205.  
  206. /**
  207. * Checks whether two cards match or not
  208. * @param card1 reference to the first card
  209. * @param card2 reference to the second card
  210. * @return true if card1 and card2 image references are the same, false otherwise
  211. */
  212. public static boolean matchingCards(Card card1, Card card2) {
  213. //checks if the two selected images are equal to each other
  214. if (card1.getImage().equals(card2.getImage())){
  215. return true;
  216. } else {
  217. return false;
  218. }
  219. }
  220.  
  221. /**
  222. * Defines the initial environment properties of this game as the program starts
  223. */
  224. public static void setup(PApplet processing) {
  225. MatchingGame.processing = processing;
  226.  
  227.  
  228. MatchingGame.images = new PImage[CARD_IMAGES_NAMES.length];
  229. //load apple.png image file as PImage object and store its reference into images[0]
  230. images[0] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[0]);
  231. //load apple.png image file as PImage object and store its reference into images[1]
  232. images[1] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[1]);
  233. //load apple.png image file as PImage object and store its reference into images[2]
  234. images[2] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[2]);
  235. //load apple.png image file as PImage object and store its reference into images[3]
  236. images[3] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[3]);
  237. //load apple.png image file as PImage object and store its reference into images[4]
  238. images[4] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[4]);
  239. //load apple.png image file as PImage object and store its reference into images[5]
  240. images[5] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[5]);
  241. initGame();
  242. }
  243.  
  244.  
  245. /**
  246. * Starts and runs the whole matching game
  247. * @param args
  248. */
  249. public static void main(String[] args) {
  250. // TODO Auto-generated method stub
  251. Utility.runApplication(); // starts the application
  252.  
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement