require "braingame"

class Text
	def initialize
		board = GameBoard.new
		while true
			show_menu
			case input = get_input
			when /^\s*\d\s*,\s*\d/
				x,y = input.scan(/^\s*(\d)\s*,\s*(\d)/)[0]
				x = x.to_i
				y = y.to_i
				@board.click x,y
				puts @board
				win if @board.win?
			when 'n'
				@board = GameBoard.new
				@board.scramble
				puts @board
			when 'u'
				@board.undo
				puts @board
			when 'q'
				return
			when 'p'
				puts @board
			else
				puts "Unknown command '#{input}'"
			end
		end
	end

	MENU="  n]ew game\n  u]ndo\n  q]uit\n  p]rint\n  x,y -> click"
	def show_menu
		puts MENU
	end

	def get_input
		return gets.chop
	end

	def win
		puts "Congratulations!  You won!"
		@board = GameBoard.new
	end
end
	

Text.new
