Tic Tac Toe Simulator - Searching a 3-D matrix for cases where 2-dimensions are identical

2 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Rik
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,:,:)]);
  1 Commento
William Murray
William Murray il 16 Feb 2018
I used your method and it yielded the same results as the search method I implemented. That said, using these functions were significantly faster than the method I was doing. I can't think of any errors with either search method, but having another method available to validate my own results makes me more confident with my program. Thanks for the help!
I am attaching the code I used for reference, this is placed outside the main loop:
%Matlab Forum Solution
Games2 = squeeze([Games(1,:,:) Games(2,:,:) Games(3,:,:)]);
Games2 = transpose(Games2);
%Check for unique values
[Rows,ia,ic] = unique(Games2(:,1:9),'rows');
Size_games = size(Rows);
Number_games(q) = Size_games(1)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Strategy & Logic in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by