<%@ page import="java.text.*,Mastermind" %>
<%

// process page

String action = request.getParameter("action"); String pegTypesString = null;
String pegCountString = null;
Mastermind mastermind = null;
int pegTypes = -1;
int pegCount = -1;

boolean hasError = false;
String message = null;

if (action == null)
{
}
else
{

        if (action.equals("New Game"))
        {
            pegTypesString = request.getParameter("pegTypes");
            pegCountString = request.getParameter("pegCount");

            try
            {
                pegTypes = Integer.valueOf(pegTypesString).intValue();
                pegCount = Integer.valueOf(pegCountString).intValue();
            }
            catch (NumberFormatException nfe)
            {
                hasError = true;
                message = "Enter integers >= 2 for <i># of Peg Types</i> and <i># of Secret Pegs</i>!";
            }

            if (pegTypes < 2 || pegCount < 2)
            {
                hasError = true;
                message = "Enter integers >= 2 for <i># of Peg Types</i> and <i># of Secret Pegs</i>!";
            }
        }
        else if (action.equals("Guess"))
        {
            mastermind = (Mastermind)session.getAttribute("mastermind");
            pegTypes = mastermind.getPegTypes();
            pegCount = mastermind.getPegCount();
            pegTypesString = pegTypes+"";
            pegCountString = pegCount+"";
        }

}

// print page
%>
<html>
<head><title>Master Mind</title></head> <body bgcolor="white">
<font size=3>
Enter <i># of Peg Types</i> and <i># of Secret Pegs</i> and click <i>New Game</i> below to generate a new master mind game. Then select guess pegs and click <i>Guess</i> to guess the secret pegs. After each guess, use the hints provided in the last column of the board to fine-tune further guesses until all secret pegs are revealed. In each hint, <i>m TP</i> and <i>n T</i> means <i>m</i> guess pegs are correct in both type and position and <i>n</i> guess pegs are correct only in type but not in position.

<p>
<form method=POST action=Mastermind.jsp> <%

if (pegTypesString == null || pegTypesString.equals("")) {
%>
<i># of Peg Types</i> <input type=text name=pegTypes size=1 maxlength=1> <%

}
else
{
%>
<i># of Peg Types</i> <input type=text name=pegTypes value=<%= pegTypesString %> size=1 maxlength=1> <%

}
%>
<br>
<%

if (pegCountString == null || pegCountString.equals("")) {
%>
<i># of Secret Peg</i> <input type=text name=pegCount size=1 maxlength=1> <%

}
else
{
%>
<i># of Secret Pegs</i> <input type=text name=pegCount value=<%= pegCountString %> size=1 maxlength=1> <%

}
%>
<p>
<i>
<input type=submit name=action value="New Game"> </i>
<p>
<%

if (action == null)
{
}
else if (hasError)
{
%>
<%= message %>
<%

}
else
{

        if (action.equals("New Game"))
        {
            mastermind = new Mastermind(pegTypes, pegCount);
            mastermind.startTimer();
        }
        else // action.equals("Guess")
        {
            int[] guessPegs = new int[pegCount];
            for (int i=0; i<pegCount; i++)
            {
                guessPegs[i] = Integer.parseInt(request.getParameter("guessPeg"+i));
            }
            mastermind.guess(guessPegs);
        }

        if (mastermind.isDone())
        {

%>
Congrats! You finished the game in <%= mastermind.getGuesses() %> guesses and <%= mastermind.getElapseTime() %>. <%

        }
        else if (mastermind.getGuesses() == mastermind.MAX_GUESSES)
        {

%>
Game Over! You have reached <%= mastermind.MAX_GUESSES %> guesses. <%

        }
        else
        {
            if (action.equals("New Game"))
            {

%>
You used 0 guesses and spent 0' 0'' so far. <%

            }
            else // action.equals("Guess")
            {

%>
You used <%= mastermind.getGuesses() %> guesses and spent <%= mastermind.getElapseTime() %> so far. <%

}
%>
<p>
<i>
<table border=1 cellspacing=0 cellpadding=0> <tr align=center>
<%

            for (int i=0; i<pegCount; i++)
            {

%>
<td>
<select name=guessPeg<%= i %> size=1>
<%

                for (int k=0; k<pegTypes; k++)
                {

%>
<option><%= k+1 %>
<%

}
%>
</select>
<%

}
%>
<td colspan=2><input type=submit name=action value="Guess"> <%

            int[][] guessPegs = mastermind.getGuessPegs();
            for (int i=mastermind.getGuesses()-1; i>=0; i--)
            {

%>
<tr align=center>
<%

                for (int j=0; j<mastermind.getPegCount(); j++)
                {

%>
<td><%= guessPegs[i][j] %>
<%

}

                int perfectMatchCount = mastermind.getPerfectMatchCounts()[i];
                int partialMatchCount = mastermind.getPositionMatchCounts()[i];

%>
<td><%= perfectMatchCount %> TP
<td><%= partialMatchCount %> T
<%

}
%>
</table>
</i>
<%

            session.setAttribute("mastermind", mastermind);
        }

}
%>
</form>
</font>
<hr>
<a href="http://www.computing-wisdom.com"><i>Computing Wisdom Inc. Home Page</i></a> </body>
</html>