tic tac toe Algorithm
Tic-tac-toe (American English), noughts and crosses (British English), or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in put three of their marks in a horizontal, vertical, or diagonal row is the winner. game played on three-in-a-row boards can be traced back to ancient Egypt, where such game boards have been found on roofing tiles dating from around 1300 BCE.An early variation of tic-tac-toe was played in the Roman Empire, around the first century BC.
In 1952, OXO (or Noughts and cross), developed by British computer scientist Sandy Douglas for the EDSAC computer at the University of Cambridge, became one of the first known video games. The first print reference to a game named" tick-tack-toe" happened in 1884, but referred to" a children's game played on a slate, consisting in trying with the eyes shut to bring the pencil down on one of the numbers of a set, the number hit being scored".
% Author: Syed Haseeb Shah (QuantumNovice)
%Simple Tic Tac Toe
% Creates a row vector
space = 1:9;
% Converts it into matrix
space = reshape(space,[3,3]);
space = string(space);
marker = 'O';
reset = 0;
changes = 0;
for r = 1:3
disp( space(r,1) + " | " + space(r,2) + " | " +space(r,3) )
disp(' | |')
disp( '___________________')
end
while reset ~= 1
pos = input( char("Player ("+marker+")<< "));
if and(space(pos) ~= 'O', space(pos) ~= 'X')
space(pos) = marker;
changes = 1;
end
clc
% Win Check
for i = 1:3
% If rows are similaroz
if or(sum(space(i, :) == ["X","X","X"]) == 3, sum(space(i, :) == ["O","O","O"]) == 3)
disp("Player "+marker+" Wins")
reset = 1;
break
end
% If cols are similar
if or(sum(space(:, i) == ["X";"X";"X"]) == 3, sum(space(:, i) == ["O";"O";"O"]) == 3)
disp("Player "+marker+" Wins")
reset = 1;
break
end
% main diagonal
if and(space(1,1) == space(2,2), space(2,2) == space(3,3))
disp("Player "+marker+" Wins")
reset = 1;
break
end
% cross diagonal
if and(space(1,3) == space(2,2), space(2,2) == space(3,1))
disp("Player "+marker+" Wins")
reset = 1;
break
end
end
% Game Draw
count = 0;
for elm = 1:9
if or(space(elm) == "X", space(elm) == "O")
count = count + 1;
end
if count == 9
disp ("Game Draw")
reset = 1;
break
end
end
for r = 1:3
disp( space(r,1) + " | " + space(r,2) + " | " +space(r,3) )
disp(' | |')
disp( '___________________')
end
% Make sure previous positions are loocked
if changes == 1
switch marker
case "X"
marker = "O";
case "O"
marker = "X";
changes = 0;
end
end
end