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