fix line calculation

This commit is contained in:
Arnas Udovic 2023-12-29 18:58:23 +02:00
parent 0aae6ce4af
commit d931d9027c
4 changed files with 10 additions and 33 deletions

View file

@ -148,13 +148,13 @@ func (game *Game) calculateState() {
} }
for _, w := range winner { for _, w := range winner {
if isSubArray(w[:], game.aiFields) { if len(game.aiFields) >= 3 && isSubArray(w[:], game.aiFields) {
game.state = GameStateAIWon game.state = GameStateAIWon
game.winnerFields = w game.winnerFields = w
game.winner = WinnerAI game.winner = WinnerAI
return return
} }
if isSubArray(w[:], game.userFields) { if len(game.userFields) >= 3 && isSubArray(w[:], game.userFields) {
game.state = GameStateAILost game.state = GameStateAILost
game.winnerFields = w game.winnerFields = w
game.winner = WinnerUser game.winner = WinnerUser

37
main.go
View file

@ -27,31 +27,12 @@ func main() {
r.GET("/", func(c *gin.Context) { r.GET("/", func(c *gin.Context) {
lang := getLang(c) lang := getLang(c)
fmt.Println(lang) c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/%s/0/x", lang))
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/%s/0/%s", lang, initGame(false)))
}) })
r.GET("/:lang", func(c *gin.Context) { r.GET("/:lang", func(c *gin.Context) {
lang := getLang(c) lang := getLang(c)
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/%s/0/%s", lang, initGame(false))) c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/%s/0/x", lang))
})
r.GET("/:lang/:ai_second", func(c *gin.Context) {
lang := getLang(c)
aiSecond, err := strconv.ParseBool(c.Param("ai_second"))
if err != nil {
aiSecond = false
}
aiSecondVar := "0"
if aiSecond {
aiSecondVar = "1"
}
c.Redirect(
http.StatusMovedPermanently,
fmt.Sprintf("/%s/%s/%s", lang, aiSecondVar, initGame(aiSecond)),
)
}) })
r.GET("/:lang/:ai_second/:game", func(c *gin.Context) { r.GET("/:lang/:ai_second/:game", func(c *gin.Context) {
@ -65,7 +46,6 @@ func main() {
game := newGame(gameFlow, aiSecond) game := newGame(gameFlow, aiSecond)
fmt.Println(gameState)
if gameState == GameStateInProgress && !game.isFinished() { if gameState == GameStateInProgress && !game.isFinished() {
gameFlow = gameFlow + getNewClick(gameFlow) gameFlow = gameFlow + getNewClick(gameFlow)
game = newGame(gameFlow, aiSecond) game = newGame(gameFlow, aiSecond)
@ -105,15 +85,6 @@ func main() {
r.Run() r.Run()
} }
func initGame(aiSecond bool) string {
if aiSecond {
return "0"
}
// get from DB
return "1"
}
func getLang(c *gin.Context) string { func getLang(c *gin.Context) string {
session := sessions.Default(c) session := sessions.Default(c)
@ -140,6 +111,10 @@ func getGameFlow(c *gin.Context) (string, int) {
return gameFlow, GameStateWaitingUser return gameFlow, GameStateWaitingUser
} }
if "x" == gameFlow {
return "", GameStateInProgress
}
if "0" == string(gameFlow[0]) { if "0" == string(gameFlow[0]) {
gameFlow = string(gameFlow[1:]) gameFlow = string(gameFlow[1:])
} }

View file

@ -10,5 +10,6 @@
</div> </div>
<div> <div>
<a class="btn btn-secondary" href="/{{.lang}}/1">Me first!</a> <a class="btn btn-secondary" href="/{{.lang}}/1">Me first!</a>
<a class="btn btn-secondary" href="/{{.lang}}/0/x">AI first</a>
</div> </div>

View file

@ -10,4 +10,5 @@
</div> </div>
<div> <div>
<a class="btn btn-secondary" href="/{{.lang}}/1">Aš pėrms!</a> <a class="btn btn-secondary" href="/{{.lang}}/1">Aš pėrms!</a>
<a class="btn btn-secondary" href="/{{.lang}}/0/x">DI pėrms</a>
</div> </div>