Skip to content

Commit

Permalink
fix tie eadge case
Browse files Browse the repository at this point in the history
  • Loading branch information
loan-mgt committed Jun 8, 2024
1 parent fcca48b commit 13fc911
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 24 additions & 13 deletions internal/services/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ func sendEndRound(gameId, winner string, tie bool) {
senders.SetScore(p.Conn, "opponent", getOpponentScore(g.Players, p))
if !tie {
senders.SendMessage(p.Conn,
fmt.Sprintf("Winner is %s! Next round will start in 3s", winner), "empty-3s")
fmt.Sprintf("Winner is %s! Next round will start in 3s", winner), "")
} else {
senders.SendMessage(p.Conn, "Tie! Next round will start in 3s", "empty-3s")
senders.SendMessage(p.Conn, "Tie! Next round will start in 3s", "")
}
}
}
Expand Down Expand Up @@ -443,14 +443,21 @@ func incrementWinner(gameId string) (string, bool) {
}

winner := ""
winnerMove := ""

for k, p := range g.Players {
if isLeftWinning(p.Move, winnerMove) {
winner = k
winnerMove = p.Move
for _, p := range g.Players {
isLeftWinner, isTie := isLeftWinning(p.Move, getOpponentMove(g.Players, p))

if !isTie {
if isLeftWinner {
winner = p.Name
} else {
winner = getOpponent(g.Players, p).Name
}
}

break
}

if winner != "" {
tmpPlayer := g.Players[winner]
tmpPlayer.Score += 1
Expand All @@ -464,24 +471,28 @@ func incrementWinner(gameId string) (string, bool) {

}

func isLeftWinning(leftMove, rightMove string) bool {
func isLeftWinning(leftMove, rightMove string) (bool, bool) {
if leftMove == rightMove {
return false, true
}

if leftMove == "paper" && rightMove == "rock" {
return true
return true, false
}

if leftMove == "rock" && rightMove == "scissors" {
return true
return true, false
}

if leftMove == "scissors" && rightMove == "paper" {
return true
return true, false
}

if leftMove != "" && rightMove == "" {
return true
return true, false
}

return false
return false, false

}

Expand Down
4 changes: 3 additions & 1 deletion template/messenger.html.tmpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{{ define "messenger" }}
<div id="console" class="flex items-center h-16 w-full justify-center flex-col">
<div id="console" class="flex flex-col justify-center items-center w-full h-16">
{{ if .Message }}
<p>{{.Message}}</p>
{{ end }}

{{ if ne .Timer "" }}
<div class="rounded-full bg-slate-400 w-[28rem] h-[1rem] flex justify-center">
<div class="rounded-full bg-orange-400 {{.Timer}} w-full h-full">
</div>
</div>
{{ end }}
</div>
{{ end }}

0 comments on commit 13fc911

Please sign in to comment.