ttt_malisa 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 69c787e1a4af6bf67a020447b13f8b22cb607c272bcdd6400cb3d21589de68aa
4
+ data.tar.gz: 07faafb8ba7c681404806d20b5c77496d7900d91496f467c2a5fedeef03870ce
5
+ SHA512:
6
+ metadata.gz: e839ae3e51b7c0a5d6f91d726b3ea17365d30b93ae6d5f32a86af102982f3125f52619b842b37e7d30668205600b21b8fdb73a42e972f24d4203e1ddbd0a4c74
7
+ data.tar.gz: acceafb6c9192622a7a8afa233bb7097fcbb9b5dec123ab7e987a541edec38615977b9a018c1e4bc762a1a1460442ea0c1306f0c710f29594dd768cd028e901d
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .vscode/
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,164 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ DisabledByDefault: true
4
+ Exclude:
5
+ - 'Vagrantfile'
6
+ - 'log/**/*'
7
+ - 'public/**/*'
8
+ - 'test/data/**/*'
9
+ - 'vendor/**/*'
10
+ # Default lint cops
11
+ Lint/AssignmentInCondition:
12
+ Enabled: true
13
+ AllowSafeAssignment: true
14
+ Layout/BlockAlignment:
15
+ Enabled: true
16
+ EnforcedStyleAlignWith: either
17
+ Layout/DefEndAlignment:
18
+ Enabled: true
19
+ EnforcedStyleAlignWith: start_of_line
20
+ SupportedStylesAlignWith:
21
+ - def
22
+ - start_of_line
23
+ AutoCorrect: false
24
+ Lint/UnusedBlockArgument:
25
+ Enabled: true
26
+ #IgnoreEmptyBlocks: true
27
+ AllowUnusedKeywordArguments: false
28
+ Lint/UnusedMethodArgument:
29
+ Enabled: true
30
+ AllowUnusedKeywordArguments: false
31
+ IgnoreEmptyMethods: true
32
+ Lint/UselessAssignment:
33
+ Enabled: true
34
+ Lint/EmptyInterpolation:
35
+ Enabled: true
36
+ # NBX project cops
37
+ Naming/AccessorMethodName:
38
+ Description: Check the naming of accessor methods for get_/set_.
39
+ Enabled: true
40
+ Naming/PredicateName:
41
+ Enabled: true
42
+ NamePrefix:
43
+ - is_
44
+ - has_
45
+ - have_
46
+ ForbiddenPrefixes:
47
+ - is_
48
+ - has_
49
+ - have_
50
+ AllowedMethods:
51
+ - is_a?
52
+ # Indent private/protected/public as deep as method definitions
53
+ Layout/AccessModifierIndentation:
54
+ Enabled: true
55
+ EnforcedStyle: indent
56
+ SupportedStyles:
57
+ - outdent
58
+ - indent
59
+ Naming/VariableName:
60
+ Enabled: true
61
+ EnforcedStyle: snake_case
62
+ SupportedStyles:
63
+ - snake_case
64
+ - camelCase
65
+ Naming/MethodName:
66
+ Enabled: true
67
+ EnforcedStyle: snake_case
68
+ SupportedStyles:
69
+ - snake_case
70
+ - camelCase
71
+ Style/MethodDefParentheses:
72
+ Enabled: true
73
+ EnforcedStyle: require_parentheses
74
+ SupportedStyles:
75
+ - require_parentheses
76
+ - require_no_parentheses
77
+ - require_no_parentheses_except_multiline
78
+ Style/MethodCallWithoutArgsParentheses:
79
+ Description: 'Do not use parentheses for method calls with no arguments.'
80
+ Enabled: true
81
+ Layout/SpaceAfterMethodName:
82
+ Enabled: true
83
+ Layout/SpaceBeforeFirstArg:
84
+ Enabled: true
85
+ Layout/TrailingEmptyLines:
86
+ Enabled: true
87
+ Layout/TrailingWhitespace:
88
+ Enabled: true
89
+ Layout/SpaceAroundOperators:
90
+ Enabled: true
91
+ Layout/EmptyLines:
92
+ Enabled: true
93
+ Layout/LeadingCommentSpace:
94
+ Enabled: true
95
+ Layout/ArrayAlignment:
96
+ Enabled: true
97
+ Layout/HashAlignment:
98
+ Enabled: true
99
+ Style/HashSyntax:
100
+ Enabled: true
101
+ Style/EmptyElse:
102
+ Enabled: true
103
+ Layout/EmptyLineBetweenDefs:
104
+ Enabled: true
105
+ Layout/IndentationConsistency:
106
+ Enabled: true
107
+ Style/NilComparison:
108
+ Enabled: true
109
+ Layout/Tab:
110
+ Enabled: true
111
+ Style/ZeroLengthPredicate:
112
+ Enabled: true
113
+ Layout/SpaceInsideStringInterpolation:
114
+ Enabled: true
115
+ Layout/CommentIndentation:
116
+ Enabled: true
117
+ Style/RedundantReturn:
118
+ Enabled: true
119
+ Layout/SpaceAfterComma:
120
+ Enabled: true
121
+ Layout/CaseIndentation:
122
+ Enabled: true
123
+ Layout/IndentationWidth:
124
+ Enabled: true
125
+ Style/NumericLiteralPrefix:
126
+ Enabled: true
127
+ Style/RedundantInterpolation:
128
+ Enabled: true
129
+ Layout/MultilineArrayBraceLayout:
130
+ Enabled: true
131
+ Style/PreferredHashMethods:
132
+ Enabled: true
133
+ Style/RedundantSelf:
134
+ Enabled: true
135
+ Style/IfInsideElse:
136
+ Enabled: true
137
+ Layout/SpaceBeforeBlockBraces:
138
+ Enabled: true
139
+ Layout/SpaceAroundBlockParameters:
140
+ Enabled: true
141
+ Lint/ShadowingOuterLocalVariable:
142
+ Enabled: true
143
+ Layout/SpaceAroundEqualsInParameterDefault:
144
+ Enabled: true
145
+ Style/TrailingCommaInArguments:
146
+ Enabled: true
147
+ Style/BlockDelimiters:
148
+ Enabled: true
149
+ IgnoredMethods:
150
+ - sig
151
+ Style/NumericPredicate:
152
+ Enabled: true
153
+ Style/ColonMethodCall:
154
+ Enabled: true
155
+ Style/StringLiterals:
156
+ Enabled: true
157
+ Security/Eval:
158
+ Enabled: true
159
+ Security/JSONLoad:
160
+ Enabled: true
161
+ Security/MarshalLoad:
162
+ Enabled: true
163
+ Security/Open:
164
+ Enabled: true
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ os: linux
3
+ rvm :
4
+ - 2.6
5
+
6
+ before_install:
7
+ - gem install bundler
8
+
@@ -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 Middlebrooks314@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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rake'
5
+ gem 'rspec'
6
+ gem 'rubocop', require: false
7
+ gem 'require_all'
8
+ end
9
+
10
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ttt_malisa (0.2.0)
5
+ require_all
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ diff-lcs (1.3)
12
+ jaro_winkler (1.5.4)
13
+ parallel (1.19.1)
14
+ parser (2.7.0.5)
15
+ ast (~> 2.4.0)
16
+ rainbow (3.0.0)
17
+ rake (13.0.1)
18
+ require_all (3.0.0)
19
+ rexml (3.2.4)
20
+ rspec (3.9.0)
21
+ rspec-core (~> 3.9.0)
22
+ rspec-expectations (~> 3.9.0)
23
+ rspec-mocks (~> 3.9.0)
24
+ rspec-core (3.9.1)
25
+ rspec-support (~> 3.9.1)
26
+ rspec-expectations (3.9.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.9.0)
29
+ rspec-mocks (3.9.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.9.0)
32
+ rspec-support (3.9.2)
33
+ rubocop (0.81.0)
34
+ jaro_winkler (~> 1.5.1)
35
+ parallel (~> 1.10)
36
+ parser (>= 2.7.0.1)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ rexml
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 2.0)
41
+ ruby-progressbar (1.10.1)
42
+ unicode-display_width (1.7.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ rake
49
+ require_all
50
+ rspec
51
+ rubocop
52
+ ttt_malisa!
53
+
54
+ BUNDLED WITH
55
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Malisa Middlebrooks
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,44 @@
1
+ # TttMalisa
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ttt_malisa`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ttt_malisa'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ttt_malisa
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ttt_malisa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ttt_malisa/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the TttMalisa project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ttt_malisa/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubocop/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ RuboCop::RakeTask.new
5
+
6
+ task default: %w[test]
7
+
8
+ task :test do
9
+ sh 'bundle exec rspec'
10
+ end
11
+
12
+ task test: :rubocop
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ttt_malisa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/run ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # require 'require_all'
3
+ # require_all 'ttt_malisa'
4
+ require_relative '../lib/ttt_malisa'
5
+
6
+ console = Console.new
7
+ board = Board.new
8
+ rules = Rules.new(board)
9
+ display = Display.new(console, board)
10
+ menu = Menu.new(display)
11
+ game_factory = GameFactory.new(board, display, rules, menu)
12
+ game = game_factory.create_game
13
+
14
+ game.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ class Board
2
+ attr_reader :squares
3
+
4
+ def initialize(squares = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '])
5
+ @squares = squares
6
+ end
7
+
8
+ def display
9
+ <<-HEREDOC
10
+ #{@squares[0]} | #{@squares[1]} | #{@squares[2]}
11
+ -----------
12
+ #{@squares[3]} | #{@squares[4]} | #{@squares[5]}
13
+ -----------
14
+ #{@squares[6]} | #{@squares[7]} | #{@squares[8]}
15
+ HEREDOC
16
+ end
17
+
18
+ def available_moves
19
+ open_squares = []
20
+ @squares.each_with_index {|_value, index| open_squares << index if !position_taken?(index)}
21
+ open_squares
22
+ end
23
+
24
+ def position_taken?(index)
25
+ @squares[index] != ' '
26
+ end
27
+
28
+ def valid_move?(index)
29
+ index.between?(0, 8) && !position_taken?(index)
30
+ end
31
+
32
+ def mark(index, mark = 'X')
33
+ @squares[index] = mark
34
+ end
35
+
36
+ def full?
37
+ @squares.all? do |index|
38
+ index != ' '
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ class ComputerPlayer
2
+ attr_reader :mark
3
+
4
+ def initialize(mark, board, display)
5
+ @board = board
6
+ @display = display
7
+ @mark = mark
8
+ end
9
+
10
+ def move
11
+ @display.make_move_message(@mark)
12
+ index = retrieve_computer_index
13
+ computer_is_thinking
14
+ @display.computer_move(index + 1)
15
+ @board.valid_move?(index) ? @board.mark(index, @mark) : move
16
+ end
17
+
18
+ private
19
+
20
+ def computer_is_thinking
21
+ @display.computer_thinking
22
+ sleep 1
23
+ end
24
+
25
+ def retrieve_computer_index
26
+ available_move = @board.available_moves
27
+ available_move[0]
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ class Console
2
+ def retrieve_user_input
3
+ gets.chomp
4
+ end
5
+
6
+ def print_message(string)
7
+ puts(string)
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ class Display
2
+ def initialize(console, board)
3
+ @console = console
4
+ @board = board
5
+ end
6
+
7
+ def welcome
8
+ @console.print_message('Welcome to Tic Tac Toe')
9
+ end
10
+
11
+ def make_move_message(mark)
12
+ @console.print_message("Player #{mark}, please enter a position 1-9 that is not already marked")
13
+ end
14
+
15
+ def retrieve_user_input
16
+ @console.retrieve_user_input
17
+ end
18
+
19
+ def alert_current_player(current_player)
20
+ @console.print_message("It is #{current_player.mark}\'s turn")
21
+ end
22
+
23
+ def setup_mark_message
24
+ @console.print_message('Please enter a single character to use as your game piece')
25
+ end
26
+
27
+ def winner_message(mark)
28
+ @console.print_message("Congratulations #{mark}!")
29
+ end
30
+
31
+ def draw_message
32
+ @console.print_message('Draw!')
33
+ end
34
+
35
+ def board
36
+ @console.print_message(@board.display)
37
+ end
38
+
39
+ def invalid_mark_message
40
+ @console.print_message('Please enter a single character that is unique')
41
+ end
42
+
43
+ def invalid_player_choice
44
+ @console.print_message('That is not a valid choice. Please try again')
45
+ end
46
+
47
+ def player_choice
48
+ @console.print_message('Please choose a player type')
49
+ @console.print_message('1: Human Player')
50
+ @console.print_message('2: Computer Player')
51
+ end
52
+
53
+ def computer_move(move)
54
+ @console.print_message(move)
55
+ end
56
+
57
+ def computer_thinking
58
+ @console.print_message('.....thinking 🤔')
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ def foo
2
+ 'foo'
3
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'board'
2
+ require_relative 'rules'
3
+ require_relative 'human_player'
4
+
5
+ class Game
6
+ attr_reader :rules, :player_one, :player_two
7
+
8
+ def initialize(display, rules, player_one, player_two)
9
+ @rules = rules
10
+ @display = display
11
+ @player_one = player_one
12
+ @player_two = player_two
13
+ @current_player = @player_one
14
+ end
15
+
16
+ def start
17
+ @display.welcome
18
+ play
19
+ end
20
+
21
+ def turn
22
+ @display.alert_current_player(@current_player)
23
+ @current_player.move
24
+ @display.board
25
+ switch_players
26
+ end
27
+
28
+ def play
29
+ @display.board
30
+ turn until rules.game_over?
31
+ game_over_message
32
+ end
33
+
34
+ def game_over_message
35
+ winner ? @display.winner_message(@current_player.mark) : @display.draw_message
36
+ end
37
+
38
+ private
39
+
40
+ def switch_players
41
+ if !rules.game_over?
42
+ @current_player == @player_one ? @current_player = @player_two : @current_player = @player_one
43
+ end
44
+ end
45
+
46
+ def winner
47
+ @current_player if rules.winning_combination?
48
+ end
49
+ end
@@ -0,0 +1,59 @@
1
+ class GameFactory
2
+ attr_reader :player_one, :player_two
3
+
4
+ def initialize(board, display, rules, menu)
5
+ @board = board
6
+ @display = display
7
+ @rules = rules
8
+ @menu = menu
9
+ end
10
+
11
+ def create_game
12
+ player_selections
13
+ player_choices
14
+ Game.new(@display, @rules, @player_one, @player_two)
15
+ end
16
+
17
+ private
18
+
19
+ def player_selections
20
+ @player_one_option = @menu.player_choice
21
+ @player_two_option = @menu.player_choice
22
+ end
23
+
24
+ def player_choices
25
+ player_one_mark = create_mark
26
+ player_two_mark = create_mark(player_one_mark)
27
+ @player_one = create_player(@player_one_option, player_one_mark)
28
+ @player_two = create_player(@player_two_option, player_two_mark)
29
+ end
30
+
31
+ def create_player(player_option, mark)
32
+ case player_option
33
+ when '1'
34
+ player = HumanPlayer.new(mark, @board, @display)
35
+ when '2'
36
+ player = ComputerPlayer.new(mark, @board, @display)
37
+ end
38
+ player
39
+ end
40
+
41
+ def create_mark(other_mark = nil)
42
+ @display.setup_mark_message
43
+ potential_mark = @display.retrieve_user_input
44
+ mark = check_game_mark(potential_mark, other_mark)
45
+ @mark = mark
46
+ end
47
+
48
+ def check_game_mark(potential_mark, other_mark)
49
+ until valid_game_piece?(potential_mark, other_mark)
50
+ @display.invalid_mark_message
51
+ potential_mark = @display.retrieve_user_input
52
+ end
53
+ potential_mark
54
+ end
55
+
56
+ def valid_game_piece?(potential_mark, other_mark)
57
+ potential_mark.length == 1 && potential_mark != other_mark
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ class HumanPlayer
2
+ attr_reader :mark
3
+
4
+ def initialize(mark, board, display)
5
+ @board = board
6
+ @display = display
7
+ @mark = mark
8
+ end
9
+
10
+ def move
11
+ @display.make_move_message(@mark)
12
+ index = @display.retrieve_user_input.to_i - 1
13
+ @board.valid_move?(index) ? @board.mark(index, @mark) : move
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ class Menu
2
+
3
+ def initialize(display)
4
+ @display = display
5
+ end
6
+
7
+ def player_choice
8
+ @display.player_choice
9
+ potential_choice = @display.retrieve_user_input
10
+ check_game_choice(potential_choice)
11
+ end
12
+
13
+ private
14
+
15
+ def check_game_choice(potential_choice)
16
+ until valid_player_choice?(potential_choice)
17
+ @display.invalid_player_choice
18
+ @display.player_choice
19
+ potential_choice = @display.retrieve_user_input
20
+ end
21
+ potential_choice
22
+ end
23
+
24
+ def valid_player_choice?(potential_choice)
25
+ potential_choice == '1' || potential_choice == '2'
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'board'
2
+ require_relative 'game'
3
+
4
+ class Rules
5
+ def initialize(board)
6
+ @board = board
7
+ end
8
+
9
+ WINNING_COMBOS = [
10
+ [0, 1, 2],
11
+ [3, 4, 5],
12
+ [6, 7, 8],
13
+ [0, 3, 6],
14
+ [1, 4, 7],
15
+ [2, 5, 8],
16
+ [0, 4, 8],
17
+ [2, 4, 6]
18
+ ]
19
+
20
+ def game_over?
21
+ winning_combination? || draw?
22
+ end
23
+
24
+ def winning_combination?
25
+ WINNING_COMBOS.any? do |x, y, z|
26
+ @board.position_taken?(x) &&
27
+ @board.squares[x] == @board.squares[y] &&
28
+ @board.squares[x] == @board.squares[z]
29
+ end
30
+ end
31
+
32
+ def draw?
33
+ !winning_combination? && @board.full?
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module TttMalisa
2
+ VERSION = '0.2.0'
3
+ end
data/lib/ttt_malisa.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "ttt_malisa/version"
2
+ require 'require_all'
3
+ require_all 'lib'
4
+
5
+ module TttMalisa
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,30 @@
1
+ require './lib/ttt_malisa/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ttt_malisa"
5
+ spec.version = TttMalisa::VERSION
6
+ spec.authors = ["Malisa Middlebrooks"]
7
+ spec.email = ["Middlebrooks314@gmail.com"]
8
+
9
+ spec.summary = %q{A gem for generating a tic tac toe game in the command line.}
10
+ spec.homepage = "https://github.com/Middlebrooks314/tic-tac-toe-ruby"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/Middlebrooks314/tic-tac-toe-ruby"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'bin'
25
+ spec.executables = ['run']
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'require_all'
29
+ spec.add_development_dependency 'rake', '~> 13.0'
30
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttt_malisa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Malisa Middlebrooks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: require_all
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ description:
42
+ email:
43
+ - Middlebrooks314@gmail.com
44
+ executables:
45
+ - run
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - ".travis.yml"
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - bin/console
60
+ - bin/run
61
+ - bin/setup
62
+ - lib/ttt_malisa.rb
63
+ - lib/ttt_malisa/board.rb
64
+ - lib/ttt_malisa/computer_player.rb
65
+ - lib/ttt_malisa/console.rb
66
+ - lib/ttt_malisa/display.rb
67
+ - lib/ttt_malisa/dummy.rb
68
+ - lib/ttt_malisa/game.rb
69
+ - lib/ttt_malisa/game_factory.rb
70
+ - lib/ttt_malisa/human_player.rb
71
+ - lib/ttt_malisa/menu.rb
72
+ - lib/ttt_malisa/rules.rb
73
+ - lib/ttt_malisa/version.rb
74
+ - ttt_malisa.gemspec
75
+ homepage: https://github.com/Middlebrooks314/tic-tac-toe-ruby
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ homepage_uri: https://github.com/Middlebrooks314/tic-tac-toe-ruby
80
+ source_code_uri: https://github.com/Middlebrooks314/tic-tac-toe-ruby
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A gem for generating a tic tac toe game in the command line.
100
+ test_files: []