-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeServlet.java
144 lines (127 loc) · 5.36 KB
/
TicTacToeServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
public class TicTacToeServlet extends HttpServlet {
private static final long serialVersionUID = -3388076538168097844L;
/**
* Handle Get requests
*
* @param req The request
* @param resp The response
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
HttpSession session = req.getSession();
int winner = -1;
Move move = null;
if (req.getRequestURI().equalsIgnoreCase("/bin/start_game")) {
Game game = null;
if (req.getParameter("botlist").equals("easy")) {
game = new Game(new PlayerEasy());
session.setAttribute("bot", "Easy");
} else if (req.getParameter("botlist").equals("aggressive")) {
game = new Game(new PlayerAggressive());
session.setAttribute("bot", "Aggressive");
} else {
game = new Game(new PlayerDefensive());
session.setAttribute("bot", "Defensive");
}
String[][] board = { { "?", "?", "?" }, { "?", "?", "?" }, { "?", "?", "?" } };
// TODO: Store the board and game variables in the user's session
session.setAttribute("game", game);
session.setAttribute("board", board);
} else if (req.getRequestURI().equalsIgnoreCase("/bin/make_move")) {
// TODO: set the move variable to point to a new Move object. The row and column
// for the Move object should come from the parameters sent with the HTML form.
move = new Move(Integer.valueOf(req.getParameter("row")), Integer.valueOf(req.getParameter("column")));
// TODO: Retrieve the Game object that is saved in the session
Game game = (Game) session.getAttribute("game");
// TODO: Activate the makeMove method on the game object with the user's move,
// and store the return value in the winner variable
winner = game.makeMove(move);
// TODO: Replace the "???" in the
// row below to update the appropriate cell in the array to the result of the
// move.
// board[???][???] = move.getResult()==1?"X":"O";
String[][] board = (String[][]) session.getAttribute("board");
board[move.getRow()][move.getColumn()] = move.getResult() == 1 ? "X" : "O";
} else {
log("NOT_FOUND: " + req.getRequestURL());
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
drawBoard(resp.getWriter(), session, winner, move);
}
private void drawBoard(PrintWriter out, HttpSession session, int winner, Move move) {
out.println("<!doctype html>");
out.println("<html>");
out.println("<head><title>Extreme Tic Tac Toe</title></head>");
out.println("<body style=\"text-align: center;\">");
// TODO: Update the line below so that the ??? is replaced with the type of bot
// they are playing
// out.println("<H1>Extreme Tic Tac Toe vs. ??? Bot</H1>");
out.println("<H1>Extreme Tic Tac Toe vs. " + session.getAttribute("bot") + " Bot</H1>");
String[][] board = (String[][]) session.getAttribute("board");
if (winner != -1) {
Game game = (Game) session.getAttribute("game");
board = game.getBoardState();
switch (winner) {
case 0:
out.println("<H2>The game ended in a tie!</H2>");
break;
case 1:
out.println("<H2>You lost!</H2>");
break;
case 2:
out.println("<H2>You won!</H2>");
break;
}
}
// TODO: If the user has just made a move, output a line
// "You [successfully chose|failed to select] row # and column #"
if (move != null)
if (move.getResult() == 2)
out.println("<P>You successfully chose row " + move.getRow() + " and column " + move.getColumn());
else
out.println("<P>You failed to select row " + move.getRow() + " and column " + move.getColumn());
out.println("<table border=1 style=\"margin-left:auto;margin-right:auto;\">");
out.println("<tr><th> </th>");
for (int i = 0; i < 3; i++) {
out.println("<th>" + i + "</th>");
}
out.println("</tr>");
for (int i = 0; i < 3; i++) {
out.println("<tr><th>" + i + "</th>");
for (int j = 0; j < 3; j++) {
out.println("<td>");
// TODO: Fix the output of the cell, so that it is in red for the cell that
// the user just chose
if (move != null && move.getRow() == i && move.getColumn() == j)
out.println("<H2 style =\"color:red;\">");
else
out.println("<H2>");
out.println(" " + board[i][j] + " </h2></th>");
}
out.println("</tr>");
}
out.println("</table>");
// TODO: Change the code below, so that you output a link to return to the main
// page
// (/index.html), if the winner variable is not equal to -1
if (winner == -1) {
out.println("<P><EM>You are playing O's</EM></P>");
out.println("<form action=\"make_move\" method=\"get\">");
out.println("<p>Select the row and column for your move.");
out.println("<P>Row (between 0 and 2): <input name=\"row\" type=\"number\" min=\"0\" max=\"2\">");
out.println("<P>Column (between 0 and 2): <input name=\"column\" type=\"number\" min=\"0\" max=\"2\">");
out.println("<p><input type=\"submit\">");
} else {
out.println("<P><A HREF=\"/index.html\">Return to main page</A>");
}
out.println("</body>");
out.println("</html>");
}
}