Class: GamesAndRpgParadise::GUI::AlignThree

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb

Overview

GamesAndRpgParadise::GUI::AlignThree

Defined Under Namespace

Classes: SwingButton

Instance Method Summary collapse

Constructor Details

#initializeAlignThree

#

initialize

#


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 36

def initialize
  reset
  # button/window display settings:
  @frame.set_size(
    @button_width  * @button_cols+((@button_cols-1) * @button_spacing),
    @button_height * @button_cols+((@button_rows-1) * @button_spacing)
  )
  create_buttons
  # Create the swing gridlayout frame next:
  @frame.set_layout(
    java.awt.GridLayout.new(@button_rows, @button_cols, @button_spacing, @button_spacing)
  )
  @frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
  @frame.setResizable(false)
  @frame.show
end

Instance Method Details

#adjacent_buttons_with_same_value(swing_button) ⇒ Object

adjacent_buttons_with_same_value



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 191

def adjacent_buttons_with_same_value(swing_button)
  coordinates = []
  # top
  coordinates << { row: swing_button.row-1, column: swing_button.column } if swing_button.row > 1
  # right
  coordinates << { row: swing_button.row, column: swing_button.column+1 } if swing_button.column < @button_cols
  # bottom
  coordinates << { row: swing_button.row+1, column: swing_button.column } if swing_button.row < @button_rows
  # left
  coordinates << { row: swing_button.row, column: swing_button.column-1 } if swing_button.column > 1
  @buttons.select {|button|
    coordinates.include?({row: button.row, column: button.column})
  }.select {|button|
    button.value == swing_button.value
  }
end

#check_adjacent_count_same_value(swing_button) ⇒ Object

#

check_adjacent_count_same_value

#


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 174

def check_adjacent_count_same_value(swing_button)
  checked = []
  not_checked = [swing_button]

  while !not_checked.empty?
    button = not_checked.pop
    checked << button
    adjacent_buttons_with_same_value(button).each do |button|
      next if checked.include? button
      next if not_checked.include? button
      not_checked << button
    end
  end
  checked
end

#create_buttonsObject

#

create_buttons

This method will create all the buttons that are placed in our 2D grid here.

#


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 114

def create_buttons
  (1 .. @button_rows).each { |row|
    (1 .. @button_cols).each { |column|

      # create a new swing button
      swing_button = SwingButton.new(javax.swing.JButton.new('0'), row, column, 0)
      swing_button.jbutton.setOpaque true
      swing_button.jbutton.setBorderPainted false
      swing_button.jbutton.setBackground Color.new(*@button_colors[swing_button.value])

      # add "click" action listener
      swing_button.jbutton.add_action_listener { |event|
        do_handle_the_click_event(swing_button, event)
      }
      @buttons << swing_button
    }
  }
  @buttons.each {|button| @frame.add(button.jbutton) }
end

#define_buttons_coloursObject

#

define_buttons_colours

This method will define RGB values for a button value.

#


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 93

def define_buttons_colours
  @button_colors = [
    [255, 255, 255], #ffffff white
    [255, 153, 153], #ff9999 pink
    [255, 153, 102], #ff9966 orange
    [255, 255, 153], #ffff99 yellow
    [153, 255, 153], #99ff99 green
    [102, 255, 204], #66ffcc green/blue
    [153, 255, 255], #99ffff light blue
    [102, 204, 255], #66ccff blue
    [153, 153, 255], #9999ff purple
    [255, 153, 255], #ff99ff purple/pink
  ]
end

#do_handle_the_click_event(swing_button, evt) ⇒ Object

#

do_handle_the_click_event

button click event handler

#


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
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 139

def do_handle_the_click_event(swing_button, evt)
  # for now, if the button has already been clicked, return
  return if swing_button.value > 0
  swing_button.value += 1
  # looping until nothing left to collapse
  begin
    # check for adjacent similar values, todo
    adjacent_buttons = check_adjacent_count_same_value swing_button
    # update single button
    if adjacent_buttons.size < @adjacent_count
      swing_button.jbutton.setText swing_button.value.to_s
      swing_button.jbutton.setBackground Color.new(*@button_colors[swing_button.value])
      return
    end

    adjacent_buttons.each { |button|
      # clicked button
      case button
      when swing_button
        button.value += 1
        button.jbutton.setText button.value.to_s
        button.jbutton.setBackground Color.new(*@button_colors[button.value])
      # adjacent buttons
      else
        button.value = 0
        button.jbutton.setText button.value.to_s
        button.jbutton.setBackground Color.new(*@button_colors[button.value])
      end
    }
  end until check_adjacent_count_same_value(swing_button).size < @adjacent_count
end

#resetObject

#

reset

#


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
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 56

def reset
  @frame = JFrame.new
  # ======================================================================= #
  # Keep track of all buttons, via an Array:
  # ======================================================================= #
  @buttons = []
  # ======================================================================= #
  # === adjacent_count
  #
  # Define how many in a row collapse.
  # ======================================================================= #
  @adjacent_count = 3
  # ======================================================================= #
  # === @button_width
  # ======================================================================= #
  @button_width   = 100
  # ======================================================================= #
  # === @button_height
  # ======================================================================= #
  @button_height  = 100
  # ======================================================================= #
  # === @button_rows
  # ======================================================================= #
  @button_rows    =   5
  # ======================================================================= #
  # === @button_cols
  # ======================================================================= #
  @button_cols    =   5
  @button_spacing =   5
  define_buttons_colours
end