TensorFlowでTicTacToeに挑戦 その3
概要
TnsorFlowでTicTacToeをやるために、OpenAiGymのTicTacToe環境作って見た。
TnsorFlowで学習したデータを、convnetjsで使ってみた。
写真
成果物
弱いな。
http://jsdo.it/ohisama1/yJXi
サンプルコード
enchant();
window.onload = function() {
var coma = '/assets/o/j/Y/2/ojY2w.png';
var ai0 = 'you';
var ai1 = 'tensorflow';
var ban = [];
var game = new Game(450, 450);
game.fps = 4;
game.preload([coma]);
game.onload = function() {
var GameScene = function() {
var scene = new Scene();
var aLabel = new Label('白 : ' + ai0 + '<br>黒 : ' + ai1);
aLabel.color = '#f33';
aLabel.x = 150;
aLabel.y = 30;
aLabel.font = '30px sans-serif';
scene.addChild(aLabel);
var label = new Label();
label.moveTo(20, 320);
scene.addChild(label);
ban = [1, 1, 1, 1, 1, 1, 1, 1, 1];
var field = [1, 1, 1, 1, 1, 1, 1, 1, 1];
for (var i = 0; i < 9; i++)
{
field[i] = new Sprite(40, 40);
field[i].image = game.assets[coma];
field[i].frame = ban[i];
field[i].x = (i % 3) * 40;
field[i].y = Math.floor(i / 3) * 40;
field[i].n = i;
field[i].on('touchstart', function() {
test(this.n);
});
scene.addChild(field[i]);
}
function isover() {
var res = -1;
for (var i = 0; i < 9; i++)
{
field[i].frame = ban[i];
if (ban[i] == 1)
{
res = 0;
}
}
if (res == -1) return res;
res = check(0);
if (res == -1) return res;
res = check(2);
return res;
}
function oku(put, iro) {
var res = 0;
if (ban[put] == 1)
{
ban[put] = iro;
res = -1;
}
return res;
}
function test(put) {
var res = oku(put, 2);
if (res == -1)
{
put = sasu(ban);
res = oku(put, 0);
}
if (isover() == -1)
{
game.replaceScene(gameover());
}
}
function check(iro) {
var res = 0;
if (ban[0] == iro && ban[1] == iro && ban[2] == iro) res = -1;
if (ban[3] == iro && ban[4] == iro && ban[5] == iro) res = -1;
if (ban[6] == iro && ban[7] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[3] == iro && ban[6] == iro) res = -1;
if (ban[1] == iro && ban[4] == iro && ban[7] == iro) res = -1;
if (ban[2] == iro && ban[5] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[4] == iro && ban[8] == iro) res = -1;
if (ban[2] == iro && ban[4] == iro && ban[6] == iro) res = -1;
return res;
}
return scene;
}
var gameover = function() {
function check(iro) {
var res = 0;
if (ban[0] == iro && ban[1] == iro && ban[2] == iro) res = -1;
if (ban[3] == iro && ban[4] == iro && ban[5] == iro) res = -1;
if (ban[6] == iro && ban[7] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[3] == iro && ban[6] == iro) res = -1;
if (ban[1] == iro && ban[4] == iro && ban[7] == iro) res = -1;
if (ban[2] == iro && ban[5] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[4] == iro && ban[8] == iro) res = -1;
if (ban[2] == iro && ban[4] == iro && ban[6] == iro) res = -1;
return res;
}
var str = "draw";
if (check(0) == -1) str = "win is ai!!";
if (check(2) == -1) str = "you win!!";
var scene = new Scene();
scene.backgroundColor = '#ccccfc';
var aLabel = new Label(str);
aLabel.color = '#033';
aLabel.x = 150;
aLabel.y = 150;
aLabel.font = '30px sans-serif';
scene.addChild(aLabel);
return scene;
};
game.replaceScene(GameScene());
}
game.start();
};
var net;
function sasu(ban) {
var res = 0;
do
{
ban2 = [0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 8; i++)
{
if (ban[i] == 0) ban2[i] = 1;
if (ban[i] == 2) ban2[i] = -1;
}
var action = Math.floor(Math.random() * 9);
ban2[action] = 1;
res = oukagai(ban2);
} while (res == 0);
return action;
}
function oukagai(ban) {
var res = 0;
var trainer = new convnetjs.Trainer(net);
var point = new convnetjs.Vol(1, 1, 9);
point.w = ban;
var prediction = net.forward(point);
if (prediction.w[1] > 0.9)
{
res = -1;
}
return res;
}
var load_from_json = function() {
$.getJSON("/assets/4/y/N/8/4yN82", function(json) {
net = new convnetjs.Net();
net.fromJSON(json);
});
}
load_from_json();
enchant();
window.onload = function() {
var coma = '/assets/o/j/Y/2/ojY2w.png';
var ai0 = 'you';
var ai1 = 'tensorflow';
var ban = [];
var game = new Game(450, 450);
game.fps = 4;
game.preload([coma]);
game.onload = function() {
var GameScene = function() {
var scene = new Scene();
var aLabel = new Label('白 : ' + ai0 + '<br>黒 : ' + ai1);
aLabel.color = '#f33';
aLabel.x = 150;
aLabel.y = 30;
aLabel.font = '30px sans-serif';
scene.addChild(aLabel);
var label = new Label();
label.moveTo(20, 320);
scene.addChild(label);
ban = [1, 1, 1, 1, 1, 1, 1, 1, 1];
var field = [1, 1, 1, 1, 1, 1, 1, 1, 1];
for (var i = 0; i < 9; i++)
{
field[i] = new Sprite(40, 40);
field[i].image = game.assets[coma];
field[i].frame = ban[i];
field[i].x = (i % 3) * 40;
field[i].y = Math.floor(i / 3) * 40;
field[i].n = i;
field[i].on('touchstart', function() {
test(this.n);
});
scene.addChild(field[i]);
}
function isover() {
var res = -1;
for (var i = 0; i < 9; i++)
{
field[i].frame = ban[i];
if (ban[i] == 1)
{
res = 0;
}
}
if (res == -1) return res;
res = check(0);
if (res == -1) return res;
res = check(2);
return res;
}
function oku(put, iro) {
var res = 0;
if (ban[put] == 1)
{
ban[put] = iro;
res = -1;
}
return res;
}
function test(put) {
var res = oku(put, 2);
if (res == -1)
{
put = sasu(ban);
res = oku(put, 0);
}
if (isover() == -1)
{
game.replaceScene(gameover());
}
}
function check(iro) {
var res = 0;
if (ban[0] == iro && ban[1] == iro && ban[2] == iro) res = -1;
if (ban[3] == iro && ban[4] == iro && ban[5] == iro) res = -1;
if (ban[6] == iro && ban[7] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[3] == iro && ban[6] == iro) res = -1;
if (ban[1] == iro && ban[4] == iro && ban[7] == iro) res = -1;
if (ban[2] == iro && ban[5] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[4] == iro && ban[8] == iro) res = -1;
if (ban[2] == iro && ban[4] == iro && ban[6] == iro) res = -1;
return res;
}
return scene;
}
var gameover = function() {
function check(iro) {
var res = 0;
if (ban[0] == iro && ban[1] == iro && ban[2] == iro) res = -1;
if (ban[3] == iro && ban[4] == iro && ban[5] == iro) res = -1;
if (ban[6] == iro && ban[7] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[3] == iro && ban[6] == iro) res = -1;
if (ban[1] == iro && ban[4] == iro && ban[7] == iro) res = -1;
if (ban[2] == iro && ban[5] == iro && ban[8] == iro) res = -1;
if (ban[0] == iro && ban[4] == iro && ban[8] == iro) res = -1;
if (ban[2] == iro && ban[4] == iro && ban[6] == iro) res = -1;
return res;
}
var str = "draw";
if (check(0) == -1) str = "win is ai!!";
if (check(2) == -1) str = "you win!!";
var scene = new Scene();
scene.backgroundColor = '#ccccfc';
var aLabel = new Label(str);
aLabel.color = '#033';
aLabel.x = 150;
aLabel.y = 150;
aLabel.font = '30px sans-serif';
scene.addChild(aLabel);
return scene;
};
game.replaceScene(GameScene());
}
game.start();
};
var net;
function sasu(ban) {
var res = 0;
do
{
ban2 = [0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 8; i++)
{
if (ban[i] == 0) ban2[i] = 1;
if (ban[i] == 2) ban2[i] = -1;
}
var action = Math.floor(Math.random() * 9);
ban2[action] = 1;
res = oukagai(ban2);
} while (res == 0);
return action;
}
function oukagai(ban) {
var res = 0;
var trainer = new convnetjs.Trainer(net);
var point = new convnetjs.Vol(1, 1, 9);
point.w = ban;
var prediction = net.forward(point);
if (prediction.w[1] > 0.9)
{
res = -1;
}
return res;
}
var load_from_json = function() {
$.getJSON("/assets/4/y/N/8/4yN82", function(json) {
net = new convnetjs.Net();
net.fromJSON(json);
});
}
load_from_json();
Author And Source
この問題について(TensorFlowでTicTacToeに挑戦 その3), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/be918fdbf963faa8adb0著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .