Tic Tac Toe Simulator - Searching a 3-D matrix for cases where 2-dimensions are identical
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
William Murray
il 15 Feb 2018
Commentato: William Murray
il 16 Feb 2018
I wrote a Monte Carlo code that randomly generates completed games of Tic-Tac-Toe in a 3x3 "A" matrix. Each game is stored in a 3x3xN "Games" matrix, where N is the number of games being simulated.
My question is the following: once I have a 3x3xN "Games" matrix of Tic-Tac-Toe games, how can I search the "Games" matrix to determine which games are identical? My goal for this code is to determine the number of unique, non-identical games of Tic-Tac-Toe.
Below I have is the code for generating a number of random games:
%Number of games per simulation
N = 1E6;
Games = zeros(3,3,N);
Unique = zeros(3,3,N+1);
ticker = 1;
for i = 1:N;
A = zeros(3,3);
flag = 0;
turn = 1;
while flag == 0;
%Player 1 goes first, Player 2 goes next, switch
if turn == 1;
n = 1;
else if turn ==0;
n = 2;
end
end
%Check to see if board is full BEFORE player makes move
Full = all(A(:) > 0);
if Full == 1
break
end
%Turn repeats until a marker is placed or board is full
flag_2 = 0;
while flag_2 == 0;
%Checks if board is full
Full = all(A(:) > 0);
if Full == 1
break
end
%Pick a coordinate
x = randi(3);
y = randi(3);
%Player Places Marker unless board is full
if A(x,y) == 0;
A(x,y) = n;
flag_2 = 1;
end
end
%Checks for win conditions (8 possible)
flag_winner = 0;
%Check rows
if A(1,1) == A(1,2) && A(1,1) == A(1,3) && A(1,1) ~= 0; %Row1
flag_winner = 1;
elseif A(2,1) == A(2,2) && A(2,1) == A(2,3) && A(2,1) ~= 0; %Row2
flag_winner = 2;
elseif A(3,1) == A(3,2) && A(3,1) == A(3,3) && A(3,1) ~= 0; %Row3
flag_winner = 3;
%Check columns
elseif A(1,1) == A(2,1) && A(1,1) == A(3,1) && A(1,1) ~= 0; %Col1
flag_winner = 4;
elseif A(1,2) == A(2,2) && A(1,2) == A(3,2) && A(1,2) ~= 0; %Col2
flag_winner = 5;
elseif A(1,3) == A(2,3) && A(1,3) == A(3,3) && A(1,3) ~= 0; %Col3
flag_winner = 6;
%Check Diagonals
elseif A(1,1) == A(2,2) && A(1,1) == A(3,3) && A(1,1) ~= 0; %UpD
flag_winner = 7;
elseif A(3,1) == A(2,2) && A(3,1) == A(1,3) && A(3,1) ~= 0; %DownD
flag_winner = 8;
end
%End game if there is a winner
if flag_winner ~= 0;
winner = n;
break
end
%Change Player's Turn
turn = 1 - turn;
end
%Stores game
Games(:,:,i) = A(:,:);
end
Below is the search method I am currently using; it is placed after the game generation code and runs through the games after all games have been made and stored in the "Games" matrix.
%Compare games to see which games are identical
%Counter variable s(i)
for i = 1:N
s(i) = 0;
Test = Games(:,:,i);
for j = i+1:N
if Test == Games(:,:,j);
s(i) = s(i) + 1;
break
end
end
end
%How many similar games?
Similar = nnz(s);
%How many unique games?
Unique(q) = N - Similar
Any assistance would be greatly appreciated!
0 Commenti
Risposta accettata
Rik
il 15 Feb 2018
If you merge the first two dimensions, you should be able to use the unique function with the 'row' option.
A2=squeeze([A(1,:,:) A(2,:,:) A(3,:,:)]);
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!