Is this psuedocode implementation correct? (TicTacToe)

2 visualizzazioni (ultimi 30 giorni)
Matt Amador
Matt Amador il 2 Nov 2017
Commentato: Eric il 6 Nov 2017
Hello there. I've been at this particular psuedocode set for quite sometime, and the MATLAB implementation indicates something wrong in this, but even with the explanation it gives, I still can't seem to figure that's wrong with it. Can anyone help out?
Here is my attempted code:
clc
clear
G = [0 0 0; 0 0 0; 0 0 0];
turn = 1;
rand(5)
if rand < 0.5
turn = -1;
end
gameOn = true;
plotTicTacToe(G)
while gameOn == true
if turn < 0
[r,c] = getMove(G,-1);
if r == 0 || c == 0
r = [0,G];
c = [0,G];
end
G(r,c) = -1;
turn = 1;
else
r = input('Give r');
c = input('Give c');
if G(r,c) ~= 0
continue
else
G(r,c) = 1;
turn = -1;
end
end
plotTicTacToe(G)
p = getWinner(G);
if p == 1
disp('You won!')
gameOn = false;
elseif p == -1
disp('Computer won!')
gameOn = false;
elseif p == 2
disp('Draw!')
gameOn = false;
end
end
  2 Commenti
Walter Roberson
Walter Roberson il 2 Nov 2017
We do not have your plotTicTacToe or getMove or getWinner so we cannot execute the code to see what error message you are getting that you do not understand.
Matt Amador
Matt Amador il 2 Nov 2017
:
function[p1] = getWinner(p)
if p == -1
disp('Computer Wins!')
elseif p == 1
disp('You won!')
elseif p == 2
disp('Draw!')
elseif p == 0
disp('No winner yet.')
end
p1 = p;
getMove:
function[G, p] = getMove(r,c)
G = [0 0 0; 0 0 0; 0 0 0];
p = G;
s = 2 * p;
sumG = sum(G);
if sum(G) == s
c = find(sumG == s);
r = find(G(c) == 0);
elseif sum(G,3) == s
c = find(sumG(1,2,3) == s);
r = find(G(r,:) == 0);
elseif sum(diag(G)) == s
r = find(diagG(fliplr) == 0);
c = 4 - r;
else
r = 0;
c = 0;
end
plotTicTacToe:
function plotTicTacToe(G)
% plotTicTacToe(G) plot the Tic-tac-toe matrix on the command window
clc
fprintf(' \n');
fprintf(' | |\n');
fprintf(' %s | %s | %s \n', getSymb(G(1)), ...
getSymb(G(4)), ...
getSymb(G(7)));
fprintf(' ---+---+---\n');
fprintf(' %s | %s | %s \n', getSymb(G(2)), ...
getSymb(G(5)), ...
getSymb(G(8)));
fprintf(' ---+---+---\n');
fprintf(' %s | %s | %s \n', getSymb(G(3)), ...
getSymb(G(6)), ...
getSymb(G(9)));
fprintf(' | |\n');
fprintf(' \n');
end
Also, getSymb to get the symbol for the plot:
function[s] = getSymb(s)
disp('-1 = X, 1 = O, 0 = Empty Space');
p = input('Give me your mark: ');
if p == -1
s = 'X';
elseif p == 1
s = 'O';
elseif p == 0
s = ' ';
end
str = s;

Accedi per commentare.

Risposte (1)

Eric
Eric il 6 Nov 2017
Did you implement these functions yourself? They seem to be all mixed up. For example, plotTicTacToe is asking for inputs, getMove is checking for winners, and getWinner is doing the winner displaying unnecessarily.
Here is some advice:
  • plotTicTacToe: Should only display the board as it currently is in G, and should not change G (i.e. through getSymb)
  • getMove: Get the next move. This depends on the second argument, i.e., whether it is the computer's turn (turn<0) or the player's. If the players, you will need to ask them where to play. If the computer's, this is where you would put the AI, from simple (using a randi to pick a valid spot) or complex (checking the board in order to go for wins or block wins). Either way, should output the row and column to place the play in.
  • getWinner: Should check all rows/columns/diagonals for three in a row from the same player and declare that player the winner by what is output: -1 (com), 0 (none yet), 1 (player), 2 (tie).
  • getSymb: Unnecessary.
Once you get these functions straightened out, let us know if you are still running into problems.
  1 Commento
Eric
Eric il 6 Nov 2017
I will add that lines 11-13 would be unnecessary if you choose the com's move well in your getMove function. Also, upon a second look, it seems you take care of the player's moves in the main function, so getMove is only for the computer's move.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by