MATLAB PROJECT FILE
TIC-TAC-TOE(XOXO)
Submitted to- By-
Alok Kumar Pradyumn Vashisht
(211127)
Date 25-11-2022
Introduction
This MATLAB Project is based Tic Tac Toe , this is a very
basic game where two players play this game .
Tic-tac-toe is a game in which two players take turns in
drawing either an ` O' or an ` X' in one square of a grid
consisting of nine squares. The winner is the first player
to get three of the same symbols in a row.
SOURCE CODE
function TicTacToe
boards = false(3, 3, 2); % Players' pieces
rep = [' 1 | 4 | 7' ; ' 2 | 5 | 8' ; ' 3 | 6 | 9'];
fprintf('Tic-Tac-Toe:\n')
nHumans = str2double(input('Enter the number of players (people): ', 's'));
if isnan(nHumans) || ceil(nHumans) ~= nHumans || nHumans < 1 || nHumans > 2
nHumans = 0;
pHuman = false(2, 1);
elseif nHumans == 1
humanFirst = input('Would the human like to go first (Y/N)? ', 's');
if length(humanFirst) == 1 && lower(humanFirst) == 'n'
pHuman = [false ; true];
else
pHuman = [true ; false];
end
else
pHuman = true(2, 1);
end
if any('o' == input('Should Player 1 use X or O? ', 's'))
marks = 'OX';
else
marks = 'XO';
end
fprintf('So Player 1 is %shuman and %cs and Player 2 is %shuman and %cs.\n',
char('not '.*~pHuman(1)), marks(1), char('not '.*~pHuman(2)), marks(2))
if nHumans > 0
fprintf('Select the space to mark by entering the space number.\n')
fprintf('No entry will quit the game.\n')
end
gameOver = false;
turn = 1;
while ~gameOver
fprintf('\n')
disp(rep)
fprintf('\n')
if pHuman(turn)
[move, isValid, isQuit] = GetMoveFromPlayer(turn, boards);
gameOver = isQuit;
else
move = GetMoveFromComputer(turn, boards);
fprintf('Player %d chooses %d\n', turn, move)
isValid = true;
isQuit = false;
end
if isValid && ~isQuit
[r, c] = ind2sub([3 3], move);
boards(r, c, turn) = true;
rep(r, 4*c) = marks(turn);
if CheckWin(boards(:, :, turn))
gameOver = true;
fprintf('\n')
disp(rep)
fprintf('\nPlayer %d wins!\n', turn)
elseif CheckDraw(boards)
gameOver = true;
fprintf('\n')
disp(rep)
fprintf('\nCat''s game!\n')
end
turn = ~(turn-1)+1;
end
end
end
function [move, isValid, isQuit] = GetMoveFromPlayer(pNum, boards)
p1 = boards(:, :, 1);
p2 = boards(:, :, 2);
moveStr = input(sprintf('Player %d: ', pNum), 's');
if isempty(moveStr)
fprintf('Play again soon!\n')
move = 0;
isValid = true;
isQuit = true;
else
move = str2double(moveStr);
isQuit = false;
if isnan(move) || move < 1 || move > 9 || p1(move) || p2(move)
fprintf('%s is an invalid move.\n', moveStr)
isQuit = 0;
isValid = false;
else
isValid = true;
end
end
end
function move = GetMoveFromComputer(pNum, boards)
if ~any(boards(:))
move = 1;
else
pMe = boards(:, :, pNum);
pThem = boards(:, :, ~(pNum-1)+1);
possMoves = find(~(pMe | pThem)).';
move = FindWin(pMe, possMoves);
if move
return
end
move = FindWin(pThem, possMoves);
if move
return
end
for m = possMoves
newPMe = pMe;
newPMe(m) = true;
if CheckFork(newPMe, pThem)
move = m;
return
end
end
notGoodMoves = false(size(possMoves));
for m = possMoves
newPMe = pMe;
newPMe(m) = true;
if CheckPair(newPMe, pThem)
nextPossMoves = possMoves;
nextPossMoves(nextPossMoves == m) = [];
theirMove = FindWin(newPMe, nextPossMoves);
newPThem = pThem;
newPThem(theirMove) = true;
if ~CheckFork(newPThem, newPMe)
move = m;
return
else
notGoodMoves(possMoves == m) = true;
end
end
end
possMoves(notGoodMoves) = [];
if any(possMoves == 5)
move = 5;
return
end
corners = [1 3 7 9];
move = intersect(possMoves, ...
corners(~(pMe(corners) | pThem(corners)) & pThem(fliplr(corners))));
if ~isempty(move)
move = move(1);
return
end
move = intersect(possMoves, corners);
if move
move = move(1);
return
end
sides = [2 4 6 8];
move = intersect(possMoves, sides);
if move
move = move(1);
return
end
possMoves = find(~(pMe | pThem));
move = possMoves(randi(length(possMoves)));
end
end
function move = FindWin(board, possMoves)
for m = possMoves
newPMe = board;
newPMe(m) = true;
if CheckWin(newPMe)
move = m;
return
end
end
move = 0;
end
function win = CheckWin(board)
win = any(all(board)) || any(all(board, 2)) || ...
all(diag(board)) || all(diag(fliplr(board)));
end
function fork = CheckFork(p1, p2)
fork = sum([sum(p1)-sum(p2) (sum(p1, 2)-sum(p2, 2)).' ...
sum(diag(p1))-sum(diag(p2)) ...
sum(diag(fliplr(p1)))-sum(diag(fliplr(p2)))] == 2) > 1;
end
function pair = CheckPair(p1, p2)
pair = any([sum(p1)-sum(p2) (sum(p1, 2)-sum(p2, 2)).' ...
sum(diag(p1))-sum(diag(p2)) ...
sum(diag(fliplr(p1)))-sum(diag(fliplr(p2)))] == 2);
end
function draw = CheckDraw(boards)
draw = all(all(boards(:, :, 1) | boards(:, :, 2)));
end
OUTPUT
References and resources
Google
GitHub
MATLAB
Word
And others…
Conclusion…..
This file contains all the basic information about
MATLAB project in detail. With the help of my
teacher we were able to do so, pictures of
outcomes and full source code is provided so that it
becomes readable and easy to understand these
codes.
Certificate
This is to certify that Pradyumn Vashisht of CSE
batch CS31 has prepared the report on Tic Tac Toe
Game . The report is the result of our efforts and
endeavours .The report is found to be worthy of
acceptance as final project for the IT Workshop lab.
He have prepared the report under the guidance of
subject teacher ,Mr Alok Kumar
subject teacher
signature
________________