Skip to content

Commit e006dfd

Browse files
committed
Add playing tokens on board
1 parent c4e759a commit e006dfd

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

.byebug_history

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
c
2+
@game.tokens
3+
quit
4+
game.tokens
5+
player
6+
quit
7+
game.tokens[0][0][:player]
8+
game.tokens[0][0].display
9+
game.tokens[0][0].methods
10+
game.tokens[0][0]
11+
game.tokens[0]
12+
game.tokens
13+
column
14+
row
15+
player
16+
quit
17+
c
18+
game.tokens[0][0][:player]
19+
game.tokens[0][0]
20+
game.tokens
21+
game
22+
quit
23+
Glimmer::LibUI.x11_colors
24+
self
25+
game
26+
quit
27+
self.methods
28+
self.game
29+
self.@game
30+
@game
31+
#@game
32+
self
33+
c
34+
@game.__id__
35+
@game.methods
36+
@game
137
quit
238
c
339
game.height

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/test/tmp/
1111
/test/version_tmp/
1212
/tmp/
13-
13+
.byebug_history
1414
# Used by dotenv library to load environment variables.
1515
# .env
1616

app/rubygo/view/rubygo.rb

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
Cell = Struct.new (:player)
2+
13
class Rubygo
24
module Model
35
class Game
46

5-
attr_accessor :height, :width, :scale, :name
7+
attr_accessor :height, :width, :scale, :name, :tokens, :cur_player
68

79
def initialize(height = 12, width = 12, scale = 70, name = "Go Game")
810
@height = height
911
@width = width
1012
@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
1219
end
1320
end
1421
end
@@ -26,19 +33,30 @@ class GameBoard
2633
game.height.times.map do |row|
2734
horizontal_box {
2835
padded false
29-
3036
game.width.times.map do |column|
3137
half = game.scale / 2
3238
area {
3339
square(0, 0, game.scale) {
3440
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
3545
}
3646
line(half,row == 0 ? half : 0, half, row == (game.height - 1)? half : game.scale) {
3747
stroke 0x000000
3848
}
3949
line(column == 0 ? half : 0, half, column == (game.width - 1) ? half : game.scale, half){
4050
stroke 0x000000
4151
}
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+
}
4260
}
4361
end
4462
}
@@ -54,10 +72,8 @@ module View
5472
class NewGameWindow
5573
include Glimmer::LibUI::CustomWindow
5674
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
6177

6278
body {
6379
window { |new_game_window|
@@ -69,13 +85,13 @@ class NewGameWindow
6985
horizontal_box {
7086
label('Board Width')
7187
spinbox(2, 20) {
72-
value <=> [@game, :width]
88+
value <=> [self, :width]
7389
}
7490
}
7591
horizontal_box {
7692
label('Board Height')
7793
spinbox(2, 20) {
78-
value <=> [@game, :height]
94+
value <=> [self, :height]
7995
}
8096
}
8197
}
@@ -89,7 +105,7 @@ class NewGameWindow
89105
}
90106
button("New Game") {
91107
on_clicked do
92-
on_create.call(@game)
108+
on_create.call(Model::Game.new(height, width))
93109
new_game_window.destroy
94110
end
95111
}
@@ -106,15 +122,6 @@ module View
106122
class Rubygo
107123
include Glimmer::LibUI::Application
108124

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-
#
118125
before_body do
119126
@game = Model::Game.new
120127

@@ -124,11 +131,11 @@ class Rubygo
124131
on_create = lambda { |game|
125132
@game.height = game.height
126133
@game.width = game.width
134+
@game.tokens = game.tokens
127135
}
128136
new_game_window(on_create: on_create).show
129137
end
130138
}
131-
menu_item('Edit Current Game')
132139
menu_item('Load Game')
133140

134141
# Enables quitting with CMD+Q on Mac with Mac Quit menu item
@@ -165,7 +172,7 @@ class Rubygo
165172
text <= [@game, :name]
166173
}
167174
vertical_box {
168-
content(@game, :width) {
175+
content(@game, :tokens) {
169176
game_board(game: @game)
170177
}
171178
}

0 commit comments

Comments
 (0)