xxxxxxxxxx
138
// This is the "Game of Amazons"
// Rules:
// White moves first, and the players alternate moves thereafter. Each move consists of two parts. First, one moves one of one's own amazons one or more empty squares in a straight line (orthogonally or diagonally), exactly as a queen moves in chess; it may not cross or enter a square occupied by an amazon of either color or an arrow. Second, after moving, the amazon shoots an arrow from its landing square to another square, using another queenlike move. This arrow may travel in any orthogonal or diagonal direction (even backwards along the same path the amazon just traveled, into or across the starting square if desired). An arrow, like an amazon, cannot cross or enter a square where another arrow has landed or an amazon of either color stands. The square where the arrow lands is marked to show that it can no longer be used. The last player to be able to make a move wins. Draws are impossible.
// How to play:
// Click on the amazon of your colour, it'll turn green, then click on a cell on the board to move the chosen amazon there, then click on a cell to shoot there. If one of parts of your turn is against the rules, nothing will happen. You can't change your moves.
function setup() {
///////////////////////////////////////////////////
// Make changes for the numbers, but not letters //
///////////////////////////////////////////////////
c = 6; // columns
r = 6; // rows
sz = 60; // sidelength of a cell
g = new Game(c, r, sz);
// g.placePiece(x, y, color) makes a new amazon of the chosen color that starts in the cell (x,y).
// Color scheme: 0 for black, 1 for white.
// You're free to put more pieces on the board.
// Just don't place them in the same cell.
// It'll kill everyone.
g.placePiece(0, 3, 0);
g.placePiece(5, 2, 0);
g.placePiece(2, 0, 1);
g.placePiece(3, 5, 1);
/////////////////////////////////
// Don't change anything below //
/////////////////////////////////
m = makeModel();
positions = [];
bad_positions = [];
moves = [];
bad_moves = [];
game_count = 0;
}
function draw() {
console.log(millis());
//noprotect
while (g.mode[1] != 'w') {
var pos = g.state();
positions.push(pos);
var probs = m.predict(tf.tensor2d(pos, [1,g.cols*g.rows*5+3]));
var coord = probs.argMax(1).arraySync()[0];
var y = coord % g.cols;
var x = (coord - y) / g.rows;
//noprotect
while (!g.event(x, y)) {
//bad_positions.push(pos);
//bad_moves.push(tf.oneHot(tf.scalar(x * g.cols + y, 'int32'), g.cols*g.rows, 0, 1/(g.cols*g.rows-1)).arraySync());
probs = probs.arraySync();
probs[0][coord] = 0;
probs = tf.tensor(probs);
coord = probs.argMax(1).arraySync()[0];
y = coord % g.cols;
x = (coord - y) / g.rows;
}
moves.push(tf.oneHot(tf.scalar(x * g.cols + y, 'int32'), g.cols*g.rows).arraySync());
}
var train_moves = []
var train_pos = [];
if (g.mode[0] == 'w') {
for(var i = 0; i < moves.length; i+=6) {
train_moves.push(moves[i], moves[i+1], moves[i+2]);
train_pos.push(positions[i], positions[i+1], positions[i+2]);
}
}
else {
for(var i = 3; i < moves.length; i+=6) {
train_moves.push(moves[i], moves[i+1], moves[i+2]);
train_pos.push(positions[i], positions[i+1], positions[i+2]);
}
}
train_moves = tf.tensor2d(train_moves);
train_pos = tf.tensor2d(train_pos);
//var train_bad_moves = tf.tensor2d(bad_moves);
//var train_bad_pos = tf.tensor2d(bad_positions);
/*
m.fit(train_pos, train_moves, {
shuffle: true,
batchSize: 16,
epochs: 20,
});*/
/*
m.fit(train_bad_pos, train_bad_moves, {
shuffle: true,
batchSize: 64,
epochs: 15,
});*/
g.clear();
g.placePiece(0, 3, 0);
g.placePiece(5, 2, 0);
g.placePiece(2, 0, 1);
g.placePiece(3, 5, 1);
game_count++
if(game_count >= 9) {
//saveResults = m.save('downloads://my-model-1');
noLoop();
}
/*
g.show();
x = floor(random(0, c));
y = floor(random(0, r));
if (g.mode[0] == 'b') {
t = 0;
while (!g.event(x, y) && t < 50) {
x = floor(random(0, c));
y = floor(random(0, r));
t++;
}
}*/
}
/*
function mouseClicked() {
x = floor(mouseX / sz);
y = floor(mouseY / sz);
if (g.mode[0] == 'w') {
g.event(x, y);
}
console.log(g.state());
}*/