Beispiel #1
0
func (g *Game) step() {
	// wait for enough players to start
	for {
		numberOfPlayers := <-g.playerJoinedCh
		if numberOfPlayers > conf.MinimalPlayerCnt-1 {
			g.isStarted = true
			break
		}
	}

	// notify views that the game started
	for _, reporter := range g.views {
		reporter.ReportGameStarted()
	}

	// we have enough players so start the first player's turn
	g.reportTurnStart(g.Players[0], g.Players[1])
	g.doTurn(g.Players[0])

	// game event loop
	for {
		// check if we have a winner
		// when we do, break the game loop
		if result, winner := g.hasWinner(); result {
			g.Winner = winner
			util.LogInfo("The winner is: %s", winner.Name)
			break
		}

		prevPlayerId := g.CurrentPlayerId
		player, err := g.getNextPlayer(prevPlayerId)
		if err != nil {
			util.LogError("No player found for id %v", prevPlayerId)
			continue
		}

		// report new turn for player
		nextPlayer, _ := g.getNextPlayer(player.Id)
		g.reportTurnStart(player, nextPlayer)

		g.doTurn(player)
	}

	// notify views
	for _, reporter := range g.views {
		reporter.ReportGameOver(g.Winner)
	}
	g.endCh <- 1 // signal the main loop
	g.cleanUp()
}
Beispiel #2
0
func letters(count int, tag string) template.HTML {
	t, err := template.New("partail").Parse("<" + tag + ">{{.}}</" + tag + ">")

	if err != nil {
		util.LogError(err.Error())
		return ""
	}

	buffer := bytes.Buffer{}

	for i := -1; i < count; i++ {
		if i < 0 {
			t.Execute(&buffer, nil)
		} else {
			t.Execute(&buffer, getLetterForNumber(i))
		}
	}

	return template.HTML(buffer.String())
}