tic_tac_toes 0.1.6 → 0.1.7
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 +1 -1
- data/bin/tic_tac_toes +7 -3
- data/lib/tic_tac_toes/core/game_state.rb +10 -1
- data/lib/tic_tac_toes/core/move_strategies/hard_ai.rb +1 -1
- data/lib/tic_tac_toes/database/pg_wrapper.rb +6 -12
- data/lib/tic_tac_toes/tasks/destroy_databases.rake +2 -2
- data/lib/tic_tac_toes/tasks/set_up_databases.rake +13 -7
- data/lib/tic_tac_toes/version.rb +1 -1
- data/spec/tic_tac_toes/database/pg_wrapper_spec.rb +28 -27
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6648939399c69e21afbe0a74301745f40b98e31
|
4
|
+
data.tar.gz: dd3b0fd1992b99d8371ec706eef9d8e2d1941873
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f68f75de8e357f772673a96bd0cca5f9c92ed4caf5d1659d9c824008c25bc017aed2675e2ea27eff51ec7c4011e695246d138c95c1a7f4c3f84ecd9b1ac3fa67
|
7
|
+
data.tar.gz: c0aa21b6e2d0c232f55bc79db3cf4b893d76ac1ae3438922235ef4570e8dc6c665988637044d9b39e8d3f6507bdf0e1a1e2d2292a7f7f5739ce8c86ef4ea6350
|
data/Gemfile.lock
CHANGED
data/bin/tic_tac_toes
CHANGED
@@ -13,7 +13,11 @@ require 'tic_tac_toes/core/history'
|
|
13
13
|
|
14
14
|
require 'tic_tac_toes/command_line/runner'
|
15
15
|
|
16
|
-
|
16
|
+
connection = PG.connect(dbname: ENV['TTT_DATABASE'],
|
17
|
+
host: ENV['TTT_HOST'],
|
18
|
+
port: ENV['TTT_PORT'],
|
19
|
+
user: ENV['TTT_USER'],
|
20
|
+
password: ENV['TTT_PASSWORD'])
|
17
21
|
|
18
22
|
prompt = TicTacToes::CommandLine::Prompt
|
19
23
|
io = TicTacToes::Core::IO.new(prompt)
|
@@ -21,7 +25,7 @@ io = TicTacToes::Core::IO.new(prompt)
|
|
21
25
|
player_factory = TicTacToes::Core::PlayerFactory.new(io)
|
22
26
|
menu = TicTacToes::CommandLine::Menu.new(io, player_factory)
|
23
27
|
|
24
|
-
|
25
|
-
history = TicTacToes::Core::History.new(
|
28
|
+
storage_wrapper = TicTacToes::Database::PGWrapper.new(connection)
|
29
|
+
history = TicTacToes::Core::History.new(storage_wrapper)
|
26
30
|
|
27
31
|
TicTacToes::CommandLine::Runner.new(io, menu, history).run
|
@@ -4,6 +4,7 @@ require 'tic_tac_toes/core/move_strategies/types'
|
|
4
4
|
module TicTacToes
|
5
5
|
module Core
|
6
6
|
class GameState
|
7
|
+
attr_writer :history
|
7
8
|
attr_reader :board, :players
|
8
9
|
|
9
10
|
def initialize(board, players, history)
|
@@ -34,7 +35,7 @@ module TicTacToes
|
|
34
35
|
end
|
35
36
|
|
36
37
|
def turn_over(move)
|
37
|
-
@history.record_move(move)
|
38
|
+
@history.record_move(move) unless move.nil?
|
38
39
|
@players.rotate!
|
39
40
|
end
|
40
41
|
|
@@ -55,6 +56,14 @@ module TicTacToes
|
|
55
56
|
Rules.determine_winner(@board, @players)
|
56
57
|
end
|
57
58
|
|
59
|
+
def marshal_dump
|
60
|
+
[@board, @players]
|
61
|
+
end
|
62
|
+
|
63
|
+
def marshal_load(array)
|
64
|
+
@board, @players = array
|
65
|
+
end
|
66
|
+
|
58
67
|
private
|
59
68
|
|
60
69
|
def record_board_size
|
@@ -4,19 +4,17 @@ require 'tic_tac_toes/core/history'
|
|
4
4
|
module TicTacToes
|
5
5
|
module Database
|
6
6
|
class PGWrapper
|
7
|
-
def initialize(
|
8
|
-
@
|
7
|
+
def initialize(connection)
|
8
|
+
@connection = connection
|
9
9
|
end
|
10
10
|
|
11
11
|
def record_game_history(history)
|
12
|
-
connection
|
13
|
-
|
14
|
-
record_moves(history, connection)
|
12
|
+
record_game_info(history, @connection)
|
13
|
+
record_moves(history, @connection)
|
15
14
|
end
|
16
15
|
|
17
16
|
def read_game_histories
|
18
|
-
|
19
|
-
games_result = connection.exec("SELECT * FROM games")
|
17
|
+
games_result = @connection.exec("SELECT * FROM games")
|
20
18
|
games = []
|
21
19
|
|
22
20
|
games_result.each_row do |row|
|
@@ -24,7 +22,7 @@ module TicTacToes
|
|
24
22
|
board_size = row[1].to_i
|
25
23
|
difficulty = row[2]
|
26
24
|
winner = row[3]
|
27
|
-
moves_result = connection.exec("SELECT * FROM moves WHERE game = #{game_id}")
|
25
|
+
moves_result = @connection.exec("SELECT * FROM moves WHERE game = #{game_id}")
|
28
26
|
|
29
27
|
history = Core::History.new(self)
|
30
28
|
|
@@ -44,10 +42,6 @@ module TicTacToes
|
|
44
42
|
|
45
43
|
private
|
46
44
|
|
47
|
-
def establish_connection
|
48
|
-
PG.connect(dbname: @database)
|
49
|
-
end
|
50
|
-
|
51
45
|
def record_game_info(history, connection)
|
52
46
|
connection.exec("INSERT INTO games (board_size, difficulty, winner) VALUES (
|
53
47
|
#{history.board_size},
|
@@ -4,6 +4,6 @@ desc 'Drop Tic_tac_toes production and test databases'
|
|
4
4
|
task :destroy_databases do
|
5
5
|
connection = PG.connect(dbname: "postgres")
|
6
6
|
|
7
|
-
connection.exec("DROP DATABASE
|
8
|
-
connection.exec("DROP DATABASE
|
7
|
+
connection.exec("DROP DATABASE #{ENV['TTT_DATABASE']}")
|
8
|
+
connection.exec("DROP DATABASE #{ENV['TTT_TEST_DATABASE']}")
|
9
9
|
end
|
@@ -1,23 +1,29 @@
|
|
1
1
|
require 'pg'
|
2
2
|
|
3
|
-
|
4
|
-
TEST_DATABASE = 'tic_tac_toes_test'
|
5
|
-
|
6
|
-
desc 'Set up tic_tac_toes production and test databases'
|
3
|
+
desc 'Set up Tic_tac_toes production and test databases'
|
7
4
|
task :set_up_databases do
|
8
5
|
create_databases
|
9
6
|
create_production_tables
|
10
7
|
end
|
11
8
|
|
9
|
+
desc 'Set up just Tic_tac_toes production tables'
|
10
|
+
task :set_up_tables do
|
11
|
+
create_production_tables
|
12
|
+
end
|
13
|
+
|
12
14
|
def create_databases
|
13
15
|
connection = PG.connect(dbname: "postgres")
|
14
16
|
|
15
|
-
connection.exec("CREATE DATABASE #{
|
16
|
-
connection.exec("CREATE DATABASE #{
|
17
|
+
connection.exec("CREATE DATABASE #{ENV['TTT_DATABASE']}")
|
18
|
+
connection.exec("CREATE DATABASE #{ENV['TTT_TEST_DATABASE']}")
|
17
19
|
end
|
18
20
|
|
19
21
|
def create_production_tables
|
20
|
-
connection = PG.connect(dbname:
|
22
|
+
connection = PG.connect(dbname: ENV['TTT_DATABASE'],
|
23
|
+
host: ENV['TTT_HOST'],
|
24
|
+
port: ENV['TTT_PORT'],
|
25
|
+
user: ENV['TTT_USER'],
|
26
|
+
password: ENV['TTT_PASSWORD'])
|
21
27
|
|
22
28
|
connection.exec("CREATE TABLE games (
|
23
29
|
id serial primary key,
|
data/lib/tic_tac_toes/version.rb
CHANGED
@@ -2,28 +2,30 @@ require 'tic_tac_toes/database/pg_wrapper'
|
|
2
2
|
require 'pg'
|
3
3
|
|
4
4
|
describe TicTacToes::Database::PGWrapper do
|
5
|
-
database = "tic_tac_toes_test"
|
6
|
-
|
7
|
-
let(:pg_wrapper) { TicTacToes::Database::PGWrapper.new(database) }
|
8
|
-
let(:history1) { double("history 1",
|
9
|
-
:board_size => 9,
|
10
|
-
:difficulty => 'Medium AI',
|
11
|
-
:moves => [["X", 1], ["O", 4]],
|
12
|
-
:winner => "X") }
|
13
|
-
let(:history2) { double("history 2",
|
14
|
-
:board_size => 16,
|
15
|
-
:difficulty => 'Easy AI',
|
16
|
-
:moves => [["&", 14]],
|
17
|
-
:winner => "*") }
|
18
|
-
|
19
5
|
before do
|
20
|
-
connection = PG.connect(dbname:
|
21
|
-
|
6
|
+
@connection = PG.connect(dbname: ENV['TTT_TEST_DATABASE'],
|
7
|
+
host: ENV['TTT_HOST'],
|
8
|
+
port: ENV['TTT_PORT'],
|
9
|
+
user: ENV['TTT_USER'],
|
10
|
+
password: ENV['TTT_PASSWORD'])
|
11
|
+
@pg_wrapper = TicTacToes::Database::PGWrapper.new(@connection)
|
12
|
+
@history1 = double('history1',
|
13
|
+
board_size: 9,
|
14
|
+
difficulty: 'Medium AI',
|
15
|
+
moves: [['X', 1], ['O', 4]],
|
16
|
+
winner: 'X')
|
17
|
+
@history2 = double('history2',
|
18
|
+
board_size: 16,
|
19
|
+
difficulty: 'Easy AI',
|
20
|
+
moves: [['&', 14]],
|
21
|
+
winner: '*')
|
22
|
+
|
23
|
+
@connection.exec("CREATE TABLE games (
|
22
24
|
id serial primary key,
|
23
25
|
board_size integer,
|
24
26
|
difficulty varchar,
|
25
27
|
winner varchar)")
|
26
|
-
connection.exec("CREATE TABLE moves (
|
28
|
+
@connection.exec("CREATE TABLE moves (
|
27
29
|
game integer REFERENCES games (id),
|
28
30
|
number integer,
|
29
31
|
token varchar,
|
@@ -31,10 +33,10 @@ describe TicTacToes::Database::PGWrapper do
|
|
31
33
|
end
|
32
34
|
|
33
35
|
describe '#record_game_history and #read_games' do
|
34
|
-
it
|
35
|
-
pg_wrapper.record_game_history(history1)
|
36
|
+
it 'records and reads a history object to and from the database' do
|
37
|
+
@pg_wrapper.record_game_history(@history1)
|
36
38
|
|
37
|
-
history_from_database = pg_wrapper.read_game_histories.first
|
39
|
+
history_from_database = @pg_wrapper.read_game_histories.first
|
38
40
|
|
39
41
|
expect(history_from_database.board_size).to eq(9)
|
40
42
|
expect(history_from_database.difficulty).to eq('Medium AI')
|
@@ -43,17 +45,16 @@ describe TicTacToes::Database::PGWrapper do
|
|
43
45
|
expect(history_from_database.winner).to eq("X")
|
44
46
|
end
|
45
47
|
|
46
|
-
it
|
47
|
-
pg_wrapper.record_game_history(history1)
|
48
|
-
pg_wrapper.record_game_history(history2)
|
49
|
-
histories_from_database = pg_wrapper.read_game_histories
|
48
|
+
it 'records and reads multiple history objects to and from the database' do
|
49
|
+
@pg_wrapper.record_game_history(@history1)
|
50
|
+
@pg_wrapper.record_game_history(@history2)
|
51
|
+
histories_from_database = @pg_wrapper.read_game_histories
|
50
52
|
expect(histories_from_database.size).to eq(2)
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
54
56
|
after do
|
55
|
-
connection
|
56
|
-
connection.exec("DROP TABLE
|
57
|
-
connection.exec("DROP TABLE games")
|
57
|
+
@connection.exec("DROP TABLE moves")
|
58
|
+
@connection.exec("DROP TABLE games")
|
58
59
|
end
|
59
60
|
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.1.
|
4
|
+
version: 0.1.7
|
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-08-
|
11
|
+
date: 2014-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|