284
|
1 |
% !TEX program = xelatex
|
6
|
2 |
\documentclass{article}
|
|
3 |
\usepackage{chessboard}
|
|
4 |
\usepackage[LSBC4,T1]{fontenc}
|
149
|
5 |
\let\clipbox\relax
|
426
|
6 |
\usepackage{../styles/style}
|
|
7 |
\usepackage{../styles/langs}
|
166
|
8 |
\usepackage{disclaimer}
|
379
|
9 |
\usepackage{ulem}
|
476
|
10 |
%\usepackage{tipauni}
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
\tikzset
|
|
15 |
{%
|
|
16 |
pics/piece/.style n args={1}{
|
|
17 |
code={%
|
|
18 |
\fill[rounded corners] (-0.1,-0.1) rectangle (-0.9, -0.9);
|
|
19 |
\fill[left color=white,rounded corners,
|
|
20 |
right color=gray,
|
|
21 |
opacity=0.7] (-0.1,-0.1) rectangle (-0.9, -0.9);
|
|
22 |
\draw[line width=0.4mm,rounded corners] (-0.1,-0.1) rectangle (-0.9, -0.9);
|
|
23 |
\draw[line width=0.2mm,rounded corners] (-0.2,-0.2) rectangle (-0.8, -0.8);
|
|
24 |
\draw[anchor=mid] (-0.5,-0.6) node {#1};
|
|
25 |
}},
|
|
26 |
pics/king/.style n args={1}{
|
|
27 |
code={%
|
|
28 |
\fill[rounded corners] (-0.1,-0.1) rectangle (-0.9, -0.9);
|
|
29 |
\fill[left color=white,rounded corners,
|
|
30 |
right color=gray,
|
|
31 |
opacity=0.7] (-0.1,-0.1) rectangle (-0.9, -0.9);
|
|
32 |
\draw[line width=0.4mm,rounded corners] (-0.1,-0.1) rectangle (-0.9, -0.9);
|
|
33 |
\draw[line width=0.2mm,rounded corners] (-0.2,-0.2) rectangle (-0.8, -0.8);
|
|
34 |
\draw[anchor=mid] (-0.5,-0.6) node {#1};
|
|
35 |
\draw[anchor=center] (-0.5,-0.25) node {\includegraphics[scale=0.015]{crown.png}};
|
|
36 |
}}
|
|
37 |
}
|
|
38 |
|
6
|
39 |
|
|
40 |
\begin{document}
|
|
41 |
|
|
42 |
\setchessboard{smallboard,
|
45
|
43 |
zero,
|
6
|
44 |
showmover=false,
|
|
45 |
boardfontencoding=LSBC4,
|
|
46 |
hlabelformat=\arabic{ranklabel},
|
|
47 |
vlabelformat=\arabic{filelabel}}
|
|
48 |
|
45
|
49 |
\mbox{}\\[-18mm]\mbox{}
|
6
|
50 |
|
477
|
51 |
\section*{Main Part 4:\\ Implementing the Shogun Board Game (8 Marks)}
|
6
|
52 |
|
265
|
53 |
\mbox{}\hfill\textit{``The problem with object-oriented languages is they’ve got all this implicit,}\\
|
|
54 |
\mbox{}\hfill\textit{environment that they carry around with them. You wanted a banana but}\\
|
|
55 |
\mbox{}\hfill\textit{what you got was a gorilla holding the banana and the entire jungle.''}\smallskip\\
|
284
|
56 |
\mbox{}\hfill\textit{ --- Joe Armstrong (creator of the Erlang programming language)}\medskip\bigskip
|
265
|
57 |
|
476
|
58 |
|
265
|
59 |
\noindent
|
476
|
60 |
You are asked to implement a Scala program for playing the Shogun
|
|
61 |
board game.\medskip
|
397
|
62 |
|
476
|
63 |
%The deadline for your submission is on 26th July at
|
|
64 |
%16:00. There will be no automated tests for the resit, but there are
|
|
65 |
%many testcases in the template and the task description. Make sure
|
|
66 |
%you use Scala \textbf{2.13.XX} for the resit---the same version as
|
|
67 |
%during the lectures. \medskip
|
50
|
68 |
|
347
|
69 |
\IMPORTANTNONE{}
|
|
70 |
|
|
71 |
\noindent
|
476
|
72 |
Also note that the running time of each task will be restricted to a
|
213
|
73 |
maximum of 30 seconds on my laptop: If you calculate a result once,
|
476
|
74 |
try to avoid to calculate the result again.
|
39
|
75 |
|
166
|
76 |
\DISCLAIMER{}
|
39
|
77 |
|
|
78 |
\subsection*{Background}
|
|
79 |
|
476
|
80 |
Shogun
|
|
81 |
(\faVolumeUp\,[shōgoon]) is a game played by two players on a chess board and is somewhat
|
|
82 |
similar to chess and checkers. A real Shogun board looks
|
|
83 |
like in the pictures on the left.
|
45
|
84 |
|
|
85 |
|
476
|
86 |
\begin{center}
|
|
87 |
\begin{tabular}{@{}ccc@{}}
|
|
88 |
\raisebox{2mm}{\includegraphics[scale=0.1]{shogun2.jpeg}}
|
|
89 |
&
|
|
90 |
\raisebox{2mm}{\includegraphics[scale=0.14]{shogun.jpeg}}
|
|
91 |
&
|
|
92 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
93 |
% chessboard
|
|
94 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
95 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
96 |
{
|
|
97 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
98 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
99 |
}
|
|
100 |
% black pieces
|
|
101 |
\foreach\x/\y/\e in {1/1/1,2/1/3,3/1/2,4/1/3,6/1/3,7/1/1,8/1/2}
|
|
102 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
|
103 |
% white pieces
|
|
104 |
\foreach\x/\y/\e in {1/8/4,2/8/2,3/8/4,5/8/4,6/8/2,7/8/3,8/8/1}
|
|
105 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
106 |
\pic[fill=white] at (5.0,1.0) {king={1}};
|
|
107 |
\pic[fill=red] at (4.0,8.0) {king={2}};
|
|
108 |
% numbers
|
|
109 |
\foreach\x in {1,...,8}
|
|
110 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
111 |
}
|
|
112 |
\foreach\y in {1,...,8}
|
|
113 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
114 |
}
|
|
115 |
\end{tikzpicture}
|
|
116 |
\end{tabular}
|
|
117 |
\end{center}
|
6
|
118 |
|
45
|
119 |
|
6
|
120 |
\noindent
|
476
|
121 |
In what follows we shall use board illustrations as shown on the right. As
|
|
122 |
can be seen there are two colours in Shogun for the pieces, red and white. Each
|
|
123 |
player has 8 pieces, one of which is a king (the piece with the crown)
|
|
124 |
and seven are pawns. At the beginning the pieces are lined up as shown
|
|
125 |
above. What sets Shogun apart from chess and checkers is that each
|
|
126 |
piece has, what I call, a kind of \textit{energy}---which for pawns is
|
|
127 |
a number between 1 and 4, and for kings between 1 and 2. The energy
|
|
128 |
determines how far a piece has to move. In the physical version of
|
|
129 |
Shogun, the pieces and the board have magnets that can change the
|
|
130 |
energy of a piece from move to move---so a piece on one field can have
|
|
131 |
energy 2 and on a different field the same piece might have energy
|
|
132 |
3. There are some further constraints on legal moves, which are
|
481
|
133 |
explained below. The point of this coursework part is to implement functions
|
476
|
134 |
about moving pieces on the Shogun board.\medskip\medskip
|
45
|
135 |
|
476
|
136 |
%and testing for when a
|
|
137 |
%checkmate occurs---i.e.~the king is attacked and cannot move
|
|
138 |
%anymore to an ``unattacked'' field (to simplify matters for
|
|
139 |
%the resit we leave out the case where the checkmate can be averted by capturing
|
|
140 |
%the attacking piece).\medskip
|
39
|
141 |
|
476
|
142 |
\noindent
|
|
143 |
Like in chess, in Shogun the players take turns of moving and
|
|
144 |
possibly capturing opposing pieces.
|
|
145 |
There are the following rules on how pieces can move:
|
213
|
146 |
|
476
|
147 |
\begin{itemize}
|
|
148 |
\item The energy of a piece determines how far, that is how many
|
|
149 |
fields, a piece has to move (remember pawns have an energy between 1 --
|
|
150 |
4, kings have an energy of only 1 -- 2). The energy of a piece might
|
|
151 |
change when the piece moves to new field.
|
|
152 |
\item Pieces can move in straight lines (up, down, left, right), or in
|
|
153 |
L-shape moves, meaning a move can make a single
|
|
154 |
90$^{\circ}$-turn. S-shape moves with more than one turn are not
|
|
155 |
allowed. Also in a single move a piece cannot go forward and then
|
|
156 |
go backward---for example with energy 3 you cannot move 2 fields up and
|
|
157 |
then 1 field down. A piece can never move diagonally.
|
|
158 |
\item A piece cannot jump over another piece and cannot stack up on top of your own pieces.
|
|
159 |
But you can capture an opponent's piece if you move to an occupied field. A captured
|
|
160 |
piece is removed from the board.
|
|
161 |
\end{itemize}
|
372
|
162 |
|
|
163 |
\noindent
|
476
|
164 |
Like in chess, checkmate is determined when the king of a player cannot
|
481
|
165 |
move anymore to a field that is not attacked, or cannot
|
476
|
166 |
capture or block the attacking piece, or the king is the only
|
481
|
167 |
piece left for a player. A non-trivial board that is checkmate is the following:
|
476
|
168 |
|
|
169 |
\begin{center}
|
|
170 |
\begin{tikzpicture}[scale=0.5,every node/.style={scale=0.5}]
|
|
171 |
% chessboard
|
|
172 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
173 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
174 |
{
|
|
175 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
176 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
177 |
}
|
|
178 |
% redpieces
|
|
179 |
\pic[fill=red] at (4,2) {king={2}};
|
|
180 |
\pic[fill=red] at (6,1) {piece={3}};
|
|
181 |
\pic[fill=red] at (4,4) {piece={4}};
|
|
182 |
\pic[fill=red] at (5,3) {piece={4}};
|
|
183 |
% white pieces
|
|
184 |
\pic[fill=white] at (7,1) {king={2}};
|
|
185 |
\pic[fill=white] at (8,5) {piece={2}};
|
|
186 |
\pic[fill=white] at (4,1) {piece={2}};
|
|
187 |
% numbers
|
|
188 |
\foreach\x in {1,...,8}
|
|
189 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
190 |
}
|
|
191 |
\foreach\y in {1,...,8}
|
|
192 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
193 |
}
|
|
194 |
\end{tikzpicture}
|
|
195 |
\end{center}
|
|
196 |
|
|
197 |
\noindent
|
|
198 |
The reason for the checkmate is that the white king on field (7, 1) is
|
|
199 |
attacked by the red pawn on \mbox{(5, 3)}. There is nowhere for the
|
|
200 |
white king to go, and no white pawn can be moved into the way of this
|
|
201 |
red pawn and white can also not capture it. When determining a possible
|
|
202 |
move, you need to be careful with pieces that might be in the
|
|
203 |
way. Consider the following position:
|
213
|
204 |
|
476
|
205 |
\begin{equation}\label{moves}
|
|
206 |
\begin{tikzpicture}[scale=0.5,every node/.style={scale=0.5}]
|
|
207 |
% chessboard
|
|
208 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
209 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
210 |
{
|
|
211 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
212 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
213 |
}
|
|
214 |
% redpieces
|
|
215 |
\fill[blue!50] (0,2) rectangle ++ (1,1);
|
|
216 |
\fill[blue!50] (1,1) rectangle ++ (1,1);
|
|
217 |
\fill[blue!50] (0,4) rectangle ++ (1,1);
|
|
218 |
\fill[blue!50] (1,5) rectangle ++ (1,1);
|
|
219 |
\fill[blue!50] (2,6) rectangle ++ (1,1);
|
|
220 |
%%\fill[blue!50] (3,7) rectangle ++ (1,1);
|
|
221 |
\fill[blue!50] (4,6) rectangle ++ (1,1);
|
|
222 |
\fill[blue!50] (5,5) rectangle ++ (1,1);
|
|
223 |
\fill[blue!50] (6,4) rectangle ++ (1,1);
|
|
224 |
\fill[blue!50] (6,2) rectangle ++ (1,1);
|
|
225 |
\fill[blue!50] (7,3) rectangle ++ (1,1);
|
|
226 |
\fill[blue!50] (4,0) rectangle ++ (1,1);
|
|
227 |
\fill[blue!50] (2,0) rectangle ++ (1,1);
|
|
228 |
\pic[fill=red] at (4,4) {piece={4}};
|
|
229 |
\pic[fill=red] at (4,8) {piece={4}};
|
|
230 |
\pic[fill=white] at (2,5) {piece={3}};
|
|
231 |
\pic[fill=white] at (4,3) {piece={2}};
|
|
232 |
\pic[fill=white] at (6,3) {piece={1}};
|
|
233 |
\pic[fill=white] at (8,4) {piece={1}};
|
|
234 |
% numbers
|
|
235 |
\foreach\x in {1,...,8}
|
|
236 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
237 |
}
|
|
238 |
\foreach\y in {1,...,8}
|
|
239 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
240 |
}
|
|
241 |
\end{tikzpicture}
|
|
242 |
\end{equation}
|
213
|
243 |
|
476
|
244 |
\noindent
|
|
245 |
The red piece in the centre on field (4, 4) can move to all the blue fields.
|
|
246 |
In particular it can move to (2, 6), because it can move 2 fields up
|
|
247 |
and 2 fields to the left---it cannot reach this field by moving two
|
|
248 |
fields to the left and then two up, because jumping over the white
|
|
249 |
piece at (2, 5) is not allowed. Similarly, the field at (6, 2) is
|
|
250 |
unreachable for the red piece because of the two white pieces at (4,
|
|
251 |
3) and (6, 3) are in the way and no S-shape move is allowed in
|
|
252 |
Shogun. The red piece on (4, 4) cannot move to the field (4, 8) at the
|
|
253 |
top, because a red piece is already there; but it can move to (8, 4)
|
|
254 |
and capture the white piece there. The moral is we always have to
|
|
255 |
explore all possible ways in order to determine whether a piece can be
|
|
256 |
moved to a field or not: in general there might be several ways and some of
|
|
257 |
them might be blocked.
|
213
|
258 |
|
|
259 |
|
|
260 |
\subsection*{Hints}
|
|
261 |
|
476
|
262 |
Useful functions about pieces and boards are defined at the beginning
|
|
263 |
of the template file. The function \texttt{.map} applies a function to
|
|
264 |
each element of a list or set; \texttt{.flatMap} works like
|
|
265 |
\texttt{map} followed by a \texttt{.flatten}---this is useful if a
|
|
266 |
function returns a set of sets, which need to be ``unioned up''. Sets
|
|
267 |
can be partitioned according to a predicate with the function
|
|
268 |
\texttt{.partition}. For example
|
|
269 |
|
|
270 |
\begin{lstlisting}
|
|
271 |
val (even, odd) = Set(1,2,3,4,5).partition(_ % 2 == 0)
|
|
272 |
// --> even = Set(2,4)
|
|
273 |
// odd = Set(1,3,5)
|
|
274 |
\end{lstlisting}
|
|
275 |
|
213
|
276 |
\noindent
|
476
|
277 |
The function \texttt{.toList} transforms a set into a list. The function
|
|
278 |
\texttt{.count} counts elements according to a predicate. For example
|
212
|
279 |
|
476
|
280 |
\begin{lstlisting}
|
|
281 |
Set(1,2,3,4,5).count(_ % 2 == 0)
|
|
282 |
// --> 2
|
|
283 |
\end{lstlisting}
|
213
|
284 |
|
476
|
285 |
%% \newpage
|
213
|
286 |
|
397
|
287 |
\subsection*{Tasks}
|
45
|
288 |
|
476
|
289 |
You are asked to implement how pieces can move on a Shogun board. Let
|
|
290 |
us first fix the basic datastructures for the implementation. A
|
|
291 |
\emph{position} (or field) is a pair of integers, like $(3, 2)$. The
|
|
292 |
board's dimension is always 8 $\times$ 8. A \emph{colour} is either
|
|
293 |
red (\texttt{Red}) or white (\texttt{Wht}). A \emph{piece} is either
|
|
294 |
a pawn or a king, and has a position, a colour and an energy (an
|
|
295 |
integer). In the template file there are functions \texttt{incx},
|
|
296 |
\texttt{decx}, \texttt{incy} and \texttt{decy} for incrementing and
|
|
297 |
decrementing the x- and y-coordinates of positions of pieces.
|
6
|
298 |
|
476
|
299 |
A \emph{board} consists of a set of pieces. We always assume that we
|
481
|
300 |
start with a consistent board and every move only generates another
|
476
|
301 |
consistent board. In this way we do not need to check, for example,
|
|
302 |
whether pieces are stacked on top of each other or located outside the
|
|
303 |
board, or have an energy outside the permitted range. There are
|
|
304 |
functions \texttt{-} and \texttt{+} for removing, respectively adding,
|
|
305 |
single pieces to a board. The function \texttt{occupied} takes a
|
|
306 |
position and a board as arguments, and returns an \texttt{Option} of a
|
|
307 |
piece when this position is occupied, otherwise \texttt{None}. The
|
|
308 |
function \texttt{occupied\_by} returns the colour of a potential piece
|
|
309 |
on that position. The function \texttt{is\_occupied} returns a boolean
|
|
310 |
for whether a position is occupied or not; \texttt{print\_board} is a
|
|
311 |
rough function that prints out a board on the console. This function
|
|
312 |
is meant for testing purposes.
|
45
|
313 |
|
378
|
314 |
|
|
315 |
|
45
|
316 |
\begin{itemize}
|
476
|
317 |
\item[(1)] You need to calculate all possible moves for a piece on a Shogun board. In order to
|
|
318 |
make sure no piece moves forwards and backwards at the same time,
|
|
319 |
and also exclude all S-shape moves, the data-structure \texttt{Move}
|
|
320 |
is introduced. A \texttt{Move} encodes all simple moves (up, down, left,
|
|
321 |
right) and L-shape moves (first right, then up and so on). This is defined
|
|
322 |
as follows:
|
|
323 |
|
|
324 |
{\small\begin{lstlisting}
|
|
325 |
abstract class Move
|
|
326 |
case object U extends Move // up
|
|
327 |
case object D extends Move // down
|
|
328 |
case object R extends Move // right
|
|
329 |
case object L extends Move // left
|
|
330 |
case object RU extends Move // ...
|
|
331 |
case object LU extends Move
|
|
332 |
case object RD extends Move
|
|
333 |
case object LD extends Move
|
|
334 |
case object UR extends Move
|
|
335 |
case object UL extends Move
|
|
336 |
case object DR extends Move
|
|
337 |
case object DL extends Move
|
|
338 |
\end{lstlisting}}
|
45
|
339 |
|
476
|
340 |
You need to implement an \texttt{eval} function that takes a piece
|
|
341 |
\texttt{pc}, a move \texttt{m}, an energy \texttt{en} and a board
|
|
342 |
\texttt{b} as arguments. The idea is to recursively calculate all
|
|
343 |
fields that can be reached by the move \texttt{m} (there might be more than
|
|
344 |
one). The energy acts as a counter and decreases in each recursive
|
|
345 |
call until 0 is reached (the final field). The function \texttt{eval} for a piece \texttt{pc}
|
|
346 |
should behave as follows:
|
6
|
347 |
|
476
|
348 |
\begin{itemize}
|
|
349 |
\item If the position of a piece is outside the board, then no field can be reached (represented by
|
|
350 |
the empty set \texttt{Set()}).
|
|
351 |
\item If the energy is 0 and the position of the piece is \textit{not} occupied, then the field can be reached
|
|
352 |
and the set \texttt{Set(pc)} is returned whereby \texttt{pc} is the piece given as argument.
|
|
353 |
\item If the energy is 0 and the position of the piece \textit{is} occupied, but occupied by a piece
|
481
|
354 |
of the opposite colour, then also the set \texttt{Set(pc)} is returned. Otherwise the empty set
|
|
355 |
\texttt{Set()} is returned.
|
476
|
356 |
\item In case the energy is > 0 and the position of the piece
|
|
357 |
\texttt{pc} is occupied, then this move is blocked and the set
|
|
358 |
\texttt{Set()} is returned.
|
|
359 |
\item In all other cases we have to analyse the move
|
|
360 |
\texttt{m}. First, the simple moves (that is \texttt{U}, \texttt{D},
|
|
361 |
\texttt{L} and \texttt{R}) we only have to increment / decrement the
|
|
362 |
x- or y-position of the piece, decrease the energy and call eval
|
481
|
363 |
recursively with the updated arguments. For example for \texttt{U} (up)
|
476
|
364 |
you need to increase the y-coordinate:
|
45
|
365 |
|
|
366 |
\begin{center}
|
476
|
367 |
\texttt{U} $\quad\Rightarrow\quad$ new arguments: \texttt{incy(pc)}, \texttt{U}, energy - 1, same board
|
48
|
368 |
\end{center}
|
148
|
369 |
|
476
|
370 |
The move \texttt{U} here acts like a ``mode'', meaning if you move
|
481
|
371 |
up, you can only move up; the mode never changes for simple moves. Similarly for the other simple moves: if
|
476
|
372 |
you move right, you can only move right and so on. In this way it is
|
|
373 |
prevented to go first to the right, and then change direction in order to go
|
|
374 |
left (same with up and down).
|
|
375 |
|
|
376 |
For the L-shape moves (\texttt{RU}, \texttt{LU}, \texttt{RD} and so on) you need to calculate two
|
|
377 |
sets of reachable fields. Say we analyse \texttt{RU}, then we first have to calculate all fields
|
|
378 |
reachable by moving to the right; then we have to calculate all moves by changing the mode to \texttt{U}.
|
|
379 |
That means there are two recursive calls to \texttt{eval}:
|
45
|
380 |
|
476
|
381 |
\begin{center}
|
|
382 |
\begin{tabular}{@{}lll@{}}
|
|
383 |
\texttt{RU} & $\Rightarrow$ & new args for call 1: \texttt{incx(pc)}, \texttt{RU}, energy - 1, same board\\
|
|
384 |
& & new args for call 2: \texttt{pc}, \texttt{U}, same energy, same board
|
|
385 |
\end{tabular}
|
|
386 |
\end{center}
|
|
387 |
|
|
388 |
In each case we receive some new piece(s) on reachable fields and therefore we return the set
|
|
389 |
containing all these fields. Similarly in the other cases.
|
48
|
390 |
\end{itemize}
|
|
391 |
|
477
|
392 |
For example in the left board below, \texttt{eval} is called with the white
|
476
|
393 |
piece in the centre and the move \texttt{RU} generates then a set of
|
477
|
394 |
new pieces corresponding to the blue fields. The difference with the
|
476
|
395 |
right board is that \texttt{eval} is called with a red piece and therefore the
|
|
396 |
field (4, 8) is not reachable anymore because it is already occupied by
|
477
|
397 |
another red piece. But (7, 5) becomes reachable because it is occupied
|
|
398 |
by a piece of the opposite colour.
|
148
|
399 |
|
476
|
400 |
\begin{center}
|
|
401 |
\begin{tabular}{cc}
|
|
402 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
403 |
% chessboard
|
|
404 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
405 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
406 |
{
|
|
407 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
408 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
409 |
}
|
|
410 |
\fill[blue!50] (5,5) rectangle ++ (1,1);
|
|
411 |
\fill[blue!50] (3,7) rectangle ++ (1,1);
|
|
412 |
\fill[blue!50] (4,6) rectangle ++ (1,1);
|
477
|
413 |
%\fill[blue!50] (6,4) rectangle ++ (1,1);
|
476
|
414 |
\fill[blue!50] (7,3) rectangle ++ (1,1);
|
|
415 |
|
|
416 |
% black pieces
|
477
|
417 |
\foreach\x/\y/\e in {2/1/3,3/1/2,4/1/3,6/1/3,7/1/1,7/5/2}
|
476
|
418 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
|
419 |
% white pieces
|
|
420 |
\foreach\x/\y/\e in {1/8/4,2/8/2,3/8/4,5/8/4,6/8/2,7/8/3,8/8/1}
|
|
421 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
422 |
\pic[fill=white] at (5.0,1.0) {king={1}};
|
|
423 |
\pic[fill=red] at (4.0,8.0) {king={2}};
|
45
|
424 |
|
476
|
425 |
\pic[fill=white] at (4,4) {piece={4}};
|
|
426 |
% numbers
|
|
427 |
\foreach\x in {1,...,8}
|
|
428 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
429 |
}
|
|
430 |
\foreach\y in {1,...,8}
|
|
431 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
432 |
}
|
|
433 |
\end{tikzpicture}
|
|
434 |
&
|
|
435 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
436 |
% chessboard
|
|
437 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
438 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
439 |
{
|
|
440 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
441 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
442 |
}
|
|
443 |
\fill[blue!50] (5,5) rectangle ++ (1,1);
|
|
444 |
\fill[blue!50] (4,6) rectangle ++ (1,1);
|
|
445 |
\fill[blue!50] (6,4) rectangle ++ (1,1);
|
|
446 |
\fill[blue!50] (7,3) rectangle ++ (1,1);
|
45
|
447 |
|
476
|
448 |
% black pieces
|
477
|
449 |
\foreach\x/\y/\e in {1/1/1,2/1/3,3/1/2,4/1/3,6/1/3,7/1/1,7/5/2}
|
476
|
450 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
|
451 |
% white pieces
|
477
|
452 |
\foreach\x/\y/\e in {1/8/4,2/8/2,3/8/4,5/8/4,6/8/2,7/8/3}
|
476
|
453 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
454 |
\pic[fill=white] at (5.0,1.0) {king={1}};
|
|
455 |
\pic[fill=red] at (4.0,8.0) {king={2}};
|
45
|
456 |
|
476
|
457 |
\pic[fill=red] at (4,4) {piece={4}};
|
|
458 |
% numbers
|
|
459 |
\foreach\x in {1,...,8}
|
|
460 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
461 |
}
|
|
462 |
\foreach\y in {1,...,8}
|
|
463 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
464 |
}
|
|
465 |
\end{tikzpicture}
|
|
466 |
\\[-5mm]
|
|
467 |
\end{tabular}
|
|
468 |
\end{center}\hfill[3 Marks]
|
45
|
469 |
|
476
|
470 |
\item[(2)] Implement an \texttt{all\_moves} function that calculates for a
|
477
|
471 |
piece and a board, \textit{all} possible onward positions. For this
|
476
|
472 |
you have to call \texttt{eval} for all possible moves \texttt{m} (that is \texttt{U},
|
477
|
473 |
\texttt{D}, \ldots, \texttt{DL}). An example for all moves for the red piece on (4, 4) is
|
|
474 |
shown in \eqref{moves} on page \pageref{moves}. Be careful about possible modifications
|
|
475 |
you need to apply to the board before you call the \texttt{eval} function.
|
481
|
476 |
Also for this task, ignore the fact that a king cannot move onto an attacked field.\\
|
476
|
477 |
\mbox{}\hfill[1 Mark]
|
|
478 |
|
|
479 |
\item[(3)] Implement a function \texttt{attacked} that takes a colour and a board
|
|
480 |
and calculates all pieces of the opposite side that are attacked. For example
|
477
|
481 |
below in the left board are all the attacked pieces by red, and on the right all for white:
|
45
|
482 |
|
476
|
483 |
\begin{center}
|
|
484 |
\begin{tabular}{cc}
|
|
485 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
486 |
% chessboard
|
|
487 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
488 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
489 |
{
|
|
490 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
491 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
492 |
}
|
|
493 |
\fill[blue!50] (7,3) rectangle ++ (1,1);
|
|
494 |
\fill[blue!50] (6,0) rectangle ++ (1,1);
|
|
495 |
|
|
496 |
|
477
|
497 |
% red pieces
|
476
|
498 |
\foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
|
|
499 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
500 |
% white pieces
|
477
|
501 |
\foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
|
476
|
502 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
|
503 |
|
|
504 |
\pic[fill=red] at (4,2) {king={2}};
|
|
505 |
\pic[fill=white] at (7,1) {king={2}};
|
213
|
506 |
|
476
|
507 |
% numbers
|
|
508 |
\foreach\x in {1,...,8}
|
|
509 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
510 |
}
|
|
511 |
\foreach\y in {1,...,8}
|
|
512 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
513 |
}
|
|
514 |
\end{tikzpicture}
|
|
515 |
&
|
|
516 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
517 |
% chessboard
|
|
518 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
519 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
520 |
{
|
|
521 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
522 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
523 |
}
|
|
524 |
\fill[blue!50] (5,0) rectangle ++ (1,1);
|
477
|
525 |
\fill[blue!50] (5,4) rectangle ++ (1,1);
|
476
|
526 |
|
477
|
527 |
% red pieces
|
476
|
528 |
\foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
|
|
529 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
530 |
% white pieces
|
477
|
531 |
\foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
|
476
|
532 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
213
|
533 |
|
476
|
534 |
\pic[fill=red] at (4,2) {king={2}};
|
|
535 |
\pic[fill=white] at (7,1) {king={2}};
|
213
|
536 |
|
476
|
537 |
% numbers
|
|
538 |
\foreach\x in {1,...,8}
|
|
539 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
540 |
}
|
|
541 |
\foreach\y in {1,...,8}
|
|
542 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
543 |
}
|
|
544 |
\end{tikzpicture}
|
|
545 |
\\[-5mm]
|
|
546 |
\end{tabular}
|
|
547 |
\end{center}\mbox{}\hfill[1 Mark]
|
|
548 |
|
|
549 |
\item[(4)] Implement a function \texttt{attackedN} that takes a piece and a board
|
|
550 |
and calculates the number of times this pieces is attacked by pieces of the opposite colour.
|
|
551 |
For example the piece on field (8, 4) above is attacked by 3 red pieces, and
|
481
|
552 |
the piece on (6, 1) by 1 white piece. In this number also include kings even
|
|
553 |
if they cannot move to this field because the would be in ``check''.
|
476
|
554 |
\\
|
|
555 |
\mbox{}\hfill[1 Mark]
|
|
556 |
|
|
557 |
\item[(5)] Implement a function \texttt{protectedN} that takes a piece and a board
|
|
558 |
and calculates the number of times this pieces is protected by pieces of the same colour.
|
|
559 |
For example the piece on field (8, 4) above is protected by 1 white pieces (the one on (8, 7)),
|
|
560 |
and the piece on (5, 3) is protected by three red pieces ((6, 1), (4, 2), and (6, 5)).
|
481
|
561 |
Similarly to \texttt{attackedN}, include in the calculated number here also the king provided it
|
|
562 |
can reach the given piece.\\
|
145
|
563 |
\mbox{}\hfill[1 Mark]
|
477
|
564 |
|
|
565 |
\item[(6)] Implement a function \texttt{legal\_moves} that behaves like \texttt{all\_moves} from (2) for
|
|
566 |
pawns, but for kings, in addition, makes sure that they do not move to an attacked field.
|
|
567 |
For example in the board below on the left, there are three possible fields the white king can
|
|
568 |
reach, but all of them are attacked by red pieces. In the board on the right where the
|
|
569 |
white king has an energy of 1, there is only one legal move, namely to move to field (8, 1).
|
|
570 |
The field (7, 2) is reachable, but is attacked; similarly capturing the red piece on field (6, 1) is
|
|
571 |
not possible because it is protected by at least another red piece.
|
|
572 |
|
|
573 |
\begin{center}
|
|
574 |
\begin{tabular}{cc}
|
|
575 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
576 |
% chessboard
|
|
577 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
578 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
579 |
{
|
|
580 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
581 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
582 |
}
|
|
583 |
\fill[blue!50] (5,1) rectangle ++ (1,1);
|
|
584 |
\fill[blue!50] (6,2) rectangle ++ (1,1);
|
|
585 |
\fill[blue!50] (7,1) rectangle ++ (1,1);
|
|
586 |
|
|
587 |
|
|
588 |
% red pieces
|
|
589 |
\foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
|
|
590 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
591 |
% white pieces
|
|
592 |
\foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
|
|
593 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
|
594 |
|
|
595 |
\pic[fill=red] at (4,2) {king={2}};
|
|
596 |
\pic[fill=white] at (7,1) {king={2}};
|
|
597 |
|
|
598 |
% numbers
|
|
599 |
\foreach\x in {1,...,8}
|
|
600 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
601 |
}
|
|
602 |
\foreach\y in {1,...,8}
|
|
603 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
604 |
}
|
|
605 |
\end{tikzpicture} &
|
|
606 |
\begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]
|
|
607 |
% chessboard
|
|
608 |
\draw[very thick,gray] (0,0) rectangle (8,8);
|
|
609 |
\foreach\x in {0,...,7}\foreach\y in {7,...,0}
|
|
610 |
{
|
|
611 |
\pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25}
|
|
612 |
\fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
|
|
613 |
}
|
|
614 |
\fill[blue!50] (5,0) rectangle ++ (1,1);
|
|
615 |
\fill[blue!50] (6,1) rectangle ++ (1,1);
|
|
616 |
\fill[blue!50] (7,0) rectangle ++ (1,1);
|
|
617 |
|
|
618 |
|
|
619 |
% red pieces
|
|
620 |
\foreach\x/\y/\e in {6/1/3,4/4/4,5/3/3,6/5/3}
|
|
621 |
\pic[fill=red] at (\x,\y) {piece={\e}};
|
|
622 |
% white pieces
|
|
623 |
\foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
|
|
624 |
\pic[fill=white] at (\x,\y) {piece={\e}};
|
|
625 |
|
|
626 |
\pic[fill=red] at (4,2) {king={2}};
|
|
627 |
\pic[fill=white] at (7,1) {king={1}};
|
|
628 |
|
|
629 |
% numbers
|
|
630 |
\foreach\x in {1,...,8}
|
|
631 |
{\draw (\x - 0.5, -0.4) node {\x};
|
|
632 |
}
|
|
633 |
\foreach\y in {1,...,8}
|
|
634 |
{\draw (-0.4, \y - 0.6, -0.4) node {\y};
|
|
635 |
}
|
|
636 |
\end{tikzpicture}
|
|
637 |
\end{tabular}
|
|
638 |
\end{center}
|
|
639 |
\mbox{}\\ \mbox{}\hfill[1 Mark]
|
|
640 |
|
430
|
641 |
\end{itemize}
|
|
642 |
|
6
|
643 |
\end{document}
|
|
644 |
|
|
645 |
%%% Local Variables:
|
|
646 |
%%% mode: latex
|
|
647 |
%%% TeX-master: t
|
|
648 |
%%% End:
|