





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 9
This page cannot be seen from the preview
Don't miss anything!






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 ];
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
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);
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
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);
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
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
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
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