Skip to content

Instantly share code, notes, and snippets.

@c0nrad
Last active Mar 20, 2016
Embed
What would you like to do?
package main
import (
"fmt"
"net/http"
"github.com/gorilla/websocket"
)
type Message struct {
// The GameID is used internally to reference games, and once the game is complete, you can view the results of the game at https://localhost/#/games/:id
GameID string
// Once the game is complete an extra message is sent down with a GameOverStatus > 0. 1=WhiteWin, 2=BlackWin, 3=Stalemate
GameOverStatus int //white win, black win, stalemate
// A list of 8 strings that layout the visible board. Print this out, and it'll all make sense. Lowercase characters are white, uppercase are black.
// k = king
// q = queen
// n = knight
// b = bishop
// r = rook
// p = pawn
Board []string
// Lets you know what color you are, for all the challenges you are white
IsWhite bool
}
type Response struct {
To string
From string
}
func main() {
dialer := websocket.Dialer{}
headers := make(http.Header)
// You can get this from the profile page
token := "xxxx-xxxx-xxxx-xxxx"
headers.Add("X-Websocket-Token", token)
// You can the challenges from the profile page
conn, _, err := dialer.Dial("wss://localhost:8080/battle/thecrazyking", headers)
if err != nil {
panic(err)
}
fmt.Println("[+] Connected!")
for {
var message Message
err = conn.ReadJSON(&message)
if err != nil {
panic(err)
}
if message.GameOverStatus > 0 {
fmt.Println("[+] Game Over!")
return
}
PrintBoard(message.Board)
response, _ := MakeMove(message)
fmt.Println(response)
err := conn.WriteJSON(response)
if err != nil {
panic(err)
}
}
func MakeMove(message Message) Response {
// Here goes your logic!
// You need to return your next move in Chess Algebraic notation
// For example to move the pawn in front of the king you would return
// Response{From: "e3", To: "e4"}
}
func PrintBoard(message Message) Response {
fmt.Println(message.Board)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment