Beispiel #1
0
func (b *TictactoeBot) PlayTurn(question bots.QuestionMessage) *bots.ReplyMessage {
	ttt := tictactoe.NewTicTacToe()

	board := question.Board.(map[string]interface{})

	ttt.Set(0, 0, board["0-0"].(string))
	ttt.Set(0, 1, board["0-1"].(string))
	ttt.Set(0, 2, board["0-2"].(string))
	ttt.Set(1, 0, board["1-0"].(string))
	ttt.Set(1, 1, board["1-1"].(string))
	ttt.Set(1, 2, board["1-2"].(string))
	ttt.Set(2, 0, board["2-0"].(string))
	ttt.Set(2, 1, board["2-1"].(string))
	ttt.Set(2, 2, board["2-2"].(string))
	ttt.SetPlayer(question.You.(string))

	logrus.Debugf("map: %q", ttt.ShowMap())

	next, err := ttt.Next()
	if err != nil {
		logrus.Fatalf("Error while getting the next piece: %v", err)
	}

	return &bots.ReplyMessage{
		Play: fmt.Sprintf("%d-%d", next.Y, next.X),
	}
}
Beispiel #2
0
func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		ttt := tictactoe.NewTicTacToe()
		ttt.Set(0, 0, c.Query("0-0"))
		ttt.Set(0, 1, c.Query("0-1"))
		ttt.Set(0, 2, c.Query("0-2"))
		ttt.Set(1, 0, c.Query("1-0"))
		ttt.Set(1, 1, c.Query("1-1"))
		ttt.Set(1, 2, c.Query("1-2"))
		ttt.Set(2, 0, c.Query("2-0"))
		ttt.Set(2, 1, c.Query("2-1"))
		ttt.Set(2, 2, c.Query("2-2"))
		ttt.SetPlayer(c.Query("you"))

		fmt.Println(ttt.ShowMap())
		fmt.Printf("Winner: %q\n", ttt.Winner())

		if c.Query("show-map") == "1" {
			c.String(200, ttt.ShowMap())
			return
		}

		next, err := ttt.Next()
		if err != nil {
			c.String(404, fmt.Sprintf("Error: %v", err))
		} else {
			c.String(200, fmt.Sprintf("%d-%d", next.Y, next.X))
		}
	})
	r.Run(":8080")
}
Beispiel #3
0
func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")

	// extract query from url
	u, err := url.Parse(r.URL.String())
	if err != nil {
		fmt.Fprintf(w, "URL error: %v:\n", err)
		return
	}

	// parse query
	m, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		fmt.Fprintf(w, "URL query error: %v:\n", err)
		return
	}

	ttt := tictactoe.NewTicTacToe()
	ttt.Set(0, 0, m.Get("0-0"))
	ttt.Set(0, 1, m.Get("0-1"))
	ttt.Set(0, 2, m.Get("0-2"))
	ttt.Set(1, 0, m.Get("1-0"))
	ttt.Set(1, 1, m.Get("1-1"))
	ttt.Set(1, 2, m.Get("1-2"))
	ttt.Set(2, 0, m.Get("2-0"))
	ttt.Set(2, 1, m.Get("2-1"))
	ttt.Set(2, 2, m.Get("2-2"))
	ttt.SetPlayer(m.Get("you"))

	next, err := ttt.Next()
	if err != nil {
		fmt.Fprintf(w, "Error: %v\n", err)
	} else {
		fmt.Fprintf(w, "%d-%d", next.Y, next.X)
	}
}