import java.util.*;
public class MotGame {

	public static void drawCards(Grid game, Card[] cardArray) {
		game.setLineColor(new Color(255,255,255));
		System.out.println("Your cards:");
		for (int i = 0; i < 9; i++) {
			if (cardArray[i] != null) {
				game.setImage(new Location(0,i), cardArray[i].getColor() + cardArray[i].getNumber() + ".gif");
				System.out.println(i + ": " + cardArray[i]);
			}
            else {
                game.setImage(new Location(0,i),"black.gif");
            }
		}
	}

	public static void main(String[] args) {
		
		Card[] cardArray = new Card[9];
		Grid game = new Grid(1,9);
		game.setTitle("MOT!");
		
		// Complete the main method here according to the instructions
		// given in the assignment:
		
		System.out.println("Welcome to MOT!");
		for (int i = 0; i < 5; i++)
			cardArray[i] = new Card();
		drawCards(game, cardArray);
		
		boolean gameOver = false;
		int numCards = 5;
		Scanner scan = new Scanner(System.in);
		
		while (!gameOver) {
			System.out.print("Pick a card to move to position 0 or enter 0 to get a new card: ");
			int position = scan.nextInt();
			if (position < 0 || position >= numCards)
				System.out.println("INVALID POSITION");
			else if (position >= 1 && position <= numCards-1) {
				if (cardArray[0].getColor().equals(cardArray[position].getColor()) || cardArray[0].getNumber()==cardArray[position].getNumber())
				{	
					cardArray[0].setColor(cardArray[position].getColor());
					cardArray[0].setNumber(cardArray[position].getNumber());
					cardArray[position].setColor(cardArray[numCards-1].getColor());
					cardArray[position].setNumber(cardArray[numCards-1].getNumber());
					cardArray[numCards-1] = null;
					numCards--;
					drawCards(game, cardArray);
					if (numCards == 1)
						gameOver = true;
				}
				else
					System.out.println("INVALID POSITION");
			}
			else  // position == 0
			{
				if (numCards == 9)
					gameOver = true;
				else {
					cardArray[numCards] = new Card();
					numCards++;
				}	
				drawCards(game, cardArray);
			}
		}
		
		if (numCards == 1)
			System.out.println("WINNER");
		else
			System.out.println("LOSER");
		
	}


}
