Created
July 19, 2013 17:27
-
-
Save smitfire/6040886 to your computer and use it in GitHub Desktop.
One Dice Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DiceGame | |
def initialize | |
roll | |
@a =* (1..6) | |
puts "what is your name" | |
@name = gets | |
puts "hello " + @name + "sup?" | |
puts "CPU rolled a " + @b.first.to_s | |
end | |
def roll | |
@b =* 1+rand(6) | |
end | |
def askme | |
puts "#{@name} pick next number" | |
d = @b.last.to_i | |
@n = gets.to_i | |
while @n == d || @n + d == 7 || @n < 1 || @n > 6 | |
puts "can't make that move" | |
@n = gets.to_i | |
end | |
@b << @n | |
if score < 31 | |
askcpu | |
end | |
end | |
def score | |
sum = 0 | |
@b.length.times do |i| | |
sum += @b[i] | |
end | |
sum | |
end | |
def askcpu | |
d = @b.last.to_i | |
c = @a.reject { |v| v+d == 7 || v-d == 0 } | |
cc = @a.reject { |s| s+d == 7 || s-d == 0 } | |
if score > 25 | |
cc.reject{ |s| s == 6 } | |
ncpu = cc.sample.to_i | |
elsif score > 26 | |
cc.reject{ |s| s >= 5 } | |
ncpu = cc.sample.to_i | |
elsif score > 27 | |
cc.reject{ |s| s >= 4 } | |
ncpu = cc.sample.to_i | |
elsif score > 28 | |
cc.reject{ |s| s >= 3 } | |
ncpu = cc.sample.to_i | |
elsif score > 29 | |
cc.reject{ |s| s >= 2 } | |
ncpu = cc.sample.to_i | |
else | |
ncpu = c.sample.to_i | |
end | |
@b << ncpu | |
puts "CPU picked #{ncpu}" | |
end | |
def scorecpu | |
sum = 0 | |
@b.length.times do |i| | |
sum += @b[i] | |
end | |
sum | |
end | |
def game | |
until score >= 31 || scorecpu >= 31 | |
puts "#{@name} score is #{score}" | |
askme | |
end | |
puts "#{score} game over" | |
end | |
end | |
a = DiceGame.new | |
a.game |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment