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

import javax.swing.*;

/**
 * @author Eric Knibbe
 * 
 * This is a GUI driver for a game where the user is presented with a grid of scrambled numbers, 
 * and the goal is to arrange the numbers in such a way so they're in order, much like 
 * those sliding-tile games you can find at dollar stores. 
 */
public class SlidingTileGUI extends JFrame {
	/**
	 * The sliding tile game.
	 */
	private SlidingTile mySlidingTile;
	/**
	 * The GUI view.
	 */
	private SlidingTileFrame myGUI;
	/**
	 * accessor for the GUI view.
	 * @return SlidingTileFrame myGUI
	 */
	public SlidingTileFrame getSlidingTileFrame() {
		return myGUI;
	}

	/**
	 * Default constructor.
	 */
	public SlidingTileGUI() {
	}

	/**
	 * Main method. Creates a new instance of the GUI, reads data from the 
	 * initial dialog box, and creates the game.
	 * @param args
	 */
	public static void main(String[] args) {
		SlidingTileGUI driver = new SlidingTileGUI();
		driver.readData();
		driver.drive();
	}

	/**
	 * Creates a dialog box in which users enter a number between 3 and 6 for how big they 
	 * want the game to be, and sends that value to SlidingTile.
	 */
	public void readData() {
		while (true) {
			String sizeAsString =
				JOptionPane.showInputDialog(
					null,
					"In this game, you slide numbered tiles around, trying to get them in the right order. \nEnter a number between 3 and 6 for how big you want the game to be: ",
					"query",
					JOptionPane.PLAIN_MESSAGE);
			int size = 0;
			try {
				size = Integer.parseInt(sizeAsString);
			} catch (NumberFormatException e) {
			}
			if (size >= 3 && size <= 6) {
				mySlidingTile = new SlidingTile(size);
				break;
			}
			JOptionPane.showMessageDialog(
				null,
				"Sorry, you must enter a number between 3 and 6.",
				"info",
				JOptionPane.PLAIN_MESSAGE);
		}
	}

	/**
	 * Creates the SlidingTileFrame view and sets it up.
	 */
	public void drive() {
		myGUI = new SlidingTileFrame(mySlidingTile);
		myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myGUI.pack();
		myGUI.setVisible(true);
	}

}
