Question: function Othello load ( ' Othello . mat', 'whitedisc', 'blackdisc', 'Board' ) ; % Initialize game variables currentPlayer = ' B ' ; gameOver =
function Othello
loadOthellomat', 'whitedisc', 'blackdisc', 'Board';
Initialize game variables
currentPlayer B;
gameOver false;
Start the game
while ~gameOver
Display the board
imshowBoard :; Board :; Board :; Board :;
Board :; Board :; Board :; Board :;
Get the player's move
row col getMovecurrentPlayer;
Check if the move is legal
if ~isValidMoveBoard currentPlayer, row, col
dispInvalid move. Please try again.;
continue;
end
Make the move
Boardrow col currentPlayer;
Flip the opponent's discs
flippedDiscs flipDiscsBoard currentPlayer, row, col;
Check for game over conditions
gameOver isGameOverBoard;
Switch players
currentPlayer currentPlayer BWcurrentPlayer WB;
end
Declare the winner
winner getWinnerBoard;
dispGame over! Winner: winner;
end
function row col getMovecurrentPlayer
Prompt the player for their move
row inputPlayer currentPlayer, enter row : ;
col inputPlayer currentPlayer, enter column : ;
end
function valid isValidMoveBoard player, row, col
Check if the square is empty
if ~isemptyBoardrow col
valid false;
return;
end
Check if the move would flank any opponent's discs
valid false;
for dr
for dc
if canFlipBoard player, row, col, dr dc
valid true;
return
end
end
end
end
function flippedDiscs flipDiscsBoard player, row, col
flippedDiscs ;
for dr
for dc
discsToFlip ;
r row dr;
c col dc;
while r && r && c && c && Boardr c ~ player && Boardr c ~
discsToFlip discsToFlip; r c;
r r dr;
c c dc;
end
if r && r && c && c && Boardr c player
flippedDiscs flippedDiscs; discsToFlip;
end
end
end
for r :sizeflippedDiscs
BoardflippedDiscsr flippedDiscsr player;
end
end
function gameOver isGameOverBoard
gameOver true;
for r :
for c :
if isemptyBoardr c
gameOver false;
return;
end
end
end
end
function winner getWinnerBoard
blackCount ;
whiteCount ;
for r :
for c :
if Boardr cB
blackCount blackCount ;
elseif Boardr cW
whiteCount whiteCount ;
end
end
end
if blackCount whiteCount
winner 'Black';
elseif whiteCount blackCount
winner 'White';
else
winner 'Tie';
end
end
function canFlip canFlipBoard player, row, col, dr dc
r row dr;
c col dc;
while r && r && c && c && Boardr c ~ player && Boardr c ~
r r dr;
c c dc;
end
canFlip
r && r && c && c && Boardr c player;
end
I believe the logic is wrong, It keeps telling me invalid move for the first move even when I put a legal move
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
