tic_tac_toe_core 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/tic_tac_toe_core/board.rb +76 -0
- data/lib/tic_tac_toe_core/computer.rb +43 -0
- data/lib/tic_tac_toe_core/game.rb +54 -0
- data/lib/tic_tac_toe_core/version.rb +3 -0
- data/spec/tic_tac_toe_core/board_spec.rb +116 -0
- data/spec/tic_tac_toe_core/computer_spec.rb +73 -0
- data/spec/tic_tac_toe_core/fake_board.rb +4 -0
- data/spec/tic_tac_toe_core/fake_display.rb +48 -0
- data/spec/tic_tac_toe_core/fake_game.rb +10 -0
- data/spec/tic_tac_toe_core/game_spec.rb +101 -0
- data/tic_tac_toe_core.gemspec +22 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 561bd042ce97f48a28503b8340f73243d50d3e8e
|
4
|
+
data.tar.gz: 92580045a03df0f7800a0d9ef0de1e4f38a6b053
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a18da915b0d2fdbd4fe74d98e3ebe7d1bda12189c75179ca67427e6f3f70c448c765ee1dd7979c35506dde675aa8e95b6f9a895e187459d65dcc6e744aea66b
|
7
|
+
data.tar.gz: c72b431903176ef08d91cfaab082bee63aa2a9b9997197f74563b0d2a4fa37f9b34caf7267bf0775e437663a124a70854a6c275e894c6638042734eeecb2fe29
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Makis Otman
|
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,29 @@
|
|
1
|
+
# TicTacToeCore
|
2
|
+
|
3
|
+
This gem contains the core functionality for a basic Tic-Tac-Toe game. It can be used with an interface for games between two human players, human vs computer or computer vs computer. The computer player uses the Minimax algorithm to determine the best move possible thus making it unbeatable.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'TicTacToeCore'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install TicTacToeCore
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/TicTacToeCore/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module TicTacToeCore
|
2
|
+
class Board
|
3
|
+
attr_reader :grid
|
4
|
+
|
5
|
+
BOARD_SIZE = 3
|
6
|
+
|
7
|
+
def initialize(grid = default_grid(BOARD_SIZE))
|
8
|
+
@grid = grid
|
9
|
+
end
|
10
|
+
|
11
|
+
def mark_position(position, mark)
|
12
|
+
raise 'Position already taken' if !grid.include?(position)
|
13
|
+
grid[position - 1] = mark
|
14
|
+
end
|
15
|
+
|
16
|
+
def draw?
|
17
|
+
available_moves.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def winner?
|
21
|
+
winning_combinations.any? { |combo| combo.all? { |cell| cell == combo.first } }
|
22
|
+
end
|
23
|
+
|
24
|
+
def available_moves
|
25
|
+
grid.select { |cell| cell.is_a? Fixnum }
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_mark
|
29
|
+
available_moves.length.odd? ? 'X' : 'O'
|
30
|
+
end
|
31
|
+
|
32
|
+
def last_move_mark
|
33
|
+
current_mark == 'X' ? 'O' : 'X'
|
34
|
+
end
|
35
|
+
|
36
|
+
def game_over?
|
37
|
+
winner? || available_moves.empty?
|
38
|
+
end
|
39
|
+
|
40
|
+
def reset_value(value)
|
41
|
+
grid[value - 1] = value
|
42
|
+
end
|
43
|
+
|
44
|
+
def win_for(mark)
|
45
|
+
winning_combinations.any? { |combo| combo.all? { |cell| cell == mark } }
|
46
|
+
end
|
47
|
+
|
48
|
+
def reset
|
49
|
+
grid.each_with_index do |move, index|
|
50
|
+
grid[index] = index + 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def winning_combinations
|
57
|
+
rows + columns + diagonals
|
58
|
+
end
|
59
|
+
|
60
|
+
def rows
|
61
|
+
grid.each_slice(BOARD_SIZE).to_a
|
62
|
+
end
|
63
|
+
|
64
|
+
def columns
|
65
|
+
rows.transpose
|
66
|
+
end
|
67
|
+
|
68
|
+
def diagonals
|
69
|
+
[] << rows.map.with_index { |row, index| row[index] } << rows.reverse.map.with_index { |row, index| row[index] }
|
70
|
+
end
|
71
|
+
|
72
|
+
def default_grid(size)
|
73
|
+
(1..size * size).to_a
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module TicTacToeCore
|
2
|
+
class Computer
|
3
|
+
attr_reader :mark
|
4
|
+
|
5
|
+
def choose_mark(board)
|
6
|
+
board.available_moves.length.odd? ? @mark = 'X' : @mark = 'O'
|
7
|
+
end
|
8
|
+
|
9
|
+
def make_move(board)
|
10
|
+
best_move = minimax(board)
|
11
|
+
board.mark_position(best_move, mark)
|
12
|
+
end
|
13
|
+
|
14
|
+
def minimax(board, depth = 0, score = {})
|
15
|
+
return 10 if board.win_for(mark)
|
16
|
+
return -10 if board.win_for(opponent)
|
17
|
+
return 0 if board.draw?
|
18
|
+
|
19
|
+
board.available_moves.each do |move|
|
20
|
+
board.mark_position(move, board.current_mark)
|
21
|
+
score[move] = minimax(board, depth += 1, {})
|
22
|
+
board.reset_value(move)
|
23
|
+
end
|
24
|
+
|
25
|
+
if depth == board.available_moves.length
|
26
|
+
move = score.max_by { |move, score| score }[0]
|
27
|
+
return move
|
28
|
+
end
|
29
|
+
|
30
|
+
if board.current_mark == mark
|
31
|
+
score.max_by { |move, score| score }[1]
|
32
|
+
else
|
33
|
+
score.min_by { |move, score| score }[1]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def opponent
|
40
|
+
@mark == 'X' ? 'O' : 'X'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'tic_tac_toe_core/board'
|
2
|
+
require 'tic_tac_toe_core/computer'
|
3
|
+
|
4
|
+
module TicTacToeCore
|
5
|
+
class Game
|
6
|
+
attr_reader :board, :computer
|
7
|
+
|
8
|
+
def initialize(board = Board.new, computer = Computer.new)
|
9
|
+
@board = board
|
10
|
+
@computer = computer
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_winner?
|
14
|
+
board.winner?
|
15
|
+
end
|
16
|
+
|
17
|
+
def is_draw?
|
18
|
+
board.draw?
|
19
|
+
end
|
20
|
+
|
21
|
+
def is_over?
|
22
|
+
board.game_over?
|
23
|
+
end
|
24
|
+
|
25
|
+
def play_next_move(input)
|
26
|
+
board.mark_position(input, board.current_mark)
|
27
|
+
end
|
28
|
+
|
29
|
+
def computer_makes_move
|
30
|
+
computer.choose_mark(board)
|
31
|
+
computer.make_move(board)
|
32
|
+
end
|
33
|
+
|
34
|
+
def reset
|
35
|
+
board.reset
|
36
|
+
end
|
37
|
+
|
38
|
+
def board_grid
|
39
|
+
board.grid
|
40
|
+
end
|
41
|
+
|
42
|
+
def current_mark
|
43
|
+
board.current_mark
|
44
|
+
end
|
45
|
+
|
46
|
+
def last_move_mark
|
47
|
+
board.last_move_mark
|
48
|
+
end
|
49
|
+
|
50
|
+
def valid_move?(move)
|
51
|
+
board.available_moves.include?(move)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'tic_tac_toe_core/board'
|
2
|
+
|
3
|
+
describe TicTacToeCore::Board do
|
4
|
+
let(:board) { TicTacToeCore::Board.new }
|
5
|
+
let(:grid_with_one_move) { ['X', 2, 3, 4, 5, 6, 7, 8, 9] }
|
6
|
+
|
7
|
+
describe '#mark_position' do
|
8
|
+
it 'marks a position on grid with an X' do
|
9
|
+
board.mark_position(1, 'X')
|
10
|
+
expect(board.grid).to eq grid_with_one_move
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'raises an error if the position is taken' do
|
14
|
+
board = TicTacToeCore::Board.new(grid_with_one_move)
|
15
|
+
expect{ board.mark_position(1, 'O') }.to raise_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#winner' do
|
20
|
+
[['X', 'X', 'X', 4, 5, 6, 7, 8, 9],
|
21
|
+
[1, 2, 3, 'X', 'X', 'X', 7, 8, 9],
|
22
|
+
[1, 2, 3, 4, 5, 6, 'X', 'X', 'X'],
|
23
|
+
['X', 2, 3, 'X', 5, 6, 'X', 8, 9],
|
24
|
+
[1, 'X', 3, 4, 'X', 6, 7, 'X', 9],
|
25
|
+
[1, 2, 'X', 4, 5, 'X', 7, 8, 'X'],
|
26
|
+
['X', 2, 3, 4, 'X', 6, 7, 8, 'X'],
|
27
|
+
[1, 2, 'X', 4, 'X', 6, 'X', 8, 9]].each do |combo|
|
28
|
+
it "returns true if there's a win" do
|
29
|
+
board = TicTacToeCore::Board.new(combo)
|
30
|
+
expect(board.winner?).to eq true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns false if there's no win" do
|
35
|
+
expect(board.winner?).to eq false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#available_moves' do
|
40
|
+
it 'returns the available moves from the board' do
|
41
|
+
board = TicTacToeCore::Board.new(['X', 'O', 3, 'X', 5, 6, 7, 'X', 9])
|
42
|
+
expect(board.available_moves).to eq [3, 5, 6, 7, 9]
|
43
|
+
another_board = TicTacToeCore::Board.new(grid_with_one_move)
|
44
|
+
expect(another_board.available_moves).to eq [2, 3, 4, 5, 6, 7, 8, 9]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#current_mark' do
|
49
|
+
it 'returns X if no move was made on the board' do
|
50
|
+
expect(board.current_mark).to eq 'X'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns O if a move was made on the board' do
|
54
|
+
board = TicTacToeCore::Board.new(grid_with_one_move)
|
55
|
+
expect(board.current_mark).to eq 'O'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#game_over?' do
|
60
|
+
it 'returns true if the board has a winner' do
|
61
|
+
board = TicTacToeCore::Board.new(['X', 'X', 'X', 4, 5, 6, 7, 8, 9])
|
62
|
+
expect(board.game_over?).to eq true
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns true if the board has no available moves' do
|
66
|
+
board = TicTacToeCore::Board.new(['X', 'O', 'X', 'O', 'X', 'O', 'O', 'X', 'O'])
|
67
|
+
expect(board.game_over?).to eq true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#last_mark' do
|
72
|
+
it "returns X if it's O turn to make a move" do
|
73
|
+
board = TicTacToeCore::Board.new(grid_with_one_move)
|
74
|
+
expect(board.last_move_mark).to eq 'X'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns O if it's X turn to make a move" do
|
78
|
+
board = TicTacToeCore::Board.new(['X', 2, 'O', 4, 5, 6, 7, 8, 9])
|
79
|
+
expect(board.last_move_mark).to eq 'O'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#reset_value' do
|
84
|
+
it 'resets the value at the given cell' do
|
85
|
+
board = TicTacToeCore::Board.new(grid_with_one_move)
|
86
|
+
board.reset_value(1)
|
87
|
+
expect(board.grid).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe '#win_for' do
|
92
|
+
it 'returns true if the mark given has won' do
|
93
|
+
board = TicTacToeCore::Board.new([1, 'X', 'X', 4, 5, 6, 7, 8, 9])
|
94
|
+
expect(board.win_for('X')).to eq false
|
95
|
+
board.mark_position(1, 'X')
|
96
|
+
expect(board.win_for('X')).to eq true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#draw' do
|
101
|
+
it 'returns true if there are no moves left' do
|
102
|
+
board = TicTacToeCore::Board.new(['X', 2, 'O', 'O', 'X', 'X', 'X', 'O', 'O'])
|
103
|
+
expect(board.draw?).to eq false
|
104
|
+
board.mark_position(2, 'X')
|
105
|
+
expect(board.draw?).to eq true
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#reset' do
|
110
|
+
it 'resets the board' do
|
111
|
+
board = TicTacToeCore::Board.new(['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 'O'])
|
112
|
+
board.reset
|
113
|
+
expect(board.grid).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'tic_tac_toe_core/computer'
|
2
|
+
|
3
|
+
describe TicTacToeCore::Computer do
|
4
|
+
describe '#choose_mark' do
|
5
|
+
it 'chooses O if available_moves is even' do
|
6
|
+
board = TicTacToeCore::Board.new(['X', 2, 3, 4, 5, 6, 7, 8, 9])
|
7
|
+
computer = TicTacToeCore::Computer.new
|
8
|
+
computer.choose_mark(board)
|
9
|
+
expect(computer.mark).to eq 'O'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'chooses X if available_moves is odd' do
|
13
|
+
board = TicTacToeCore::Board.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
14
|
+
computer = TicTacToeCore::Computer.new
|
15
|
+
computer.choose_mark(board)
|
16
|
+
expect(computer.mark).to eq 'X'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#make_move' do
|
21
|
+
it 'makes a move on the board' do
|
22
|
+
board = TicTacToeCore::Board.new(['X', 2, 3, 'X', 'O', 6, 7, 8, 9])
|
23
|
+
computer = TicTacToeCore::Computer.new
|
24
|
+
computer.choose_mark(board)
|
25
|
+
computer.make_move(board)
|
26
|
+
expect(board.grid).to eq ['X', 2, 3, 'X', 'O', 6, 'O', 8, 9]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#minimax' do
|
31
|
+
it 'returns winning move' do
|
32
|
+
board = TicTacToeCore::Board.new(['X', 'X', 3, 'O', 5, 6, 'O', 8, 9])
|
33
|
+
computer = TicTacToeCore::Computer.new
|
34
|
+
computer.choose_mark(board)
|
35
|
+
expect(computer.minimax(board)).to eq 3
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'returns move preventing loss' do
|
39
|
+
board = TicTacToeCore::Board.new([1, 'O', 3, 'X', 5, 6, 'X', 8, 9])
|
40
|
+
computer = TicTacToeCore::Computer.new
|
41
|
+
computer.choose_mark(board)
|
42
|
+
expect(computer.minimax(board)).to eq 1
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns move preventing diagonal fork' do
|
46
|
+
board = TicTacToeCore::Board.new(['X', 2, 3, 4, 'O', 6, 7, 8, 'X'])
|
47
|
+
computer = TicTacToeCore::Computer.new
|
48
|
+
computer.choose_mark(board)
|
49
|
+
expect(computer.minimax(board)).to eq 2
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns move preventing alternative diagonal fork' do
|
53
|
+
board = TicTacToeCore::Board.new(['O', 2, 3, 4, 'X', 6, 7, 8, 'X'])
|
54
|
+
computer = TicTacToeCore::Computer.new
|
55
|
+
computer.choose_mark(board)
|
56
|
+
expect(computer.minimax(board)).to eq 3
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns move preventing edge trap' do
|
60
|
+
board = TicTacToeCore::Board.new([1, 'X', 3, 'X', 'O', 6, 7, 8, 9])
|
61
|
+
computer = TicTacToeCore::Computer.new
|
62
|
+
computer.choose_mark(board)
|
63
|
+
expect(computer.minimax(board)).to eq 1
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'returns move preventing reverse edge trap' do
|
67
|
+
board = TicTacToeCore::Board.new([1, 2, 3, 4, 'O', 'X', 7, 'X', 9])
|
68
|
+
computer = TicTacToeCore::Computer.new
|
69
|
+
computer.choose_mark(board)
|
70
|
+
expect(computer.minimax(board)).to eq 3
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module TicTacToeCore
|
2
|
+
|
3
|
+
class FakeDisplay
|
4
|
+
|
5
|
+
def initialize(moves)
|
6
|
+
@moves = moves
|
7
|
+
end
|
8
|
+
|
9
|
+
def ask_for_game_type
|
10
|
+
@moves.shift
|
11
|
+
end
|
12
|
+
|
13
|
+
def greet_players
|
14
|
+
end
|
15
|
+
|
16
|
+
def print_winning_message_for(mark)
|
17
|
+
end
|
18
|
+
|
19
|
+
def show_board(grid)
|
20
|
+
end
|
21
|
+
|
22
|
+
def ask_for_move(mark)
|
23
|
+
@moves.shift
|
24
|
+
end
|
25
|
+
|
26
|
+
def invalid_move_message
|
27
|
+
end
|
28
|
+
|
29
|
+
def another_round?
|
30
|
+
end
|
31
|
+
|
32
|
+
def print_farewell_message
|
33
|
+
end
|
34
|
+
|
35
|
+
def print_draw_message
|
36
|
+
end
|
37
|
+
|
38
|
+
def play_again?
|
39
|
+
end
|
40
|
+
|
41
|
+
def computer_goes_first?(choice)
|
42
|
+
choice == '2'
|
43
|
+
end
|
44
|
+
|
45
|
+
def clear_screen
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'tic_tac_toe_core/game'
|
2
|
+
|
3
|
+
describe TicTacToeCore::Game do
|
4
|
+
let(:board) { TicTacToeCore::Board.new }
|
5
|
+
let(:game) { TicTacToeCore::Game.new(board) }
|
6
|
+
let(:winning_grid) { ['X', 'X', 'X', 4, 5, 6, 7, 8, 9] }
|
7
|
+
let(:full_grid) { ['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 'O'] }
|
8
|
+
|
9
|
+
describe '#has_winner?' do
|
10
|
+
it 'returns true if there is a winner' do
|
11
|
+
board = TicTacToeCore::Board.new(winning_grid)
|
12
|
+
game = TicTacToeCore::Game.new(board)
|
13
|
+
expect(game.has_winner?).to eq true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns false if there is no winner' do
|
17
|
+
expect(game.has_winner?).to eq false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#is_draw?' do
|
22
|
+
it 'returns true if the game is a draw' do
|
23
|
+
board = TicTacToeCore::Board.new(full_grid)
|
24
|
+
game = TicTacToeCore::Game.new(board)
|
25
|
+
expect(game.is_draw?).to eq true
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns false if the game is not a draw' do
|
29
|
+
expect(game.is_draw?).to eq false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#is_over?' do
|
34
|
+
it 'returns true if the game has reached an end state' do
|
35
|
+
board = TicTacToeCore::Board.new(winning_grid)
|
36
|
+
game = TicTacToeCore::Game.new(board)
|
37
|
+
expect(game.is_over?).to eq true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'returns false if the game has not reached an end state' do
|
41
|
+
expect(game.is_over?).to eq false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#play_next_move' do
|
46
|
+
it 'marks the given position on the board' do
|
47
|
+
game.play_next_move(1)
|
48
|
+
expect(board.grid).to eq ['X', 2, 3, 4, 5, 6, 7, 8, 9]
|
49
|
+
game.play_next_move(5)
|
50
|
+
expect(board.grid).to eq ['X', 2, 3, 4, 'O', 6, 7, 8, 9]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#computer_makes_move' do
|
55
|
+
it 'computer makes a move on the board' do
|
56
|
+
board = TicTacToeCore::Board.new(['X', 'X', 3, 4, 'O', 6, 7, 8, 9])
|
57
|
+
game = TicTacToeCore::Game.new(board)
|
58
|
+
game.computer_makes_move
|
59
|
+
expect(board.grid).to eq ['X', 'X', 'O', 4, 'O', 6, 7, 8, 9]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#reset' do
|
64
|
+
it 'resets the board' do
|
65
|
+
board = TicTacToeCore::Board.new(full_grid)
|
66
|
+
game = TicTacToeCore::Game.new(board)
|
67
|
+
game.reset
|
68
|
+
expect(board.grid).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#board_grid' do
|
73
|
+
it "returns the board's grid" do
|
74
|
+
expect(game.board_grid).to eq board.grid
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#current_mark' do
|
79
|
+
it 'returns the current mark' do
|
80
|
+
expect(game.current_mark).to eq 'X'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#last_move_mark' do
|
85
|
+
it 'returns the mark of the last move made' do
|
86
|
+
board = TicTacToeCore::Board.new(['X', 2, 3, 4, 5, 6, 7, 8, 9])
|
87
|
+
game = TicTacToeCore::Game.new(board)
|
88
|
+
expect(game.last_move_mark).to eq 'X'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#valid_move?' do
|
93
|
+
it 'returns true if the move is valid' do
|
94
|
+
expect(game.valid_move?(1)).to eq true
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'returns false if move is not valid' do
|
98
|
+
expect(game.valid_move?('X')).to eq false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tic_tac_toe_core/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tic_tac_toe_core"
|
8
|
+
spec.version = TicTacToeCore::VERSION
|
9
|
+
spec.authors = ["Makis Otman"]
|
10
|
+
spec.email = ["makisotman@gmail.com"]
|
11
|
+
spec.summary = %q{Core functionality of the TicTacToe Game which can be used with a variety of interfaces.}
|
12
|
+
spec.description = %q{This gem contains the core functionality for a basic Tic-Tac-Toe game. It can be used with an interface for games between two human players, human vs computer or computer vs computer. The computer player uses the Minimax algorithm to determine the best move possible thus making it unbeatable.}
|
13
|
+
spec.homepage = "https://github.com/Maikon/tic_tac_toe_core"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tic_tac_toe_core
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Makis Otman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-16 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
description: This gem contains the core functionality for a basic Tic-Tac-Toe game.
|
42
|
+
It can be used with an interface for games between two human players, human vs computer
|
43
|
+
or computer vs computer. The computer player uses the Minimax algorithm to determine
|
44
|
+
the best move possible thus making it unbeatable.
|
45
|
+
email:
|
46
|
+
- makisotman@gmail.com
|
47
|
+
executables: []
|
48
|
+
extensions: []
|
49
|
+
extra_rdoc_files: []
|
50
|
+
files:
|
51
|
+
- ".gitignore"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/tic_tac_toe_core/board.rb
|
57
|
+
- lib/tic_tac_toe_core/computer.rb
|
58
|
+
- lib/tic_tac_toe_core/game.rb
|
59
|
+
- lib/tic_tac_toe_core/version.rb
|
60
|
+
- spec/tic_tac_toe_core/board_spec.rb
|
61
|
+
- spec/tic_tac_toe_core/computer_spec.rb
|
62
|
+
- spec/tic_tac_toe_core/fake_board.rb
|
63
|
+
- spec/tic_tac_toe_core/fake_display.rb
|
64
|
+
- spec/tic_tac_toe_core/fake_game.rb
|
65
|
+
- spec/tic_tac_toe_core/game_spec.rb
|
66
|
+
- tic_tac_toe_core.gemspec
|
67
|
+
homepage: https://github.com/Maikon/tic_tac_toe_core
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.3.0
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Core functionality of the TicTacToe Game which can be used with a variety
|
91
|
+
of interfaces.
|
92
|
+
test_files:
|
93
|
+
- spec/tic_tac_toe_core/board_spec.rb
|
94
|
+
- spec/tic_tac_toe_core/computer_spec.rb
|
95
|
+
- spec/tic_tac_toe_core/fake_board.rb
|
96
|
+
- spec/tic_tac_toe_core/fake_display.rb
|
97
|
+
- spec/tic_tac_toe_core/fake_game.rb
|
98
|
+
- spec/tic_tac_toe_core/game_spec.rb
|
99
|
+
has_rdoc:
|