Repository for golang small applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

montyhall.go 2.0KB

4 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package main
  2. import (
  3. "fmt"
  4. "balhau.net/tinker/hall"
  5. )
  6. func main() {
  7. iterations := 1000
  8. type SolutionCounter struct {
  9. Conservative int
  10. ChangerBefore int
  11. ChangerAfter int
  12. }
  13. counter := SolutionCounter{0, 0, 0}
  14. for i := 0; i < iterations; i++ {
  15. montyOne := hall.MontyHall{}
  16. montyTwo := hall.MontyHall{}
  17. conservative := hall.ConservativePlayer{}
  18. changer := hall.ChangerPlayer{}
  19. montyOne.SelectDoor()
  20. conservative.SelectDoor()
  21. montyTwo.SelectDoor()
  22. changer.SelectDoor()
  23. changerBefore := changer.GetDoor()
  24. conservativeBefore := conservative.GetDoor()
  25. openPortConservative := montyOne.OpenDoor(conservativeBefore)
  26. openPortChanger := montyTwo.OpenDoor(changerBefore)
  27. conservative.ChangeDoor(openPortConservative)
  28. changer.ChangeDoor(openPortChanger)
  29. if conservative.GetDoor() == montyOne.GetDoor() {
  30. counter.Conservative += 1
  31. }
  32. if changer.GetDoor() == montyTwo.GetDoor() {
  33. counter.ChangerAfter += 1
  34. }
  35. if changerBefore == montyTwo.GetDoor() {
  36. counter.ChangerBefore += 1
  37. }
  38. conservSol := "x"
  39. changerSol := "x"
  40. if conservative.GetDoor() == montyOne.GetDoor() {
  41. conservSol = "o"
  42. }
  43. if changer.GetDoor() == montyTwo.GetDoor() {
  44. changerSol = "o"
  45. }
  46. fmt.Println(fmt.Sprintf("[Conservative: s=%d,p=%d,b=%d,a=%d] -- %s", montyOne.GetDoor(), openPortConservative, conservativeBefore, conservative.GetDoor(), conservSol))
  47. fmt.Println(fmt.Sprintf("[Changer: s=%d,p=%d,b=%d,a=%d] -- %s", montyTwo.GetDoor(), openPortChanger, changerBefore, changer.GetDoor(), changerSol))
  48. fmt.Println()
  49. }
  50. conservative_accuracy := (float32(counter.Conservative) / float32(iterations)) * 100
  51. changer_before_accuracy := (float32(counter.ChangerBefore) / float32(iterations)) * 100
  52. changer_after_accuracy := (float32(counter.ChangerAfter) / float32(iterations)) * 100
  53. fmt.Printf(fmt.Sprintf("[Conservative: %d/%f, ChangerBefore: %d/%f, ChangerAfter: %d/%f]\n", counter.Conservative, conservative_accuracy, counter.ChangerBefore, changer_before_accuracy, counter.ChangerAfter, changer_after_accuracy))
  54. }