1
+ Cell = Struct . new ( :player )
2
+
1
3
class Rubygo
2
4
module Model
3
5
class Game
4
6
5
- attr_accessor :height , :width , :scale , :name
7
+ attr_accessor :height , :width , :scale , :name , :tokens , :cur_player
6
8
7
9
def initialize ( height = 12 , width = 12 , scale = 70 , name = "Go Game" )
8
10
@height = height
9
11
@width = width
10
12
@scale = scale
11
- @name = name
13
+ @tokens = height . times . map do
14
+ width . times . map do
15
+ Cell . new ( 0 )
16
+ end
17
+ end
18
+ @cur_player = 1
12
19
end
13
20
end
14
21
end
@@ -26,19 +33,30 @@ class GameBoard
26
33
game . height . times . map do |row |
27
34
horizontal_box {
28
35
padded false
29
-
30
36
game . width . times . map do |column |
31
37
half = game . scale / 2
32
38
area {
33
39
square ( 0 , 0 , game . scale ) {
34
40
fill r : 240 , g : 215 , b : 141 , a : 1.0
41
+ on_mouse_up do |clicked_event |
42
+ game . tokens [ row ] [ column ] [ :player ] = game . cur_player
43
+ game . cur_player = -game . cur_player
44
+ end
35
45
}
36
46
line ( half , row == 0 ? half : 0 , half , row == ( game . height - 1 ) ? half : game . scale ) {
37
47
stroke 0x000000
38
48
}
39
49
line ( column == 0 ? half : 0 , half , column == ( game . width - 1 ) ? half : game . scale , half ) {
40
50
stroke 0x000000
41
51
}
52
+ circle ( half , half , half - 8 ) {
53
+ fill <= [ game . tokens [ row ] [ column ] , :player , on_read : -> ( player ) {
54
+ return if player == 0
55
+ return :white if player == 1
56
+ :black
57
+ } ]
58
+
59
+ }
42
60
}
43
61
end
44
62
}
@@ -54,10 +72,8 @@ module View
54
72
class NewGameWindow
55
73
include Glimmer ::LibUI ::CustomWindow
56
74
option :on_create , default : lambda { |user | }
57
-
58
- before_body do
59
- @game = Model ::Game . new
60
- end
75
+ option :height , default : 12
76
+ option :width , default : 12
61
77
62
78
body {
63
79
window { |new_game_window |
@@ -69,13 +85,13 @@ class NewGameWindow
69
85
horizontal_box {
70
86
label ( 'Board Width' )
71
87
spinbox ( 2 , 20 ) {
72
- value <=> [ @game , :width ]
88
+ value <=> [ self , :width ]
73
89
}
74
90
}
75
91
horizontal_box {
76
92
label ( 'Board Height' )
77
93
spinbox ( 2 , 20 ) {
78
- value <=> [ @game , :height ]
94
+ value <=> [ self , :height ]
79
95
}
80
96
}
81
97
}
@@ -89,7 +105,7 @@ class NewGameWindow
89
105
}
90
106
button ( "New Game" ) {
91
107
on_clicked do
92
- on_create . call ( @game )
108
+ on_create . call ( Model :: Game . new ( height , width ) )
93
109
new_game_window . destroy
94
110
end
95
111
}
@@ -106,15 +122,6 @@ module View
106
122
class Rubygo
107
123
include Glimmer ::LibUI ::Application
108
124
109
- ## Add options like the following to configure CustomWindow by outside consumers
110
- #
111
- # options :title, :background_color
112
- # option :width, default: 320
113
- # option :height, default: 240
114
-
115
- ## Use before_body block to pre-initialize variables to use in body and
116
- # to setup application menu
117
- #
118
125
before_body do
119
126
@game = Model ::Game . new
120
127
@@ -124,11 +131,11 @@ class Rubygo
124
131
on_create = lambda { |game |
125
132
@game . height = game . height
126
133
@game . width = game . width
134
+ @game . tokens = game . tokens
127
135
}
128
136
new_game_window ( on_create : on_create ) . show
129
137
end
130
138
}
131
- menu_item ( 'Edit Current Game' )
132
139
menu_item ( 'Load Game' )
133
140
134
141
# Enables quitting with CMD+Q on Mac with Mac Quit menu item
@@ -165,7 +172,7 @@ class Rubygo
165
172
text <= [ @game , :name ]
166
173
}
167
174
vertical_box {
168
- content ( @game , :width ) {
175
+ content ( @game , :tokens ) {
169
176
game_board ( game : @game )
170
177
}
171
178
}
0 commit comments