ttt_cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0e3130ed99f7669b379a865337c538654d95aee
4
+ data.tar.gz: f9e192fcd7207bdd6c4ea287b0c1b969613fe7dd
5
+ SHA512:
6
+ metadata.gz: 61b3e839ca952620e95379c20748160a9935ff98bd40aae2e5d184e2df2307623ffd565dc93355a20f33aac9e7e53bbb50235819a285ce6d983b93f020ddc98e
7
+ data.tar.gz: 11e38bec6994aeefc80fef4b3279e7921f78f4e65b109401c010afd6ee800a75168b61cc8b8918be0e255a51e2577afcb121c95f85acfda4faddaa4519510a6c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -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 dan@pelensky.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 ttt_cli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dan Pelensky
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
+ # TttCli
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_cli`. 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_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ttt_cli
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_cli. 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.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ttt_cli"
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/play ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cli'
4
+
5
+ CLI.new
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
data/lib/cli.rb ADDED
@@ -0,0 +1,155 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
2
+ require 'game'
3
+ require 'board'
4
+ require 'human_player_cli'
5
+ require 'simple_computer'
6
+ require 'perfect_computer'
7
+
8
+ class CLI
9
+
10
+ OFFSET = 1
11
+
12
+ def initialize(input=$stdin, output=$stdout)
13
+ @input = input
14
+ @output = output
15
+ run_app
16
+ end
17
+
18
+ def select_board_size(options)
19
+ get_valid_input(options)
20
+ end
21
+
22
+ def choose_player_type(options)
23
+ get_valid_input(options)
24
+ end
25
+
26
+ def place_marker(options)
27
+ offset_spaces = options.map {|space| space + OFFSET}
28
+ get_valid_input(offset_spaces) - OFFSET
29
+ end
30
+
31
+ def get_valid_input(options)
32
+ choice = @input.gets.chomp.to_i
33
+ if options.include? choice
34
+ choice
35
+ else
36
+ @output.puts "Invalid Selection"
37
+ get_valid_input(options)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def run_app
44
+ start_of_game
45
+ until @game.game_over?
46
+ single_turn
47
+ end
48
+ print_outcome
49
+ end
50
+
51
+ def start_of_game
52
+ clear_screen
53
+ print_welcome
54
+ board = setup_board
55
+ player_x = choose_player("X")
56
+ player_o = choose_player("O")
57
+ setup_game(board, player_x, player_o)
58
+ end
59
+
60
+ def single_turn
61
+ clear_screen
62
+ print_board
63
+ print_players_turn
64
+ @game.take_turn
65
+ end
66
+
67
+ def setup_board
68
+ print_board_size
69
+ choice = select_board_size([3,4])
70
+ return Board.new(Array.new(choice * choice))
71
+ end
72
+
73
+ def choose_player(marker)
74
+ clear_screen
75
+ print_choose_player(marker)
76
+ choice = choose_player_type([1,2,3])
77
+ return HumanPlayer.new(marker, self) if choice == 1
78
+ return SimpleComputer.new(marker) if choice == 2
79
+ return PerfectComputer.new(marker) if choice == 3
80
+ end
81
+
82
+ def setup_game(board, player_x, player_o)
83
+ @game = Game.new(board, player_x, player_o)
84
+ end
85
+
86
+ def print_welcome
87
+ @output.puts "Tic Tac Toe"
88
+ end
89
+
90
+ def print_players_turn
91
+ @output.puts "#{@game.current_player.marker}, take your turn"
92
+ end
93
+
94
+ def print_board
95
+ board_split_into_rows = split_board_into_rows(board_with_index_in_empty_spaces)
96
+ board_split_into_rows.each_with_index do |row, row_index|
97
+ single_row = ""
98
+ row.each_with_index {|space, space_index| single_row = format_single_row(space, space_index, board_split_into_rows, single_row)}
99
+ print_board_rows_with_separator(row_index, row, single_row)
100
+ end
101
+ end
102
+
103
+ def board_with_index_in_empty_spaces
104
+ @game.board.spaces.each_with_index.map {|space, index| !space.nil? ? space : index + OFFSET }
105
+ end
106
+
107
+ def split_board_into_rows(spaces)
108
+ spaces.each_slice(@game.board.number_of_rows)
109
+ end
110
+
111
+ def format_single_row(space, space_index, board_split_into_rows, single_row)
112
+ formatted_number = add_space_to_single_digit_number(space)
113
+ single_row = add_separator_where_applicable(space_index, board_split_into_rows, single_row, formatted_number)
114
+ end
115
+
116
+ def add_space_to_single_digit_number(space)
117
+ space.to_s.length == 1 ? "#{space} " : "#{space}"
118
+ end
119
+
120
+ def add_separator_where_applicable(space_index, board_split_into_rows, single_row, formatted_number)
121
+ space_index == board_split_into_rows.size - 1 ? single_row += " #{formatted_number}" : single_row += " #{formatted_number} |"
122
+ end
123
+
124
+ def print_board_rows_with_separator(row_index, row, single_row)
125
+ @output.puts row_index == row.size - 1 ? single_row : single_row + "\n" + "-" * single_row.length
126
+ end
127
+
128
+ def print_game_over
129
+ @output.puts "Game Over"
130
+ end
131
+
132
+ def print_outcome
133
+ clear_screen
134
+ print_game_over
135
+ print_board
136
+ @output.puts @game.board.winner ? "#{@game.board.winner} is the winner" : "Tied Game"
137
+ end
138
+
139
+ def print_choose_player(marker)
140
+ @output.puts "Choose player type for #{marker}"
141
+ @output.puts "1) Human"
142
+ @output.puts "2) Simple Computer"
143
+ @output.puts "3) Expert Computer"
144
+ end
145
+
146
+ def print_board_size
147
+ @output.puts "Choose the number of rows on the board"
148
+ @output.puts "Select 3 or 4"
149
+ end
150
+
151
+ def clear_screen
152
+ @output.puts "\e[2J\e[f"
153
+ end
154
+
155
+ end
@@ -0,0 +1,13 @@
1
+ class HumanPlayer
2
+
3
+ attr_reader :marker
4
+
5
+ def initialize(marker, ui)
6
+ @marker = marker
7
+ @ui = ui
8
+ end
9
+
10
+ def choose_space(board)
11
+ @ui.place_marker(board.check_available_spaces)
12
+ end
13
+ end
data/lib/play.rb ADDED
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
2
+ require 'cli'
3
+
4
+ CLI.new
@@ -0,0 +1,3 @@
1
+ module TttCli
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ttt_cli.rb ADDED
File without changes
data/ttt_cli.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ttt_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ttt_cli"
8
+ spec.version = TttCli::VERSION
9
+ spec.authors = ["Dan Pelensky"]
10
+ spec.email = ["dan@pelensky.com"]
11
+
12
+ spec.summary = "A command line game of Tic Tac Toe"
13
+ spec.description = "Playable on a 3x3 or 4x4 board. Uses my ttt_core gem"
14
+ spec.homepage = "https://github.com/pelensky/ttt_cli"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.executables = ["play"]
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_dependency "ttt_core", "~> 0.5.0"
27
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttt_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Pelensky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-07 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ttt_core
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.0
69
+ description: Playable on a 3x3 or 4x4 board. Uses my ttt_core gem
70
+ email:
71
+ - dan@pelensky.com
72
+ executables:
73
+ - play
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/play
87
+ - bin/setup
88
+ - lib/cli.rb
89
+ - lib/human_player_cli.rb
90
+ - lib/play.rb
91
+ - lib/ttt_cli.rb
92
+ - lib/ttt_cli/version.rb
93
+ - ttt_cli.gemspec
94
+ homepage: https://github.com/pelensky/ttt_cli
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.10
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A command line game of Tic Tac Toe
118
+ test_files: []