Final Program with comments

Othello

%%

clear
clc

fprintf(‘Welcome to Othello! \n’)
fprintf(‘\nPlayer 1 will be the Black Team \n’)
fprintf(‘\nPlayer 2 will be the White Team \n’)
fprintf(‘\nThe black team will go first \n’)
fprintf(‘\nThe board will start with a set of crossed black and white pieces in the center.’)
fprintf(‘\nEach piece played must be laid adjacent to an another piece, surrounding an oppenents pieces on two adjacent sides will reasult \nin the capturing of the opponents pieces within your pieces.’)
fprintf(‘\ncaptured pieces will flip and change to your color.’)
fprintf(‘\nIt can happen that a piece is played so that pieces or rows of pieces in more than one direction \nare trapped between the new piece played and other pieces of the same color. In \nthis case, all the pieces in all viable directions change color \n’)
fprintf(‘\nThe game is over when neither player has a legal move ie: when the board is full. \n’)

[Othelo,Othelo_test] = resetBoard();
Othelo
player = 1;
while(true)
legal = false;
while(~legal)
[x,y] = location(player);
[legal,x,y] = is_legal(Othelo,Othelo_test,x,y,player);
end

Othelo(y,x) = player;
Othelo_test(y+1,x+1) = player;
Othelo_test = checkMoves(Othelo_test, y+1, x+1);
Othelo = update(Othelo_test) % this is the board after all proper adjustments are made, and what the sprites should represent
[gameOver,player] = endGame(Othelo, player);
if(gameOver)
break;
end
player = mod(player*2,3);
end
fprintf(“Player %i Wins!”, player)
%%
function [x,y] = location(play)
fprintf(“Player %i your move\n”,play)
x = input(‘Enter the x coordinet ‘);
y = input(‘Enter the y coordinet ‘);
while(x>8||y>8||x<1||y<1)
fprintf(“You must input coordinetes within the board\n”)
x = input(‘Enter the x coordinet ‘);
y = input(‘Enter the y coordinet ‘);
end
end
%% is_legal determines if a legal move is made
function [proper,x,y] = is_legal(small_Othelo,BEEG_Othelo,x,y,playerNum)
proper = false;
while (BEEG_Othelo(y+1,x+1)~=0)
fprintf(“you must place a piece in an empty slot\n”)
[x,y] = location(playerNum);
end

if(BEEG_Othelo(y+1 +1,x+1)~=0) % checks up
proper = true;
end
if(BEEG_Othelo(y+1 -1,x+1)~=0) % checks down
proper = true;
end
if(BEEG_Othelo(y+1,x+1 – 1)~=0) % checks left
proper = true;
end
if(BEEG_Othelo(y+1,x+1 + 1)~=0) % checks right
proper = true;
end
if(BEEG_Othelo(y+1 +1,x+1 +1)~=0) % checks up right
proper = true;
end
if(BEEG_Othelo(y+1 -1,x+1 +1)~=0) % checks down right
proper = true;
end
if(BEEG_Othelo(y+1 +1,x+1 -1)~=0) % checks up left
proper = true;
end
if(BEEG_Othelo(y+1 -1,x+1 -1)~=0) % checks down left
proper = true;
end
if(proper == false)
fprintf(“you must place your piece next to an exsisting piece\n”)
end
end
%% Resets the board to start with a b&w cross in the center needs an edit
% edit needed: should let the players pick the first four spots
function [Othelo,Othelo_test] = resetBoard()
boardSize = 8;
Othelo = [0,0];
for i = 1:boardSize
for j = 1:boardSize
Othelo(i,j) = 0;
end
end
Othelo(4,4) = 1;
Othelo(5,5) = 1;
Othelo(5,4) = 2;
Othelo(4,5) = 2;
Othelo_test = [0,0];

for i = 1:9
for j = 1:9
Othelo_test(i+1,j+1) = 0;
if(i<9&&j<9)
Othelo_test(i+1,j+1) = Othelo(i,j);
end
end
end
end
%% Check moves calls the the check: up down left etc function to initiate changes to the board
function [Othelo_works] = checkMoves(Big_othelo,y,x)

Big_othelo = checkUp(Big_othelo ,y , x);
Big_othelo = checkDown(Big_othelo ,y , x);
Big_othelo = checkLeft(Big_othelo ,y , x);
Big_othelo = checkRight(Big_othelo ,y , x);
Big_othelo = checkUpRight(Big_othelo ,y , x);
Big_othelo = checkUpLeft(Big_othelo ,y , x);
Big_othelo = checkDownRight(Big_othelo ,y , x);
Big_othelo = checkDownLeft(Big_othelo ,y , x);
Othelo_works = Big_othelo;

end
%% check the up moves
function [Othelo] = checkUp(Big_othelo ,Prow , Pcol)
row = Prow-1;
col = Pcol;
flip = false;
while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
row = row-1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow-1;
col = Pcol;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
row = row – 1;
end
Othelo = Big_othelo;
end
%% Check the down moves
function [Othelo] = checkDown(Big_othelo ,Prow , Pcol)
row = Prow+1;
col = Pcol;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
row = row+1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow+1;
col = Pcol;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
row = row + 1;
end
Othelo = Big_othelo;
end

%% check the left moves
function [Othelo] = checkLeft(Big_othelo ,Prow , Pcol)
row = Prow;
col = Pcol – 1;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
col = col-1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow;
col = Pcol – 1;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
col = col – 1;
end
Othelo = Big_othelo;
end
%% check the right moves
function [Othelo] = checkRight(Big_othelo ,Prow , Pcol)
row = Prow;
col = Pcol + 1;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
col = col+1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow;
col = Pcol + 1;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
col = col + 1;
end
Othelo = Big_othelo;
end
%% check the up right moves
% after this function the code may contain errors, will check later
function [Othelo] = checkUpRight(Big_othelo ,Prow , Pcol)
row = Prow – 1;
col = Pcol + 1;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
row = row – 1;
col = col+1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow – 1;
col = Pcol + 1;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
col = col + 1;
row = row – 1;
end
Othelo = Big_othelo;
end
%% check the up right moves
function [Othelo] = checkUpLeft(Big_othelo ,Prow , Pcol)
row = Prow – 1;
col = Pcol – 1;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
row = row – 1;
col = col-1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow – 1;
col = Pcol – 1;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
col = col – 1;
row = row – 1;
end
Othelo = Big_othelo;
end
%% check the down Left moves
function [Othelo] = checkDownLeft(Big_othelo ,Prow , Pcol)
row = Prow + 1;
col = Pcol – 1;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
row = row + 1;
col = col – 1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow + 1;
col = Pcol – 1;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col) = Big_othelo(Prow,Pcol);
col = col – 1;
row = row + 1;
end
Othelo = Big_othelo;
end
%% check the down Right moves
function [Othelo] = checkDownRight(Big_othelo ,Prow , Pcol)
row = Prow + 1;
col = Pcol + 1;
flip = false;

while(Big_othelo(row,col)~=0 && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
row = row + 1;
col = col + 1;
if(Big_othelo(row,col) == Big_othelo(Prow,Pcol))
flip = true;
end
end
row = Prow + 1;
col = Pcol + 1;
while(flip == true && Big_othelo(row,col) ~= Big_othelo(Prow,Pcol))
Big_othelo(row,col);
Big_othelo(Prow,Pcol);

Big_othelo(row,col) = Big_othelo(Prow,Pcol);
col = col + 1;
row = row + 1;
end
Othelo = Big_othelo;
end
%% update updates the othelo board to the othelo test
function [newOthelo] = update(Othelo)
newOthelo = [0;0];
for i = 2:9
for j = 2: 9
newOthelo(i-1,j-1) = Othelo(i,j);
end
end
end
%% function end the game and decides a winner
function [GameOver, player] = endGame(Board, playerNum)
GameOver = true;
count1 = 0;
count2 = 0;
for i = 1:8
for j = 1:8
if(Board(i,j)==0)
GameOver = false;
elseif(Board(i,j)==1)
count1 = count1+1;
elseif(Board(i,j)==2)
count2 = count2+1;
end
end

end
if(GameOver)
if(count1 > count2)
player = 1;
elseif(count1 < count2)
player = 2;
end
else
player = playerNum;
end
end

Hangman

Hangman script file and gameboard:

https://docs.google.com/document/d/1W8WmhtpXC0JADzPyjNwE4qq69DdnLK1B6k2d0zj-O0w/edit

Leave a Reply

Your email address will not be published. Required fields are marked *