MATLAB Answers

0

How can I write a smart TicTacToe code?

Asked by Christian Nill on 30 Apr 2019
Latest activity Answered by Geoff Hayes
on 15 May 2019
I am tasked with writing an algorithm that will play Tic-Tac-Toe against my classmates. I know how I want it to play, but I'm new to MATLAB and my professor isn't a very good teacher. I would like it to play to win. For example, if my code goes first, it will pick a corner. Based on what the second player's code does, it will base it's next move to win/tie. I'm not asking for a handout, just some tips that will point me in the right direction. The game involves four .m files which are below. The function game is my part of the code that will use the other functions to operate.
function tictactoe(f_user1,f_user2)
% clear all
% clc
%addpath()
user1=f_user1;
user2=f_user2;
user1wins=0;
user2wins=0;
user2loss=0;
user1loss=0;
ties=0;
user1_forfiet=0;
user2_forfiet=0;
for games=1:1000
%%
board=zeros(3,3);
turn=randi([1 2],1,1);
p=0;
while p==0
turn=turn+1;
if rem(turn,2)~=0
userboard=user1(board,-1);
else
userboard=user2(board,1);
end
p=legalmove(board,userboard,p);
if p~=3
p=winner(userboard);
end
board=userboard;
end
%%
if p==3
if rem(turn,2)==0
user2_forfiet=user2_forfiet+1;
user2loss=user2loss+1;
user1wins=user1wins+1;
else
user1_forfiet=user1_forfiet+1;
user1loss=user1loss+1;
user2wins=user2wins+1;
end
elseif p==2
ties=ties+1;
elseif p==-1
user1wins=user1wins+1;
user2loss=user2loss+1;
else
user2wins=user2wins+1;
user1loss=user1loss+1;
end
end
user1_stats=[user1wins user1loss ties user1_forfiet]
user2_stats=[user2wins user2loss ties user2_forfiet]
----------------------------------------------------------
function p=legalmove(board,userboard,p)
if any(size(userboard)~=size(board))
p=3;
elseif sum(sum(abs(board-userboard)))~=1;
p=3;
else
p=p;
end
----------------------------------------------------------
function p = winner(X)
% p = winner(X) returns
% p = 0, no winner yet,
% p = -1, user 1 has won,
% p = 1, user 2 has won,
% p = 2, game is a draw.
for p = [-1 1]
s = 3*p;
win = any(sum(X) == s) || any(sum(X') == s) || ...
sum(diag(X)) == s || sum(diag(fliplr(X))) == s;
if win
return
end
end
p = 2*all(X(:) ~= 0);
----------------------------------------------------------
function B=game(B,x)
r=randi(3,1);
c=randi(3,1);
while B(r,c) ~= 0
r=randi(3,1);
c=randi(3,1);
end
if B(r,c) == 0
r=randi(1,3);
elseif B(r,c) ~= 0
c=randi(1,3);
end
B(r,c)=x;

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 15 May 2019

Christian - what are the inputs f_user1 and f_user2? Are one of these the functions that you will create?
As for adding intelligence to your game, you would have to define what the rules are based on the moves by the opponents If the opponent has made its move, you could have one or more functions that determine whether
  • you can win on the next move (so the opponents last move is irrelevant)
  • you cannot win on the next move but the opponent can (so you need to block)
  • neither can win on the next move so you may want to try to find the smartest move so that you win on the next turn.
For the first function, are there empty squares where you can add your x (or o) so that you win? If so, then take that square.
For the second function, are there any empty squares where the opponent can add his/her x (or o) so that they win? If so, then take that square.
etc.
function [board] = myTicTacToeAI(board, marker)
if any(any(board) == 0)
% you are making the first move so choose a random corner
else
% check if you can win on this move
if iCanWinOnNextMove(board, marker)
% make the winning move
elseif opponentCanWinOnNextMove(board, marker)
% block their move
else
% position yourself to win on your next move
end
end
end
Not sure if the above is helpful but it give an idea on what to do. Note that I'm assuming that marker is the value you use to populate the board (the x or o) and that iCanWinOnNextMove and opponentCanWinOnNextMove functions that you would need to define.

  0 Comments

Sign in to comment.