Last active
May 19, 2017 20:22
-
-
Save griebd/51253abe172c58f6d0f10bc804eb358f to your computer and use it in GitHub Desktop.
theaigames.com -> go (board game) competition -> starter bot in go (golang)
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
/***************************************************************************** | |
The MIT License (MIT) | |
Copyright (c) 2017 Adriano Grieb | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*****************************************************************************/ | |
/* | |
This bot simple plays valid random moves, and only this! | |
It may pass at any time, at random... | |
It may always pass even if there is a valid move, it doesn't recognize it... | |
It may fail unexpectedly... ;) | |
*/ | |
package main | |
import ( | |
"bufio" | |
"bytes" | |
"math/rand" | |
"os" | |
"strconv" | |
"time" | |
) | |
type settings struct { | |
timebank int | |
// Maximum time in milliseconds that your bot can have in its time bank | |
time_per_move int | |
// Time in milliseconds that is added to your bot's time bank each move | |
player_names []byte | |
// A list of all player names in this match, including your bot's name | |
your_bot []byte | |
// The name of your bot for this match | |
your_botid byte | |
// The number used in a field update as your bot's chips | |
field_width int | |
// The width, i.e. amount of columns, of the field | |
field_height int | |
// The height, i.e. amount of rows, of the field | |
max_rounds int | |
// The maximum number of rounds the game can last | |
} | |
type game struct { | |
field []byte | |
// The complete playing field in the current game state | |
round int | |
// The number of the current round | |
move int | |
// The amount of moves done in the game (including the current move) | |
bot_points int | |
// The amount of points in the game the given player currently has | |
opponent_points int | |
// The amount of points in the game the given player currently has | |
last_move_x int | |
last_move_y int | |
// The last move given player has performed (given player will be opponent) | |
} | |
func main() { | |
var settings settings | |
var game game | |
s := rand.NewSource(time.Now().UnixNano()) | |
r := rand.New(s) | |
stdin := bufio.NewScanner(os.Stdin) | |
for stdin.Scan() { | |
fields := bytes.Fields(stdin.Bytes()) | |
if len(fields) == 0 { | |
os.Stderr.WriteString("input error: no data (empty)\n") | |
continue | |
} | |
switch { | |
case bytes.Equal(fields[0], []byte("settings")): | |
parseSettings(&settings, &game, fields[1], fields[2]) | |
case bytes.Equal(fields[0], []byte("update")): | |
parseUpdate(&settings, &game, fields[1], fields[2], fields[3:]) | |
case bytes.Equal(fields[0], []byte("action")): | |
doAction(&settings, &game, r) | |
default: | |
os.Stderr.WriteString("input error: unknown command \"") | |
os.Stderr.Write(fields[0]) | |
os.Stderr.WriteString("\"\n") | |
} | |
} | |
if err := stdin.Err(); err != nil { | |
os.Stderr.WriteString("input error: ") | |
os.Stderr.WriteString(err.Error()) | |
} | |
} | |
func parseSettings(settings *settings, game *game, which, value []byte) { | |
switch { | |
case bytes.Equal(which, []byte("timebank")): | |
settings.timebank, _ = strconv.Atoi(string(value)) | |
case bytes.Equal(which, []byte("time_per_move")): | |
settings.time_per_move, _ = strconv.Atoi(string(value)) | |
case bytes.Equal(which, []byte("player_names")): | |
settings.player_names = make([]byte, len(value)) | |
copy(settings.player_names, value) | |
case bytes.Equal(which, []byte("your_bot")): | |
settings.your_bot = make([]byte, len(value)) | |
copy(settings.your_bot, value) | |
case bytes.Equal(which, []byte("your_botid")): | |
settings.your_botid = value[0] | |
case bytes.Equal(which, []byte("field_width")): | |
settings.field_width, _ = strconv.Atoi(string(value)) | |
if settings.field_height > 0 { | |
game.field = make([]byte, settings.field_height*settings.field_width) | |
} | |
case bytes.Equal(which, []byte("field_height")): | |
settings.field_height, _ = strconv.Atoi(string(value)) | |
if settings.field_width > 0 { | |
game.field = make([]byte, settings.field_height*settings.field_width) | |
} | |
case bytes.Equal(which, []byte("max_rounds")): | |
settings.max_rounds, _ = strconv.Atoi(string(value)) | |
default: | |
os.Stderr.WriteString("input error: unknown setting type \"") | |
os.Stderr.Write(which) | |
os.Stderr.WriteString("\"\n") | |
} | |
} | |
func parseUpdate(settings *settings, game *game, player, which []byte, | |
value [][]byte) { | |
switch { | |
case bytes.Equal(player, []byte("game")): | |
switch { | |
case bytes.Equal(which, []byte("round")): | |
game.round, _ = strconv.Atoi(string(value[0])) | |
case bytes.Equal(which, []byte("move")): | |
game.move, _ = strconv.Atoi(string(value[0])) | |
case bytes.Equal(which, []byte("field")): | |
for i, val := range bytes.Split(value[0], []byte(",")) { | |
if bytes.Equal(val, []byte("-1")) { | |
game.field[i] = '9' | |
} else { | |
game.field[i] = val[0] | |
} | |
} | |
default: | |
os.Stderr.WriteString("input error: unknown update (game) type \"") | |
os.Stderr.Write(which) | |
os.Stderr.WriteString("\"\n") | |
} | |
case bytes.Equal(player, settings.your_bot): | |
if bytes.Equal(which, []byte("points")) { | |
game.bot_points, _ = strconv.Atoi(string(value[0])) | |
} else { | |
os.Stderr.WriteString("input error: unknown update (your bot) type \"") | |
os.Stderr.Write(which) | |
os.Stderr.WriteString("\"\n") | |
} | |
case bytes.Contains(player, []byte("player")): | |
switch { | |
case bytes.Equal(which, []byte("points")): | |
game.opponent_points, _ = strconv.Atoi(string(value[0])) | |
case bytes.Equal(which, []byte("last_move")): | |
if bytes.Equal(value[0], []byte("place_move")) { | |
game.last_move_x, _ = strconv.Atoi(string(value[1])) | |
game.last_move_y, _ = strconv.Atoi(string(value[2])) | |
} else { | |
os.Stderr.WriteString("input error: last move: \"") | |
os.Stderr.Write(value[0]) | |
os.Stderr.WriteString("\"\n") | |
} | |
default: | |
os.Stderr.WriteString("input error: unknown update (opponent) type \"") | |
os.Stderr.Write(which) | |
os.Stderr.WriteString("\"\n") | |
} | |
default: | |
os.Stderr.WriteString("input error: unknown update player \"") | |
os.Stderr.Write(which) | |
os.Stderr.WriteString("\"\n") | |
} | |
} | |
func doAction(settings *settings, game *game, r *rand.Rand) { | |
move := getRandomMove(settings, game, r) | |
field := make([]byte, len(game.field)) | |
copy(field, game.field) | |
for !hasLiberties(settings, field, move) { | |
move = getRandomMove(settings, game, r) | |
} | |
if move == -1 { | |
os.Stdout.WriteString("pass\n") | |
} else { | |
os.Stdout.WriteString("place_move ") | |
os.Stdout.WriteString(strconv.Itoa(move % settings.field_width)) | |
os.Stdout.WriteString(" ") | |
os.Stdout.WriteString(strconv.Itoa(move / settings.field_height)) | |
os.Stdout.WriteString("\n") | |
} | |
} | |
func getRandomMove(settings *settings, game *game, r *rand.Rand) int { | |
movesCount := 0 | |
for _, val := range game.field { | |
if val == '0' { | |
movesCount += 1 | |
} | |
} | |
move := r.Intn(movesCount + 1) | |
if move != movesCount { | |
for i, val := range game.field { | |
if val == '0' { | |
move -= 1 | |
if move == 0 { | |
return i | |
} | |
} | |
} | |
} | |
return -1 | |
} | |
func hasLiberties(settings *settings, field []byte, pos int) bool { | |
if pos == -1 { | |
return true | |
} | |
field[pos] = 8 | |
x := pos % settings.field_width | |
y := pos / settings.field_height | |
if x > 0 && field[pos-1] == '0' { | |
return true | |
} | |
if x < settings.field_width-1 && field[pos+1] == '0' { | |
return true | |
} | |
if y > 0 && field[pos-settings.field_width] == '0' { | |
return true | |
} | |
if y < settings.field_height-1 && field[pos+settings.field_width] == '0' { | |
return true | |
} | |
if x > 0 && field[pos-1] == settings.your_botid { | |
if hasLiberties(settings, field, pos-1) { | |
return true | |
} | |
} | |
if x < settings.field_width-1 && field[pos+1] == settings.your_botid { | |
if hasLiberties(settings, field, pos+1) { | |
return true | |
} | |
} | |
if y > 0 && field[pos-settings.field_width] == settings.your_botid { | |
if hasLiberties(settings, field, pos-settings.field_width) { | |
return true | |
} | |
} | |
if y < settings.field_height-1 && | |
field[pos+settings.field_width] == settings.your_botid { | |
if hasLiberties(settings, field, pos+settings.field_width) { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment