cws/main_cw04.tex
changeset 477 a4e1f63157d8
parent 476 7550c816187a
child 481 e03a0100ec46
equal deleted inserted replaced
476:7550c816187a 477:a4e1f63157d8
    46                hlabelformat=\arabic{ranklabel},
    46                hlabelformat=\arabic{ranklabel},
    47                vlabelformat=\arabic{filelabel}}
    47                vlabelformat=\arabic{filelabel}}
    48 
    48 
    49 \mbox{}\\[-18mm]\mbox{}
    49 \mbox{}\\[-18mm]\mbox{}
    50 
    50 
    51 \section*{Main Part 4:\\ Implementing the Shogun Board Game (7 Marks)}
    51 \section*{Main Part 4:\\ Implementing the Shogun Board Game (8 Marks)}
    52 
    52 
    53 \mbox{}\hfill\textit{``The problem with object-oriented languages is they’ve got all this implicit,}\\
    53 \mbox{}\hfill\textit{``The problem with object-oriented languages is they’ve got all this implicit,}\\
    54 \mbox{}\hfill\textit{environment that they carry around with them. You wanted a banana but}\\
    54 \mbox{}\hfill\textit{environment that they carry around with them. You wanted a banana but}\\
    55 \mbox{}\hfill\textit{what you got was a gorilla holding the banana and the entire jungle.''}\smallskip\\
    55 \mbox{}\hfill\textit{what you got was a gorilla holding the banana and the entire jungle.''}\smallskip\\
    56 \mbox{}\hfill\textit{ --- Joe Armstrong (creator of the Erlang programming language)}\medskip\bigskip
    56 \mbox{}\hfill\textit{ --- Joe Armstrong (creator of the Erlang programming language)}\medskip\bigskip
   386 
   386 
   387   In each case we receive some new piece(s) on reachable fields and therefore we return the set
   387   In each case we receive some new piece(s) on reachable fields and therefore we return the set
   388   containing all these fields. Similarly in the other cases.
   388   containing all these fields. Similarly in the other cases.
   389 \end{itemize}
   389 \end{itemize}
   390 
   390 
   391 For example on the left board below, \texttt{eval} is called with the white
   391 For example in the left board below, \texttt{eval} is called with the white
   392 piece in the centre and the move \texttt{RU} generates then a set of
   392 piece in the centre and the move \texttt{RU} generates then a set of
   393 new pieces corresponding to the blue fileds. The difference on the
   393 new pieces corresponding to the blue fields. The difference with the
   394 right board is that \texttt{eval} is called with a red piece and therefore the
   394 right board is that \texttt{eval} is called with a red piece and therefore the
   395 field (4, 8) is not reachable anymore because it is already occupied by
   395 field (4, 8) is not reachable anymore because it is already occupied by
   396 another red piece.
   396 another red piece. But (7, 5) becomes reachable because it is occupied
       
   397 by a piece of the opposite colour.
   397 
   398 
   398 \begin{center}
   399 \begin{center}
   399 \begin{tabular}{cc}  
   400 \begin{tabular}{cc}  
   400 \begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]    
   401 \begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]    
   401 % chessboard
   402 % chessboard
   406   \fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
   407   \fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
   407 }
   408 }
   408 \fill[blue!50] (5,5) rectangle ++ (1,1);
   409 \fill[blue!50] (5,5) rectangle ++ (1,1);
   409 \fill[blue!50] (3,7) rectangle ++ (1,1);
   410 \fill[blue!50] (3,7) rectangle ++ (1,1);
   410 \fill[blue!50] (4,6) rectangle ++ (1,1);
   411 \fill[blue!50] (4,6) rectangle ++ (1,1);
   411 \fill[blue!50] (6,4) rectangle ++ (1,1);
   412 %\fill[blue!50] (6,4) rectangle ++ (1,1);
   412 \fill[blue!50] (7,3) rectangle ++ (1,1);
   413 \fill[blue!50] (7,3) rectangle ++ (1,1);
   413 
   414 
   414 % black pieces
   415 % black pieces
   415 \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}
   416 \foreach\x/\y/\e in {2/1/3,3/1/2,4/1/3,6/1/3,7/1/1,7/5/2}
   416   \pic[fill=white] at (\x,\y) {piece={\e}};
   417   \pic[fill=white] at (\x,\y) {piece={\e}};
   417 % white pieces
   418 % white pieces
   418 \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}
   419 \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}
   419   \pic[fill=red] at (\x,\y)     {piece={\e}};
   420   \pic[fill=red] at (\x,\y)     {piece={\e}};
   420 \pic[fill=white] at (5.0,1.0) {king={1}};
   421 \pic[fill=white] at (5.0,1.0) {king={1}};
   442 \fill[blue!50] (4,6) rectangle ++ (1,1);
   443 \fill[blue!50] (4,6) rectangle ++ (1,1);
   443 \fill[blue!50] (6,4) rectangle ++ (1,1);
   444 \fill[blue!50] (6,4) rectangle ++ (1,1);
   444 \fill[blue!50] (7,3) rectangle ++ (1,1);
   445 \fill[blue!50] (7,3) rectangle ++ (1,1);
   445 
   446 
   446 % black pieces
   447 % black pieces
   447 \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}
   448 \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}
   448   \pic[fill=white] at (\x,\y) {piece={\e}};
   449   \pic[fill=white] at (\x,\y) {piece={\e}};
   449 % white pieces
   450 % white pieces
   450 \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}
   451 \foreach\x/\y/\e in {1/8/4,2/8/2,3/8/4,5/8/4,6/8/2,7/8/3}
   451   \pic[fill=red] at (\x,\y)     {piece={\e}};
   452   \pic[fill=red] at (\x,\y)     {piece={\e}};
   452 \pic[fill=white] at (5.0,1.0) {king={1}};
   453 \pic[fill=white] at (5.0,1.0) {king={1}};
   453 \pic[fill=red]   at (4.0,8.0) {king={2}};
   454 \pic[fill=red]   at (4.0,8.0) {king={2}};
   454 
   455 
   455 \pic[fill=red] at (4,4) {piece={4}};
   456 \pic[fill=red] at (4,4) {piece={4}};
   464 \\[-5mm]                    
   465 \\[-5mm]                    
   465 \end{tabular}                    
   466 \end{tabular}                    
   466 \end{center}\hfill[3 Marks]
   467 \end{center}\hfill[3 Marks]
   467 
   468 
   468 \item[(2)] Implement an \texttt{all\_moves} function that calculates for a
   469 \item[(2)] Implement an \texttt{all\_moves} function that calculates for a
   469   piece and a board, \textit{all} pieces on legal onward positions. For this
   470   piece and a board, \textit{all} possible onward positions. For this
   470   you have to call \texttt{eval} for all possible moves \texttt{m} (that is \texttt{U},
   471   you have to call \texttt{eval} for all possible moves \texttt{m} (that is \texttt{U},
   471   \texttt{D}, \ldots, \texttt{DL}). An example for all moves for the red piece on (4, 4) are
   472   \texttt{D}, \ldots, \texttt{DL}). An example for all moves for the red piece on (4, 4) is
   472   shown in \eqref{moves} on page \pageref{moves}.\\ 
   473   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   Also for this task ignore the fact that a king cannot move onto an attacked field.\\ 
   473   \mbox{}\hfill[1 Mark]
   476   \mbox{}\hfill[1 Mark]
   474 
   477 
   475 \item[(3)] Implement a function \texttt{attacked} that takes a colour and a board
   478 \item[(3)] Implement a function \texttt{attacked} that takes a colour and a board
   476   and calculates all pieces of the opposite side that are attacked. For example
   479   and calculates all pieces of the opposite side that are attacked. For example
   477   below on the left are all the attacked pieces by red, and on the right for white:
   480   below in the left board are all the attacked pieces by red, and on the right all for white:
   478 
   481 
   479 \begin{center}
   482 \begin{center}
   480 \begin{tabular}{cc}      
   483 \begin{tabular}{cc}      
   481 \begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]    
   484 \begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]    
   482 % chessboard
   485 % chessboard
   488 }
   491 }
   489 \fill[blue!50] (7,3) rectangle ++ (1,1);
   492 \fill[blue!50] (7,3) rectangle ++ (1,1);
   490 \fill[blue!50] (6,0) rectangle ++ (1,1);
   493 \fill[blue!50] (6,0) rectangle ++ (1,1);
   491 
   494 
   492 
   495 
   493 % black pieces
   496 % red pieces
   494 \foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
   497 \foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
   495   \pic[fill=red] at (\x,\y) {piece={\e}};
   498   \pic[fill=red] at (\x,\y) {piece={\e}};
   496 % white pieces
   499 % white pieces
   497 \foreach\x/\y/\e in {8/4/2,4/1/2,8/7/3}
   500 \foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
   498   \pic[fill=white] at (\x,\y)     {piece={\e}};
   501   \pic[fill=white] at (\x,\y)     {piece={\e}};
   499 
   502 
   500 \pic[fill=red] at (4,2) {king={2}};
   503 \pic[fill=red] at (4,2) {king={2}};
   501 \pic[fill=white] at (7,1) {king={2}};
   504 \pic[fill=white] at (7,1) {king={2}};
   502 
   505 
   516 {
   519 {
   517   \pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25} 
   520   \pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25} 
   518   \fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
   521   \fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
   519 }
   522 }
   520 \fill[blue!50] (5,0) rectangle ++ (1,1);
   523 \fill[blue!50] (5,0) rectangle ++ (1,1);
   521 
   524 \fill[blue!50] (5,4) rectangle ++ (1,1);
   522 
   525 
   523 % black pieces
   526 % red pieces
   524 \foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
   527 \foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
   525   \pic[fill=red] at (\x,\y) {piece={\e}};
   528   \pic[fill=red] at (\x,\y) {piece={\e}};
   526 % white pieces
   529 % white pieces
   527 \foreach\x/\y/\e in {8/4/2,4/1/2,8/7/3}
   530 \foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
   528   \pic[fill=white] at (\x,\y)     {piece={\e}};
   531   \pic[fill=white] at (\x,\y)     {piece={\e}};
   529 
   532 
   530 \pic[fill=red] at (4,2) {king={2}};
   533 \pic[fill=red] at (4,2) {king={2}};
   531 \pic[fill=white] at (7,1) {king={2}};
   534 \pic[fill=white] at (7,1) {king={2}};
   532 
   535 
   553   and calculates the number of times this pieces is protected by pieces of the same colour.
   556   and calculates the number of times this pieces is protected by pieces of the same colour.
   554   For example the piece on field (8, 4) above is protected by 1 white pieces (the one on (8, 7)),
   557   For example the piece on field (8, 4) above is protected by 1 white pieces (the one on (8, 7)),
   555   and the piece on (5, 3) is protected by three red pieces ((6, 1), (4, 2), and (6, 5)).
   558   and the piece on (5, 3) is protected by three red pieces ((6, 1), (4, 2), and (6, 5)).
   556   \\
   559   \\
   557   \mbox{}\hfill[1 Mark]
   560   \mbox{}\hfill[1 Mark]
       
   561 
       
   562 \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.
       
   564   For example in the board below on the left, there are three possible fields the white king can
       
   565   reach, but all of them are attacked by red pieces. In the board on the right where the
       
   566   white king has an energy of 1, there is only one legal move, namely to move to field (8, 1).
       
   567   The field (7, 2) is reachable, but is attacked; similarly capturing the red piece on field (6, 1) is
       
   568   not possible because it is protected by at least another red piece.
       
   569 
       
   570   \begin{center}
       
   571   \begin{tabular}{cc}    
       
   572   \begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]    
       
   573 % chessboard
       
   574 \draw[very thick,gray] (0,0) rectangle (8,8);
       
   575 \foreach\x in {0,...,7}\foreach\y in {7,...,0}
       
   576 {
       
   577   \pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25} 
       
   578   \fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
       
   579 }
       
   580 \fill[blue!50] (5,1) rectangle ++ (1,1);
       
   581 \fill[blue!50] (6,2) rectangle ++ (1,1);
       
   582 \fill[blue!50] (7,1) rectangle ++ (1,1);
       
   583 
       
   584 
       
   585 % red pieces
       
   586 \foreach\x/\y/\e in {6/1/3,4/4/4,5/3/4,6/5/3}
       
   587   \pic[fill=red] at (\x,\y) {piece={\e}};
       
   588 % white pieces
       
   589 \foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
       
   590   \pic[fill=white] at (\x,\y)     {piece={\e}};
       
   591 
       
   592 \pic[fill=red] at (4,2) {king={2}};
       
   593 \pic[fill=white] at (7,1) {king={2}};
       
   594 
       
   595 % numbers
       
   596 \foreach\x in {1,...,8}
       
   597 {\draw (\x - 0.5, -0.4) node {\x};
       
   598 }
       
   599 \foreach\y in {1,...,8}
       
   600 {\draw (-0.4, \y - 0.6, -0.4) node {\y};
       
   601 }
       
   602 \end{tikzpicture}  &
       
   603  \begin{tikzpicture}[scale=0.45,every node/.style={scale=0.45}]    
       
   604 % chessboard
       
   605 \draw[very thick,gray] (0,0) rectangle (8,8);
       
   606 \foreach\x in {0,...,7}\foreach\y in {7,...,0}
       
   607 {
       
   608   \pgfmathsetmacro\blend{Mod(\x+\y,2)==0?75:25} 
       
   609   \fill[gray!\blend] (\x,\y) rectangle ++ (1,1);
       
   610 }
       
   611 \fill[blue!50] (5,0) rectangle ++ (1,1);
       
   612 \fill[blue!50] (6,1) rectangle ++ (1,1);
       
   613 \fill[blue!50] (7,0) rectangle ++ (1,1);
       
   614 
       
   615 
       
   616 % red pieces
       
   617 \foreach\x/\y/\e in {6/1/3,4/4/4,5/3/3,6/5/3}
       
   618   \pic[fill=red] at (\x,\y) {piece={\e}};
       
   619 % white pieces
       
   620 \foreach\x/\y/\e in {8/4/1,4/1/2,8/7/3,6/7/2}
       
   621   \pic[fill=white] at (\x,\y)     {piece={\e}};
       
   622 
       
   623 \pic[fill=red] at (4,2) {king={2}};
       
   624 \pic[fill=white] at (7,1) {king={1}};
       
   625 
       
   626 % numbers
       
   627 \foreach\x in {1,...,8}
       
   628 {\draw (\x - 0.5, -0.4) node {\x};
       
   629 }
       
   630 \foreach\y in {1,...,8}
       
   631 {\draw (-0.4, \y - 0.6, -0.4) node {\y};
       
   632 }
       
   633 \end{tikzpicture}                      
       
   634 \end{tabular}                     
       
   635 \end{center}
       
   636 \mbox{}\\ \mbox{}\hfill[1 Mark]
       
   637 
   558 \end{itemize}
   638 \end{itemize}
   559 
   639 
   560 \end{document}
   640 \end{document}
   561 
   641 
   562 %%% Local Variables: 
   642 %%% Local Variables: