gameboard 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a3e533fd420dc06eccaed5652ed1cdee4124a92
4
- data.tar.gz: 44990056f6c175c4ba2bbf3a5c854034855dcd46
3
+ metadata.gz: cc06eb2b5a787b4ffa5f157f737ac4e06c3431dd
4
+ data.tar.gz: 2cc0b36fd6542e428b3af420f0abde9debf42347
5
5
  SHA512:
6
- metadata.gz: 260e45aa189cb9cf5e52e0541312382bc6cce88d57a145951cb1b82fdb09326143ce633ac6f39a3264573606d1238264a1d4b01efbcbdeed2e83b019a07f1e2d
7
- data.tar.gz: 454d61ce7ce5b8ba97d05357a08f719d7e90c0b2b6c1caec0712759986b1332f3e4e97d860eb98274e486855dcd9af12167fe767640261ce08ef0e7ea04d1b1c
6
+ metadata.gz: c7824e45aaf8091103d4be8fcbfc8cf394fcbf87d4601edbc1ae1035ceda53e7722b9ad86b7a2be9aebb080371298ea57497c4ab5c9bfc22f9a6acf94782e7e8
7
+ data.tar.gz: dfc07732f725670ba6ebdd2e9fe31c2e610bf71a52fabf178027e262333ea85ea26c3b8fddc9c87fab2799ff1615e86b8bb5b1999524ecb83d2cdce973b22a3a
data/lib/gameboard.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "gameboard/version"
2
+ require "gameboard/board"
2
3
 
3
4
  module Gameboard
4
5
  # Your code goes here...
6
+
5
7
  end
@@ -1,7 +1,9 @@
1
1
  require_relative './cell'
2
+
2
3
  module Gameboard
3
4
  class Board
4
5
  attr_reader :height, :width, :board
6
+ include Enumerable
5
7
 
6
8
  def initialize(height:false, width: false, cells: false, preset: false)
7
9
  raise ArgumentError unless ((height.is_a?(Integer) && width.is_a?(Integer)) || !!preset)
@@ -11,13 +13,60 @@ module Gameboard
11
13
  preset ? load_game(preset) : new_board
12
14
  end
13
15
 
14
- def new_board
15
- @board = []
16
- width.times do |x|
17
- height.times do |y|
18
- @board << Cell.new(Coordinate.new(x,y), value: cells)
16
+ def each(&block)
17
+ return enum_for(__method__) if block.nil?
18
+ board.each(&block)
19
+ board
20
+ end
21
+
22
+ def each_value(&block)
23
+ return enum_for(__method__) if block.nil?
24
+ each{|cell| yield(cell.value)}
25
+ end
26
+
27
+ def each_coordinate(&block)
28
+ return enum_for(__method__) if block.nil?
29
+ each { |cell| yield(cell.coord.position) }
30
+ end
31
+
32
+ def delta(point, slope, coord = false)
33
+ delta = point.zip(slope).map {|point| point.reduce(:+) }
34
+ piece = board.find { |cell| cell.coord.position == delta }
35
+ raise "Off Grid" unless !!piece
36
+ coord ? piece.coord.position : piece.value
37
+ end
38
+
39
+ def diagonal(coords = false)
40
+ diagonals = []
41
+
42
+ height.times do |i|
43
+ diagonals << get_diagonal([0, i], coords)
44
+ diagonals << get_diagonal([0, height - 1 - i], coords, false)
45
+ end
46
+ (1...width).each do
47
+ |i| diagonals << get_diagonal([i, 0], coords)
48
+ diagonals << get_diagonal([i, height - 1], coords, false)
49
+ end
50
+
51
+ diagonals
52
+ end
53
+
54
+ def find_cell(coord)
55
+ board.find {|cell| cell.coord.position == coord}
56
+ end
57
+
58
+ def full?
59
+ board.none? { |cell| cell.value == cells }
60
+ end
61
+
62
+ def horizontal(coords = false)
63
+ columns = []
64
+ height.times do |y|
65
+ columns << board.select { |cell| cell.coord.y == y }.map do |cell|
66
+ coords ? cell.coord.position : cell.value
19
67
  end
20
68
  end
69
+ columns
21
70
  end
22
71
 
23
72
  def load_game(saved_game)
@@ -32,18 +81,20 @@ module Gameboard
32
81
  end
33
82
  end
34
83
 
35
- def find_cell(coord)
36
- @board.find {|cell| cell.coord.position == coord}
84
+ def neighbors(coord, points = false)
85
+ temp = Coordinate.new(coord[0], coord[1])
86
+ valid_neighbors(temp).map do |cell|
87
+ points ? cell.coord.position : cell.value
88
+ end
37
89
  end
38
90
 
39
- def horizontal(coords = false)
40
- columns = []
41
- height.times do |y|
42
- columns << board.select { |cell| cell.coord.y == y }.map do |cell|
43
- coords ? cell.coord.position : cell.value
91
+ def new_board
92
+ @board = []
93
+ width.times do |x|
94
+ height.times do |y|
95
+ @board << Cell.new(Coordinate.new(x,y), value: cells)
44
96
  end
45
97
  end
46
- columns
47
98
  end
48
99
 
49
100
  def vertical(coords = false)
@@ -56,25 +107,6 @@ module Gameboard
56
107
  columns
57
108
  end
58
109
 
59
- def diagonal(coords = false)
60
- diagonals = []
61
-
62
- height.times do |i|
63
- diagonals << get_diagonal([0, i], coords)
64
- diagonals << get_diagonal([0, height - 1 - i], coords, false)
65
- end
66
- (1...width).each do
67
- |i| diagonals << get_diagonal([i, 0], coords)
68
- diagonals << get_diagonal([i, height - 1], coords, false)
69
- end
70
-
71
- diagonals
72
- end
73
-
74
- def full?
75
- @board.none? { |cell| cell.value == cells }
76
- end
77
-
78
110
  private
79
111
  attr_reader :cells
80
112
 
@@ -88,5 +120,9 @@ module Gameboard
88
120
  end
89
121
  diagonal.compact.map{|cell| coords ? cell.coord.position : cell.value }
90
122
  end
123
+
124
+ def valid_neighbors(coord)
125
+ coord.neighbors.collect { |point| find_cell(point) }.compact
126
+ end
91
127
  end
92
128
  end
@@ -3,10 +3,11 @@ module Gameboard
3
3
  class Cell
4
4
  attr_accessor :value
5
5
  attr_reader :coord
6
+
6
7
  def initialize(coord, value: false)
7
8
  raise TypeError unless coord.is_a?(Coordinate)
8
9
  @value = value
9
10
  @coord = coord
10
11
  end
11
12
  end
12
- end
13
+ end
@@ -1,6 +1,7 @@
1
1
  module Gameboard
2
2
  class Coordinate
3
3
  attr_reader :position, :x, :y
4
+
4
5
  def initialize(x,y)
5
6
  invalid_type = "Coordinates must be integers!"
6
7
  raise invalid_type unless (x.is_a?(Integer) && y.is_a?(Integer))
@@ -8,5 +9,21 @@ module Gameboard
8
9
  @x = x
9
10
  @y = y
10
11
  end
12
+
13
+ def neighbors
14
+ relative_neighbors.map { |point| delta(point) }
15
+ end
16
+ private
17
+ def delta(point)
18
+ point.zip(position).map {|combined| combined.reduce(:+) }
19
+ end
20
+
21
+ def relative_neighbors
22
+ [
23
+ [-1, 1], [0, 1], [1, 1],
24
+ [-1, 0], [1, 0],
25
+ [-1, -1], [-1, 0], [-1, 1]
26
+ ]
27
+ end
11
28
  end
12
- end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Gameboard
2
- VERSION = "1.2.0"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gameboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampson Crowley