game_board 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/README.md +62 -0
- data/Rakefile +2 -0
- data/game_board.gemspec +16 -0
- data/lib/game_board/version.rb +3 -0
- data/lib/game_board.rb +89 -0
- data/spec/game_board_spec.rb +62 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Matthew Powers, Elliot Shiu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
## GameBoard
|
2
|
+
|
3
|
+
This class helps calculate the rows, columns, diagonals, and diagonals of a coordinate for nested array data structures. It is useful for games like Tic-Tac-Toe, Connect Four, Battleship, etc.
|
4
|
+
|
5
|
+
## Getting Started
|
6
|
+
|
7
|
+
You can add GameBoard to your Gemfile with:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'game_board'
|
11
|
+
```
|
12
|
+
|
13
|
+
Run the bundle command afterwards to install the gem.
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Create a new 4x3 game board:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
board = GameBoard.new(4,3)
|
21
|
+
```
|
22
|
+
|
23
|
+
Populate the game board with numbers:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
cells = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
|
27
|
+
board.set_grid(cells)
|
28
|
+
```
|
29
|
+
|
30
|
+
Get or set a value at a coordinate (row_index, column_index):
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
board.get_cell(0,0) => 1
|
34
|
+
board.set_cell(0,0, 'X')
|
35
|
+
board.get_cell(0,0) => X
|
36
|
+
```
|
37
|
+
|
38
|
+
Get the two diagonals at a coordinate:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
board.coordinate_diagonals(2, 2) => [[1, 5, 9], [9, 11]]
|
42
|
+
```
|
43
|
+
|
44
|
+
Get all diagonals in the grid:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
board.diagonals
|
48
|
+
```
|
49
|
+
|
50
|
+
Get a row or column:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
board.row(1) => [4, 5, 6]
|
54
|
+
board.column(2) => [3, 6, 9, 12]
|
55
|
+
```
|
56
|
+
|
57
|
+
Get all rows or columns:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
board.rows
|
61
|
+
board.columns
|
62
|
+
```
|
data/Rakefile
ADDED
data/game_board.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/game_board/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matthew Powers", "Elliot Shiu"]
|
6
|
+
gem.email = ["matthewkevinpowers@gmail.com", "elliot@sandbochs.com"]
|
7
|
+
gem.description = %q{This is a GameBoard class with methods to help analyze the grid.}
|
8
|
+
gem.summary = %q{A class to represent game boards}
|
9
|
+
gem.homepage = "https://github.com/MrPowers/GameBoard"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "game_board"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = GameBoard::VERSION
|
16
|
+
end
|
data/lib/game_board.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'game_board/version'
|
2
|
+
|
3
|
+
class GameBoard
|
4
|
+
attr_reader :grid, :number_of_rows, :number_of_columns
|
5
|
+
|
6
|
+
def initialize(number_of_rows, number_of_columns)
|
7
|
+
@grid = Array.new(number_of_rows) { Array.new(number_of_columns) }
|
8
|
+
@number_of_rows = number_of_rows
|
9
|
+
@number_of_columns = number_of_columns
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_grid(new_grid)
|
13
|
+
grid.replace(new_grid)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_cell(row_index, column_index)
|
17
|
+
grid[row_index][column_index]
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_cell(row_index, column_index, value)
|
21
|
+
grid[row_index][column_index] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
def row(row_index)
|
25
|
+
grid[row_index]
|
26
|
+
end
|
27
|
+
|
28
|
+
def rows
|
29
|
+
grid
|
30
|
+
end
|
31
|
+
|
32
|
+
def column(column_index)
|
33
|
+
columns[column_index]
|
34
|
+
end
|
35
|
+
|
36
|
+
def columns
|
37
|
+
grid.transpose
|
38
|
+
end
|
39
|
+
|
40
|
+
# returns the two diagonals from one coordinate
|
41
|
+
def coordinate_diagonals(row_index, column_index)
|
42
|
+
[diagonal_from_coordinate(row_index, column_index, grid),
|
43
|
+
diagonal_from_coordinate(row_index, column_index_reflected_about_y_axis(column_index), grid_reflected_about_y_axis)]
|
44
|
+
end
|
45
|
+
# returns all possible diagonals from the grid
|
46
|
+
def diagonals
|
47
|
+
down_right_diagonals(grid) + down_right_diagonals(grid_reflected_about_x_axis)
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_s
|
51
|
+
grid.collect{ |row| row.to_s }.join("\n")
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
# returns the down-right diagonal from a coordinate
|
57
|
+
def diagonal_from_coordinate(row_index, column_index, grid_array)
|
58
|
+
row_index < column_index ? lower = row_index : lower = column_index
|
59
|
+
down_right_diagonal(row_index - lower, column_index - lower, grid_array)
|
60
|
+
end
|
61
|
+
|
62
|
+
def column_index_reflected_about_y_axis(column_index)
|
63
|
+
number_of_columns - column_index - 1
|
64
|
+
end
|
65
|
+
|
66
|
+
def down_right_diagonal(row_index, column_index, grid_array)
|
67
|
+
diag = []
|
68
|
+
while (row_index < number_of_rows) && (column_index < number_of_columns)
|
69
|
+
diag << grid_array[row_index][column_index]
|
70
|
+
row_index += 1
|
71
|
+
column_index += 1
|
72
|
+
end
|
73
|
+
diag
|
74
|
+
end
|
75
|
+
|
76
|
+
def down_right_diagonals(grid_array)
|
77
|
+
number_of_rows.times.map { |row_index| down_right_diagonal(row_index, 0, grid_array) } +
|
78
|
+
number_of_columns.times.map { |column_index| down_right_diagonal(1, column_index, grid_array)}
|
79
|
+
end
|
80
|
+
|
81
|
+
def grid_reflected_about_y_axis
|
82
|
+
grid.map { |row| row.reverse }
|
83
|
+
end
|
84
|
+
|
85
|
+
def grid_reflected_about_x_axis
|
86
|
+
grid.reverse
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative '../lib/game_board'
|
2
|
+
|
3
|
+
describe GameBoard do
|
4
|
+
|
5
|
+
let(:board) { GameBoard.new(4, 3) }
|
6
|
+
let(:grid) { [[1, 2, 3],
|
7
|
+
[4, 5, 6],
|
8
|
+
[7, 8, 9],
|
9
|
+
[10, 11, 12]] }
|
10
|
+
before do
|
11
|
+
board.set_grid(grid)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#set_grid" do
|
15
|
+
it "sets a new grid for the game board" do
|
16
|
+
new_grid = [[1, 1, 1],[2, 2, 2],[3, 3, 3],[4, 4, 4]]
|
17
|
+
expect { board.set_grid(new_grid) }.to change { board.grid }.from([[1, 2, 3],[4, 5, 6], [7, 8, 9], [10, 11, 12]]).to([[1, 1, 1],[2, 2, 2],[3, 3, 3],[4, 4, 4]])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "#get_cell" do
|
22
|
+
it { board.get_cell(0, 2).should eq 3 }
|
23
|
+
end
|
24
|
+
|
25
|
+
context "#set_cell" do
|
26
|
+
it "sets a cell at a coordinate" do
|
27
|
+
expect { board.set_cell(1, 1, "BOB")}.to change { board.get_cell(1, 1) }.from(5).to("BOB")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context '#row' do
|
32
|
+
it { board.row(1).should eq [4, 5, 6] }
|
33
|
+
end
|
34
|
+
|
35
|
+
context '#rows' do
|
36
|
+
it { board.rows.should eq [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] }
|
37
|
+
end
|
38
|
+
|
39
|
+
context '#column' do
|
40
|
+
it { board.column(2).should eq [3, 6, 9, 12] }
|
41
|
+
end
|
42
|
+
|
43
|
+
context '#columns' do
|
44
|
+
it "calculate all columns" do
|
45
|
+
board.columns === ([[1, 2, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "#coordinate_diagonals" do
|
50
|
+
it { board.coordinate_diagonals(0, 0).should eq [[1, 5, 9], [1]] }
|
51
|
+
it { board.coordinate_diagonals(1, 1).should eq [[1, 5, 9], [3, 5, 7]] }
|
52
|
+
it { board.coordinate_diagonals(2, 2).should eq [[1, 5, 9], [9, 11]] }
|
53
|
+
end
|
54
|
+
|
55
|
+
context '#diagonals' do
|
56
|
+
it 'calculates all diagonals' do
|
57
|
+
board.diagonals === [[10], [7, 11], [4, 8, 12], [1, 5, 9], [2, 6], [3],
|
58
|
+
[1], [4, 2], [7, 5, 3], [10, 8, 6], [11, 9], [12]]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: game_board
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Powers
|
9
|
+
- Elliot Shiu
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: This is a GameBoard class with methods to help analyze the grid.
|
16
|
+
email:
|
17
|
+
- matthewkevinpowers@gmail.com
|
18
|
+
- elliot@sandbochs.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- game_board.gemspec
|
29
|
+
- lib/game_board.rb
|
30
|
+
- lib/game_board/version.rb
|
31
|
+
- spec/game_board_spec.rb
|
32
|
+
homepage: https://github.com/MrPowers/GameBoard
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: A class to represent game boards
|
56
|
+
test_files:
|
57
|
+
- spec/game_board_spec.rb
|