Question: function Othello load ( ' Othello . mat', 'whitedisc', 'blackdisc', 'Board' ) ; % Initialize game variables currentPlayer = ' B ' ; gameOver =

function Othello
load('Othello.mat', 'whitedisc', 'blackdisc', 'Board');
% Initialize game variables
currentPlayer ='B';
gameOver = false;
% Start the game
while ~gameOver
% Display the board
imshow([Board{1, :}; Board{2, :}; Board{3, :}; Board{4, :}; ...
Board{5, :}; Board{6, :}; Board{7, :}; Board{8, :}]);
% Get the player's move
[row, col]= getMove(currentPlayer);
% Check if the move is legal
if ~isValidMove(Board, currentPlayer, row, col)
disp('Invalid move. Please try again.');
continue;
end
% Make the move
Board{row, col}= currentPlayer;
% Flip the opponent's discs
flippedDiscs = flipDiscs(Board, currentPlayer, row, col);
% Check for game over conditions
gameOver = isGameOver(Board);
% Switch players
currentPlayer =(currentPlayer =='B')*'W'+(currentPlayer =='W')*'B';
end
% Declare the winner
winner = getWinner(Board);
disp(['Game over! Winner: ', winner]);
end
function [row, col]= getMove(currentPlayer)
% Prompt the player for their move
row = input(['Player ', currentPlayer, ', enter row (1-8): ']);
col = input(['Player ', currentPlayer, ', enter column (1-8): ']);
end
function valid = isValidMove(Board, player, row, col)
% Check if the square is empty
if ~isempty(Board{row, col})
valid = false;
return;
end
% Check if the move would flank any opponent's discs
valid = false;
for dr =[-1,1,0,0,-1,1,-1,1]
for dc =[-1,1,-1,1,-1,1,-1,1]
if canFlip(Board, player, row, col, dr, dc)
valid = true;
return
end
end
end
end
function flippedDiscs = flipDiscs(Board, player, row, col)
flippedDiscs =[];
for dr =[-1,1,0,0,-1,1,-1,1]
for dc =[-1,1,-1,1,-1,1,-1,1]
discsToFlip =[];
r = row + dr;
c = col + dc;
while r >=1 && r <=8 && c >=1 && c <=8 && Board{r, c} ~= player && Board{r, c} ~=''
discsToFlip =[discsToFlip; r, c];
r = r + dr;
c = c + dc;
end
if r >=1 && r <=8 && c >=1 && c <=8 && Board{r, c}== player
flippedDiscs =[flippedDiscs; discsToFlip];
end
end
end
for r =1:size(flippedDiscs,1)
Board{flippedDiscs(r,1), flippedDiscs(r,2)}= player;
end
end
function gameOver = isGameOver(Board)
gameOver = true;
for r =1:8
for c =1:8
if isempty(Board{r, c})
gameOver = false;
return;
end
end
end
end
function winner = getWinner(Board)
blackCount =0;
whiteCount =0;
for r =1:8
for c =1:8
if Board{r, c}=='B'
blackCount = blackCount +1;
elseif Board{r, c}=='W'
whiteCount = whiteCount +1;
end
end
end
if blackCount > whiteCount
winner = 'Black';
elseif whiteCount > blackCount
winner = 'White';
else
winner = 'Tie';
end
end
function canFlip = canFlip(Board, player, row, col, dr, dc)
r = row + dr;
c = col + dc;
while r >=1 && r <=8 && c >=1 && c <=8 && Board{r, c} ~= player && Board{r, c} ~=''
r = r + dr;
c = c + dc;
end
canFlip
= r >=1 && r <=8 && c >=1 && c <=8 && Board{r, 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!