/*
 * Created on Dec 12, 2003
 * CS 108
 * Section A
 */
package finalproject;

import ann.easyio.*;

/**
 * @author Eric Knibbe
 *
 * This class implements a command-line based driver for a sliding tile game.
 */
public class SlidingTileCLI {
	
	//default constructor
	public SlidingTileCLI () {
	}

	//main method
	public static void main(String[] args) {
		SlidingTileCLI cli = new SlidingTileCLI();
		SlidingTile st;
		Screen theScreen = new Screen();
		Keyboard theKeyboard = new Keyboard();
		theScreen.println(
			"In this game, you slide numbered tiles around, trying to get them in the right order.");
		while (true) {
			theScreen.print(
				"Enter a number between 3 and 6 for how big you want the game to be: ");
			int size = theKeyboard.readInt();
			if (size >= 3 && size <= 6) {
				st = new SlidingTile(size);
				break;
			}
			theScreen.println(
				"Sorry, you must enter a number between 3 and 6.");
		}
		theScreen.println(
			"This game has "
				+ (st.getSize() * st.getSize())
				+ " tiles. To move a tile, type its number and press return. ");
		while (true) {
			theScreen.print("\n");
			cli.printTiles(st.getSize(), theScreen, st);
			if (st.puzzleIsSovled()) break;
			theScreen.print("Enter a number: ");
			int value = theKeyboard.readInt();
			st.setSelectedTileIndex(value);
			if (st.selectionIsValid(value)) {
				st.exchangeTiles();
			} else {
				theScreen.println("Sorry, that's not a valid selection.");
			}
		}
		theScreen.print("Nice job");
	}
	
	public void printTiles(int size, Screen theScreen, SlidingTile st) {
		int count = 0;
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (st.getMixedArrayItem(count) == (st.getSize() * st.getSize())) {
					theScreen.print("\t \t");
					count++;
				} else {
					theScreen.print(" " + st.getMixedArrayItem(count) + " \t");
					count++;
				}
			}
			theScreen.print("\n");
		}
	}
}
