gomoku 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4871071f274fed79f3fcf6134767e2a81cc99df3
4
+ data.tar.gz: f7ca22e0fa35c082f31bb9a6ca16cdfe0e4f2e4a
5
+ SHA512:
6
+ metadata.gz: 07d691d25593e4660c67ef624d0b0486bb31053a5dbd7cbadd71833f2fdf0904e5823500262a9a12c7b741b20bb1558eb690cc277341f178ad8e8e7d71d929b5
7
+ data.tar.gz: 03329eeb2cdf48a336685d94e4d8a2cbbcebdec16aadaf6f1bddf5e1966774a56821e354bf7caf8155f383121e2747115d0b6bacd98b172a99ea49cf38809cf7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2015 David Cristofaro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # Gomoku
2
+
3
+ Classic five-in-a-row board game
4
+
5
+ ## Installation
6
+
7
+ Install it as a gem:
8
+
9
+ $ gem install gomoku
10
+
11
+ And run it:
12
+
13
+ $ gomoku
data/assets/black.png ADDED
Binary file
data/assets/grid.png ADDED
Binary file
data/assets/white.png ADDED
Binary file
data/bin/gomoku ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'gomoku'
4
+
5
+ # Show the Gomoku window
6
+ Gomoku::Gomoku.new.show
@@ -0,0 +1,123 @@
1
+ require 'gosu'
2
+ require 'gomoku/utility'
3
+
4
+ class Board
5
+ attr_reader :state
6
+
7
+ def initialize window
8
+ # Setup sprites
9
+ @grid = Gosu::Image.new(window, File.expand_path('../../../assets/grid.png', __FILE__), true)
10
+ @black = Gosu::Image.new(window, File.expand_path('../../../assets/black.png', __FILE__), true)
11
+ @white = Gosu::Image.new(window, File.expand_path('../../../assets/white.png', __FILE__), true)
12
+ end
13
+
14
+ def reset
15
+ # Default value in @state is invalid move (outside grid)
16
+ @state = Hash.new(:invalid)
17
+
18
+ # Setup blank spaces within grid range
19
+ for r in (1..19)
20
+ for c in (1..19)
21
+ @state[[r,c]] = :empty
22
+ end
23
+ end
24
+ end
25
+
26
+ def draw
27
+ # Draw the empty grid
28
+ @grid.draw(0, 0, 0)
29
+
30
+ # Loop through board cells and render stones
31
+ for r in (1..19)
32
+ for c in (1..19)
33
+ color = @state[[r,c]]
34
+ draw_stone color, r, c
35
+ end
36
+ end
37
+ end
38
+
39
+ def draw_stone color, r, c
40
+ case color
41
+ when :black
42
+ @black.draw(Utility.c_to_x(c), Utility.r_to_y(r), 1)
43
+ when :white
44
+ @white.draw(Utility.c_to_x(c), Utility.r_to_y(r), 1)
45
+ end
46
+ end
47
+
48
+ def check_win r, c
49
+ current = @state[[r,c]]
50
+
51
+ # No winner if we're on an empty cell
52
+ if current == :empty
53
+ return :none
54
+ end
55
+
56
+ # Test horizontal sequence
57
+ if c < 16
58
+ count = 0
59
+ (0..4).each do |offset|
60
+ if @state[[r, c+offset]] == current
61
+ count += 1
62
+ else break
63
+ end
64
+ end
65
+ # Winner found
66
+ if count == 5
67
+ return :horizontal
68
+ end
69
+ end
70
+
71
+ # Test vertical sequence
72
+ if r < 16
73
+ count = 0
74
+ (0..4).each do |offset|
75
+ if @state[[r+offset, c]] == current
76
+ count += 1
77
+ else break
78
+ end
79
+ end
80
+ # Winner found
81
+ if count == 5
82
+ return :vertical
83
+ end
84
+ end
85
+
86
+ # Test diagonal-up sequence
87
+ if r > 4 and c < 16
88
+ count = 0
89
+ (0..4).each do |offset|
90
+ if @state[[r-offset, c+offset]] == current
91
+ count += 1
92
+ else break
93
+ end
94
+ end
95
+ # Winner found
96
+ if count == 5
97
+ return :diagonal_up
98
+ end
99
+ end
100
+
101
+ # Test diagonal-down sequence
102
+ if r < 16 and c < 16
103
+ count = 0
104
+ (0..4).each do |offset|
105
+ if @state[[r+offset, c+offset]] == current
106
+ count += 1
107
+ else break
108
+ end
109
+ end
110
+ # Winner found
111
+ if count == 5
112
+ return :diagonal_down
113
+ end
114
+ end
115
+
116
+ # No winner
117
+ return :none
118
+ end
119
+
120
+ def is_empty? r, c
121
+ state[[r, c]] == :empty
122
+ end
123
+ end
@@ -0,0 +1,32 @@
1
+ require 'gomoku/player'
2
+
3
+ class Computer < Player
4
+ def is_human?
5
+ false
6
+ end
7
+
8
+ def update
9
+ catch :break do
10
+ for r in (1..19)
11
+ for c in (1..19)
12
+
13
+ if @board.state[[r,c]] == :empty
14
+
15
+ @board.state[[r,c]] = @window.turn
16
+ # Update turn
17
+ if @window.turn == :black
18
+ @window.turn = :white
19
+ else
20
+ @window.turn = :black
21
+ end
22
+
23
+ # Update flag
24
+ @window.process_turn = true
25
+
26
+ throw :break
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ require 'gomoku/player'
2
+
3
+ class Human < Player
4
+ def is_human?
5
+ true
6
+ end
7
+
8
+ def update
9
+ # Update mouse hover location
10
+ @hover_r = Utility.y_to_r @window.mouse_y
11
+ @hover_c = Utility.x_to_c @window.mouse_x
12
+ end
13
+
14
+ def draw
15
+ # Draw the hover for next piece
16
+ if Utility.in_range?(@hover_r, @hover_c) and @board.is_empty?(@hover_r, @hover_c)
17
+ @board.draw_stone @color, @hover_r, @hover_c
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ class Player
2
+ def initialize window, board, color
3
+ @window = window
4
+ @board = board
5
+ @color = color
6
+ end
7
+
8
+ def update
9
+ end
10
+
11
+ def draw
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ # Helper methods
2
+ module Utility
3
+ def Utility.r_to_y r
4
+ r*40 - 20
5
+ end
6
+
7
+ def Utility.c_to_x c
8
+ c*40 - 20
9
+ end
10
+
11
+ def Utility.x_to_c x
12
+ (x.to_i + 20) / 40
13
+ end
14
+
15
+ def Utility.y_to_r y
16
+ (y.to_i + 20) / 40
17
+ end
18
+
19
+ def Utility.in_range? r, c
20
+ r >= 1 and r <= 19 and c >= 1 and c <= 19
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Gomoku
2
+ VERSION = '0.1.0'
3
+ end
data/lib/gomoku.rb ADDED
@@ -0,0 +1,188 @@
1
+ require 'gosu'
2
+ require 'gomoku/version'
3
+ require 'gomoku/utility'
4
+ require 'gomoku/board'
5
+ require 'gomoku/human'
6
+ require 'gomoku/computer'
7
+
8
+ class Gomoku::Gomoku < Gosu::Window
9
+ def initialize
10
+ super 800, 800, false
11
+ self.caption = "Gomoku"
12
+
13
+ @board = Board.new(self)
14
+ @black_player = Human.new(self, @board, :black)
15
+ @white_player = Human.new(self, @board, :white)
16
+
17
+ # Start a new game
18
+ new_game
19
+ end
20
+
21
+ def new_game
22
+ # Reset the board
23
+ @board.reset
24
+
25
+ # Black goes first
26
+ @turn = :black
27
+
28
+ # Setup flag to indicate a turn needs to be processed
29
+ @process_turn = false
30
+
31
+ @winner = false
32
+ end
33
+
34
+ def needs_cursor?
35
+ true
36
+ end
37
+
38
+ def button_down id
39
+ case id
40
+ when Gosu::KbEscape
41
+ close
42
+ when Gosu::MsLeft
43
+ # If no winner, attempt a move
44
+ if !@winner
45
+ # Get the location of the click
46
+ click_r = Utility.y_to_r mouse_y
47
+ click_c = Utility.x_to_c mouse_x
48
+
49
+ # Check for black space
50
+ if @board.state[[click_r, click_c]] == :empty
51
+ # Perform move
52
+ @board.state[[click_r, click_c]] = @turn
53
+ # Update turn
54
+ if @turn == :black
55
+ @turn = :white
56
+ else
57
+ @turn = :black
58
+ end
59
+
60
+ # Update flag
61
+ @process_turn = true
62
+ end
63
+ else
64
+ # We already have a winner, so start new game
65
+ new_game
66
+ end
67
+ end
68
+ end
69
+
70
+ def button_up id
71
+ end
72
+
73
+ def update
74
+ case @turn
75
+ when :black
76
+ @black_player.update
77
+ when :white
78
+ @white_player.update
79
+ end
80
+
81
+ # If a move has been made, process the turn
82
+ if @process_turn
83
+ # Loop through board cells and check winner, break when found
84
+ catch :break do
85
+ for r in (1..19)
86
+ for c in (1..19)
87
+ win = @board.check_win r, c
88
+ if win != :none
89
+ @winner = true
90
+ @winner_direction = win
91
+ @winner_r = r
92
+ @winner_c = c
93
+ throw :break
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ # Done processing, reset flag
100
+ @process_turn = false
101
+ end
102
+ end
103
+
104
+ def draw
105
+ @board.draw
106
+
107
+ if !@winner
108
+ case @turn
109
+ when :black
110
+ @black_player.draw
111
+ when :white
112
+ @white_player.draw
113
+ end
114
+ else
115
+ # Winner, mark the winning sequence
116
+
117
+ # Draw three adjacent 1px lines to make a single 3px line.
118
+ # Worst code I ever wrote.
119
+ case @winner_direction
120
+ when :horizontal
121
+ line_x1_1 = Utility.c_to_x(@winner_c)
122
+ line_y1_1 = Utility.r_to_y(@winner_r) + 20
123
+ line_x1_2 = Utility.c_to_x(@winner_c + 5)
124
+ line_y1_2 = Utility.r_to_y(@winner_r) + 20
125
+
126
+ line_x2_1 = line_x1_1
127
+ line_y2_1 = line_y1_1 + 1
128
+ line_x2_2 = line_x1_2
129
+ line_y2_2 = line_y1_2 + 1
130
+
131
+ line_x3_1 = line_x1_1
132
+ line_y3_1 = line_y1_1 + 2
133
+ line_x3_2 = line_x1_2
134
+ line_y3_2 = line_y1_2 + 2
135
+ when :vertical
136
+ line_x1_1 = Utility.c_to_x(@winner_c) + 20
137
+ line_y1_1 = Utility.r_to_y(@winner_r)
138
+ line_x1_2 = Utility.c_to_x(@winner_c) + 20
139
+ line_y1_2 = Utility.r_to_y(@winner_r + 5)
140
+
141
+ line_x2_1 = line_x1_1 + 1
142
+ line_y2_1 = line_y1_1
143
+ line_x2_2 = line_x1_2 + 1
144
+ line_y2_2 = line_y1_2
145
+
146
+ line_x3_1 = line_x1_1 + 2
147
+ line_y3_1 = line_y1_1
148
+ line_x3_2 = line_x1_2 + 2
149
+ line_y3_2 = line_y1_2
150
+ when :diagonal_up
151
+ line_x1_1 = Utility.c_to_x(@winner_c)
152
+ line_y1_1 = Utility.r_to_y(@winner_r + 1)
153
+ line_x1_2 = Utility.c_to_x(@winner_c + 5)
154
+ line_y1_2 = Utility.r_to_y(@winner_r - 5)
155
+
156
+ line_x2_1 = line_x1_1
157
+ line_y2_1 = line_y1_1 + 1
158
+ line_x2_2 = line_x1_2 + 1
159
+ line_y2_2 = line_y1_2
160
+
161
+ line_x3_1 = line_x1_1 + 1
162
+ line_y3_1 = line_y1_1 + 1
163
+ line_x3_2 = line_x1_2 + 1
164
+ line_y3_2 = line_y1_2 + 1
165
+ when :diagonal_down
166
+ line_x1_1 = Utility.c_to_x(@winner_c)
167
+ line_y1_1 = Utility.r_to_y(@winner_r)
168
+ line_x1_2 = Utility.c_to_x(@winner_c + 5)
169
+ line_y1_2 = Utility.r_to_y(@winner_r + 5)
170
+
171
+ line_x2_1 = line_x1_1 + 1
172
+ line_y2_1 = line_y1_1
173
+ line_x2_2 = line_x1_2
174
+ line_y2_2 = line_y1_2 - 1
175
+
176
+ line_x3_1 = line_x1_1
177
+ line_y3_1 = line_y1_1 + 1
178
+ line_x3_2 = line_x1_2 - 1
179
+ line_y3_2 = line_y1_2
180
+ end
181
+
182
+ # Render the three lines
183
+ draw_line(line_x1_1, line_y1_1, Gosu::Color.argb(0xffff0000), line_x1_2, line_y1_2, Gosu::Color.argb(0xffff0000), 2, :default)
184
+ draw_line(line_x2_1, line_y2_1, Gosu::Color.argb(0xffff0000), line_x2_2, line_y2_2, Gosu::Color.argb(0xffff0000), 2, :default)
185
+ draw_line(line_x3_1, line_y3_1, Gosu::Color.argb(0xffff0000), line_x3_2, line_y3_2, Gosu::Color.argb(0xffff0000), 2, :default)
186
+ end
187
+ end
188
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gomoku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Cristofaro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gosu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ executables:
86
+ - gomoku
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - assets/black.png
93
+ - assets/grid.png
94
+ - assets/white.png
95
+ - bin/gomoku
96
+ - lib/gomoku.rb
97
+ - lib/gomoku/board.rb
98
+ - lib/gomoku/computer.rb
99
+ - lib/gomoku/human.rb
100
+ - lib/gomoku/player.rb
101
+ - lib/gomoku/utility.rb
102
+ - lib/gomoku/version.rb
103
+ homepage: https://github.com/dtcristo/gomoku
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Classic five-in-a-row board game
127
+ test_files: []