Пример #1
0
func handleConnect(conn *websocket.Conn) {
	player := httpPlayer{game.Black, conn}

	var c commandFromClient
	err := conn.ReadJSON(&c)
	if err != nil {
		log.Println("websocket error:", err)
		return
	}

	switch c.Command {
	case "new_game":
		var args newGameArgs
		if err := json.Unmarshal(c.Args, &args); err != nil {
			log.Printf("bad new_game args: %s (%s)", err, string(c.Args))
			return
		}

		game, err := game.NewGame(game.Config{
			Black:  player,
			White:  cpuPlayer,
			Width:  args.Width,
			Height: args.Height,
			Logger: os.Stderr,
		})

		if err != nil {
			log.Printf("error creating game: %s", err)
			return
		}

		result := game.Play()

		gameOver := commandToClient{
			Command: "game_over",
			Args: map[string]string{
				"message": fmt.Sprintf("%s (%s) is the winner!", result.Winner.Color(), result.Winner.Name()),
			},
		}
		if err := conn.WriteJSON(gameOver); err != nil {
			log.Printf("error sending game_over: %s", err)
			return
		}
	default:
		log.Printf("unknown command from http player: %s", c.Command)
	}
}
Пример #2
0
func Go(cpuPlayer game.Player) {
	config := game.Config{
		Black: cliPlayer{game.Black},
		White: cpuPlayer,

		Width:  8,
		Height: 8,
	}

	g, err := game.NewGame(config)
	if err != nil {
		panic(err)
	}

	winner := g.Play()

	fmt.Fprintf(os.Stdout, "\n%s is the winner!\n", winner.Name())
}
Пример #3
0
func Go(whitePlayer, blackPlayer game.Player) {
	if blackPlayer == nil {
		blackPlayer = cliPlayer{game.Black}
	}

	config := game.Config{
		Black: blackPlayer,
		White: whitePlayer,

		Width:  8,
		Height: 8,
	}

	g, err := game.NewGame(config)
	if err != nil {
		panic(err)
	}

	result := g.Play()

	fmt.Fprintf(os.Stdout, "%s is the winner with %d remaining!\n", result.Winner.Color(), result.Score)
}