Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////// ALL ASSIGNMENTS INCLUDE THIS SECTION /////////////////////
- //
- // Title: Matching Game(Project 2-CS300)
- // Files: MatchingGame.java
- // Course: CS 300, Fall, 2019
- //
- // Author: Parker Swanson
- // Email: pjswanson2@wisc.edu
- // Lecturer's Name: Mouna Kacem
- //
- //////////////////// PAIR PROGRAMMERS COMPLETE THIS SECTION ///////////////////
- //
- // Partner Name: Cameron Hake
- // Partner Email: chake@wisc.edu
- // Partner Lecturer's Name: Mouna Kacem
- //
- // VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
- // __X_ Write-up states that pair programming is allowed for this assignment.
- // __X_ We have both read and understand the course Pair Programming Policy.
- // __X_ We have registered our team prior to the team registration deadline.
- //
- ///////////////////////////// CREDIT OUTSIDE HELP /////////////////////////////
- //
- // Students who get help from sources other than their partner must fully
- // acknowledge and credit those sources of help here. Instructors and TAs do
- // not need to be credited here, but tutors, friends, relatives, room mates,
- // strangers, and others do. If you received no outside help from either type
- // of source, then please explicitly indicate NONE.
- //
- // Persons: NONE
- // Online Sources: NONE
- //
- /////////////////////////////// 80 COLUMNS WIDE ///////////////////////////////
- import java.io.File;
- import java.util.Random;
- import processing.core.PApplet;
- import processing.core.PImage;
- /**
- * @author Parker Swanson
- *This class creates and allows a user to play a matching card game.
- */
- public class MatchingGame {
- //Congratulations message
- private final static String CONGRA_MSG = "CONGRATULATIONS! YOU WON!";
- //Cards not matched message
- private final static String NOT_MATCHED = "CARDS NOT MATCHED. Try again!";
- //Cards matched message
- private final static String MATCHED = "CARDS MATCHED! Good Job!";
- //2D-array which stores cards coordinates on the window display
- private final static float[][] CARDS_COORDINATES =
- new float[][] {{170, 170}, {324, 170}, {478, 170}, {632, 170},
- {170, 324}, {324, 324}, {478, 324}, {632, 324},
- {170, 478}, {324, 478}, {478, 478}, {632, 478}};
- //Array that stores the card images filenames
- private final static String[] CARD_IMAGES_NAMES = new String[] {"apple.png",
- "ball.png", "peach.png", "redFlower.png", "shark.png", "yellowFlower.png"};
- private static PApplet processing; // PApplet object that represents
- //the graphic display window
- private static Card[] cards; // one dimensional array of cards
- private static PImage[] images; // array of images of the different cards
- private static Random randGen; // generator of random numbers
- private static Card selectedCard1; // First selected card
- private static Card selectedCard2; // Second selected card
- private static boolean winner; // boolean evaluated true if the game is won,
- //and false otherwise
- private static int matchedCardsCount; // number of cards matched so far
- //in one session of the game
- private static String message; // Displayed message to the display window
- private static int count = 0; //tracker to help with the mousePressed method
- /**
- * Initializes the Game
- */
- public static void initGame() {
- randGen = new Random(Utility.getSeed());
- selectedCard1 = null;
- selectedCard2 = null;
- matchedCardsCount = 0;
- winner = false;
- message = "";
- cards = new Card[CARD_IMAGES_NAMES.length*2];
- int[] positions = new int[12];
- int runner = 0;
- //runs until the board has been filled with 12 cards
- while (runner < 12){
- int randInt = randGen.nextInt(6);//creates a random number between 0 and 5
- if (positions[randInt] < 2){//sees if the random number has already been used twice
- positions[randInt]++;
- cards[runner] = new Card(images[randInt], CARDS_COORDINATES[runner][0],
- CARDS_COORDINATES[runner][1]);//creates a card and assigns it to a spot on the board
- runner++;
- }
- }
- }
- /**
- * Displays a given message to the display window
- * @param message to be displayed to the display window
- */
- public static void displayMessage(String message) {
- processing.fill(0);
- processing.textSize(20);
- processing.text(message, processing.width / 2, 50);
- processing.textSize(12);
- }
- /**
- * Callback method called each time the user presses a key
- */
- public static void keyPressed() {
- //checks to see if the key pressed is an n or N
- if (processing.key == 'n' || processing.key == 'N') {
- initGame();
- count = 0;
- }
- }
- /**
- * Callback method draws continuously this application window display
- */
- public static void draw() {
- // Set the color used for the background of the Processing window
- processing.background(245, 255, 250); // Mint cream color
- //draws all of the cards
- for (int i = 0; i < cards.length; i ++) {
- cards[i].draw();
- }
- displayMessage(message);
- }
- /**
- * Checks whether the mouse is over a given Card
- * @return true if the mouse is over the storage list, false otherwise
- */
- public static boolean isMouseOver(Card card) {
- int xPosition = processing.mouseX;
- int yPosition = processing.mouseY;
- int height = card.getHeight();
- int width = card.getWidth();
- //checks to see if the mouse is over a specified card
- if (((yPosition >= card.getY() - height/2) && (yPosition <= card.getY() + height/2)) &&
- ((xPosition >= card.getX() - width/2) && (xPosition <= card.getX() + width/2))){
- return true;
- } else {
- return false;
- }
- }
- /**
- * Callback method called each time the user presses the mouse
- */
- public static void mousePressed() {
- if ((count == 2) && (matchingCards(selectedCard1, selectedCard2) != true)) {
- selectedCard1.setVisible(false);
- selectedCard2.setVisible(false);
- selectedCard1.deselect();
- selectedCard2.deselect();
- selectedCard1 = null;
- selectedCard2 = null;
- count = 0;
- message = NOT_MATCHED;
- displayMessage(message);
- } else if ((count == 2) && (matchingCards(selectedCard1, selectedCard2))) {
- selectedCard1.setVisible(true);
- selectedCard2.setVisible(true);
- count = 0;
- matchedCardsCount ++;
- if (matchedCardsCount == 6) {
- message = CONGRA_MSG;
- } else {
- message = MATCHED;
- }
- displayMessage(message);
- }
- //sets selectedCard1 and selectedCard2 to the images the user clicked
- for (int i = 0; i < cards.length; i ++) {
- if ((isMouseOver(cards[i])) && (count == 0)) {
- selectedCard1 = cards[i];
- selectedCard1.setVisible(true);
- selectedCard1.select();
- count ++;
- return;
- } else if (isMouseOver(cards[i])) {
- selectedCard2 = cards[i];
- selectedCard2.setVisible(true);
- selectedCard2.select();
- count = 2;
- break;
- }
- }
- }
- /**
- * Checks whether two cards match or not
- * @param card1 reference to the first card
- * @param card2 reference to the second card
- * @return true if card1 and card2 image references are the same, false otherwise
- */
- public static boolean matchingCards(Card card1, Card card2) {
- //checks if the two selected images are equal to each other
- if (card1.getImage().equals(card2.getImage())){
- return true;
- } else {
- return false;
- }
- }
- /**
- * Defines the initial environment properties of this game as the program starts
- */
- public static void setup(PApplet processing) {
- MatchingGame.processing = processing;
- MatchingGame.images = new PImage[CARD_IMAGES_NAMES.length];
- //load apple.png image file as PImage object and store its reference into images[0]
- images[0] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[0]);
- //load apple.png image file as PImage object and store its reference into images[1]
- images[1] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[1]);
- //load apple.png image file as PImage object and store its reference into images[2]
- images[2] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[2]);
- //load apple.png image file as PImage object and store its reference into images[3]
- images[3] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[3]);
- //load apple.png image file as PImage object and store its reference into images[4]
- images[4] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[4]);
- //load apple.png image file as PImage object and store its reference into images[5]
- images[5] = processing.loadImage("images" + File.separator + CARD_IMAGES_NAMES[5]);
- initGame();
- }
- /**
- * Starts and runs the whole matching game
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Utility.runApplication(); // starts the application
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement