Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
56 views3 pages

Matlab Code Tic Tac Toe

This document outlines the implementation of a Tic Tac Toe game using MATLAB, featuring a graphical user interface. It includes functionality for player turns, win checking, and resetting the game. The code defines the game board, player interactions, and visual feedback for wins and draws.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Matlab Code Tic Tac Toe

This document outlines the implementation of a Tic Tac Toe game using MATLAB, featuring a graphical user interface. It includes functionality for player turns, win checking, and resetting the game. The code defines the game board, player interactions, and visual feedback for wins and draws.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

function tic_tac_toe

board = zeros(3,3); % 0: empty, 1: X, -1: O


currentPlayer = 1; % 1: X, -1: O
gameActive = true;

% Figure with dark beige background


f = figure('Name','Tic Tac
Toe','NumberTitle','off','MenuBar','none','Position',[500 300 350
420],'Resize','off','Color',[129/1400 140/1400 120/1400]); % dark beige

% Status text
statusText = uicontrol('Style','text','String','Player X''s
turn','Position',[25 370 300 30],'FontSize',16,'BackgroundColor',[129/1400
140/1400 120/1400],'ForegroundColor',[248/248 243/248
217/248],'HorizontalAlignment','center');

% 3x3 game board buttons


btnSize = 80;
spacing = 5;
buttons = gobjects(3,3);
for row = 1:3
for col = 1:3
xpos = (col-1)*(btnSize+spacing) + 35;
ypos = 260 - (row-1)*(btnSize+spacing);
buttons(row,col) = uicontrol('Style','pushbutton','Position',
[xpos ypos btnSize btnSize],'FontSize',32,'String','','BackgroundColor',
[2/10 2/10 2/10],'ForegroundColor',[248/248 243/248
217/248],'Callback',@(src,~)buttonPress(src,row,col));
end
end

% Reset button
uicontrol('Style','pushbutton','String','Reset','Position',[125 30 100
40],'FontSize',14,'BackgroundColor',[0.2 0.2 0.2],'ForegroundColor',
[248/248 243/248 217/248],'Callback',@resetGame);

% Button press callback


function buttonPress(src,row,col)
if ~gameActive || board(row,col) ~= 0
return
end
if currentPlayer == 1
src.String = 'X';
else
src.String = 'O';
end
board(row,col) = currentPlayer;
if checkWin(board, currentPlayer)
statusText.String = sprintf('Player %s wins!',
playerStr(currentPlayer));
highlightWin(board, currentPlayer, buttons);
gameActive = false;
set(buttons, 'Enable', 'off'); % Disable buttons
elseif all(board(:) ~= 0)
statusText.String = 'It''s a draw!';
gameActive = false;
set(buttons, 'Enable', 'off'); % Disable buttons
else
currentPlayer = -currentPlayer;
statusText.String = sprintf('Player %s''s turn',
playerStr(currentPlayer));
end
end

% Win checking function


function win = checkWin(b, player)
target = player * 3;
win = any(sum(b, 1) == target) || any(sum(b, 2) == target) ||
(sum(diag(b)) == target) || (sum(diag(fliplr(b))) == target);
end

% Highlight the winning line


function highlightWin(b, player, buttons)
% Rows
for row = 1:3
if all(b(row,:) == player)
for c = 1:3
buttons(row,c).BackgroundColor = [];
end
return;
end
end
% Columns
for col = 1:3
if all(b(:,col) == player)
for r = 1:3
buttons(r,col).BackgroundColor = [248/248 243/248
217/248];
end
return;
end
end
% Main diagonal
if all(diag(b) == player)
for d = 1:3
buttons(d,d).BackgroundColor = [248/248 243/248 217/248];
end
return;
end
% Anti-diagonal
if all(diag(fliplr(b)) == player)
buttons(1,3).BackgroundColor = [248/248 243/248 217/248];
buttons(2,2).BackgroundColor = [248/248 243/248 217/248];
buttons(3,1).BackgroundColor = [248/248 243/248 217/248];
end
end

% Player string
function s = playerStr(p)
if p == 1, s = 'X'; else, s = 'O'; end
end

% Reset game
function resetGame(~,~)
board(:) = 0;
currentPlayer = 1;
gameActive = true;
statusText.String = 'Player X''s turn';
for r = 1:3
for c = 1:3
buttons(r,c).String = '';
buttons(r,c).BackgroundColor = [2/10 2/10 2/10];
buttons(r,c).Enable = 'on';
end
end
end
end

You might also like