Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char[][] nums;
- boolean[][] matched;
- boolean[][] flipped;
- int nRows = 4;
- int nCols = 4;
- boolean win = false;
- float winTime = 0f;
- void setup() {
- frameRate(60);
- size(500,500);
- ArrayList<Integer> l = new ArrayList<Integer>();
- for(int i = 0; i < (nRows * nCols)/2; i++) {
- l.add(i+65);
- l.add(i+65);
- }
- nums = new char[nRows][nCols];
- for(int r = 0; r < nRows; r++) {
- for(int c = 0; c < nCols; c++) {
- int idx = (int)random(0,l.size());
- nums[r][c] = (char)((int)l.get(idx));
- l.remove(idx);
- }
- }
- matched = new boolean[nRows][nCols];
- flipped = new boolean[nRows][nCols];
- textAlign(CENTER);
- }
- void draw() {
- background(#03CEFF);
- textSize(50);
- for(int r = 0; r < nRows; r++) {
- for(int c = 0; c < nCols; c++) {
- if(matched[r][c] || flipped[r][c]) {
- fill(#FAB505);
- } else {
- fill(#FFD66C);
- }
- rect(40 + 80 * c, 40 + 110 * r, 60, 90);
- if(matched[r][c] || flipped[r][c]) {
- fill(#000000);
- text(nums[r][c], 40 + 80 * c + 30, 40 + 110 * r + 45);
- }
- if(!win && !matched[r][c] && mouseX > 40 + 80 * c && mouseX < 40 + 80 * c + 60 && mouseY > 40 + 110 * r && mouseY < 40 + 110 * r + 90) {
- fill(255,255,255,128);
- rect(40 + 80 * c, 40 + 110 * r, 60, 90);
- }
- }
- }
- if(win) {
- if(winTime > 0f) winTime -= 1/60f;
- if(winTime < 0f) {
- fill(255,255,255,200);
- noStroke();
- rect(0,0,width,height);
- stroke(0);
- fill(0);
- textSize(100);
- text("you win", width/2, height/2);
- }
- }
- }
- void mousePressed() {
- if(win) return;
- int flippedCol = -1, flippedRow = -1;
- for(int r = 0; r < nRows; r++) {
- for(int c = 0; c < nCols; c++) {
- if(flipped[r][c]) {
- flippedCol = c;
- flippedRow = r;
- break;
- }
- }
- }
- for(int r = 0; r < nRows; r++) {
- for(int c = 0; c < nCols; c++) {
- if(matched[r][c]) continue;
- boolean otherFlipe = (flippedCol > -1 && flippedRow > -1);
- if(mouseX > 40 + 80 * c && mouseX < 40 + 80 * c + 60 && mouseY > 40 + 110 * r && mouseY < 40 + 110 * r + 90) {
- if(r == flippedRow && c == flippedCol) {
- flipped[r][c] = false;
- return;
- }
- if(otherFlipe) {
- flipped[flippedRow][flippedCol] = false;
- if(nums[flippedRow][flippedCol] == nums[r][c]) {
- matched[flippedRow][flippedCol] = true;
- matched[r][c] = true;
- }
- } else {
- flipped[r][c] = true;
- }
- int numMatched = 0;
- for(int rr = 0; rr < nRows; rr++) {
- for(int cc = 0; cc < nCols; cc++) {
- if(matched[rr][cc]) numMatched++;
- }
- }
- if(numMatched == nRows * nCols) {
- win = true;
- winTime = 1f;
- }
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement