Commits

Jason Scheirer committed 8f532b3

Work-in-progress

Comments (0)

Files changed (1)

 	return probability
 }
 
+func (self *NRun) HopelessToWinByN(desired_wins int, desired_length int) bool {
+	return (self.Wins() + (desired_length - self.Length())) < desired_wins
+}
+
 func (self *NRun) NextRound(is_win bool) NRun {
-	return NRun{append(self.Runs, is_win)}
+	var nextrun = make([]bool, self.Length() + 1)
+	copy(nextrun, self.Runs)
+	nextrun[self.Length()] = is_win
+	return NRun{nextrun}
+}
+
+func (self *NRun) Winner() bool {
+	return self.Wins() > (self.Length() / 2)
 }
 
 func main() {
-	desired_length := 6
+	desired_length := 15
+	desired_wins := desired_length / 2
+	win_probability := float64(0.)
 	narray := []NRun{NRun{[]bool{true}}, NRun{[]bool{false}}}
-	fmt.Printf("This is %v\n", narray)
 	for {
 		if len(narray) == 0 {
 			break
 		}
 		item := narray[0]
 		narray = narray[1:]
-		if item.Length() == desired_length && item.Wins() >= (desired_length/2) {
-			fmt.Printf("Here's a winning item: %v -> %v\n", item, item.Probability())
+
+		if item.Length() == desired_length && item.Wins() > desired_wins {
+			win_probability += item.Probability()
+			//fmt.Printf("Here's a winning item: %v -> %v\n", item, item.Probability())
+		} else if item.Length() < desired_length {
+			if item.HopelessToWinByN(desired_wins, desired_length) {
+				//fmt.Printf("Here's a hopeless item: %v\n", item)
+			} else {
+				narray = append(narray, item.NextRound(true), item.NextRound(false))
+			}
 		} else {
-			fmt.Printf("Here's a losing item: %v -> %v\n", item, item.Probability())
-		}
-
-		if item.Length() < desired_length {
-			narray = append(narray, item.NextRound(true), item.NextRound(false))
+			//fmt.Printf("Here's a useless item: %v\n", item)
 		}
 	}
-	fmt.Printf("Et voila\n")
+	fmt.Printf("Et voila. %v chance of winning.\n", win_probability * 100.)
 }