Add a Greed dice game.
[rbot] / data / rbot / plugins / games / greed.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Greed dice game plugin for rbot
5 #
6 # Author:: Okasu <oka.sux@gmail.com>
7 #
8 # Distributed under the same license as rbot itself
9 #
10
11
12 class Greed < Plugin
13
14   def initialize
15     super
16     @scoreboard = {}
17   end
18
19   def help(plugin, topic="")
20     "Simple dice game. Rules: https://en.wikipedia.org/wiki/Greed_(dice_game)"
21   end
22
23   def diceroll
24     dice = Array.new
25     for i in 0..5 do
26       dice[i] = rand(6) + 1
27     end
28     dice
29   end
30
31   def scores
32     roll = diceroll
33     one =  two = three = four = five = six = score = 0
34     roll.each do |x|
35       case x
36       when 1
37         one += 1
38         if one == 3
39           score += 1000
40         elsif one == 6
41           score += 7000
42         else
43           score += 100
44         end
45       when 2
46         two += 1
47         if two == 3
48           score += 200
49         elsif two == 4
50           score += 400
51         end
52       when 3
53         three += 1
54         if three == 3
55           score += 300
56         elsif three == 5
57           score += 1200
58         end
59       when 4
60         four += 1
61         if four == 3
62           score += 400
63         elsif four == 6
64           score += 3600
65         end
66       when 5
67         five += 1
68         if five == 3
69           score += 500
70         else
71           score += 50
72         end
73       when 6
74         six += 6
75         if six == 3
76           score += 600
77         end
78       end
79     end
80     if roll.sort == [1,2,3,4,5,6]
81       score = 1200
82     elsif roll.sort == [2,2,3,3,4,4]
83       score = 800
84     end
85     [score, roll]
86   end
87
88   def greed(m, params)
89     player = scores
90     mhash = {m.sourcenick => player[0]}
91     @scoreboard.merge! mhash
92     m.reply "You have #{player[0]} points. (#{player[1].join("-")})"
93     if params[:single] == "bot"
94       bot = scores
95       m.reply "I have #{bot[0]} points. (#{bot[1].join("-")})"
96       if player[0] < bot[0]
97         m.reply "Bot wins!"
98       else
99         m.reply "Human player wins!"
100       end
101     end
102     if @scoreboard.values.size == 2
103       if @scoreboard.values[0] > @scoreboard.values[1]
104         m.reply "#{@scoreboard.keys.first} wins!"
105       else
106         m.reply "#{@scoreboard.keys.last} wins!"
107       end
108     @scoreboard.clear
109     end
110   end
111 end
112
113 plugin = Greed.new
114 plugin.map "greed :single", :action => :greed, :requirements => {:single => /bot/}, :thread => "yes"
115 plugin.map "greed", :action => :greed, :thread => "yes"