summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/games/greed.rb
blob: 3fa7d3be8a85d26df79a399de16fd5022367b83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#-- vim:sw=2:et
#++
#
# :title: Greed dice game plugin for rbot
#
# Author:: Okasu <oka.sux@gmail.com>
#
# Distributed under the same license as rbot itself
#


class Greed < Plugin

  def initialize
    super
    @scoreboard = {}
  end

  def help(plugin, topic="")
    "Simple dice game. Rules: https://en.wikipedia.org/wiki/Greed_(dice_game)"
  end

  def diceroll
    dice = Array.new
    for i in 0..5 do
      dice[i] = rand(6) + 1
    end
    dice.sort
  end

  def scores
    roll = diceroll
    one =  two = three = four = five = six = score = 0
    roll.each do |x|
      case x
      when 1
        one += 1
        if one == 3
          score += 1000
        elsif one == 6
          score += 7000
        else
          score += 100
        end
      when 2
        two += 1
        if two == 3
          score += 200
        elsif two == 4
          score += 400
        end
      when 3
        three += 1
        if three == 3
          score += 300
        elsif three == 5
          score += 1200
        end
      when 4
        four += 1
        if four == 3
          score += 400
        elsif four == 6
          score += 3600
        end
      when 5
        five += 1
        if five == 3
          score += 500
        else
          score += 50
        end
      when 6
        six += 6
        if six == 3
          score += 600
        end
      end
    end
    if roll == [1,2,3,4,5,6]
      score = 1200
    elsif roll == [2,2,3,3,4,4]
      score = 800
    end
    [score, roll]
  end

  def greed(m, params)
    player = scores
    mhash = {m.sourcenick => player[0]}
    @scoreboard.merge! mhash
    m.reply "You have #{player[0]} points. (#{player[1].join(" ")})"
    if params[:single] == "bot"
      bot = scores
      m.reply "I have #{bot[0]} points. (#{bot[1].join(" ")})"
      if player[0] < bot[0]
        m.reply "Bot wins!"
      else
        m.reply "Human player wins!"
      end
    end
    if @scoreboard.values.size == 2
      if @scoreboard.values[0] > @scoreboard.values[1]
        m.reply "#{@scoreboard.keys.first} wins!"
      else
        m.reply "#{@scoreboard.keys.last} wins!"
      end
    @scoreboard.clear
    end
  end
end

plugin = Greed.new
plugin.map "greed :single", :action => :greed, :requirements => {:single => /bot/}, :thread => "yes"
plugin.map "greed", :action => :greed, :thread => "yes"