Skip to content

Commit

Permalink
close connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty committed Jun 14, 2022
1 parent 2708639 commit 25d41ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
57 changes: 28 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ func main() {
s.Start(listener)
fmt.Println(`awaiting connections on port ` + strconv.Itoa(port))

runGame(remote.AwaitImplementation(s), remote.AwaitImplementation(s))
for {
impl1, close1 := remote.AwaitImplementation(s)
impl2, close2 := remote.AwaitImplementation(s)
runGame(impl1, impl2)
close1()
close2()
}
}

func runGame(impl1, impl2 game.Implementation) {
Expand All @@ -43,23 +49,16 @@ func runGame(impl1, impl2 game.Implementation) {
fmt.Println()

if winner1 == winner2 {
exit(`It's a tie! Both programs lost and won once.`)
fmt.Println(`It's a tie! Both programs lost and won once.`)
}

if winner1 == 1 {
exit(fmt.Sprintf(`%s won both games!`, p1.Name()))
}

if winner1 == 2 {
exit(fmt.Sprintf(`%s won both games!`, p2.Name()))
fmt.Printf("%s won both games!\n", p1.Name())
} else if winner1 == 2 {
fmt.Printf("%s won both games!\n", p2.Name())
} else {
panic(`unable to figure out who won: ` + strconv.Itoa(winner1))
}

panic(`unable to figure out who won`)
}

func exit(msg string) {
fmt.Println(msg)
os.Exit(0)
}

func playGame(p1, p2 game.Player) int {
Expand All @@ -76,31 +75,31 @@ func playGame(p1, p2 game.Player) int {
currentPlayer, otherPlayer := p2, p1
winner := 0

allNames := game.AllNames()
// allNames := game.AllNames()
for turn(&p, used, currentPlayer, &c) {
if noMoreAnswers(allNames, used, c) {
fmt.Printf("There are no correct answers left! %s loses!\n", otherPlayer.Name())
break
}
// if noMoreAnswers(allNames, used, c) {
// fmt.Printf("There are no correct answers left! %s loses!\n", otherPlayer.Name())
// break
// }
currentPlayer, otherPlayer = otherPlayer, currentPlayer
winner = 1 - winner
}

p1.GameOver(winner == 1)
p2.GameOver(winner == 2)
p1.GameOver(winner == 0)
p2.GameOver(winner == 1)

return winner + 1
}

func noMoreAnswers(allNames []game.Pokémon, used map[game.Pokémon]bool, c rune) bool {
for _, pok := range game.AllNames() {
if pok.Start() == c && !used[pok] {
return false
}
}
// func noMoreAnswers(allNames []game.Pokémon, used map[game.Pokémon]bool, c rune) bool {
// for _, pok := range game.AllNames() {
// if pok.Start() == c && !used[pok] {
// return false
// }
// }

return true
}
// return true
// }

func turn(p *game.Pokémon, used map[game.Pokémon]bool, p1 game.Player, c *rune) bool {
*p = p1.Play(*p)
Expand Down
4 changes: 2 additions & 2 deletions remote/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/PotatoesFall/pokegame/remote/socket"
)

func AwaitImplementation(s socket.Server) game.Implementation {
func AwaitImplementation(s socket.Server) (impl game.Implementation, close func()) {
h := socket.Handlers{}
conn := s.AwaitConnection(h, func() {})

Expand All @@ -27,7 +27,7 @@ func AwaitImplementation(s socket.Server) game.Implementation {
}

return p
}
}, conn.Close
}

type player struct {
Expand Down

0 comments on commit 25d41ca

Please sign in to comment.