Example #1
0
func main() {
	flag.Parse()
	if err := parseGameUrl(*url_arg); err != nil {
		fmt.Println("Could not parse game URL!", err)
		return
	}
	if err := runPlayerCommand(*player_arg); err != nil {
		fmt.Println("Could not execute player command! ", err)
		return
	}
	game_state.Create(ayu.DefaultSize)
	go io.Copy(os.Stderr, player_err)
	lines := make(chan string)
	go readStrings(player_out, '\n', lines)
	for !game_state.Over() {
		if (game_state.Next() == 0) == (white_key != nil) {
			// Player's turn
			if len(game_state.History) == 0 {
				player_in.Write([]byte("Start\n"))
			}
			move_str := strings.TrimSpace(<-lines)
			if move, ok := ayu.ParseMove(move_str); !ok {
				fmt.Println("Could not parse move:", move_str)
				break
			} else if !game_state.Execute(move) {
				fmt.Println("Player made invalid move:", move_str)
				break
			} else if err := postLastMove(); err != nil {
				fmt.Printf("Failed to post move '%s': %s\n", move, err)
				break
			} else {
				fmt.Println(">", move)
			}
		} else {
			// Opponent's turn
			if err := pollGame(len(game_state.History) + 1); err != nil {
				fmt.Println("Could not poll game state!", err)
				break
			}
			last_move := game_state.History[len(game_state.History)-1]
			fmt.Println("<", last_move)
			player_in.Write([]byte(last_move.String() + "\n"))
		}
	}
	fmt.Println("Game over.")
	player_in.Write([]byte("Quit\n"))
	player_cmd.Wait()
}
Example #2
0
func (ag AyuGame) ParseMove(s string) (interface{}, bool) {
	return ayu.ParseMove(s)
}