import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.util.Random; // Copyright 2007 by Chrix - visit www.funsciencewithyourcomputer.org public class SmartMonkey { public static void main(String[] args) throws Exception { Screen screen = new Screen(); screen.setVisible(true); screen.showBoxes(); Thread.sleep(5000); screen.startGame(); while (!screen.isGameOver()) Thread.sleep(1000); } static class Screen extends Frame { int WIDTH = 900; int BOX_WIDTH = 100; int NBR_BOXES = 5; int REVEALED = 1, MASKED = 2, FOUND = 3, FAILED = 4; int[][] boxes = new int[NBR_BOXES][3]; int currentBox = -1; int nErrors = 0; public Screen() { super("Smart Monkey"); setSize(WIDTH, WIDTH); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (currentBox == -1) return; int box = getBox(e.getX() - 50, e.getY() - 50); if (box < 0) return; if (box == currentBox) { boxes[currentBox][2] = FOUND; currentBox++; } else { boxes[box][2] = FAILED; nErrors++; } repaint(); } }); } public void showBoxes() { Random random = new Random(); for (int i = 0; i < NBR_BOXES; i++) { int x, y; do { x = random.nextInt(WIDTH - BOX_WIDTH - 50); y = random.nextInt(WIDTH - BOX_WIDTH - 50); } while (overlap(x, y, i)); boxes[i][0] = x; boxes[i][1] = y; boxes[i][2] = REVEALED; } repaint(); } public void startGame() { for (int i = 0; i < NBR_BOXES; i++) boxes[i][2] = MASKED; currentBox = 0; repaint(); } boolean overlap(int x, int y, int nbr) { for (int i = 0; i < nbr; i++) { if (Math.abs(boxes[i][0] - x) < BOX_WIDTH && Math.abs(boxes[i][1] - y) < BOX_WIDTH) return true; } return false; } public void paint(Graphics g) { BufferedImage image = (BufferedImage) createImage(WIDTH, WIDTH); Graphics g2 = image.getGraphics(); for (int number = 0; number < NBR_BOXES; number++) { int x = boxes[number][0]; int y = boxes[number][1]; int state = boxes[number][2]; drawBox(g2, x, y, state, number + 1); } g.drawImage(image, 50, 50, this); if (isGameOver()) drawWinner(g); } public void update(Graphics g) { paint(g); } void drawBox(Graphics g, int x, int y, int state, int number) { if (state == FOUND) return; if (state == FAILED) g.setColor(Color.RED); else g.setColor(Color.BLACK); if (state == REVEALED) { g.setFont(new Font("Tahoma", Font.BOLD, 72)); g.drawString("" + number, x + BOX_WIDTH / 2, y + BOX_WIDTH / 2); } else { g.fillRect(x, y, BOX_WIDTH, BOX_WIDTH); } } void drawWinner(Graphics g) { g.setFont(new Font("Tahoma", Font.PLAIN, 32)); g.drawString("Errors: "+nErrors, WIDTH / 3, WIDTH / 5); if (nErrors <= 1) g.drawString("You are a smart monkey", WIDTH / 3, WIDTH / 3); else g.drawString("You are a human", WIDTH / 3, WIDTH / 3); } int getBox(int x, int y) { for (int i = 0; i < NBR_BOXES; i++) { if (x >= boxes[i][0] && x < boxes[i][0] + BOX_WIDTH && y >= boxes[i][1] && y < boxes[i][1] + BOX_WIDTH) return i; } return -1; } boolean isGameOver() { return currentBox == NBR_BOXES; } } }