goTicTactoeBot.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Mini max algorythm for Tictactoe Bot (https://botsArena.tinad.fr or bollosseum)
  3. * By Gnieark https://blog-du-grouik.tinad.fr 2018-06
  4. * I am learning golang it's my first script, don't take it seriously
  5. */
  6. package main
  7. import (
  8. "flag"
  9. "log"
  10. "fmt"
  11. "net/http"
  12. "encoding/json"
  13. "io/ioutil"
  14. "git.ventresmous.fr/gnieark/goTicTactoeBot/tictactoe"
  15. )
  16. //Plate somewhere to put the json encoded map
  17. type Plate struct{
  18. Cell00 string `json:"0-0,omitempty"`
  19. Cell01 string `json:"0-1,omitempty"`
  20. Cell02 string `json:"0-2,omitempty"`
  21. Cell10 string `json:"1-0,omitempty"`
  22. Cell11 string `json:"1-1,omitempty"`
  23. Cell12 string `json:"1-2,omitempty"`
  24. Cell20 string `json:"2-0,omitempty"`
  25. Cell21 string `json:"2-1,omitempty"`
  26. Cell22 string `json:"2-2,omitempty"`
  27. }
  28. //QuestionMessage Somewhere to put the whole JSON message from botsarena
  29. type QuestionMessage struct {
  30. GameID string `json:"game-id,omitempty"`
  31. Action string `json:"action,omitempty"`
  32. Game string `json:"game,omitempty"`
  33. Players int `json:"players,omitempty"`
  34. Board Plate `json:"board"`
  35. You string `json:"you,omitempty"`
  36. PlayerIndex int `json:"player-index,omitempty"`
  37. }
  38. func parseQuery(w http.ResponseWriter, r *http.Request){
  39. allowedHeaders := "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization,X-CSRF-Token"
  40. w.Header().Set("Access-Control-Allow-Origin", "*")
  41. w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  42. w.Header().Set("Access-Control-Allow-Headers", allowedHeaders)
  43. decoder := json.NewDecoder(r.Body)
  44. var questionMessage QuestionMessage
  45. err := decoder.Decode(&questionMessage)
  46. if err != nil {
  47. panic(err)
  48. }
  49. switch questionMessage.Action{
  50. case "init":
  51. w.Write([]byte("{\"name\":\"GniearkGolangTictactoe\"}"))
  52. case "play-turn":
  53. //Convert the map to an exploitable array
  54. var tmap [3][3]int
  55. tmap[0][0] = tictactoeSymbolsToInt(questionMessage.Board.Cell00,questionMessage.You)
  56. tmap[0][1] = tictactoeSymbolsToInt(questionMessage.Board.Cell01,questionMessage.You)
  57. tmap[0][2] = tictactoeSymbolsToInt(questionMessage.Board.Cell02,questionMessage.You)
  58. tmap[1][0] = tictactoeSymbolsToInt(questionMessage.Board.Cell10,questionMessage.You)
  59. tmap[1][1] = tictactoeSymbolsToInt(questionMessage.Board.Cell11,questionMessage.You)
  60. tmap[1][2] = tictactoeSymbolsToInt(questionMessage.Board.Cell12,questionMessage.You)
  61. tmap[2][0] = tictactoeSymbolsToInt(questionMessage.Board.Cell20,questionMessage.You)
  62. tmap[2][1] = tictactoeSymbolsToInt(questionMessage.Board.Cell21,questionMessage.You)
  63. tmap[2][2] = tictactoeSymbolsToInt(questionMessage.Board.Cell22,questionMessage.You)
  64. target, score := tictactoe.PlayOn(tmap, 1)
  65. fmt.Fprintf(w, "{\"play\":\"%d-%d\",\"Comment\":\"score %d\"}", target.X, target.Y,score)
  66. default:
  67. w.Write([]byte("Error " + questionMessage.Action ))
  68. }
  69. }
  70. func tictactoeSymbolsToInt (symbolToTest string,mySymbol string) int{
  71. switch symbolToTest {
  72. case mySymbol:
  73. return 1
  74. case "":
  75. return 0
  76. case " ":
  77. return 0
  78. default:
  79. return 2
  80. }
  81. }
  82. func arena(w http.ResponseWriter, r *http.Request){
  83. data, err := ioutil.ReadFile("./arena.html")
  84. if err == nil {
  85. w.Write(data)
  86. } else {
  87. w.WriteHeader(404)
  88. w.Write([]byte("404 Something went wrong - " + http.StatusText(404)))
  89. }
  90. }
  91. func main() {
  92. var listenAddr string
  93. flag.StringVar(&listenAddr, "listen-addr", ":5000", "server listen address")
  94. flag.Parse()
  95. http.HandleFunc("/arena", arena)
  96. http.HandleFunc("/", parseQuery)
  97. log.Fatal(http.ListenAndServe(listenAddr, nil))
  98. }