/**
 * @(#)LetterMove.java	11/03/05
 * 
 * Copyright 2005 3GP01, KCL.  All unmodified code is copyright of its respective owner.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * The GNU General Public Licence should be contained within licence.txt;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.util.*;

/**
 * This class defines a LetterMove - a specific Letter coupled with x and y co-ordinates.
 * The class implements methods to get and set a LetterMove.
 * Multiple LetterMoves make up a <b>Move</b> 
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see LetterMove
 * @see Word
 */

public class LetterMove 
{
	private int x;

	private int y;

	private Letter letter;

	public LetterMove(int xx, int yy, Letter l) 
	{
		x = xx;
		y = yy;
		letter = l;
	}

	public int getX() 
	{
		return x;
	}

	public int getY() 
	{
		return y;
	}

	public Letter getLetter() 
	{
		return letter;
	}

	public List findHorizontalWords(Board b) 
	{
		List res = new ArrayList();
		// if no left-right adjacent letters:
		if (x == 0 || !b.isOccupied(x - 1, y)) 
		{
			if (x == 14 || !b.isOccupied(x + 1, y)) 
			{ // System.out.println("Nothing to left or right of " + x +
				//                    " " + y);
				return res;
			}
			int i = x + 1;
			Word w = new Word(x, y, 0, y); // endx unknown
			w.addEnd(letter);
			// System.out.println("New word: " + w); 
			while (i <= 14 && b.isOccupied(i, y)) 
			{
				w.addEnd(b.getSquare(i, y).getLetter());
				//System.out.println("Found word: " + w); 
				i++;
			}
			w.setEndX(i - 1);
			res.add(w);
			return res;
		}
		int k = x - 1;
		Word w1 = new Word(0, y, 0, y); // xstart, xend?
		// System.out.println("New word: " + w1); 
		w1.addEnd(letter);

		while (k >= 0 && b.isOccupied(k, y)) 
		{
			w1.addFront(b.getSquare(k, y).getLetter());
			k--;
		}
		w1.setStartX(k + 1);
		int i = x + 1;
		while (i <= 14 && b.isOccupied(i, y)) 
		{
			w1.addEnd(b.getSquare(i, y).getLetter());
			// System.out.println("Found word: " + w1); 
			i++;
		}
		w1.setEndX(i - 1);
		res.add(w1);
		return res;
	}

	public List findVerticalWords(Board b) 
	{
		List res = new ArrayList();
		// if no up-down adjacent letters:
		if (y == 0 || !b.isOccupied(x, y - 1)) {
			if (y == 14 || !b.isOccupied(x, y + 1)) { // System.out.println("Nothing above, below " + x +
				//                    " " + y);
				return res;
			}
			int j = y + 1;
			Word w = new Word(x, y, x, 0); // endy unknown
			w.addEnd(letter);
			// System.out.println("New word: " + w); 
			while (j <= 14 && b.isOccupied(x, j)) 
			{
				w.addEnd(b.getSquare(x, j).getLetter());
				// System.out.println("Found word: " + w); 
				j++;
			}
			w.setEndY(j - 1);
			res.add(w);
			return res;
		}
		int k = y - 1;
		Word w1 = new Word(x, 0, x, 0);
		// System.out.println("New word: " + w1); 
		w1.addEnd(letter);
		while (k >= 0 && b.isOccupied(x, k)) 
		{
			w1.addFront(b.getSquare(x, k).getLetter());
			// System.out.println("Found word: " + w1); 
			k--;
		}
		w1.setStartY(k + 1);
		int j = y + 1;
		while (j <= 14 && b.isOccupied(x, j)) 
		{
			w1.addEnd(b.getSquare(x, j).getLetter());
			// System.out.println("Found word: " + w1); 
			j++;
		}
		w1.setEndY(j - 1);
		res.add(w1);
		return res;
	}

	public int getScore(Board b) 
	{
		Square square = b.getSquare(x, y);
		return square.getLetterScore(letter);
	}

	public String toString() 
	{
		return "letter move: " + letter + " At: " + x + " " + y;
	}

	public String toXml() 
	{
		String res = "  <lettermove>\n" + "    <x>" + x + "</x>\n" + "    <y>"
				+ y + "</y>\n" + letter.toXml() + "  </lettermove>\n";
		return res;
	}
}

