Chess - Fundamental Programming Concepts - Solved Homework, Exercises of Computer Programming

This course provides an introduction to programming and problem solving using a high-level programming language. We use matlab to practice our concepts in coding. Some important keywords in this homework are: Chess, Initialization, Board Validation, Moving Pieces, Performing Multiple Moves, Possible Moves, Helper Functions, Pawn, Bishop, Knight

Typology: Exercises

2012/2013

Uploaded on 08/17/2013

zaid
zaid 🇮🇳

4.5

(2)

59 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 Chess
1.a Initialization
Initialization can be written as a simple assignment.
function board = initialize()
% Returns an initialized chessboard
board = [ 8 9 10 11 12 10 9 8;
77777777;
00000000;
00000000;
00000000;
00000000;
11111111;
23456432];
1.b Board Validation
A simple validation can be done by counting the number of pieces and figuring
out if there is an impossible scenario is in place. One can think of many compli-
cated situations to create conditional statements and then combine them with
logical operators for the validation. But since we will return only a true/false
value, at any point when we detect an invalid situation, we can set the output
to false and return.
Let’s write a couple conditions. The first one would be to check whether each
player has less than 9 pawns. Since pawns can be converted to other pieces
when they advance to the other edge of the board, for each extra piece of other
pieces, the number of lost pawns has to compensate for the difference. When
we count the number of excess pieces the bishop is tricky! Remember that our
validation function is incomplete.
function v = isvalid(board)
% Checks if the matrix provided as a chessboard is valid
% Consider counting pieces, board size etc. You don't need
% to consider pawns reaching the lowest or highest ranks.
% (but in the solutions we did.)
% Check board size
if ˜isequal(size(board),[8 8]), v = 0; return;
% Check number of pawns
nwp = sum(sum(board == 1)); % or use nnz function!
nbp = sum(sum(board == 7));
num pawn s = nwp <9 && nbp <9;
if ˜num pawns, v = 0; return;
% Number of kings
nwk = sum(sum(board == 6));
1
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Chess - Fundamental Programming Concepts - Solved Homework and more Exercises Computer Programming in PDF only on Docsity!

1 Chess

1.a Initialization

Initialization can be written as a simple assignment.

function board = initialize() % Returns an initialized chessboard

board = [ 8 9 10 11 12 10 9 8; 7 7 7 7 7 7 7 7; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0; 1 1 1 1 1 1 1 1; 2 3 4 5 6 4 3 2 ];

1.b Board Validation

A simple validation can be done by counting the number of pieces and figuring

out if there is an impossible scenario is in place. One can think of many compli-

cated situations to create conditional statements and then combine them with

logical operators for the validation. But since we will return only a true/false

value, at any point when we detect an invalid situation, we can set the output

to false and return.

Let’s write a couple conditions. The first one would be to check whether each

player has less than 9 pawns. Since pawns can be converted to other pieces

when they advance to the other edge of the board, for each extra piece of other

pieces, the number of lost pawns has to compensate for the difference. When

we count the number of excess pieces the bishop is tricky! Remember that our

validation function is incomplete.

function v = isvalid(board) % Checks if the matrix provided as a chessboard is valid % Consider counting pieces, board size etc. You don't need % to consider pawns reaching the lowest or highest ranks. % (but in the solutions we did.)

% Check board size if ˜isequal(size(board),[8 8]), v = 0; return;

% Check number of pawns nwp = sum(sum(board == 1)); % or use nnz function! nbp = sum(sum(board == 7)); num pawns = nwp < 9 && nbp < 9; if ˜num pawns, v = 0; return;

% Number of kings nwk = sum(sum(board == 6));

nbk = sum(sum(board ==12)); % We can only have one king for each player if nwk ˜= 1, v = 0; return; if nbk ˜= 1, v = 0; return;

% Number of bishops nwb = sum(sum(board == 4)); nbb = sum(sum(board ==10));

% Number of knights nwn = sum(sum(board == 3)); nbn = sum(sum(board == 9));

% Number of rooks nwr = sum(sum(board == 2)); nbr = sum(sum(board == 8));

% Number of queens nwq = sum(sum(board == 5)); nbq = sum(sum(board ==11));

% For each player initially we have two bishops sitting % on black or white squares. For a player, we can ignore % at most one bishop sitting on a white/black squares. % The r+c value is even for white squares, and odd for % black squares!

[rwb, cwb] = find(board == 4); wb = rwb+cwb; % r+c values for White's bishops wb on white = sum(rem(wb,2) == 0); wb on black = sum(rem(wb,2) == 1); % Minimum excess bishops for White nweb = max(wb on white − 1, 0) + max(wb on black − 1, 0);

[rbb, cbb] = find(board == 4); bb = rbb+cbb; % r+c values for White's bishops bb on white = sum(rem(bb,2) == 0); bb on black = sum(rem(bb,2) == 1); % Minimum excess bishops for Black nbeb = max(wb on white − 1, 0) + max(wb on black − 1, 0);

% Excess pieces for White nwe = nweb + max(nwn−2,0) + max(nwr−2,0) + max(nwq−2,0); if (nwp−8) < nwe, v = 0; return; % Excess pieces for Black nbe = nbeb + max(nbn−2,0) + max(nbr−2,0) + max(nbq−2,0); if (nbp−8) < nbe, v = 0; return;

% Two kings can't be neighbors! [rwk, cwk] = find(board == 6); [rbk, cbk] = find(board ==12); if abs(rwk−rbk) == 1 | | abs(cwk−cbk) == 1, v = 0; return;

% The pawns of a given player can't be at their home rank! wp = sum(sum(board(1,:) == 7)); bp = sum(sum(board(8,:) == 1)); if wp > 0 | | bp > 0, v = 0; return;

1.e Possible Moves

Assuming we have functions for each piece which return its possible moves, we

can scan the board piece by piece to generate their possible moves and combine

them together. We ignore castling, en passant etc. We also assume that we are

not in check position, so we don’t need to move our King or need to protect it

right now.

function moves = possible moves(board, bw, filename) % Returns the possible moves on the current board. % If bw is 0, moves correspond to White, if bw is 1 % moves correspond to Black. The moves are also written % to a text file specified by its filename. % The storage in the file should be in coordinate notation. % Each move should occupy a single line.

t = []; for r = 1: for c = 1: m = []; if bw == 0, switch board(r,c) case 1, m = pawn moves(r,c,board,bw); case 2, m = rook moves(r,c,board,bw); case 3, m = knight moves(r,c,board,bw); case 4, m = bishop moves(r,c,board,bw); case 5, m = queen moves(r,c,board,bw); case 6, m = king moves(r,c,board,bw); end else switch board(r,c) case 7, m = pawn moves(r,c,board,bw); case 8, m = rook moves(r,c,board,bw); case 9, m = knight moves(r,c,board,bw); case 10,m = bishop moves(r,c,board,bw); case 11,m = queen moves(r,c,board,bw); case 12,m = king moves(r,c,board,bw); end end

t = append moves(r,c,t,m); end end

moves = write moves to file(t,filename);

The target locations return by piece functions have to be appended with their

source location. We can employ array expansion using comma and semicolons.

The size of array t is (4×numberOfDiscoveredMovesSoFar), whereas the size of

m is (2×numberOfMovesOfTheCurrentPiece). We can append two rows on top

of m, and paste the result on the side of t.

function t = append moves(r,c,t,m) % Assuming m stores the target positions in two rows l = size(m,2); if l > 0, t = [t, [[rones(1,l); cones(1,l)]; m]]; end

The subfunction which writes the moves to a text file is as listed as follows:

function moves = write moves to file(t,fname) moves = [ char(t(2,:)−1+'A'); char(9−t(1,:)+'0'); char(t(4,:)−1+'A'); char(9−t(3,:)+'0')]; f = fopen(fname,'w'); for ii = 1:size(moves,2) fprintf(f,'%c%c−%c%c\n',moves(1,ii),moves(2,ii),moves(3,ii),moves(4,ii)); end fclose(f);

Helper Functions

In the code listings for piece move functions, we made use of the following helper

functions.

function ob = onboard(r,c) ob = r < 9 && r > 0 && c < 9 && c > 0;

function io = isopponent(r,c,board,bw) io = any(board(r,c) == ((1:6)+(1−bw)*6));

function e = isitempty(r,c,board) e = onboard(r,c) && (board(r,c) == 0);

function yn = cantake(r,c,board,bw) yn = onboard(r,c) && isopponent(r,c,board,bw);

function yn = canmove(r,c,board,bw) yn = onboard(r,c) && (cantake(r,c,board,bw) | | isitempty(r,c,board));

end

j = c; for i = (r−1):−1:1 % North−West direction j = j − 1; if j > 0 && possible(i,j), t = [t, [i;j]]; end if blocked, break; end end

k = c; blocked = 0; for i = (r−1):−1:1 % North−East direction k = k + 1; if k < 9 && possible(i,k), t = [t, [i;k]]; end if blocked, break; end end

The subfunction possible is given as follows:

function p = possible(a,b) p = ˜blocked && canmove(a,b,board,bw); if ˜isitempty(a,b,board), blocked = 1; end end % possible

end % bishop moves

Knight

Knight makes L-shaped moves and it can jump over other pieces.

function t = knight moves(r,c,board,bw) t = [];

if r > 2 if c > 1 && possible(r−2,c−1), t = [t, [r−2; c−1]]; end if c < 8 && possible(r−2,c+1), t = [t, [r−2; c+1]]; end end

if c > 2 if r > 1 && possible(r−1,c−2), t = [t, [r−1; c−2]]; end if r < 8 && possible(r+1,c−2), t = [t, [r+1; c−2]]; end end

if r < 7 if c > 1 && possible(r+2,c−1), t = [t, [r+2; c−1]]; end if c < 8 && possible(r+2,c+1), t = [t, [r+2; c+1]]; end end

if c < 7 if r > 1 && possible(r−1,c+2), t = [t, [r−1; c+2]]; end if r < 8 && possible(r+1,c+2), t = [t, [r+1; c+2]]; end end

function p = possible(a,b) p = canmove(a,b,board,bw); end

end

Rook

function t = rook moves(r,c,board,bw) t = [];

blocked = 0; for i = r−1:−1: if possible(i,c), t = [t,[i;c]]; end if blocked, break; end end

blocked = 0; for i = r+1: if possible(i,c), t = [t,[i;c]]; end if blocked, break; end end

blocked = 0; for i = c−1:−1: if possible(r,i), t = [t,[r;i]]; end if blocked, break; end end

blocked = 0; for i = c+1: if possible(r,i), t = [t,[r;i]]; end if blocked, break; end end

function p = possible(a,b) p = ˜blocked && canmove(a,b,board,bw); if ˜isitempty(a,b,board), blocked = 1; end end % possible

end % rook moves