import java.util.*; public class Memory { static final String ANSI_CLS = "\u001b[2J"; static final String ANSI_HOME = "\u001b[H"; public static void main(String[] args) { Game g = new Game(); } } class Game { boolean quit = false; int numCards = 16; Card[] cards; Random rnd = new Random(new Date().getTime()); int gameState = 0; int selRow1, selCol1, selRow2, selCol2; int foundMatches = 0; int badMatches = 0; String errMessage = ""; public Game() { initializeDeck(); resetInput(); while(!quit) { clearScreen(); drawScreen(); int inp = getUserInput(); handleInput(inp); } System.out.println("Thanks for playing!"); System.out.println(""); } public void initializeDeck() { cards = new Card[numCards]; for(int i = 0; i < numCards; i++) { cards[i] = new Card(-1); } for(int i = 0; i < numCards/2; i++) { int idx = rnd.nextInt(numCards); while(cards[idx].value != -1) { idx = rnd.nextInt(numCards); } cards[idx].value = i; while(cards[idx].value != -1) { idx = rnd.nextInt(numCards); } cards[idx].value = i; } } public void drawScreen() { printScore(); if(gameState == 0 || gameState == 2) { System.out.println(" 0 1 2 3"); } else { System.out.println(" "); } System.out.println(" v v v v"); int row = 0; for(int i = 0; i < numCards; i++) { int sqrt = (int)Math.sqrt(numCards); if(i % sqrt == 0) { if(gameState == 1 || gameState == 3) { System.out.print(row+">"); } else { System.out.print(" >"); } } System.out.print(" "); cards[i].Draw(); if(i % sqrt == sqrt-1) { System.out.println(""); row++; } } if(!"".equals(errMessage)) { System.out.println(errMessage); } if(gameState == 0 || gameState == 2) { System.out.print("Select Column: "); } else if(gameState == 1 || gameState == 3) { System.out.print("Select Row: "); } } public void resetInput() { selRow1 = selCol1 = -1; selRow2 = selCol2 = -1; } public void handleInput(int i) { int sqrt = (int)Math.sqrt(numCards); if(gameState == 0) { selCol1 = i; hideUnsolvedCards(); errMessage = ""; } else if(gameState == 1) { selRow1 = i; cards[(selRow1*sqrt)+selCol1].visible = true; } else if(gameState == 2) { selCol2 = i; } else if(gameState == 3) { selRow2 = i; cards[(selRow2*sqrt)+selCol2].visible = true; int crd1 = (selRow1*sqrt)+selCol1; int crd2 = (selRow2*sqrt)+selCol2; if(selRow1 == selRow2 && selCol1 == selCol2) { errMessage = "That's the same card..."; } else { if(cards[crd1].value == cards[crd2].value) { cards[crd1].solved = true; cards[crd2].solved = true; foundMatches++; } else { badMatches++; } } resetInput(); if(foundMatches >= numCards) { quit = true; System.out.println("YOU WIN!"); printScore(); } } gameState = (gameState+1) % 4; } public void printScore() { System.out.println("Matches: "+foundMatches+"/"+(numCards/2)+" ("+badMatches+" Incorrect)"); } public void clearScreen() { System.out.println(Memory.ANSI_CLS+Memory.ANSI_HOME); System.out.flush(); } public void hideUnsolvedCards() { for(int i = 0; i < numCards; i++) { cards[i].visible = false; } } public int getUserInput() { Scanner r = new Scanner(System.in); String inp = ""; int ret = -1; while(ret == -1) { inp = r.nextLine(); if("q".equals(inp) || "quit".equals(inp) || "exit".equals(inp)) { quit = true; ret = -2; } else { try { ret = Integer.parseInt(inp); } catch(Exception e) { ret = -1; } } } return ret; } } class Card { boolean visible = false; boolean solved = false; int value; public Card(int v) { this.value = v; } public void Draw() { if(this.visible || this.solved) { System.out.print(this.value); } else { System.out.print("#"); } } }