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