Skip to content

Commit

Permalink
Non square board size support (#271)
Browse files Browse the repository at this point in the history
* non square board size

* clean

* typo
  • Loading branch information
sanderland authored May 15, 2020
1 parent 7407006 commit 5bade48
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
42 changes: 22 additions & 20 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,11 @@ class Bot {
return false;
}

this.command(`boardsize ${state.width}`, () => {}, eb);
if (state.width === state.height) {
this.command(`boardsize ${state.width}`, () => {}, eb);
} else {
this.command(`boardsize ${state.width} ${state.height}`, () => {}, eb);
}
this.command("clear_board", () => {}, eb);
this.command(`komi ${state.komi}`, () => {}, eb);
//this.log(state);
Expand All @@ -378,32 +382,32 @@ class Bot {

let have_initial_state = false;
if (state.initial_state) {
const black = decodeMoves(state.initial_state.black, state.width);
const white = decodeMoves(state.initial_state.white, state.width);
const black = decodeMoves(state.initial_state.black, state.width, state.height);
const white = decodeMoves(state.initial_state.white, state.width, state.height);
have_initial_state = (black.length || white.length);

for (let i = 0; i < black.length; ++i)
this.command(`play black ${move2gtpvertex(black[i], state.width)}`, () => {}, eb);
this.command(`play black ${move2gtpvertex(black[i], state.width, state.height)}`, () => {}, eb);
for (let i = 0; i < white.length; ++i)
this.command(`play white ${move2gtpvertex(white[i], state.width)}`, () => {}, eb);
this.command(`play white ${move2gtpvertex(white[i], state.width, state.height)}`, () => {}, eb);
}

// Replay moves made
let color = state.initial_player;
const doing_handicap = (!have_initial_state && state.free_handicap_placement && state.handicap > 1);
const handicap_moves = [];
const moves = decodeMoves(state.moves, state.width);
const moves = decodeMoves(state.moves, state.width, state.height);
for (let i = 0; i < moves.length; ++i) {
const move = moves[i];

// Use set_free_handicap for handicap stones, play otherwise.
if (doing_handicap && handicap_moves.length < state.handicap) {
handicap_moves.push(move);
if (handicap_moves.length === state.handicap)
this.sendHandicapMoves(handicap_moves, state.width);
this.sendHandicapMoves(handicap_moves, state.width, state.height);
else continue; // don't switch color.
} else {
this.command(`play ${color} ${move2gtpvertex(move, state.width)}`)
this.command(`play ${color} ${move2gtpvertex(move, state.width, state.height)}`)
}

color = (color === 'black' ? 'white' : 'black');
Expand Down Expand Up @@ -487,7 +491,7 @@ class Bot {
if (!resign && !pass) {
if (move && move[0]) {
x = gtpchar2num(move[0]);
y = state.width - parseInt(move.substr(1))
y = state.height - parseInt(move.substr(1))
} else {
this.log(`${cmd} failed, resigning`);
resign = true;
Expand Down Expand Up @@ -518,14 +522,14 @@ class Bot {
}, 5000);
}
}
sendMove(move, width, color){
if (config.DEBUG) this.log("Calling sendMove with", move2gtpvertex(move, width));
this.command(`play ${color} ${move2gtpvertex(move, width)}`);
sendMove(move, width, height, color){
if (config.DEBUG) this.log("Calling sendMove with", move2gtpvertex(move, width, height));
this.command(`play ${color} ${move2gtpvertex(move, width, height)}`);
}
sendHandicapMoves(moves, width) {
sendHandicapMoves(moves, width, height) {
let cmd = "set_free_handicap";
for (let i = 0; i < moves.length; i++)
cmd += ` ${move2gtpvertex(moves[i], width)}`;
cmd += ` ${move2gtpvertex(moves[i], width, height)}`;
this.command(cmd);
}
// Called on game over, in case you need something special.
Expand All @@ -534,10 +538,8 @@ class Bot {
}
}

function decodeMoves(move_obj, board_size) {
function decodeMoves(move_obj, width, height) {
const ret = [];
const width = board_size;
const height = board_size;

/*
if (DEBUG) {
Expand Down Expand Up @@ -585,7 +587,7 @@ function decodeMoves(move_obj, board_size) {
for (let i = 0; i < moves.length; ++i) {
if (i%2) { /* even are the 'splits', which should always be blank unless there is an error */
let x = pretty_char2num(moves[i][0]);
let y = height-parseInt(moves[i].substring(1));
let y = height - parseInt(moves[i].substring(1));
if ( ((width && x >= width) || x < 0) ||
((height && y >= height) || y < 0) ) {
x = y = -1;
Expand Down Expand Up @@ -630,11 +632,11 @@ function pretty_char2num(ch) {
if (ch === ".") return -1;
return "abcdefghjklmnopqrstuvwxyz".indexOf(ch.toLowerCase());
}
function move2gtpvertex(move, board_size) {
function move2gtpvertex(move, width, height) {
if (move.x < 0) {
return "pass";
}
return num2gtpchar(move['x']) + (board_size-move['y'])
return num2gtpchar(move['x']) + (height-move['y'])
}
function num2gtpchar(num) {
if (num === -1)
Expand Down
8 changes: 4 additions & 4 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ class Game {
this.state.moves.push(move.move);

// Log opponent moves
const m = decodeMoves(move.move, this.state.width)[0];
const m = decodeMoves(move.move, this.state.width, this.state.height)[0];
if ((this.my_color === "white" && (this.state.handicap) >= this.state.moves.length) ||
move.move_number % 2 === this.opponent_evenodd)
this.log(`Got ${move2gtpvertex(m, this.state.width)}`);
this.log(`Got ${move2gtpvertex(m, this.state.width, this.state.height)}`);
} catch (e) {
console.error(e)
}
Expand All @@ -206,7 +206,7 @@ class Game {
this.makeMove(this.state.moves.length);
} else {
// If we are white, we wait for opponent to make extra moves.
if (this.bot) this.bot.sendMove(decodeMoves(move.move, this.state.width)[0], this.state.width, this.my_color === "black" ? "white" : "black");
if (this.bot) this.bot.sendMove(decodeMoves(move.move, this.state.width, this.state.height)[0], this.state.width, this.state.height, this.my_color === "black" ? "white" : "black");
if (config.DEBUG) this.log("Waiting for opponent to finish", this.state.handicap - this.state.moves.length, "more handicap moves");
if (this.state.moves.length ===1) { // remind once, avoid spamming the reminder
this.sendChat("Waiting for opponent to place all handicap stones"); // reminding human player in ingame chat
Expand All @@ -217,7 +217,7 @@ class Game {
// We just got a move from the opponent, so we can move immediately.
//
if (this.bot) {
this.bot.sendMove(decodeMoves(move.move, this.state.width)[0], this.state.width, this.my_color === "black" ? "white" : "black");
this.bot.sendMove(decodeMoves(move.move, this.state.width, this.state.height)[0], this.state.width, this.state.height, this.my_color === "black" ? "white" : "black");
}

if (config.corrqueue && this.state.time_control.speed === "correspondence" && Game.corr_moves_processing > 0) {
Expand Down

0 comments on commit 5bade48

Please sign in to comment.