package edu.compsci;

import cs1.android.*;

// Implementation of the popular memory game in which the
// goal is to match pairs of hidden tiles, such that on each
// move only a pair of tiles can be revealed.

public class MemoryTilesApp extends AndroidApp
{
	// board's upper-left corner and flag/cell size
	final int BOARD_ULX = 61;
	final int BOARD_ULY = 141;
	final int FLAG_SIZE = 66;

	// Draw the game game board as a grid of images.
	// "blank.png" - images were guessed correctly
	// "hidden.png" - images left to guess
	public void drawBoard(String[][] tiles)
	{
		canvas.clear();
		for (int r = 0; r < tiles.length; ++r) {
			for (int c = 0; c < tiles[r].length; ++c) {
				int x = BOARD_ULX + c*FLAG_SIZE;
				int y = BOARD_ULY + r*FLAG_SIZE;
				if (tiles[r][c].equals("blank.png")) {
					canvas.drawImage(x, y, "blank.png");
				}
				else {
					canvas.drawImage(x, y, "hidden.png");
				}
			}
		}
	}

	public void run() 
	{	
		// the empty board representations
		String[][] tiles = { 
				{"cl.png", "mw.png", "np.png", "au.png"},
				{"us.png", "bg.png", "gh.png", "np.png"},
				{"gh.png", "au.png", "cl.png", "kr.png"},
				{"bg.png", "mw.png", "kr.png", "us.png"}
		};

		canvas.setBackground("DarkKhaki");

		// as long as player has not guessed all tiles
		int hiddenPairs = tiles.length*tiles.length / 2;		
		while (hiddenPairs > 0) {
			drawBoard(tiles);

			canvas.drawText(canvas.getWidth()/2, canvas.getHeight()/8, 
					"Touch two tiles!", 24, "white");

			// wait for the player to touch a cell and convert the touch
			// coordinates to (row, col) position in the board.			
			Touch touch = canvas.waitForTouch();
			int row1 = (touch.getY() - BOARD_ULY + FLAG_SIZE/2) / FLAG_SIZE;
			int col1 = (touch.getX() - BOARD_ULX + FLAG_SIZE/2) / FLAG_SIZE;

			// fnid the center of the touched cell in order to draw the image
			// since the touch is not necessarily in the center
			int x1 = BOARD_ULX + col1*FLAG_SIZE;
			int y1 = BOARD_ULY + row1*FLAG_SIZE;
			canvas.drawImage(x1, y1, tiles[row1][col1]);

			// same for the next tile -- wait for touch, convert to position
			// in the game board and find where to draw the image (see above)
			touch = canvas.waitForTouch();
			int row2 = (touch.getY() - BOARD_ULY + FLAG_SIZE/2) / FLAG_SIZE;
			int col2 = (touch.getX() - BOARD_ULX + FLAG_SIZE/2) / FLAG_SIZE;

			int x2 = BOARD_ULX + col2*FLAG_SIZE;
			int y2 = BOARD_ULY + row2*FLAG_SIZE;
			canvas.drawImage(x2, y2, tiles[row2][col2]);

			// give the player a chance to remember the tiles
			canvas.sleep(1);

			// check if the two tiles are the same and update the board
			if (tiles[row1][col1].equals(tiles[row2][col2]) &&
					!tiles[row1][col1].equals("blank")) 
			{
				tiles[row1][col1] = "blank.png";
				tiles[row2][col2] = "blank.png";
				hiddenPairs = hiddenPairs - 1;
			}
		}

		// refresh the board and congratulate the player
		drawBoard(tiles);
		canvas.drawText(canvas.getWidth()/2, canvas.getHeight()/8, "Well done!", 24, "white");
	}
}
