cws/main_cw04.tex
changeset 481 e03a0100ec46
parent 477 a4e1f63157d8
equal deleted inserted replaced
480:a623dd1f2898 481:e03a0100ec46
   128 determines how far a piece has to move. In the physical version of
   128 determines how far a piece has to move. In the physical version of
   129 Shogun, the pieces and the board have magnets that can change the
   129 Shogun, the pieces and the board have magnets that can change the
   130 energy of a piece from move to move---so a piece on one field can have
   130 energy of a piece from move to move---so a piece on one field can have
   131 energy 2 and on a different field the same piece might have energy
   131 energy 2 and on a different field the same piece might have energy
   132 3. There are some further constraints on legal moves, which are
   132 3. There are some further constraints on legal moves, which are
   133 explained below.  The point of this part is to implement functions
   133 explained below.  The point of this coursework part is to implement functions
   134 about moving pieces on the Shogun board.\medskip\medskip
   134 about moving pieces on the Shogun board.\medskip\medskip
   135 
   135 
   136 %and testing for when a
   136 %and testing for when a
   137 %checkmate occurs---i.e.~the king is attacked and cannot move
   137 %checkmate occurs---i.e.~the king is attacked and cannot move
   138 %anymore to an ``unattacked'' field (to simplify matters for
   138 %anymore to an ``unattacked'' field (to simplify matters for
   160   piece is removed from the board.
   160   piece is removed from the board.
   161 \end{itemize}  
   161 \end{itemize}  
   162 
   162 
   163 \noindent
   163 \noindent
   164 Like in chess, checkmate is determined when the king of a player cannot
   164 Like in chess, checkmate is determined when the king of a player cannot
   165 move anymore to a field that is not attacked, or a player cannot
   165 move anymore to a field that is not attacked, or cannot
   166 capture or block the attacking piece, or the king is the only
   166 capture or block the attacking piece, or the king is the only
   167 piece left for a player. A board that is checkmate is the following:
   167 piece left for a player. A non-trivial board that is checkmate is the following:
   168 
   168 
   169 \begin{center}
   169 \begin{center}
   170 \begin{tikzpicture}[scale=0.5,every node/.style={scale=0.5}]    
   170 \begin{tikzpicture}[scale=0.5,every node/.style={scale=0.5}]    
   171 % chessboard
   171 % chessboard
   172 \draw[very thick,gray] (0,0) rectangle (8,8);
   172 \draw[very thick,gray] (0,0) rectangle (8,8);
   295 integer).  In the template file there are functions \texttt{incx},
   295 integer).  In the template file there are functions \texttt{incx},
   296 \texttt{decx}, \texttt{incy} and \texttt{decy} for incrementing and
   296 \texttt{decx}, \texttt{incy} and \texttt{decy} for incrementing and
   297 decrementing the x- and y-coordinates of positions of pieces.
   297 decrementing the x- and y-coordinates of positions of pieces.
   298 
   298 
   299 A \emph{board} consists of a set of pieces. We always assume that we
   299 A \emph{board} consists of a set of pieces. We always assume that we
   300 start with a consistent board and every move generates another
   300 start with a consistent board and every move only generates another
   301 consistent board. In this way we do not need to check, for example,
   301 consistent board. In this way we do not need to check, for example,
   302 whether pieces are stacked on top of each other or located outside the
   302 whether pieces are stacked on top of each other or located outside the
   303 board, or have an energy outside the permitted range. There are
   303 board, or have an energy outside the permitted range. There are
   304 functions \texttt{-} and \texttt{+} for removing, respectively adding,
   304 functions \texttt{-} and \texttt{+} for removing, respectively adding,
   305 single pieces to a board.  The function \texttt{occupied} takes a
   305 single pieces to a board.  The function \texttt{occupied} takes a
   349 \item If the position of a piece is outside the board, then no field can be reached (represented by
   349 \item If the position of a piece is outside the board, then no field can be reached (represented by
   350   the empty set \texttt{Set()}).
   350   the empty set \texttt{Set()}).
   351 \item If the energy is 0 and the position of the piece is \textit{not} occupied, then the field can be reached
   351 \item If the energy is 0 and the position of the piece is \textit{not} occupied, then the field can be reached
   352   and the set \texttt{Set(pc)} is returned whereby \texttt{pc} is the piece given as argument.
   352   and the set \texttt{Set(pc)} is returned whereby \texttt{pc} is the piece given as argument.
   353 \item If the energy is 0 and the position of the piece \textit{is} occupied, but occupied by a piece
   353 \item If the energy is 0 and the position of the piece \textit{is} occupied, but occupied by a piece
   354   of the opposite colour, then also the set \texttt{Set(pc)} is returned.
   354   of the opposite colour, then also the set \texttt{Set(pc)} is returned. Otherwise the empty set
       
   355   \texttt{Set()} is returned.
   355 \item In case the energy is > 0 and the position of the piece
   356 \item In case the energy is > 0 and the position of the piece
   356   \texttt{pc} is occupied, then this move is blocked and the set
   357   \texttt{pc} is occupied, then this move is blocked and the set
   357   \texttt{Set()} is returned.  
   358   \texttt{Set()} is returned.  
   358 \item In all other cases we have to analyse the move
   359 \item In all other cases we have to analyse the move
   359   \texttt{m}. First, the simple moves (that is \texttt{U}, \texttt{D},
   360   \texttt{m}. First, the simple moves (that is \texttt{U}, \texttt{D},
   360   \texttt{L} and \texttt{R}) we only have to increment / decrement the
   361   \texttt{L} and \texttt{R}) we only have to increment / decrement the
   361   x- or y-position of the piece, decrease the energy and call eval
   362   x- or y-position of the piece, decrease the energy and call eval
   362   recursively with the updated arguments. For example for \texttt{U}
   363   recursively with the updated arguments. For example for \texttt{U} (up)
   363   you need to increase the y-coordinate:
   364   you need to increase the y-coordinate:
   364 
   365 
   365   \begin{center}
   366   \begin{center}
   366   \texttt{U} $\quad\Rightarrow\quad$ new arguments: \texttt{incy(pc)}, \texttt{U}, energy - 1, same board  
   367   \texttt{U} $\quad\Rightarrow\quad$ new arguments: \texttt{incy(pc)}, \texttt{U}, energy - 1, same board  
   367   \end{center}
   368   \end{center}
   368 
   369 
   369   The move \texttt{U} here acts like a ``mode'', meaning if you move
   370   The move \texttt{U} here acts like a ``mode'', meaning if you move
   370   up, you can only move up; the mode never changes. Similarly for the other simple moves: if
   371   up, you can only move up; the mode never changes for simple moves. Similarly for the other simple moves: if
   371   you move right, you can only move right and so on. In this way it is
   372   you move right, you can only move right and so on. In this way it is
   372   prevented to go first to the right, and then change direction in order to go
   373   prevented to go first to the right, and then change direction in order to go
   373   left (same with up and down).
   374   left (same with up and down).
   374   
   375   
   375   For the L-shape moves (\texttt{RU}, \texttt{LU}, \texttt{RD} and so on) you need to calculate two
   376   For the L-shape moves (\texttt{RU}, \texttt{LU}, \texttt{RD} and so on) you need to calculate two
   470   piece and a board, \textit{all} possible onward positions. For this
   471   piece and a board, \textit{all} possible onward positions. For this
   471   you have to call \texttt{eval} for all possible moves \texttt{m} (that is \texttt{U},
   472   you have to call \texttt{eval} for all possible moves \texttt{m} (that is \texttt{U},
   472   \texttt{D}, \ldots, \texttt{DL}). An example for all moves for the red piece on (4, 4) is
   473   \texttt{D}, \ldots, \texttt{DL}). An example for all moves for the red piece on (4, 4) is
   473   shown in \eqref{moves} on page \pageref{moves}. Be careful about possible modifications
   474   shown in \eqref{moves} on page \pageref{moves}. Be careful about possible modifications
   474   you need to apply to the board  before you call the \texttt{eval} function.
   475   you need to apply to the board  before you call the \texttt{eval} function.
   475   Also for this task ignore the fact that a king cannot move onto an attacked field.\\ 
   476   Also for this task, ignore the fact that a king cannot move onto an attacked field.\\ 
   476   \mbox{}\hfill[1 Mark]
   477   \mbox{}\hfill[1 Mark]
   477 
   478 
   478 \item[(3)] Implement a function \texttt{attacked} that takes a colour and a board
   479 \item[(3)] Implement a function \texttt{attacked} that takes a colour and a board
   479   and calculates all pieces of the opposite side that are attacked. For example
   480   and calculates all pieces of the opposite side that are attacked. For example
   480   below in the left board are all the attacked pieces by red, and on the right all for white:
   481   below in the left board are all the attacked pieces by red, and on the right all for white:
   546 \end{center}\mbox{}\hfill[1 Mark]
   547 \end{center}\mbox{}\hfill[1 Mark]
   547 
   548 
   548 \item[(4)] Implement a function \texttt{attackedN} that takes a piece and a board
   549 \item[(4)] Implement a function \texttt{attackedN} that takes a piece and a board
   549   and calculates the number of times this pieces is attacked by pieces of the opposite colour.
   550   and calculates the number of times this pieces is attacked by pieces of the opposite colour.
   550   For example the piece on field (8, 4) above is attacked by 3 red pieces, and
   551   For example the piece on field (8, 4) above is attacked by 3 red pieces, and
   551   the piece on (6, 1) by 1 white piece.
   552   the piece on (6, 1) by 1 white piece. In this number also include kings even
       
   553   if they cannot move to this field because the would be in ``check''.  
   552   \\
   554   \\
   553   \mbox{}\hfill[1 Mark]
   555   \mbox{}\hfill[1 Mark]
   554 
   556 
   555 \item[(5)] Implement a function \texttt{protectedN} that takes a piece and a board
   557 \item[(5)] Implement a function \texttt{protectedN} that takes a piece and a board
   556   and calculates the number of times this pieces is protected by pieces of the same colour.
   558   and calculates the number of times this pieces is protected by pieces of the same colour.
   557   For example the piece on field (8, 4) above is protected by 1 white pieces (the one on (8, 7)),
   559   For example the piece on field (8, 4) above is protected by 1 white pieces (the one on (8, 7)),
   558   and the piece on (5, 3) is protected by three red pieces ((6, 1), (4, 2), and (6, 5)).
   560   and the piece on (5, 3) is protected by three red pieces ((6, 1), (4, 2), and (6, 5)).
   559   \\
   561   Similarly to \texttt{attackedN}, include in the calculated number here also the king provided it
       
   562   can reach the given piece.\\
   560   \mbox{}\hfill[1 Mark]
   563   \mbox{}\hfill[1 Mark]
   561 
   564 
   562 \item[(6)] Implement a function \texttt{legal\_moves} that behaves like \texttt{all\_moves} from (2) for
   565 \item[(6)] Implement a function \texttt{legal\_moves} that behaves like \texttt{all\_moves} from (2) for
   563   pawns, but for kings, in addition, makes sure that they do not move to an attacked field.
   566   pawns, but for kings, in addition, makes sure that they do not move to an attacked field.
   564   For example in the board below on the left, there are three possible fields the white king can
   567   For example in the board below on the left, there are three possible fields the white king can