Advertisement
jackd5011

Bad Code Coding Challenge #41 - Card Match Game

Jul 21st, 2020
1,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. char[][] nums;
  2. boolean[][] matched;
  3. boolean[][] flipped;
  4.  
  5. int nRows = 4;
  6. int nCols = 4;
  7.  
  8. boolean win = false;
  9. float winTime = 0f;
  10.  
  11. void setup() {
  12.   frameRate(60);
  13.   size(500,500);
  14.   ArrayList<Integer> l = new ArrayList<Integer>();
  15.   for(int i = 0; i < (nRows * nCols)/2; i++) {
  16.    l.add(i+65);
  17.    l.add(i+65);
  18.   }
  19.   nums = new char[nRows][nCols];
  20.   for(int r = 0; r < nRows; r++) {
  21.    for(int c = 0; c < nCols; c++) {
  22.      int idx = (int)random(0,l.size());
  23.       nums[r][c] = (char)((int)l.get(idx));
  24.       l.remove(idx);
  25.    }
  26.   }
  27.   matched = new boolean[nRows][nCols];
  28.   flipped = new boolean[nRows][nCols];
  29.   textAlign(CENTER);
  30. }
  31.  
  32. void draw() {
  33.   background(#03CEFF);
  34.   textSize(50);
  35.   for(int r = 0; r < nRows; r++) {
  36.    for(int c = 0; c < nCols; c++) {
  37.        if(matched[r][c] || flipped[r][c]) {
  38.         fill(#FAB505);
  39.        } else {
  40.          fill(#FFD66C);
  41.        }
  42.        rect(40 + 80 * c, 40 + 110 * r, 60, 90);
  43.        if(matched[r][c] || flipped[r][c]) {
  44.         fill(#000000);
  45.         text(nums[r][c], 40 + 80 * c + 30, 40 + 110 * r + 45);
  46.        }
  47.         if(!win && !matched[r][c] && mouseX > 40 + 80 * c && mouseX < 40 + 80 * c + 60 && mouseY > 40 + 110 * r && mouseY < 40 + 110 * r + 90) {
  48.          fill(255,255,255,128);
  49.          rect(40 + 80 * c, 40 + 110 * r, 60, 90);
  50.         }
  51.    }
  52.   }
  53.   if(win) {
  54.    if(winTime > 0f) winTime -= 1/60f;
  55.    if(winTime < 0f) {
  56.      fill(255,255,255,200);
  57.      noStroke();
  58.      rect(0,0,width,height);
  59.      stroke(0);
  60.      fill(0);
  61.      textSize(100);
  62.      text("you win", width/2, height/2);
  63.    }
  64.   }
  65. }
  66.  
  67. void mousePressed() {
  68.   if(win) return;
  69.   int flippedCol = -1, flippedRow = -1;
  70.   for(int r = 0; r < nRows; r++) {
  71.    for(int c = 0; c < nCols; c++) {
  72.       if(flipped[r][c]) {
  73.       flippedCol = c;
  74.       flippedRow = r;
  75.       break;
  76.      }
  77.     }
  78.   }
  79.   for(int r = 0; r < nRows; r++) {
  80.    for(int c = 0; c < nCols; c++) {
  81.      if(matched[r][c]) continue;
  82.     boolean otherFlipe = (flippedCol > -1 && flippedRow > -1);
  83.     if(mouseX > 40 + 80 * c && mouseX < 40 + 80 * c + 60 && mouseY > 40 + 110 * r && mouseY < 40 + 110 * r + 90) {
  84.       if(r == flippedRow && c == flippedCol) {
  85.        flipped[r][c] = false;
  86.        return;
  87.       }
  88.       if(otherFlipe) {
  89.         flipped[flippedRow][flippedCol] = false;
  90.        if(nums[flippedRow][flippedCol] == nums[r][c]) {
  91.         matched[flippedRow][flippedCol] = true;
  92.         matched[r][c] = true;
  93.        }
  94.       } else {
  95.        flipped[r][c] = true;
  96.       }
  97.       int numMatched = 0;
  98.       for(int rr = 0; rr < nRows; rr++) {
  99.        for(int cc = 0; cc < nCols; cc++) {
  100.          if(matched[rr][cc]) numMatched++;
  101.        }
  102.       }
  103.       if(numMatched == nRows * nCols) {
  104.        win = true;
  105.        winTime = 1f;
  106.       }
  107.       return;
  108.     }
  109.    }
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement