Skip to content

Commit

Permalink
added matlab version
Browse files Browse the repository at this point in the history
  • Loading branch information
BartMassey committed Nov 10, 2012
1 parent 84c4195 commit 4fcb53a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ settings I could manage. The C was compiled with -O2 and the
Java with -O. The JavaScript was run with the d8 shell of
the v8 interpreter [1], with the Java-based rhino
interpreter shell [2], and with the js shell of SpiderMonkey
[3]. I don't have access to Matlab; I would be curious to
find out how its performance compares with Octave's on this
benchmark.
[3]. The Matlab version [4] was run on a different machine,
because I had access to Matlab there.

Timings on my home machine (2GHz Intel Core 2 Quad Q9300)
are as follows:
Expand All @@ -27,6 +26,7 @@ are as follows:
JavaScript [3]: 3.0s
Python: 8.3s
Nickle: 14s
Matlab [4]: 15s
Octave: 3.5m

Yes, Octave is 4000x slower than C on this benchmark!
Expand Down
72 changes: 72 additions & 0 deletions matlab/gamevalue.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
% Copyright © 2012 Bart Massey
% [This program is licensed under the "MIT License"]
% Please see the file COPYING in the source
% distribution of this software for license terms.

% Perfect Tic-Tac-Toe player in Matlab / Octave


% Returns the value of the game to the
% side on move if the game is over, or
% -2 if the game is still in progress.
function v = gamevalue(onmove, S)
% first scan for wins
for side = [-onmove, onmove]
v = side * onmove;
% scan for diagonal
n = 0;
for d = 1:3
if S(d, d) == side
n = n + 1;
end
end
if n == 3
return
end
% scan for opposite diagonal
n = 0;
for d = 1:3
if S(d, 4 - d) == side
n = n + 1;
end
end
if n == 3
return
end
% scan for rows
for r = 1:3
n = 0;
for c = 1:3
if S(r, c) == side
n = n + 1;
end
end
if n == 3
return
end
end
% scan for cols
for c = 1:3
n = 0;
for r = 1:3
if S(r, c) == side
n = n + 1;
end
end
if n == 3
return
end
end
end
% scan for blanks
v = -2; % game not over
for r = 1:3
for c = 1:3
if S(r, c) == 0
return
end
end
end
% game is a draw
v = 0;
end
44 changes: 44 additions & 0 deletions matlab/negamax.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
% Copyright ?? 2012 Bart Massey
% [This program is licensed under the "MIT License"]
% Please see the file COPYING in the source
% distribution of this software for license terms.

% Perfect Tic-Tac-Toe player in Matlab / Octave

% Pseudocode:

% To calculate the negamax value of a position s:
% If s is a final state (game over)
% return the value of s to the side on move
% v <- -1
% for each legal move m in s
% s' <- m(s)
% v' <- -negamax(s')
% if v' > v
% v <- v'
% return v

function v = negamax(onmove, S)
% printf("tomove: %d\n", onmove);
% S
v = gamevalue(onmove, S);
if v ~= -2 % game is over
% printf("result: *%d\n", v);
return
end
v = -1;
for r = 1:3
for c = 1:3
if S(r, c) == 0
% printf("examining %d %d\n", r, c);
S(r, c) = onmove;
v0 = -negamax(-onmove, S);
if (v0 > v)
v = v0;
end
S(r, c) = 0;
end
end
end
% printf("result: %d\n", v);
end
8 changes: 8 additions & 0 deletions matlab/ttt.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
% Copyright ?? 2012 Bart Massey
% [This program is licensed under the "MIT License"]
% Please see the file COPYING in the source
% distribution of this software for license terms.

% Perfect Tic-Tac-Toe player in Matlab / Octave

negamax(1, [0 0 0; 0 0 0; 0 0 0])

0 comments on commit 4fcb53a

Please sign in to comment.