tictactoe_j8th 0.2.0
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 +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +15 -0
- data/lib/tictactoe_j8th/ai.rb +96 -0
- data/lib/tictactoe_j8th/board.rb +63 -0
- data/lib/tictactoe_j8th/game.rb +47 -0
- data/lib/tictactoe_j8th/human.rb +11 -0
- data/lib/tictactoe_j8th/version.rb +3 -0
- data/lib/tictactoe_j8th.rb +10 -0
- data/spec/ai_spec.rb +136 -0
- data/spec/board_spec.rb +85 -0
- data/spec/game_spec.rb +141 -0
- data/spec/human_spec.rb +11 -0
- data/spec/spec_helper.rb +89 -0
- data/tictactoe_j8th.gemspec +23 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 66d20ba2e1cacc30029cc615d91958fc24a1ced6
|
4
|
+
data.tar.gz: c123145b5bc58bf774032b247aa37bd7d03f7ac4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6ebd647c5a33b7190c95476a08b2d3a0b8a4cc295a595c44db7c0d85cfe72aa5b88c63dc1571e46461bc14f58003970bbeaab03a4dee74ae7aba0bec022447b
|
7
|
+
data.tar.gz: b4ef4f7a71cc547795842f883d8ce665ea73ac38f312e4b2d5a2a775ff948d952788eed9e0569a37d77617c87d9a8074832085d0a8fd8bf30ab989d9cdbdd800
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Write your name
|
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,39 @@
|
|
1
|
+
# TictactoeJ8th
|
2
|
+
|
3
|
+
A gem encapsulating the logic of a tic tac toe game with an unbeatable AI.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'tictactoe_j8th'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install tictactoe_j8th
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Everything is in a TictactoeJ8th module.
|
24
|
+
You get Game, Board, Human, and AI.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'tictactoe_j8th'
|
28
|
+
|
29
|
+
include TictactoeJ8th
|
30
|
+
Game.new(Board.new, Human.new(:X), AI.new(:O))
|
31
|
+
```
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/tictactoe_j8th/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc 'Run the normal test suite (same as \'rake spec\').'
|
4
|
+
task default: %w[spec]
|
5
|
+
|
6
|
+
desc 'Run the normal test suite, excluding some slower tests.'
|
7
|
+
task :spec do
|
8
|
+
system 'rspec --tag ~slow'
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Run the full test suite.'
|
12
|
+
task :fullspec do
|
13
|
+
system 'rspec'
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module TictactoeJ8th
|
2
|
+
class AI
|
3
|
+
|
4
|
+
attr_reader :token
|
5
|
+
|
6
|
+
def initialize(token)
|
7
|
+
@token = token
|
8
|
+
end
|
9
|
+
|
10
|
+
def move(board)
|
11
|
+
return nil if board.full?
|
12
|
+
|
13
|
+
# ####### Speed Hacks #######
|
14
|
+
# Hacks to speed up minimax by bypassing most of the possible games.
|
15
|
+
# The return statements here are important. We make a move, then
|
16
|
+
# bail out so that we don't end up analyzing all the possible games further down.
|
17
|
+
# TODO: See if there is a craftier, less hackish way to speed things up.
|
18
|
+
|
19
|
+
# If the board is empty, always take the middle.
|
20
|
+
if board.empty?
|
21
|
+
board.place(token, 0)
|
22
|
+
return 0
|
23
|
+
end
|
24
|
+
|
25
|
+
open_spots = get_open_spots(board)
|
26
|
+
# If there is one spot taken, then always take the middle.
|
27
|
+
# Unless the middle is taken already, then take the corner.
|
28
|
+
if open_spots.count == Board::BOARD_SIZE-1
|
29
|
+
spot = board[4].nil? ? 4 : 0
|
30
|
+
board.place(token, spot)
|
31
|
+
return spot
|
32
|
+
end
|
33
|
+
# ###### End Hacks ########
|
34
|
+
|
35
|
+
|
36
|
+
enemy_token = discover_enemy_token(board)
|
37
|
+
# Better here, but we do it above for our speed hacks.
|
38
|
+
#open_spots = get_open_spots(board)
|
39
|
+
games = Hash.new
|
40
|
+
|
41
|
+
open_spots.each do |i|
|
42
|
+
board_copy = board.create_copy
|
43
|
+
board_copy.place(token, i)
|
44
|
+
game = Game.new(board_copy, AI.new(enemy_token), AI.new(token))
|
45
|
+
if game.winner == token
|
46
|
+
board.place(token, i)
|
47
|
+
return i
|
48
|
+
end
|
49
|
+
games[i] = game
|
50
|
+
end
|
51
|
+
|
52
|
+
scores = Hash.new
|
53
|
+
|
54
|
+
games.each do |i, game|
|
55
|
+
game.play
|
56
|
+
scores[i] = 1 if game.winner == token
|
57
|
+
scores[i] = -1 if game.winner == enemy_token
|
58
|
+
scores[i] = 0 if game.winner.nil? and game.game_over?
|
59
|
+
end
|
60
|
+
|
61
|
+
max = scores.max_by { |k, v| v }
|
62
|
+
spot = max[0]
|
63
|
+
board.place(token, spot)
|
64
|
+
spot
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
private
|
70
|
+
# TODO: Messy, reconsider this later.
|
71
|
+
# Determine the enemy token.
|
72
|
+
# Anything we find on the board that is not our token is considered the 'enemy_token'
|
73
|
+
# We assume there is only one enemy token.
|
74
|
+
# If we do not find any enemy tokens on the board, we assume the enemy token to be 'O',
|
75
|
+
# or we assume it to be 'X' if our own token is 'O'.
|
76
|
+
def discover_enemy_token(board)
|
77
|
+
enemy_token = nil
|
78
|
+
(0..Board::BOARD_SIZE-1).each do |i|
|
79
|
+
enemy_token = board[i] if board[i] != token and board[i] != nil
|
80
|
+
end
|
81
|
+
if enemy_token.nil?
|
82
|
+
enemy_token = token == :X ? :O : :X
|
83
|
+
end
|
84
|
+
enemy_token
|
85
|
+
end
|
86
|
+
|
87
|
+
def get_open_spots(board)
|
88
|
+
open_spots = Array.new
|
89
|
+
(0..Board::BOARD_SIZE-1).each do |i|
|
90
|
+
open_spots.push(i) if board[i].nil?
|
91
|
+
end
|
92
|
+
open_spots
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module TictactoeJ8th
|
2
|
+
class Board
|
3
|
+
BOARD_SIZE = 9
|
4
|
+
BOARD_LINES = [
|
5
|
+
[0, 1, 2],
|
6
|
+
[3, 4, 5],
|
7
|
+
[6, 7, 8],
|
8
|
+
|
9
|
+
[0, 3, 6],
|
10
|
+
[1, 4, 7],
|
11
|
+
[2, 5, 8],
|
12
|
+
|
13
|
+
[0, 4, 8],
|
14
|
+
[2, 4, 6]
|
15
|
+
]
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@board = Array.new(BOARD_SIZE)
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty?
|
22
|
+
board.all? { |value| value.nil? }
|
23
|
+
end
|
24
|
+
|
25
|
+
def place(item, position)
|
26
|
+
board[position] = item if board[position].nil?
|
27
|
+
end
|
28
|
+
|
29
|
+
def full?
|
30
|
+
return false if board.include? nil
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](spot)
|
35
|
+
board[spot]
|
36
|
+
end
|
37
|
+
|
38
|
+
def lines
|
39
|
+
array = []
|
40
|
+
BOARD_LINES.each do |line|
|
41
|
+
hash = {}
|
42
|
+
line.each do |i|
|
43
|
+
hash[i] = board[i]
|
44
|
+
end
|
45
|
+
array.push(hash)
|
46
|
+
end
|
47
|
+
array
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_board(array)
|
51
|
+
@board = array
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_copy
|
55
|
+
copy = Board.new
|
56
|
+
copy.set_board(@board.dup)
|
57
|
+
copy
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
attr_accessor :board
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module TictactoeJ8th
|
2
|
+
class Game
|
3
|
+
|
4
|
+
attr_reader :board
|
5
|
+
|
6
|
+
def initialize(board, player1, player2)
|
7
|
+
@board = board
|
8
|
+
@player1 = player1
|
9
|
+
@player2 = player2
|
10
|
+
|
11
|
+
@playerup = @player1
|
12
|
+
end
|
13
|
+
|
14
|
+
def game_over?
|
15
|
+
return true if @board.full? or not winner.nil?
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
def winner
|
20
|
+
@board.lines.each do |line|
|
21
|
+
return @player1.token if line.values.all? { |v| v == @player1.token }
|
22
|
+
return @player2.token if line.values.all? { |v| v == @player2.token }
|
23
|
+
end
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def turn(spot = nil)
|
28
|
+
if spot.nil?
|
29
|
+
spot = @playerup.move(@board)
|
30
|
+
else
|
31
|
+
@board.place(@playerup.token, spot)
|
32
|
+
end
|
33
|
+
@playerup = @playerup == @player1 ? @player2 : @player1
|
34
|
+
spot
|
35
|
+
end
|
36
|
+
|
37
|
+
def play
|
38
|
+
turn
|
39
|
+
play unless game_over?
|
40
|
+
end
|
41
|
+
|
42
|
+
def player_up
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/spec/ai_spec.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'tictactoe_j8th/board'
|
2
|
+
require 'tictactoe_j8th/game'
|
3
|
+
require 'tictactoe_j8th/ai'
|
4
|
+
|
5
|
+
include TictactoeJ8th
|
6
|
+
|
7
|
+
describe 'AI' do
|
8
|
+
|
9
|
+
let(:board) { Board.new }
|
10
|
+
let(:ai1) { AI.new(:X) }
|
11
|
+
let(:ai2) { AI.new(:O) }
|
12
|
+
|
13
|
+
context '#initialize' do
|
14
|
+
it 'Sets a token for the AI.' do
|
15
|
+
ai = AI.new(:O)
|
16
|
+
expect(ai.token).to eq(:O)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context '#move' do
|
21
|
+
it 'makes a move on a board.' do
|
22
|
+
ai1.move(board)
|
23
|
+
expect(board.empty?).to eq(false)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'makes a move on a non-empty board' do
|
27
|
+
board.place(ai1.token, 0)
|
28
|
+
board.place(ai2.token, 4)
|
29
|
+
board.place(ai1.token, 2)
|
30
|
+
|
31
|
+
ai1.move(board)
|
32
|
+
|
33
|
+
tokens = []
|
34
|
+
(0..8).each do |i|
|
35
|
+
tokens.push(board[i]) unless board[i].nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
expect(tokens.count).to eq(4)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'blocks a win for the opposing player.' do
|
42
|
+
# No, this is a state where ai2 has already won, so it is not
|
43
|
+
# reasonable to expect the AI to move in space 8.
|
44
|
+
#board = Board.new
|
45
|
+
#board.place(ai2.token, 0)
|
46
|
+
#board.place(ai1.token, 1)
|
47
|
+
#board.place(ai2.token, 4)
|
48
|
+
#ai1.move(board)
|
49
|
+
#expect(board[8]).to eq(ai1.token)
|
50
|
+
|
51
|
+
# Nope, this one too!
|
52
|
+
#board.place(ai1.token, 0)
|
53
|
+
#board.place(ai2.token, 3)
|
54
|
+
#board.place(ai1.token, 1)
|
55
|
+
#ai2.move(board)
|
56
|
+
#expect(board[2]).to eq(ai2.token)
|
57
|
+
|
58
|
+
# Finally, a reasonable test.
|
59
|
+
board.place(ai1.token, 4)
|
60
|
+
board.place(ai2.token, 0)
|
61
|
+
board.place(ai1.token, 5)
|
62
|
+
ai2.move(board)
|
63
|
+
expect(board[3]).to eq(ai2.token)
|
64
|
+
# And another this test keeps going.
|
65
|
+
# Each move is a block, so we should reasonable expect 2 AI's
|
66
|
+
# to play the game out this way.
|
67
|
+
ai1.move(board)
|
68
|
+
expect(board[6]).to eq(ai1.token)
|
69
|
+
ai2.move(board)
|
70
|
+
expect(board[2]).to eq(ai2.token)
|
71
|
+
ai1.move(board)
|
72
|
+
expect(board[1]).to eq(ai1.token)
|
73
|
+
ai2.move(board)
|
74
|
+
expect(board[7]).to eq(ai2.token)
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'takes a win for itself' do
|
79
|
+
board.place(ai1.token, 0)
|
80
|
+
board.place(ai1.token, 1)
|
81
|
+
board.place(ai2.token, 3)
|
82
|
+
ai1.move(board)
|
83
|
+
expect(board[2]).to eq(ai1.token)
|
84
|
+
|
85
|
+
board = Board.new
|
86
|
+
board.place(ai1.token, 4)
|
87
|
+
board.place(ai1.token, 3)
|
88
|
+
board.place(ai2.token, 6)
|
89
|
+
ai1.move(board)
|
90
|
+
expect(board[5]).to eq(ai1.token)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'takes a win before it takes a block' do
|
94
|
+
board.place(ai1.token, 2)
|
95
|
+
board.place(ai1.token, 5)
|
96
|
+
board.place(ai2.token, 1)
|
97
|
+
board.place(ai2.token, 4)
|
98
|
+
ai1.move(board)
|
99
|
+
expect(board[8]).to eq(ai1.token)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'returns the spot where the ai chose to move' do
|
103
|
+
spot = ai1.move(board)
|
104
|
+
found = nil
|
105
|
+
(0..Board::BOARD_SIZE-1).each do |i|
|
106
|
+
found = i if board[i] == ai1.token
|
107
|
+
end
|
108
|
+
expect(spot).to eq(found)
|
109
|
+
end
|
110
|
+
|
111
|
+
# This test written to fix a bug where the spot moved was not returned if it was the last move of the game.
|
112
|
+
it 'returns the spot where the ai chose to move on the last move of the game' do
|
113
|
+
board.place(ai1.token, 4)
|
114
|
+
board.place(ai2.token, 1)
|
115
|
+
board.place(ai1.token, 0)
|
116
|
+
board.place(ai2.token, 2)
|
117
|
+
spot = ai1.move(board)
|
118
|
+
expect(spot).to eq(8)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
context 'Comprehensive AI Testing. (AI vs AI)', :slow => true do
|
124
|
+
it 'never loses. This means that the result of every game between two AI\'s is a draw.' do
|
125
|
+
100.times do
|
126
|
+
board = Board.new
|
127
|
+
ai1 = AI.new(:X)
|
128
|
+
ai2 = AI.new(:O)
|
129
|
+
game = Game.new(board, ai1, ai2)
|
130
|
+
game.play
|
131
|
+
expect(game.winner).to be_nil
|
132
|
+
expect(game.game_over?).to eq(true)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
data/spec/board_spec.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'tictactoe_j8th/board'
|
2
|
+
|
3
|
+
include TictactoeJ8th
|
4
|
+
|
5
|
+
def fill_board
|
6
|
+
9.times { |i| board.place(:X, i) }
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Board do
|
10
|
+
let(:board) { Board.new }
|
11
|
+
|
12
|
+
context '#place' do
|
13
|
+
it 'places the mark at position 0' do
|
14
|
+
board.place(:something, 0)
|
15
|
+
expect(board[0]).to eq(:something)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'places the mark at position 1 and returns true' do
|
19
|
+
expect(board.place(:something, 1)).to eq(:something)
|
20
|
+
expect(board[1]).to eq(:something)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'does not place the mark if taken and returns false' do
|
24
|
+
board.place(:something, 1)
|
25
|
+
expect(board.place(:something_else, 1)).to be_nil
|
26
|
+
expect(board[1]).to eq(:something)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#empty?' do
|
31
|
+
it 'returns true when the board is empty' do
|
32
|
+
expect(board.empty?).to eq(true)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'returns false when the board is not empty' do
|
36
|
+
board.place(:something, 0)
|
37
|
+
expect(board.empty?).to eq(false)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context '#full?' do
|
42
|
+
it 'returns false with a new board' do
|
43
|
+
expect(board.full?).to eq(false)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns true when the board is full.' do
|
47
|
+
fill_board
|
48
|
+
expect(board.full?).to eq(true)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'does NOT return true just because we fill the last spot on the board. (it returns false)' do
|
52
|
+
board.place(:X, 8)
|
53
|
+
expect(board.full?).to eq(false)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context '#[]' do
|
58
|
+
it 'Gets the value of the given spot' do
|
59
|
+
board.place('X', 0)
|
60
|
+
expect(board[0]).to eq('X')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context '#lines' do
|
65
|
+
it 'returns all the lines for a given board' do
|
66
|
+
board.place('X', 0)
|
67
|
+
board.place('X', 3)
|
68
|
+
board.place('O', 4)
|
69
|
+
expect(board.lines).to eq(
|
70
|
+
[
|
71
|
+
{0 => 'X', 1 => nil, 2 => nil},
|
72
|
+
{3 => 'X', 4 => 'O', 5 => nil},
|
73
|
+
{6 => nil, 7 => nil, 8 => nil},
|
74
|
+
|
75
|
+
{0 => 'X', 3 => 'X', 6 => nil},
|
76
|
+
{1 => nil, 4 => 'O', 7 => nil},
|
77
|
+
{2 => nil, 5 => nil, 8 => nil},
|
78
|
+
|
79
|
+
{0 => 'X', 4 => 'O', 8 => nil},
|
80
|
+
{2 => nil, 4 => 'O', 6 => nil}
|
81
|
+
]
|
82
|
+
)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/spec/game_spec.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'tictactoe_j8th/game'
|
2
|
+
require 'tictactoe_j8th/board'
|
3
|
+
require 'tictactoe_j8th/ai'
|
4
|
+
|
5
|
+
include TictactoeJ8th
|
6
|
+
|
7
|
+
def count_tokens(player, board)
|
8
|
+
tokens = []
|
9
|
+
(0..Board::BOARD_SIZE).each do |i|
|
10
|
+
tokens.push(board[i]) if board[i] == player.token
|
11
|
+
end
|
12
|
+
tokens.count
|
13
|
+
end
|
14
|
+
|
15
|
+
describe Game do
|
16
|
+
let(:board) { Board.new }
|
17
|
+
let(:player1) { AI.new(:X) }
|
18
|
+
let(:player2) { AI.new(:O) }
|
19
|
+
let(:game) { Game.new(board, player1, player2) }
|
20
|
+
|
21
|
+
context '#game_over?' do
|
22
|
+
it 'returns false for a game with a new board' do
|
23
|
+
expect(game.game_over?).to eq(false)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'returns true for a game with a winner' do
|
27
|
+
board.place(player1.token, 0)
|
28
|
+
board.place(player1.token, 1)
|
29
|
+
board.place(player1.token, 2)
|
30
|
+
|
31
|
+
expect(game.game_over?).to eq(true)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns true for a draw game' do
|
35
|
+
# X O X
|
36
|
+
# X O O
|
37
|
+
# O X X
|
38
|
+
board.place(player1.token, 0)
|
39
|
+
board.place(player2.token, 1)
|
40
|
+
board.place(player1.token, 2)
|
41
|
+
|
42
|
+
board.place(player1.token, 3)
|
43
|
+
board.place(player2.token, 4)
|
44
|
+
board.place(player2.token, 5)
|
45
|
+
|
46
|
+
board.place(player2.token, 6)
|
47
|
+
board.place(player1.token, 7)
|
48
|
+
board.place(player1.token, 8)
|
49
|
+
|
50
|
+
expect(game.game_over?).to eq(true)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context '#winner' do
|
55
|
+
it 'returns nil when there is no winner' do
|
56
|
+
expect(game.winner).to be_nil
|
57
|
+
end
|
58
|
+
|
59
|
+
[
|
60
|
+
[0, 1, 2],
|
61
|
+
[3, 4, 5],
|
62
|
+
[6, 7, 8],
|
63
|
+
|
64
|
+
[0, 3, 6],
|
65
|
+
[1, 4, 7],
|
66
|
+
[2, 5, 8],
|
67
|
+
|
68
|
+
[0, 4, 8],
|
69
|
+
[2, 4, 6]
|
70
|
+
].each do |(a, b, c)|
|
71
|
+
it ":X is the winner when its marks are at (#{a}, #{b}, #{c})" do
|
72
|
+
board.place(:X, a)
|
73
|
+
board.place(:X, b)
|
74
|
+
board.place(:X, c)
|
75
|
+
expect(game.winner).to eq(:X)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context '#turn' do
|
81
|
+
it 'makes a player move on the board' do
|
82
|
+
game.turn
|
83
|
+
expect(board.empty?).to eq(false)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'makes the player move in the given spot, if a spot is given' do
|
87
|
+
game.turn(3)
|
88
|
+
expect(board[3]).to eq(player1.token)
|
89
|
+
|
90
|
+
game.turn(1)
|
91
|
+
expect(board[1]).to eq(player2.token)
|
92
|
+
|
93
|
+
game.turn(5)
|
94
|
+
expect(board[5]).to eq(player1.token)
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'alternates players across invocations' do
|
98
|
+
game.turn
|
99
|
+
expect(count_tokens(player1, board)).to eq(1)
|
100
|
+
|
101
|
+
game.turn
|
102
|
+
expect(count_tokens(player2, board)).to eq(1)
|
103
|
+
|
104
|
+
game.turn
|
105
|
+
expect(count_tokens(player1, board)).to eq(2)
|
106
|
+
expect(count_tokens(player2, board)).to eq(1)
|
107
|
+
|
108
|
+
game.turn
|
109
|
+
expect(count_tokens(player1, board)).to eq(2)
|
110
|
+
expect(count_tokens(player2, board)).to eq(2)
|
111
|
+
end
|
112
|
+
|
113
|
+
# May be unnecessary, but very explicit?
|
114
|
+
it 'starts with player1' do
|
115
|
+
game.turn
|
116
|
+
expect(count_tokens(player1, board)).to eq(1)
|
117
|
+
expect(count_tokens(player2, board)).to eq(0)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'returns the spot where the player moved' do
|
121
|
+
spot = game.turn()
|
122
|
+
found = nil
|
123
|
+
(0..Board::BOARD_SIZE-1).each do |i|
|
124
|
+
found = i if not game.board[i].nil?
|
125
|
+
end
|
126
|
+
expect(spot).to eq(found)
|
127
|
+
|
128
|
+
game = Game.new(Board.new, AI.new(:X), AI.new(:O))
|
129
|
+
spot = game.turn(7)
|
130
|
+
expect(spot).to eq(7)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context '#play' do
|
135
|
+
it 'takes turns until the game is over' do
|
136
|
+
game.play
|
137
|
+
expect(game.game_over?).to eq(true)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
data/spec/human_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
RSpec.configure do |config|
|
18
|
+
# rspec-expectations config goes here. You can use an alternate
|
19
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
20
|
+
# assertions if you prefer.
|
21
|
+
config.expect_with :rspec do |expectations|
|
22
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
23
|
+
# and `failure_message` of custom matchers include text for helper methods
|
24
|
+
# defined using `chain`, e.g.:
|
25
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
26
|
+
# # => "be bigger than 2 and smaller than 4"
|
27
|
+
# ...rather than:
|
28
|
+
# # => "be bigger than 2"
|
29
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
30
|
+
end
|
31
|
+
|
32
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
33
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
34
|
+
config.mock_with :rspec do |mocks|
|
35
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
36
|
+
# a real object. This is generally recommended, and will default to
|
37
|
+
# `true` in RSpec 4.
|
38
|
+
mocks.verify_partial_doubles = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# The settings below are suggested to provide a good initial experience
|
42
|
+
# with RSpec, but feel free to customize to your heart's content.
|
43
|
+
=begin
|
44
|
+
# These two settings work together to allow you to limit a spec run
|
45
|
+
# to individual examples or groups you care about by tagging them with
|
46
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
47
|
+
# get run.
|
48
|
+
config.filter_run :focus
|
49
|
+
config.run_all_when_everything_filtered = true
|
50
|
+
|
51
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
52
|
+
# For more details, see:
|
53
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
54
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
55
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
56
|
+
config.disable_monkey_patching!
|
57
|
+
|
58
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
59
|
+
# be too noisy due to issues in dependencies.
|
60
|
+
config.warnings = true
|
61
|
+
|
62
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
63
|
+
# file, and it's useful to allow more verbose output when running an
|
64
|
+
# individual spec file.
|
65
|
+
if config.files_to_run.one?
|
66
|
+
# Use the documentation formatter for detailed output,
|
67
|
+
# unless a formatter has already been configured
|
68
|
+
# (e.g. via a command-line flag).
|
69
|
+
config.default_formatter = 'doc'
|
70
|
+
end
|
71
|
+
|
72
|
+
# Print the 10 slowest examples and example groups at the
|
73
|
+
# end of the spec run, to help surface which specs are running
|
74
|
+
# particularly slow.
|
75
|
+
config.profile_examples = 10
|
76
|
+
|
77
|
+
# Run specs in random order to surface order dependencies. If you find an
|
78
|
+
# order dependency and want to debug it, you can fix the order by providing
|
79
|
+
# the seed, which is printed after each run.
|
80
|
+
# --seed 1234
|
81
|
+
config.order = :random
|
82
|
+
|
83
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
84
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
85
|
+
# test failures related to randomization by passing the same `--seed` value
|
86
|
+
# as the one that triggered the failure.
|
87
|
+
Kernel.srand config.seed
|
88
|
+
=end
|
89
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tictactoe_j8th/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tictactoe_j8th"
|
8
|
+
spec.version = TictactoeJ8th::VERSION
|
9
|
+
spec.authors = ["j8th"]
|
10
|
+
spec.email = ["jgoodman@8thlight.com"]
|
11
|
+
spec.summary = %q{TicTacToe game logic with an unbeatable AI.}
|
12
|
+
spec.description = %q{A gem encapsulating the logic of a tic tac toe game with an unbeatable AI.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tictactoe_j8th
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- j8th
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
description: A gem encapsulating the logic of a tic tac toe game with an unbeatable
|
56
|
+
AI.
|
57
|
+
email:
|
58
|
+
- jgoodman@8thlight.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/tictactoe_j8th.rb
|
69
|
+
- lib/tictactoe_j8th/ai.rb
|
70
|
+
- lib/tictactoe_j8th/board.rb
|
71
|
+
- lib/tictactoe_j8th/game.rb
|
72
|
+
- lib/tictactoe_j8th/human.rb
|
73
|
+
- lib/tictactoe_j8th/version.rb
|
74
|
+
- spec/ai_spec.rb
|
75
|
+
- spec/board_spec.rb
|
76
|
+
- spec/game_spec.rb
|
77
|
+
- spec/human_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- tictactoe_j8th.gemspec
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.0.14
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: TicTacToe game logic with an unbeatable AI.
|
104
|
+
test_files:
|
105
|
+
- spec/ai_spec.rb
|
106
|
+
- spec/board_spec.rb
|
107
|
+
- spec/game_spec.rb
|
108
|
+
- spec/human_spec.rb
|
109
|
+
- spec/spec_helper.rb
|