Keep getting the error "not enoguh input arguments"
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I'm trying to write a code for a tic tac toe game. I have a loop going and I am working on changing the elements of an array, given an input from the player. Th eproblem is I keep getting the errors shown in the screenshot below.
I just have no idea what I'm doing wrong, any help would be greatly appreciated, thanks!
board = [
["[-]", "[-]", "[-]"];
["[-]", "[-]", "[-]"];
["[-]", "[-]", "[9]"]];
inGame = true;
player = 1;
while inGame == true
player_input = 3;
%input("\nWhere would you like to input your first character?: ");
if not(within(player_input))
disp("Oops! Please input a number between 1-9!")
continue
end
if not(free_space(player_input))
disp("Please try a different space!")
continue
end
replace(board, player_input, player)
checkWin(board, player) %if no win is found, it should do nothing and just continue loop
end
% checks if input is within 1-9
function isWithin = within(player_input)
if (player_input>9) || (player_input<1)
isWithin = false;
else
isWithin = true;
end
end
% converts player input to coordinates
function x = xCoordinate(player_input)
x = floor(player_input/3) + 1;
end
function y = yCoordinate(player_input)
y = mod(player_input, 3) + 1;
end
% checks if space is free
function free = free_space(board, player_input)
x = xCoordinate(player_input);
y = yCoordinate(player_input);
if board(x, y) ~= "[-]"
print("\nOops! That space is already taken!")
free = false;
else
free = true;
end
end
% marks the selected box HAVING TROUBLE
function replace(board, player_input, player)
if not(free_space(board, player_input))
false %want it to return false
else
x = xCoordinate(player_input);
y = yCoordinate(player_input);
if player == -1
board(x,y) = "[X]"
else
board(x, y) = "[O]"
end
end
end
% check win
function checkWin(board, player)
mark = "";
winner = "";
if player == -1
mark = "[X]"
winner = "Player 1 "
else
mark = "[O]"
winner = "Player 2 "
end
%possible wins (by rows)
if ((board(1, 1) == mark) && (board(1,2) == mark) && (board(1, 3) == mark)) || ((board(2, 1) == mark) && (board(2,2) == mark) && (board(2,3) == mark)) || ((board(3, 1) == mark) && (board(3,2) == mark) && (board(3, 3) == mark))
fprintf("%d wins!", winner)
inGame = false;
%possible wins (by columns)
elseif (board(1, 1) == mark && board(2,1) == mark && board(3, 1) == mark) || (board(1, 2) == mark && board(2,2) == mark && board(3,2) == mark) || (board(1, 3) == mark && board(2,3) == mark && board(3, 3) == mark)
fprintf("%d wins!", winner)
inGame = false;
%possible wins (by diagonals)
elseif (board(1, 1) == mark && board(2,2) == mark && board(3, 3) == mark) || (board(3, 1) == mark && board(2,2) == mark && board(1,3) == mark)
fprintf("%d wins!", winner)
inGame = false;
else
% do nothing
end
end
0 件のコメント
採用された回答
the cyclist
2022 年 10 月 11 日
You call the free_space function like this:
if not(free_space(player_input))
but the free_space function takes two arguments:
% checks if space is free
function free = free_space(board, player_input)
x = xCoordinate(player_input);
y = yCoordinate(player_input);
if board(x, y) ~= "[-]"
print("\nOops! That space is already taken!")
free = false;
else
free = true;
end
end
Looks like you need to add the first argument board.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Camera Calibration についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!