/* A simple memory game. Generates a random SIZE x SIZE board. Pick the coordianates of the card you want to flip over. If two cards match, they stay face up. Program ends when you match all the cards. Yes, the code is extremely messy. I'm new to programming :P */ import java.util.Scanner; public class Memory { public static final int SIZE = 4; public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Memory[][] board = generateBoard(); printBoard(board); int x, y, a, b; do { System.out.println("Which card do you want to turn over?"); x = keyboard.nextInt() - 1; y = keyboard.nextInt() - 1; if(x >= SIZE || y >= SIZE) { System.out.println("The card doesn't exist!"); continue; } if(board[x][y].matched) { System.out.println("You've already matched this one!"); continue; } turn(board, x, y); System.out.println("Pick another one."); a = keyboard.nextInt() - 1; b = keyboard.nextInt() - 1; if (a == x && b == y) { System.out.println("That's the same card!"); continue; } if(a >= SIZE || b >= SIZE) { System.out.println("The card doesn't exist!"); turnBack(board, x, y); continue; } if(board[a][b].matched) { System.out.println("You've already matched this one!"); continue; } turn(board, a, b); if(board[x][y].getFace() == board[a][b].getFace()) { board[x][y].correct(board); board[a][b].correct(board); System.out.println("It's a match!"); turnBack(board, x, y); turnBack(board, a, b); printBoard(board); } else { System.out.println("Try again."); printBoard(board); turnBack(board, x, y); turnBack(board, a, b); } }while(true); } private boolean faceUp; private boolean matched; private char face; public Memory(char letter) { matched = false; faceUp = false; face = letter; } public static void turn(Memory[][] board, int x, int y) { board[x][y].faceUp = true; printBoard(board); } public static void turnBack(Memory[][] board, int x, int y) { board[x][y].faceUp = false; } public void correct(Memory[][] board) { matched = true; boolean finished = false; for(Memory[] element : board) { for(Memory element2 : element) { if(!element2.matched) return; else finished = true; } } if(finished) System.out.println("Victory!"); System.exit(0); } public char getFace() { return face; } public static Memory[][] generateBoard() { Memory[][] board = new Memory[SIZE][SIZE]; boolean[][] available = new boolean[SIZE][SIZE]; int i1, i2; for(i1 = 0; i1 < SIZE; i1++) { for(i2 = 0; i2 < SIZE; i2++) available[i1][i2] = true; } int freeSpaces = SIZE * SIZE; char letter = 'a'; int numberUsed = 0; int rand1, rand2; do { rand1 = (int)(Math.random() * SIZE); rand2 = (int)(Math.random() * SIZE); if(available[rand1][rand2]) { available[rand1][rand2] = false; board[rand1][rand2] = new Memory(letter); freeSpaces--; numberUsed++; } if(numberUsed == 2) { numberUsed = 0; letter++; } }while(freeSpaces > 0); return board; } public static void showBoard(Memory[][] board) { for(Memory[] element : board) { for (Memory element2 : element) System.out.print(element2.face + " "); System.out.println(); } } public static void printBoard(Memory[][] board) { for(Memory[] element : board) { for(Memory element2 : element) { if(element2.faceUp || element2.matched) System.out.print(element2.face + " "); else System.out.print('x' + " "); } System.out.println(); } } }