tic_tac_toes 0.0.3 → 0.0.4
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 +4 -4
- data/Gemfile.lock +5 -5
- data/lib/tic_tac_toes/board.rb +1 -1
- data/lib/tic_tac_toes/version.rb +1 -1
- data/lib/ui/adapter.rb +84 -0
- data/spec/ui/adapter_spec.rb +53 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d95fc15ec4e75672f05b36b32bd538a2cc2151ad
|
4
|
+
data.tar.gz: 52c4c7899c4665ab00c11207aa40404ad2f9eb93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c979a01b45ab43d4ec06b8c09ea443935cf49d8d315fba3b12489ac538d0d9504a28a2162301659b5eec5062fad56f2702925161bd5d6d188ada9526700dd32
|
7
|
+
data.tar.gz: 21d2c928f2e06b78c6224642f56b097e122d9d2a46eb4b347d9393faa34b33adf31e7df9e8d46c595cae07ced0906110f51d17e4ea70e2f01c698fa12b558270
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
tic_tac_toes (0.0.
|
4
|
+
tic_tac_toes (0.0.3)
|
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.
|
17
|
+
rspec-core (3.0.2)
|
18
18
|
rspec-support (~> 3.0.0)
|
19
|
-
rspec-expectations (3.0.
|
19
|
+
rspec-expectations (3.0.2)
|
20
20
|
diff-lcs (>= 1.2.0, < 2.0)
|
21
21
|
rspec-support (~> 3.0.0)
|
22
|
-
rspec-mocks (3.0.
|
22
|
+
rspec-mocks (3.0.2)
|
23
23
|
rspec-support (~> 3.0.0)
|
24
|
-
rspec-support (3.0.
|
24
|
+
rspec-support (3.0.2)
|
25
25
|
|
26
26
|
PLATFORMS
|
27
27
|
ruby
|
data/lib/tic_tac_toes/board.rb
CHANGED
data/lib/tic_tac_toes/version.rb
CHANGED
data/lib/ui/adapter.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'tic_tac_toes/board'
|
2
|
+
require 'tic_tac_toes/game_state'
|
3
|
+
require 'tic_tac_toes/player_factory'
|
4
|
+
|
5
|
+
module UI
|
6
|
+
class NullHistory
|
7
|
+
def record_board_size(size)
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def record_move(move)
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Adapter
|
17
|
+
def self.new_board_structure
|
18
|
+
board = TicTacToes::Board.new
|
19
|
+
board.spaces
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.move_made(board_structure, move)
|
23
|
+
move = move.to_i
|
24
|
+
game_state = UI::Adapter.game_state_from_board_structure(board_structure)
|
25
|
+
|
26
|
+
human_player = game_state.players.first
|
27
|
+
game_state.board.place(human_player, move)
|
28
|
+
game_state.turn_over(move)
|
29
|
+
|
30
|
+
computer_player = game_state.players.first
|
31
|
+
computer_player.place_and_return_move(game_state.board, game_state.players)
|
32
|
+
|
33
|
+
UI::Adapter.game_state_to_board_structure(game_state)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.game_state_from_board_structure(board_structure)
|
37
|
+
player_factory = TicTacToes::PlayerFactory.new('unused_io')
|
38
|
+
human_player = player_factory.generate_human_player('X')
|
39
|
+
computer_player = player_factory.generate_computer_player('O', :hard)
|
40
|
+
players = [human_player, computer_player]
|
41
|
+
|
42
|
+
board_structure_with_players = replace_tokens_with_players(board_structure, human_player, computer_player)
|
43
|
+
board = board_from_structure(board_structure_with_players)
|
44
|
+
|
45
|
+
TicTacToes::GameState.new(board, players, NullHistory.new)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.game_state_to_board_structure(game_state)
|
49
|
+
structure_with_players = game_state.board.spaces
|
50
|
+
|
51
|
+
replace_players_with_tokens(structure_with_players)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def self.replace_tokens_with_players(board_structure, human_player, computer_player)
|
57
|
+
board_structure.map do |space|
|
58
|
+
case space
|
59
|
+
when 'X'
|
60
|
+
human_player
|
61
|
+
when 'O'
|
62
|
+
computer_player
|
63
|
+
else
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.replace_players_with_tokens(board_structure)
|
70
|
+
board_structure.map { |space| space.token unless space.nil? }
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.board_from_structure(board_structure)
|
74
|
+
row_size = Math.sqrt(board_structure.count).to_i
|
75
|
+
board = TicTacToes::Board.new(row_size: row_size)
|
76
|
+
|
77
|
+
board_structure.each_with_index do |player, index|
|
78
|
+
board.place(player, index)
|
79
|
+
end
|
80
|
+
|
81
|
+
board
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'tic_tac_toes/board'
|
2
|
+
require 'tic_tac_toes/game_state'
|
3
|
+
require 'tic_tac_toes/player_factory'
|
4
|
+
require 'ui/adapter'
|
5
|
+
|
6
|
+
describe UI::Adapter do
|
7
|
+
describe '#new_board_structure' do
|
8
|
+
it 'returns a new board structure' do
|
9
|
+
new_board_structure = [nil, nil, nil, nil, nil, nil, nil, nil, nil]
|
10
|
+
expect(UI::Adapter.new_board_structure).to eq(new_board_structure)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#move_made' do
|
15
|
+
it 'takes a move (as string) and a board structure and returns an updated board structure' do
|
16
|
+
board_structure = ["X", nil, nil, nil, "O", nil, nil, nil, nil]
|
17
|
+
move = "2"
|
18
|
+
updated_board_structure = ["X", "O", "X", nil, "O", nil, nil, nil, nil]
|
19
|
+
|
20
|
+
expect(UI::Adapter.move_made(board_structure, move)).to eq(updated_board_structure)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#game_state_from_board_structure' do
|
25
|
+
it 'returns a game state object based on a board structure' do
|
26
|
+
board_structure = [nil, nil, nil, nil, "X", nil, nil, nil, nil]
|
27
|
+
|
28
|
+
game_state = UI::Adapter.game_state_from_board_structure(board_structure)
|
29
|
+
middle_space = game_state.board.space(4)
|
30
|
+
first_player = game_state.players.first
|
31
|
+
second_player = game_state.players.last
|
32
|
+
|
33
|
+
expect(middle_space.token).to eq('X')
|
34
|
+
expect(first_player.token).to eq('X')
|
35
|
+
expect(second_player.token).to eq('O')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#game_state_to_board_structure' do
|
40
|
+
it 'returns a board structure based on a game state object' do
|
41
|
+
player_factory = TicTacToes::PlayerFactory.new('unused_io')
|
42
|
+
first_player = player_factory.generate_human_player('X')
|
43
|
+
second_player = player_factory.generate_computer_player('O', :hard)
|
44
|
+
players = [first_player, second_player]
|
45
|
+
board = TicTacToes::Board.new
|
46
|
+
board.place(first_player, 4)
|
47
|
+
|
48
|
+
game_state = TicTacToes::GameState.new(board, players, UI::NullHistory.new)
|
49
|
+
board_structure = [nil, nil, nil, nil, "X", nil, nil, nil, nil]
|
50
|
+
expect(UI::Adapter.game_state_to_board_structure(game_state)).to eq(board_structure)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
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.
|
4
|
+
version: 0.0.4
|
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-
|
11
|
+
date: 2014-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/tic_tac_toes/rules.rb
|
108
108
|
- lib/tic_tac_toes/strings.rb
|
109
109
|
- lib/tic_tac_toes/version.rb
|
110
|
+
- lib/ui/adapter.rb
|
110
111
|
- spec/command_line/menu_spec.rb
|
111
112
|
- spec/command_line/runner_spec.rb
|
112
113
|
- spec/database/pg_wrapper_spec.rb
|
@@ -125,6 +126,7 @@ files:
|
|
125
126
|
- spec/tic_tac_toes/player_spec.rb
|
126
127
|
- spec/tic_tac_toes/rules_spec.rb
|
127
128
|
- spec/tic_tac_toes/strings_spec.rb
|
129
|
+
- spec/ui/adapter_spec.rb
|
128
130
|
- tic_tac_toes.gemspec
|
129
131
|
homepage: http://github.com/bspatafora/tic_tac_toes
|
130
132
|
licenses:
|
@@ -169,3 +171,4 @@ test_files:
|
|
169
171
|
- spec/tic_tac_toes/player_spec.rb
|
170
172
|
- spec/tic_tac_toes/rules_spec.rb
|
171
173
|
- spec/tic_tac_toes/strings_spec.rb
|
174
|
+
- spec/ui/adapter_spec.rb
|