tic_tac_toes 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b916612f87bbddbda20c8c47a5c0697dd0f3df9
4
- data.tar.gz: e540d090a918ef060bdf219f1eb35c70389572ef
3
+ metadata.gz: 961f1ca9125169c762eac941829d166fb4ba42a2
4
+ data.tar.gz: a067d71fefcd398e645959f8fa798b805d753e5e
5
5
  SHA512:
6
- metadata.gz: bc76fb53614059ee235d7f4c22727c2171b05f982c11f6cbc098f3c05dc6559de85d1116bb5889f112f6802155dd5f75a06ec32524f121254b4ff23d5fcf70b5
7
- data.tar.gz: 161f61d05e232f78e5a80c4582ece012dcb66ac1ac72cb4f61608e8207e6fab4888aaacef172ecfb4dae90c65a8a12c5ef511942ed1df02d7948323e1a08d230
6
+ metadata.gz: 0e689cbb947b0bae18400a45b578f2b68b85eb18c148923ad763301a37e90483f640b80227161c95f5532535964b93fbaeaad616c017658b34d820b1c48158ac
7
+ data.tar.gz: ee969a45d65f3702ed0d3ac10c496d2a380b4fd25c31c7d27a3479308f775d6795ba595b25a9fbf864fa21234324658537e627ad25f7356ff43ed0e11e984937
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tic_tac_toes (0.0.4)
4
+ tic_tac_toes (0.0.6)
5
5
  pg
6
6
 
7
7
  GEM
@@ -14,14 +14,14 @@ GEM
14
14
  rspec-core (~> 3.0.0)
15
15
  rspec-expectations (~> 3.0.0)
16
16
  rspec-mocks (~> 3.0.0)
17
- rspec-core (3.0.2)
17
+ rspec-core (3.0.3)
18
18
  rspec-support (~> 3.0.0)
19
- rspec-expectations (3.0.2)
19
+ rspec-expectations (3.0.3)
20
20
  diff-lcs (>= 1.2.0, < 2.0)
21
21
  rspec-support (~> 3.0.0)
22
- rspec-mocks (3.0.2)
22
+ rspec-mocks (3.0.3)
23
23
  rspec-support (~> 3.0.0)
24
- rspec-support (3.0.2)
24
+ rspec-support (3.0.3)
25
25
 
26
26
  PLATFORMS
27
27
  ruby
@@ -4,6 +4,8 @@ module TicTacToes
4
4
  module MoveStrategies
5
5
  module HardAI
6
6
  def self.move(board, players)
7
+ return second_move(board) if nine_board_second_move?(board)
8
+
7
9
  open_spaces = Hash[board.open_spaces.map { |space| [space, nil] }]
8
10
 
9
11
  open_spaces.each do |space, score|
@@ -15,6 +17,8 @@ module TicTacToes
15
17
  open_spaces.each { |space, score| return space if score == best_score }
16
18
  end
17
19
 
20
+ private
21
+
18
22
  def self.minimax(board, current_player, players)
19
23
  return score(board, players) if Rules.game_over?(board, players)
20
24
 
@@ -54,6 +58,30 @@ module TicTacToes
54
58
  0
55
59
  end
56
60
  end
61
+
62
+ def self.nine_board_second_move?(board)
63
+ board.size == 9 && board.open_spaces.count == 8
64
+ end
65
+
66
+ def self.second_move(board)
67
+ if nine_board_corner_occupied(board) || nine_board_side_occupied(board)
68
+ 4
69
+ elsif nine_board_center_occupied(board)
70
+ 0
71
+ end
72
+ end
73
+
74
+ def self.nine_board_corner_occupied(board)
75
+ board.space(0) || board.space(2) || board.space(6) || board.space(8)
76
+ end
77
+
78
+ def self.nine_board_side_occupied(board)
79
+ board.space(1) || board.space(3) || board.space(5) || board.space(7)
80
+ end
81
+
82
+ def self.nine_board_center_occupied(board)
83
+ board.space(4) ? true : false
84
+ end
57
85
  end
58
86
  end
59
87
  end
@@ -1,3 +1,3 @@
1
1
  module TicTacToes
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -18,6 +18,32 @@ describe TicTacToes::MoveStrategies::HardAI do
18
18
 
19
19
  expect(hard_ai.move(board, players)).to eql(best_move)
20
20
  end
21
+
22
+ context "when playing on a 3x3 board" do
23
+ it "returns 4 when the opponent’s first move was a corner" do
24
+ board = TestBoardGenerator.generate([nil, nil, nil,
25
+ nil, nil, nil,
26
+ nil, nil, x])
27
+
28
+ expect(hard_ai.move(board, players)).to eq(4)
29
+ end
30
+
31
+ it "returns 4 when the opponent’s first move was an edge" do
32
+ board = TestBoardGenerator.generate([nil, nil, nil,
33
+ nil, nil, x,
34
+ nil, nil, nil])
35
+
36
+ expect(hard_ai.move(board, players)).to eq(4)
37
+ end
38
+
39
+ it "returns 0 when the opponent’s first move was the center" do
40
+ board = TestBoardGenerator.generate([nil, nil, nil,
41
+ nil, x, nil,
42
+ nil, nil, nil])
43
+
44
+ expect(hard_ai.move(board, players)).to eq(0)
45
+ end
46
+ end
21
47
  end
22
48
 
23
49
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tic_tac_toes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Spatafora
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-18 00:00:00.000000000 Z
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler