-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconnect4.rb
116 lines (96 loc) · 3.51 KB
/
connect4.rb
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
# Copyright (c) 2007-2024 Andy Maleh
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'glimmer-dsl-swt'
require_relative 'connect4/model/grid'
class Connect4
include Glimmer::UI::CustomShell
COLOR_BACKGROUND = rgb(63, 104, 212)
COLOR_EMPTY_SLOT = :white
COLOR_COIN1 = rgb(236, 223, 56)
COLOR_COIN2 = rgb(176, 11, 23)
attr_accessor :current_player_color
before_body do
@grid = Model::Grid.new
select_player_color
end
after_body do
observe(@grid, :current_player) do
select_player_color
end
observe(@grid, :game_over) do |game_over_value|
if game_over_value
game_over_message = case game_over_value
when true
"Game over!\nDraw!"
when 1
"Game over!\nPlayer 1 (yellow) wins!"
when 2
"Game over!\nPlayer 2 (red) wins!"
end
message_box {
text 'Game Over!'
message game_over_message
}.open
@grid.restart!
end
end
end
body {
shell(:no_resize) {
grid_layout(Model::Grid::WIDTH, true)
text 'Glimmer Connect 4'
background COLOR_BACKGROUND
Model::Grid::HEIGHT.times do |row_index|
Model::Grid::WIDTH.times do |column_index|
canvas {
layout_data {
width_hint 50
height_hint 50
}
background :transparent
the_oval = oval(0, 0, 50, 50) {
background <= [@grid.slot_rows[row_index][column_index], :value, on_read: ->(v) { v == 0 ? COLOR_EMPTY_SLOT : (v == 1 ? COLOR_COIN1 : COLOR_COIN2)}]
}
if row_index == 0
entered = false
on_mouse_enter do
entered = true
the_oval.background = current_player_color if @grid.slot_rows[row_index][column_index].value == 0
end
on_mouse_exit do
entered = false
the_oval.background = COLOR_EMPTY_SLOT if @grid.slot_rows[row_index][column_index].value == 0
end
on_mouse_up do
@grid.insert!(column_index)
the_oval.background = current_player_color if entered && @grid.slot_rows[row_index][column_index].value == 0
end
end
}
end
end
}
}
def select_player_color
self.current_player_color = @grid.current_player == 1 ? COLOR_COIN1 : COLOR_COIN2
end
end
Connect4.launch