I am trying to figure out why every move is considered invalid. The game is supposed ot run with two players playing a game of Othello.
function Othelloooooooo()
load Othello.mat;
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:};Board{7,:};Board{8,:}])
disp('Player 1 = White, Player 2 = Black');
player = 1;
game = 1;
while game == 1
%% Player's Turn
if player == 1
disp('Player 1 Turn');
oppcolor = 'blackdisc';
playercolor = 'whitedisc';
else
disp('Player 2 Turn');
oppcolor = 'whitedisc';
playercolor = 'blackdisc';
end
[row,column] = promptuserinput();
[empty, adj] = checkValidMove(row,column,oppcolor,playercolor,Board);
while empty == 0 || adj == 0
[row, column] = promptuserinput();
[empty, adj] = checkValidMove(row,column,playercolor,Board);
end
Board = makeMove(row, column, playercolor, oppcolor, Board);
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:};Board{7,:};Board{8,:}]);
player = 3 - player; % Switch player
% Check for end of game
if ~anyValidMoves(Board, oppcolor)
disp('No valid moves left. Game over.');
break;
end
game = input('Please enter if you would like to continue the game (1 for yes, 2 for no): ');
if game == 2
break;
end
end %end for while loop
% Display winner
disp('Game over.');
winner = getWinner(Board);
if winner == 0
disp('It is a draw!');
elseif winner == 1
disp('Player 1 (White) wins!');
else
disp('Player 2 (Black) wins!');
end
end
function x = checkAdjacent(row, column, color, Board)
directions = [-1, 0; 1, 0; 0, -1; 0, 1; -1, -1; -1, 1; 1, -1; 1, 1];
x = 0; % Initialize to zero
for d = 1:size(directions, 1)
dir = directions(d, :);
r = row + dir(1);
c = column + dir(2);
if r >= 1 && r <= 8 && c >= 1 && c <= 8 && strcmp(Board{r, c}, color)
x = 1; % At least one adjacent square has the same color
break; % No need to check further, so break the loop
end
end
end
function validMovesExist = anyValidMoves(Board, playercolor)
validMovesExist = false;
for row = 1:8
for column = 1:8
[empty, adj] = checkValidMove(row,column,playercolor,Board);
if empty == 1 && adj == 1
validMovesExist = true;
return;
end
end
end
end
function [empty, adj] = checkValidMove(row, column, oppcolor, playercolor, Board)
% Check if the space is empty
empty = isempty(Board{row,column});
if empty == 1
empty = 0;
warning('Not an empty square move, try again');
return;
end
% Check if adjacent squares have an opposite color
adj = checkAdjacent(row, column, oppcolor, Board);
if adj == 0
warning('Not a valid move');
return;
end
% Check if the move is valid and flip discs if needed
BoardTemp = Board;
BoardTemp{row, column} = playercolor;
flipped = flipDiscs(row, column, oppcolor, playercolor, BoardTemp);
if isempty(flipped)
adj = 0;
warning('Not a valid move');
return;
end
empty = 1;
end
function flipped = flipDiscs(row, column, oppcolor, playercolor, Board)
flipped = {};
directions = [-1, 0; 1, 0; 0, -1; 0, 1; -1, -1; -1, 1; 1, -1; 1, 1];
for d = 1:size(directions, 1)
dir = directions(d, :);
r = row + dir(1);
c = column + dir(2);
toFlip = {};
while r >= 1 && r <= 8 && c >= 1 && c <= 8 && ~isempty(Board{r, c}) && strcmp(Board{r, c}, oppcolor)
toFlip{end+1} = [r, c];
r = r + dir(1);
c = c + dir(2);
end
if r >= 1 && r <= 8 && c >= 1 && c <= 8 && ~isempty(Board{r, c}) && strcmp(Board{r, c}, playercolor)
for i = 1:numel(toFlip)
coord = toFlip{i};
flipped{end+1} = coord;
Board{coord(1), coord(2)} = playercolor;
end
end
end
end
function Board = makeMove(row, column, playercolor, oppcolor, Board)
Board{row, column} = playercolor;
flipped = flipDiscs(row, column, oppcolor, playercolor, Board);
end
function [row,column] = promptuserinput()
t = 1;
while t == 1
row = input('Please enter a row value:');
column = input('Please enter a column value:');
if row < 1 | row > 8 | column < 1 | column > 8
t = 1;
warning('Please enter a value between 1 and 8 (ex: 5).');
else
t = 2;
end
end
end
I have tried making sure that my function callings are correct and that they are in the right order. However, I have not figured it out yet. I think part of the issue lies in checking to see if the move is valid and the check adjacent function.
Rachel Evers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.