File: canvas-fifteen.rb

package info (click to toggle)
ruby-gnome2 0.15.0-1.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 7,704 kB
  • ctags: 8,558
  • sloc: ansic: 69,912; ruby: 19,511; makefile: 97; xml: 35; sql: 13
file content (186 lines) | stat: -rw-r--r-- 4,532 bytes parent folder | download | duplicates (5)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#  -*- indent-tabs-mode: nil -*-
class CanvasSampleFifteen < Gtk::VBox
  PIECE_SIZE = 50

  def test_win
    0.upto(14) do |i|
      if @board[i].nil? || @board[i].num != i
        return
      end
    end
    #    dialog = Gnome::MessageBox.new("You stud, you win!", Gnome::MessageBox::INFO, "OK")
    #    dialog.set_modal(true)
    #    dialog.run_and_close()
  end

  def piece_event(item, event)
    case event.event_type
    when Gdk::Event::ENTER_NOTIFY
      item.text.set({:fill_color => "white"})
    when Gdk::Event::LEAVE_NOTIFY
      item.text.set({:fill_color => "black"})
    when Gdk::Event::BUTTON_PRESS
      y = item.pos / 4
      x = item.pos % 4

      move = true

      if (y > 0) && @board[(y - 1) * 4 + x].nil?
        dx = 0.0
        dy = -1.0
        y -= 1
      elsif (y < 3) && @board[(y + 1) * 4 + x].nil?
        dx = 0.0
        dy = 1.0
        y += 1
      elsif (x > 0) && @board[y * 4 + x - 1].nil?
        dx = -1.0
        dy = 0.0
        x -= 1
      elsif (x < 3) && @board[y * 4 + x + 1].nil?
        dx = 1.0
        dy = 0.0
        x += 1
      else
        move = false
      end

      if move
        newpos = y * 4 + x
        @board[item.pos] = nil
        @board[newpos] = item
        item.pos = newpos
        item.move(dx * PIECE_SIZE, dy * PIECE_SIZE)
        test_win()
      end
    end
  end

  SCRAMBLE_MOVES = 32
  def scramble
    srand()

    # First, find the blank spot
    pos = 0
    0.upto(15) do |i|
      if @board[i].nil?
        pos = i
        break
      end
    end

    # "Move the blank spot" around in order to scramble the pieces

    0.upto(SCRAMBLE_MOVES) do
      dir = rand(4).to_i

      x = y = 0

      if (dir == 0) && (pos > 3) # up
        y = -1
      elsif (dir == 1) && (pos < 12) # down
        y = 1
      elsif (dir == 2) && ((pos % 4) != 0) # left
        x = -1
      elsif (dir == 3) && ((pos % 4) != 3) # right
        x = 1
      else
        retry
      end

      oldpos = pos + y * 4 + x;
      @board[pos] = @board[oldpos];
      @board[oldpos] = nil
      @board[pos].pos = pos
      @board[pos].move(-x * PIECE_SIZE, -y * PIECE_SIZE)
      @canvas.update_now()
      pos = oldpos
    end
  end

  def initialize
    super(false, 4)
    border_width = 4
    show()

    alignment = Gtk::Alignment.new(0.5, 0.5, 0.0, 0.0)
    pack_start(alignment, true, true, 0)
    alignment.show()

    frame = Gtk::Frame.new
    frame.set_shadow_type(Gtk::SHADOW_IN);
    alignment.add(frame)
    frame.show()

    # Create the canvas and board

    @canvas = Gnome::Canvas.new()
    @canvas.set_size_request(PIECE_SIZE * 4 + 1, PIECE_SIZE * 4 + 1)
    @canvas.set_scroll_region(0, 0, PIECE_SIZE * 4 + 1, PIECE_SIZE * 4 + 1)
    frame.add(@canvas)
    @canvas.show()

    @board = Array.new(16)

    0.upto(14) do |i|
      @board[i] = Piece.new(@canvas.root, {})
      @board[i].setup(self, i)
    end
    @board[15] = nil;

    # Scramble button
    button = Gtk::Button.new("Scramble")
    pack_start(button, false, false, 0)
    button.signal_connect("clicked") do |button|
      scramble()
    end
    button.show()
  end

  class Piece < Gnome::CanvasGroup
    attr_reader :text, :num, :pos
    attr_writer :pos

    def initialize(*arg)
      super(*arg)
    end

    def setup(app, i)
      y = i / 4
      x = i % 4

      self.set({:x => x * PIECE_SIZE,
                 :y => y * PIECE_SIZE})
      Gnome::CanvasRect.new(self,
                            {:x1 => 0.0,
                              :y1 => 0.0,
                              :x2 => PIECE_SIZE,
                              :y2 => PIECE_SIZE,
                              :fill_color => get_piece_color(x, y),
                              :outline_color => "black",
                              :width_pixels => 0})
      
      @text = Gnome::CanvasText.new(self,
                                    {:text => i.to_s,
                                      :x => PIECE_SIZE / 2.0,
                                      :y => PIECE_SIZE / 2.0,
                                      :font => "Sans bold 24",
                                      :anchor => Gtk::ANCHOR_CENTER,
                                      :fill_color => "black"})
      @num = i
      @pos = i
      self.signal_connect("event") do |item, event|
        app.piece_event(item, event)
      end
    end

    private
    def get_piece_color(x, y)
      r = ((4 - x) * 255) / 4;
      g = ((4 - y) * 255) / 4;
      b = 128;
      return sprintf("#%02x%02x%02x", r, g, b)
    end

  end
end