tictactoe_tracypholmes 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25d5c24b29e9ab49966ef65b773f918d293fa91e
4
- data.tar.gz: 18abd79386e8aef4ba2a173371dc0fa6511c7f97
3
+ metadata.gz: 5f84355db22f944b713ead6d2b9a3f962ad4b5d5
4
+ data.tar.gz: 60110fd2f554ee92d8da79f002155026d045d8be
5
5
  SHA512:
6
- metadata.gz: e2af2ed37091a770ae530addfdb7990b1586b465530fca92ed01864a06e22203ca2313c1b805b4e6504306a214065d5b4dc08498b5da9a90e242d83392fab3fe
7
- data.tar.gz: 5edbfc3ef5ae32f1875f25147d9f1d78db90c5d71fb2edc9874f2c3e61f2e7a3af0d6cbaa6c6cbfcd326f381097e2ed2ba9992a262bfd714815a0a579a2a39d2
6
+ metadata.gz: 06e6642fa16ba23c9f0bc987d10b930e91bd48d8cfd3a6f5645fabf9211466d5cdab7aa25c240ce47a87933f0cd448a749753f8451fce0924d851c3231cd061d
7
+ data.tar.gz: 9ca64fda4dadd62ea349dd9a4cf14a6e24b5af65d277dc2b38f073143996187c741b4a1e01d5721562766a01367a41bf77152e4b86c6daa570bb333cbbe0916c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tracyholmes@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tictactoe_tracypholmes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 tracypholmes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # TictactoeTracypholmes
2
+
3
+
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'tictactoe_tracypholmes'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install tictactoe_tracypholmes
21
+
22
+ ## Usage
23
+
24
+ To play, run `tictactoe_tracypholmes`
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/tictactoe_tracypholmes` for an interactive prompt that will allow you to experiment.
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/tictactoe` for an interactive prompt that will allow you to experiment.
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tracypholmes/tictactoe_tracypholmes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
37
+
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ # task :default => :spec
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -1,4 +1,8 @@
1
1
  require 'tictactoe_tracypholmes/version'
2
+ require 'tictactoe_tracypholmes/players/computer'
3
+ require 'tictactoe_tracypholmes/players/human'
4
+ require 'tictactoe_tracypholmes/board'
5
+ require 'tictactoe_tracypholmes/player'
2
6
  require 'tictactoe_tracypholmes/game'
3
7
  require 'colorize'
4
8
 
@@ -0,0 +1,65 @@
1
+ class Board
2
+ attr_accessor :cells
3
+
4
+ def initialize()
5
+ reset!
6
+ end
7
+
8
+ def reset!
9
+ @cells = Array.new(9, " ")
10
+ end
11
+
12
+ def display
13
+ puts " #{cells[0]} | #{cells[1]} | #{cells[2]} "
14
+ puts '-----------'
15
+ puts " #{cells[3]} | #{cells[4]} | #{cells[5]} "
16
+ puts '-----------'
17
+ puts " #{cells[6]} | #{cells[7]} | #{cells[8]} "
18
+ puts "\n"
19
+ end
20
+
21
+ def input_index(input) # this one line is all over the place!
22
+ input.to_i - 1
23
+ end
24
+
25
+ # position method
26
+ def position(input)
27
+ @cells[input_index(input)]
28
+ end
29
+
30
+ # define full here - every element on the board contains "X" or "O"
31
+ def full?
32
+ @cells.all? do |mark|
33
+ mark == 'X' || mark == 'O'
34
+ end
35
+ end
36
+
37
+ # define turn_count here
38
+ def turn_count
39
+ counter = 0
40
+ @cells.each do |occupied_spot|
41
+ counter += 1 if occupied_spot != ' '
42
+ end
43
+ counter
44
+ end
45
+
46
+ # taken? method
47
+ def taken?(input)
48
+ exxo = position(input)
49
+ exxo != " " ? true:false
50
+ end
51
+
52
+ # valid_move? method here
53
+ def valid_move?(input)
54
+ if position(input) == " " && input.to_i.between?(1, 9)
55
+ true # input on the board && input not taken
56
+ else
57
+ false # return false or nil here for invalid move
58
+ end
59
+ end
60
+
61
+ # define update
62
+ def update(input, player)
63
+ @cells[input_index(input)] = player.token
64
+ end
65
+ end
@@ -0,0 +1,91 @@
1
+ require 'tictactoe_tracypholmes/version'
2
+ require 'tictactoe_tracypholmes/players/computer'
3
+ require 'tictactoe_tracypholmes/players/human'
4
+ require 'tictactoe_tracypholmes/board'
5
+ require 'tictactoe_tracypholmes/player'
6
+ require 'tictactoe_tracypholmes'
7
+ require 'colorize'
8
+
9
+ module TictactoeTracypholmes
10
+ class Game
11
+ WIN_COMBINATIONS = [
12
+ [0, 1, 2],
13
+ [3, 4, 5],
14
+ [6, 7, 8],
15
+ [0, 3, 6],
16
+ [1, 4, 7],
17
+ [2, 5, 8],
18
+ [0, 4, 8],
19
+ [6, 4, 2]
20
+ ].freeze
21
+
22
+ attr_accessor :board, :player_1, :player_2
23
+
24
+ # need to add name somewhere in the initialization?
25
+
26
+ def initialize(player_1 = Players::Human.new('X'), player_2 = Players::Human.new('O'), board = Board.new)
27
+ @player_1 = player_1
28
+ @player_2 = player_2
29
+ @board = board
30
+ end
31
+
32
+ # define current_player here
33
+ def current_player
34
+ @board.turn_count.even? ? player_1 : player_2
35
+ end
36
+
37
+ # over? here - won, is a draw, or full
38
+ def over?
39
+ won? || draw?
40
+ end
41
+
42
+ def won?
43
+ WIN_COMBINATIONS.detect do |win_combo|
44
+ if @board.cells[win_combo[0]] == @board.cells[win_combo[1]] &&
45
+ @board.cells[win_combo[1]] == @board.cells[win_combo[2]] &&
46
+ (@board.cells[win_combo[0]] == 'X' || @board.cells[win_combo[0]] == 'O')
47
+ return win_combo
48
+ else
49
+ false
50
+ end
51
+ end
52
+ end
53
+
54
+ # define draw? here
55
+ def draw?
56
+ !won? && board.full?
57
+ end
58
+
59
+ # define winner here
60
+ def winner
61
+ if win_combo = won?
62
+ @board.cells[win_combo.first]
63
+ end
64
+ end
65
+
66
+ # define turn method here
67
+ def turn # work on this dadgum method
68
+ move = current_player.move(@board)
69
+ unless board.valid_move?(move)
70
+ puts 'NOT a valid move. Play again, please!'.red
71
+ turn
72
+ end
73
+ @board.update(move, current_player)
74
+ end
75
+
76
+ # define #play here
77
+ def play
78
+ until over? # until the game is over
79
+ @board.display
80
+ turn # take turns
81
+ end
82
+ if won?
83
+ @board.display
84
+ puts "Congratulations #{winner}!".green
85
+ elsif draw?
86
+ @board.display
87
+ puts "Cat's Game!".yellow
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,9 @@
1
+ module TictactoeTracypholmes
2
+ class Player
3
+ attr_reader :token # it 'cannot be changed once assigned in initialize'
4
+
5
+ def initialize(token)
6
+ @token = token
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ require 'tictactoe_tracypholmes/board'
2
+ require 'tictactoe_tracypholmes/player'
3
+
4
+
5
+ module TictactoeTracypholmes
6
+ module Players
7
+ class Players::Computer < Player # represents a computer player of Tic Tac Toe. Implement a #move method that accepts a board and returns the move the computer wants to make in the form of a 1-9 string.
8
+
9
+
10
+ def move(board)
11
+ move = nil
12
+
13
+ # Center move if possible
14
+ if !board.taken?(5)
15
+ move = '5'
16
+ # If player2 and the scoundrel took 5, goto the first position
17
+ elsif board.turn_count == 1
18
+ move = '1'
19
+ # Go for the corners! Presumes you went first
20
+ elsif board.turn_count == 2
21
+ move = [1, 3, 7, 9].find{|i| !board.taken?(i)}.to_s
22
+
23
+ # Player2 side strategy
24
+ elsif board.turn_count == 3 && (board.position(1) == board.position(9) || board.position(3) == board.position(7))
25
+ move = "2"
26
+
27
+ # You worked hard on these combinations - let's see who has the best combos thus far
28
+ else Game::WIN_COMBINATIONS.find do |c|
29
+
30
+ # Do I have a winner?
31
+ if c.select{|i| board.position(i+1) == token}.size == 2 && c.any?{|i| board.position(i+1) == " "}
32
+ move = c.select{|i| !board.taken?(i+1)}.first.to_i.+(1).to_s
33
+
34
+ # No winner - let's block the other guy
35
+ elsif c.select{|i| board.position(i+1) != " " && board.position(i+1) != token}.size == 2 && c.any?{|i| board.position(i+1) == " "}
36
+ move = c.select{|i| !board.taken?(i+1)}.first.to_i.+(1).to_s
37
+ end
38
+ end
39
+
40
+ # No one has a winner, let's play out the corners and sides if possible
41
+ move = [1, 3, 7, 9, 2, 4, 6, 8].find{|i| !board.taken?(i)}.to_s if move == nil
42
+ end
43
+ move
44
+
45
+ # input = (1..9).to_a.sample
46
+ # input.to_s
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ require 'tictactoe_tracypholmes/player'
2
+
3
+ module TictactoeTracypholmes
4
+ module Players
5
+ class Human < Player
6
+ # move method
7
+
8
+ attr_accessor :input
9
+
10
+ def move(board)
11
+ puts 'Please make a move by entering a number from 1 - 9.'
12
+ gets.chomp
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module TictactoeTracypholmes
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Board" do
4
+ let(:board){Board.new}
5
+
6
+ describe '#cells' do
7
+ it 'has an attribute to store the cells of the board' do
8
+ expect(board).to respond_to(:cells)
9
+ expect(board).to respond_to(:cells=)
10
+
11
+ board.cells = []
12
+ expect(board.cells).to match_array([])
13
+ end
14
+ end
15
+
16
+ describe '#reset!' do
17
+ it 'can reset the state of the cells in the board' do
18
+ expect(board).to respond_to(:reset!)
19
+ board.cells = ["X"]
20
+ board.reset!
21
+ expect(board.cells).to_not match_array(["X"])
22
+ end
23
+ it 'sets the cells of the board to a 9 element array of " "' do
24
+ board.cells = ["X"]
25
+ board.reset!
26
+ expect(board.cells).to match_array(Array.new(9, " "))
27
+ end
28
+ end
29
+
30
+ describe '#initialize' do
31
+ it 'sets the cells of the board to a 9 element array of " "' do
32
+ board = Board.new
33
+ expect(board.cells).to match_array(Array.new(9, " "))
34
+ end
35
+ end
36
+
37
+ describe '#display' do
38
+ it 'prints the board' do
39
+ board.cells = ["X", "X", "X", "X", "O", "O", "X", "O", "O"]
40
+
41
+ output = capture_puts{ board.display }
42
+
43
+ expect(output).to include(" X | X | X ")
44
+ expect(output).to include("-----------")
45
+ expect(output).to include(" X | O | O ")
46
+ expect(output).to include("-----------")
47
+ expect(output).to include(" X | O | O ")
48
+
49
+
50
+ board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
51
+
52
+ output = capture_puts{ board.display }
53
+
54
+ expect(output).to include(" X | O | X ")
55
+ expect(output).to include("-----------")
56
+ expect(output).to include(" O | X | X ")
57
+ expect(output).to include("-----------")
58
+ expect(output).to include(" O | X | O ")
59
+ end
60
+ end
61
+
62
+ describe '#position' do
63
+ it 'takes in user input and returns the value of the board cell' do
64
+ board.cells = ["O", " ", " ", " ", "X", " ", " ", " ", "X"]
65
+
66
+ expect(board.position("1")).to eq("O")
67
+ expect(board.position("2")).to eq(" ")
68
+ expect(board.position("5")).to eq("X")
69
+ end
70
+ end
71
+
72
+ describe '#full?' do
73
+ it 'returns true for a full board' do
74
+ board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
75
+
76
+ expect(board.full?).to be_truthy
77
+ end
78
+
79
+ it 'returns false for an in-progress game' do
80
+ board.cells = ["X", " ", "X", " ", "X", " ", "O", "O", " "]
81
+
82
+ expect(board.full?).to be_falsey
83
+ end
84
+ end
85
+
86
+ describe '#turn_count' do
87
+ it 'returns the amount of turns based on cell value' do
88
+ board.cells = ["O", " ", " ", " ", "X", " ", " ", " ", "X"]
89
+
90
+ expect(board.turn_count).to eq(3)
91
+ end
92
+ end
93
+
94
+ describe '#taken?' do
95
+ it 'returns true if the position is X or O' do
96
+ board.cells = ["X", " ", " ", " ", " ", " ", " ", " ", "O"]
97
+
98
+ expect(board.taken?("1")).to be(true)
99
+ expect(board.taken?("9")).to be(true)
100
+ end
101
+
102
+ it 'returns false if the position is empty or blank' do
103
+ board.cells = ["X", " ", " ", " ", " ", " ", " ", " ", "O"]
104
+
105
+ expect(board.taken?("2")).to be(false)
106
+ expect(board.taken?("7")).to be(false)
107
+ end
108
+ end
109
+
110
+ describe '#valid_move?' do
111
+ it 'returns true for user input between 1-9 that is not taken' do
112
+ board.cells = [" ", " ", " ", " ", "X", " ", " ", " ", " "]
113
+
114
+ expect(board.valid_move?("1")).to be_truthy
115
+ expect(board.valid_move?("5")).to be_falsey
116
+ expect(board.valid_move?("invalid")).to be_falsey
117
+ end
118
+ end
119
+
120
+ describe '#update' do
121
+ it 'updates the cells in the board with the player token according to the input' do
122
+ player = double("player", :token => "X")
123
+
124
+ board.update("1", player)
125
+ expect(board.position("1")).to eq("X")
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Player' do
4
+
5
+ describe '#initialize' do
6
+ it 'accepts a token to assign' do
7
+ player = Player.new("X")
8
+ expect(player.token).to eq("X")
9
+ end
10
+ end
11
+
12
+ describe '#token' do
13
+ it 'cannot be changed once assigned in initialize' do
14
+ player = Player.new("O")
15
+ expect(player.token).to eq("O")
16
+
17
+ expect{player.token = "O"}.to raise_error(NoMethodError)
18
+ expect(player).to_not respond_to(:token=)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Players::Human' do
4
+ it 'inherits from Player' do
5
+ expect(Players::Human.ancestors).to include(Player)
6
+ end
7
+
8
+ describe '#move' do
9
+ it 'asks the user for input and returns it' do
10
+ human = Players::Human.new("X")
11
+ allow($stdout).to receive(:puts)
12
+
13
+ expect(human).to receive(:gets).and_return("1")
14
+
15
+ expect(human.move([])).to eq("1")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,340 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Game' do
4
+ describe '::WIN_COMBINATIONS' do
5
+ it 'defines a constant WIN_COMBINATIONS with arrays for each win combination' do
6
+ expect(Game::WIN_COMBINATIONS).to include_array([0,1,2])
7
+ expect(Game::WIN_COMBINATIONS).to include_array([3,4,5])
8
+ expect(Game::WIN_COMBINATIONS).to include_array([6,7,8])
9
+ expect(Game::WIN_COMBINATIONS).to include_array([0,3,6])
10
+ expect(Game::WIN_COMBINATIONS).to include_array([1,4,7])
11
+ expect(Game::WIN_COMBINATIONS).to include_array([2,5,8])
12
+ expect(Game::WIN_COMBINATIONS).to include_array([0,4,8])
13
+ expect(Game::WIN_COMBINATIONS).to include_array([6,4,2])
14
+ end
15
+ end
16
+
17
+ describe '#board' do
18
+ it 'provides access to the board' do
19
+ game = Game.new
20
+ game.board = []
21
+ expect(game.board).to eq([])
22
+ end
23
+ end
24
+
25
+ describe '#player_1' do
26
+ it 'provides access to player_1' do
27
+ game = Game.new
28
+ player_1 = Player.new("X")
29
+ game.player_1 = player_1
30
+ expect(game.player_1).to eq(player_1)
31
+ end
32
+ end
33
+
34
+ describe '#player_2' do
35
+ it 'provides access to player_2' do
36
+ game = Game.new
37
+ player_2 = Player.new("X")
38
+ game.player_2 = player_2
39
+ expect(game.player_2).to eq(player_2)
40
+ end
41
+ end
42
+
43
+ describe 'initialize' do
44
+ it 'accepts 2 players and a board' do
45
+ board = Board.new
46
+ player_1 = Player.new("X")
47
+ player_2 = Player.new("O")
48
+
49
+ game = Game.new(player_1, player_2, board)
50
+
51
+ expect(game.player_1).to eq(player_1)
52
+ expect(game.player_2).to eq(player_2)
53
+ expect(game.board).to eq(board)
54
+ end
55
+
56
+ it 'defaults to two human players, X and O, with an empty board' do
57
+ game = Game.new
58
+
59
+ expect(game.player_1).to be_a(Players::Human)
60
+ expect(game.player_1.token).to eq("X")
61
+
62
+ expect(game.player_2).to be_a(Players::Human)
63
+ expect(game.player_2.token).to eq("O")
64
+
65
+ expect(game.board.cells).to match_array(Array.new(9, " "))
66
+ end
67
+ end
68
+
69
+ describe '#current_player' do
70
+ it 'returns the correct player, X, for the third move' do
71
+ game = Game.new
72
+ game.board.cells = ["O", " ", " ", " ", "X", " ", " ", " ", " "]
73
+
74
+ expect(game.current_player.token).to eq("X")
75
+ end
76
+ end
77
+
78
+ describe '#over?' do
79
+ it 'returns true for a draw' do
80
+ game = Game.new
81
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
82
+
83
+ expect(game.over?).to be_truthy
84
+ end
85
+
86
+ it 'returns true for a won game' do
87
+ game = Game.new
88
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "O", "X"]
89
+
90
+ expect(game.over?).to be_truthy
91
+ end
92
+
93
+ it 'returns false for an in-progress game' do
94
+ game = Game.new
95
+ game.board.cells = ["X", " ", "X", " ", "X", " ", "O", "O", " "]
96
+
97
+ expect(game.over?).to be_falsey
98
+ end
99
+ end
100
+
101
+ describe '#won?' do
102
+ it 'returns false for a draw' do
103
+ game = Game.new
104
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
105
+
106
+ expect(game.won?).to be_falsey
107
+ end
108
+
109
+ it 'returns true for a win' do
110
+ game = Game.new
111
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "O", "X"]
112
+
113
+ expect(game.won?).to be_truthy
114
+ end
115
+ end
116
+
117
+ describe '#draw?' do
118
+ it 'returns true for a draw' do
119
+ game = Game.new
120
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
121
+
122
+ expect(game.draw?).to be_truthy
123
+ end
124
+
125
+ it 'returns false for a won game' do
126
+ game = Game.new
127
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "O", "X"]
128
+
129
+ expect(game.draw?).to be_falsey
130
+ end
131
+
132
+ it 'returns false for an in-progress game' do
133
+ game = Game.new
134
+ game.board.cells = ["X", " ", "X", " ", "X", " ", "O", "O", "X"]
135
+
136
+ expect(game.draw?).to be_falsey
137
+ end
138
+ end
139
+
140
+ describe '#winner' do
141
+ it 'returns X when X won' do
142
+ game = Game.new
143
+ game.board.cells = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
144
+
145
+ expect(game.winner).to eq("X")
146
+ end
147
+
148
+ it 'returns O when O won' do
149
+ game = Game.new
150
+ game.board.cells = ["X", "O", " ", " ", "O", " ", " ", "O", "X"]
151
+
152
+ expect(game.winner).to eq("O")
153
+ end
154
+
155
+ it 'returns nil when no winner' do
156
+ game = Game.new
157
+ game.board.cells = ["X", "O", " ", " ", " ", " ", " ", "O", "X"]
158
+
159
+ expect(game.winner).to be_nil
160
+ end
161
+ end
162
+
163
+ describe 'turn' do
164
+ it 'makes valid moves' do
165
+ game = Game.new
166
+ allow($stdout).to receive(:puts)
167
+
168
+ expect(game.player_1).to receive(:gets).and_return("1")
169
+
170
+ game.turn
171
+ end
172
+
173
+ it 'asks for input again after a failed validation' do
174
+ game = Game.new
175
+ allow($stdout).to receive(:puts)
176
+
177
+ expect(game.player_1).to receive(:gets).and_return("invalid")
178
+ expect(game.player_1).to receive(:gets).and_return("1")
179
+
180
+ game.turn
181
+ end
182
+
183
+ it 'changes to player 2 after the first turn' do
184
+ game = Game.new
185
+ allow($stdout).to receive(:puts)
186
+
187
+ expect(game.player_1).to receive(:gets).and_return("1")
188
+ expect(game.player_2).to receive(:gets).and_return("2")
189
+
190
+ game.turn
191
+ game.turn
192
+ end
193
+ end
194
+
195
+ describe 'play' do
196
+ it 'asks for players input on a turn of the game' do
197
+ game = Game.new
198
+ allow($stdout).to receive(:puts)
199
+ allow(game).to receive(:over?).and_return(false, true)
200
+
201
+ expect(game.player_1).to receive(:gets).at_least(:once).and_return("1")
202
+
203
+ game.play
204
+ end
205
+
206
+ it 'checks if the game is over after every turn' do
207
+ game = Game.new
208
+ allow($stdout).to receive(:puts)
209
+ allow(game.player_1).to receive(:gets).and_return("1", "2")
210
+ allow(game.player_2).to receive(:gets).and_return("4", "5")
211
+
212
+ expect(game).to receive(:over?).at_least(:twice).and_return(false, false, true)
213
+
214
+ game.play
215
+ end
216
+
217
+ it 'plays the first turn of the game' do
218
+ game = Game.new
219
+ allow($stdout).to receive(:puts)
220
+ allow(game.player_1).to receive(:gets).and_return("1")
221
+
222
+ allow(game).to receive(:over?).and_return(false, true)
223
+
224
+ game.play
225
+
226
+ expect(game.board.cells).to eq(["X", " ", " ", " ", " ", " ", " ", " ", " "])
227
+ end
228
+
229
+ it 'plays the first few turns of the game' do
230
+ game = Game.new
231
+
232
+ allow($stdout).to receive(:puts)
233
+ allow(game.player_1).to receive(:gets).and_return("1", "2")
234
+ allow(game.player_2).to receive(:gets).and_return("4")
235
+
236
+ allow(game).to receive(:over?).and_return(false, false, false, true)
237
+
238
+ game.play
239
+
240
+ expect(game.board.cells).to eq(["X", "X", " ", "O", " ", " ", " ", " ", " "])
241
+ end
242
+
243
+ it 'checks if the game is won after every turn' do
244
+ game = Game.new
245
+ allow($stdout).to receive(:puts)
246
+ allow(game.player_1).to receive(:gets).and_return("1", "2", "3")
247
+ allow(game.player_2).to receive(:gets).and_return("4", "5", "6")
248
+ allow(game).to receive(:winner).and_return("X")
249
+
250
+ expect(game).to receive(:won?).at_least(:twice).and_return(false, false, true)
251
+
252
+ game.play
253
+ end
254
+
255
+ it 'checks if the game is a draw after every turn' do
256
+ game = Game.new
257
+ allow($stdout).to receive(:puts)
258
+ allow(game.player_1).to receive(:gets).and_return("1", "2")
259
+ allow(game.player_2).to receive(:gets).and_return("3", "4")
260
+
261
+ expect(game).to receive(:draw?).at_least(:twice).and_return(false, false, true)
262
+
263
+ game.play
264
+ end
265
+
266
+ it 'stops playing if someone has won' do
267
+ game = Game.new
268
+ game.board.cells = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
269
+
270
+ allow($stdout).to receive(:puts)
271
+
272
+ expect(game).to_not receive(:turn)
273
+
274
+ game.play
275
+ end
276
+
277
+ it 'congratulates the winner X' do
278
+ game = Game.new
279
+ game.board.cells = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
280
+ allow($stdout).to receive(:puts)
281
+
282
+ expect($stdout).to receive(:puts).with("Congratulations X!")
283
+
284
+ game.play
285
+ end
286
+
287
+ it 'congratulates the winner O' do
288
+ game = Game.new
289
+ game.board.cells = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
290
+
291
+ allow($stdout).to receive(:puts)
292
+
293
+ expect($stdout).to receive(:puts).with("Congratulations O!")
294
+
295
+ game.play
296
+ end
297
+
298
+ it 'stops playing in a draw' do
299
+ game = Game.new
300
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
301
+
302
+ allow($stdout).to receive(:puts)
303
+
304
+ expect(game).to_not receive(:turn)
305
+
306
+ game.play
307
+ end
308
+
309
+ it 'prints "Cat\'s Game!" on a draw' do
310
+ game = Game.new
311
+ game.board.cells = ["X", "O", "X", "O", "X", "X", "O", "X", "O"]
312
+
313
+ allow($stdout).to receive(:puts)
314
+
315
+ expect($stdout).to receive(:puts).with("Cat's Game!")
316
+
317
+ game.play
318
+ end
319
+
320
+ it 'plays through an entire game' do
321
+ game = Game.new
322
+ allow($stdout).to receive(:puts)
323
+
324
+ expect(game.player_1).to receive(:gets).and_return("1")
325
+ expect(game.player_2).to receive(:gets).and_return("2")
326
+ expect(game.player_1).to receive(:gets).and_return("3")
327
+ expect(game.player_2).to receive(:gets).and_return("4")
328
+ expect(game.player_1).to receive(:gets).and_return("5")
329
+ expect(game.player_2).to receive(:gets).and_return("6")
330
+ expect(game.player_1).to receive(:gets).and_return("7")
331
+
332
+ expect($stdout).to receive(:puts).with("Congratulations X!")
333
+
334
+ game.play
335
+ end
336
+ end
337
+
338
+ describe 'start' do
339
+ end
340
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Players::Computer' do
4
+ it 'inherits from Player' do
5
+ expect(Players::Computer.ancestors).to include(Player)
6
+ end
7
+
8
+ describe '#move' do
9
+ it 'returns a valid position for the computer to move' do
10
+ computer = Players::Computer.new("X")
11
+ board = Board.new
12
+
13
+ valid_moves = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
14
+
15
+ computer_move = computer.move(board)
16
+
17
+ expect(valid_moves).to include(computer_move)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ # require_relative "../config/environment.rb"
3
+
4
+ RSpec.configure do |config|
5
+ config.order = :default
6
+ end
7
+
8
+ RSpec::Matchers.define :include_array do |expected|
9
+ match do |actual|
10
+ actual.any?{|array| match_array(expected).matches?(array)}
11
+ end
12
+ end
13
+
14
+ def run_file(file)
15
+ eval(File.read(file), binding)
16
+ end
17
+
18
+ def get_variable_from_file(file, variable)
19
+ file_scope = binding
20
+ file_scope.eval(File.read(file))
21
+
22
+ begin
23
+ return file_scope.local_variable_get(variable)
24
+ rescue NameError
25
+ raise NameError, "local variable `#{variable}' not defined in #{file}."
26
+ end
27
+ end
28
+
29
+ def capture_puts
30
+ begin
31
+ old_stdout = $stdout
32
+ $stdout = StringIO.new('','w')
33
+ yield
34
+ $stdout.string
35
+ ensure
36
+ $stdout = old_stdout
37
+ end
38
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,2 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'tictactoe_tracypholmes/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "tictactoe_tracypholmes"
9
+ s.version = TictactoeTracypholmes::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["tracypholmes"]
12
+ s.email = ["tracyholmes@gmail.com"]
13
+
14
+ s.summary = %q{TictTacToe with AI from your terminal}
15
+ s.description = %q{Run `tictactoe_tracypholmes` in your terminal after installing to play.}
16
+ s.homepage = "https://github.com/tracypholmes/tictactoe_tracypholmes"
17
+ s.license = "MIT"
18
+
19
+ s.required_rubygems_version = ">= 2.3.6"
20
+
21
+ s.add_development_dependency "bundler", "~> 1.3"
22
+ s.add_development_dependency "rake", "~> 10.0"
23
+ s.add_development_dependency 'rspec', '~> 0'
24
+ s.add_development_dependency 'pry', '~> 0'
25
+ s.add_dependency 'colorize', '~> 0.8.1'
26
+
27
+ s.files = `git ls-files`.split($/)
28
+ s.test_files = s.files.grep(%r{^(test |s| features)/})
29
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
+ s.require_paths = ["lib"]
31
+
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tictactoe_tracypholmes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - tracypholmes
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.13'
19
+ version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.13'
26
+ version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,23 +80,43 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.8.1
83
- description: TicTacToe game with somewhat unbeatable AI. run `tictactoe_tracypholmes`
84
- to play.
83
+ description: Run `tictactoe_tracypholmes` in your terminal after installing to play.
85
84
  email:
86
85
  - tracyholmes@gmail.com
87
86
  executables:
87
+ - setup
88
88
  - tictactoe_tracypholmes
89
89
  extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
+ - ".gitignore"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
92
98
  - bin/setup
93
99
  - bin/tictactoe_tracypholmes
94
100
  - lib/tictactoe_tracypholmes.rb
101
+ - lib/tictactoe_tracypholmes/board.rb
102
+ - lib/tictactoe_tracypholmes/game.rb
103
+ - lib/tictactoe_tracypholmes/player.rb
104
+ - lib/tictactoe_tracypholmes/players/computer.rb
105
+ - lib/tictactoe_tracypholmes/players/human.rb
106
+ - lib/tictactoe_tracypholmes/version.rb
107
+ - spec/01_board_spec.rb
108
+ - spec/02_player_spec.rb
109
+ - spec/03_human_player_spec.rb
110
+ - spec/04_game_spec.rb
111
+ - spec/05_computer_player_spec.rb
112
+ - spec/spec_helper.rb
113
+ - tasks/rspec.rake
114
+ - tictactoe_tracypholmes.gemspec
95
115
  homepage: https://github.com/tracypholmes/tictactoe_tracypholmes
96
116
  licenses:
97
117
  - MIT
98
118
  metadata: {}
99
- post_install_message: Thanks for installing!
119
+ post_install_message:
100
120
  rdoc_options: []
101
121
  require_paths:
102
122
  - lib
@@ -109,11 +129,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
129
  requirements:
110
130
  - - ">="
111
131
  - !ruby/object:Gem::Version
112
- version: '0'
132
+ version: 2.3.6
113
133
  requirements: []
114
134
  rubyforge_project:
115
135
  rubygems_version: 2.6.7
116
136
  signing_key:
117
137
  specification_version: 4
118
- summary: TictTacToe with AI
138
+ summary: TictTacToe with AI from your terminal
119
139
  test_files: []