Quote (HaloGod @ May 5 2015 07:40pm)
ok ty i'll try this when i get home
Strategy2:
Code
class Dice
attr_accessor :numberOfDice
def initialize(numberOfDice)
@numberOfDice = numberOfDice
end
def roll
results = []
@numberOfDice.times do
results.push(Random.rand(1..6))
end
results
end
end
10.times do
scoreBoard = {1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0}
turns = 0
lost = true
dice = Dice.new(2)
loop do
if(scoreBoard.values.inject(:+) != 9) then
turns += 1
else
break
end
results = dice.roll
#p results
if(results.inject(:+) <= 9 && scoreBoard[results.inject(:+)] == 0) then
scoreBoard[results.inject(:+)] = 1
lost = false
end
if(scoreBoard[results.sort.last] == 0) then
scoreBoard[results.sort.last] = 1
lost = false
end
if(scoreBoard[results.sort.first] == 0) then
scoreBoard[results.sort.first] = 1
lost = false
end
if(lost == true)
#p results
print "(Lost) "
break
end
lost = true
end
puts "It took #{turns} turns"
#p scoreBoard
#puts
end
If you need a larger sample space change the line "10.times do" to a larger number. This is how many times the game is played. If you need counting of wins/losses per game as program output I can also do that.
Quote
The third would be to try to use combinations of mid value numbers.
If you could explain this a bit more I might be able to do your third strategy.
With my calculations and changing the games played to 100, the first strat wins 3% of the time (didn't average), and strat 2 wins 5% of the time. This is sampling each strat 100 times once, you would probably get more accurate results sampling each strat 100 times multiple times and averaging the results.
This post was edited by AbDuCt on May 5 2015 05:46pm