-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84c4195
commit 4fcb53a
Showing
4 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |