コード例 #1
0
ファイル: main.go プロジェクト: pelmers/gothello
/// Perform num simulations with bc and wc controllers.
/// If randomize, each game's first 10 turns are played randomly.
/// Return values (black wins, white wins, draws), where their sum = num
func performSimulations(num int, randomize bool, bc, wc g.Controller) (int, int, int) {
	wins_b, wins_w, draws := 0, 0, 0
	score_channel := make(chan Score, num)
	for j := 0; j < num; j++ {
		game := g.InitGame(bc, wc)
		// play the full game and send the score back
		go func() {
			playFullGame(game, randomize, false, bc, wc)
			score_channel <- Score{game.Score(g.BLACK), game.Score(g.WHITE)}
		}()
	}
	percent, prev_percent := 0, 0
	for j := 0; j < num; j++ {
		score := <-score_channel
		if score.black > score.white {
			wins_b++
		} else if score.black == score.white {
			draws++
		} else {
			wins_w++
		}
		percent = (wins_b + wins_w + draws) * 100 / num
		if percent > prev_percent {
			progress_bar(WIDTH, percent)
			prev_percent = percent
		}
	}
	return wins_b, wins_w, draws
}
コード例 #2
0
ファイル: main.go プロジェクト: pelmers/gothello
func main() {
	black := flag.String("black", "human", "Black controller: human, shallow, search, random")
	white := flag.String("white", "human", "White controller: human, shallow, search, random")
	show := flag.Bool("s", false, "Show AI moves")
	randomize := flag.Bool("r", false, "Randomize first 5 moves of each side")
	simulate := flag.Int("sim", 0, "Number of AI vs. AI games to simulate")
	flag.Parse()
	bc, wc := ParseControllers(black, white, show)
	if *simulate <= 0 {
		// just play out the game regularly
		game := g.InitGame(bc, wc)
		playFullGame(game, *randomize, *show, bc, wc)
		fmt.Printf("%s%s", game, "\nGame over.\n")
	} else {
		runtime.GOMAXPROCS(runtime.NumCPU())
		fmt.Printf("Setting GOMAXPROCS to %d\n", runtime.NumCPU())
		wins_b, wins_w, draws := performSimulations(*simulate, *randomize, bc, wc)
		fmt.Printf("\nBlack: %.2f%%\nWhite: %.2f%%\nDraw: %.2f%%\n",
			float64(wins_b)/float64(*simulate)*100.0,
			float64(wins_w)/float64(*simulate)*100.0,
			float64(draws)/float64(*simulate)*100.0)
	}
}