tictactoe_gto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35013ac3fc7ff1badb1f46bbba25eec38afef54126ce4f5fe1e377ec57e42cf0
4
+ data.tar.gz: 95274555c4894c136a84caa8357b8f2ce9d2ff2d94ec75a505027a0da8f581ec
5
+ SHA512:
6
+ metadata.gz: 575b61543cac6fd34e70b3703eb726a27490cd0ef27f31c0feb4f0edb6097e4fe6e204b33255059415e9870d0d37ff031fde3c7fea5af42f4f14762747a1afba
7
+ data.tar.gz: f764289fe9b2ffa8b5c6f717d98abcecccecd4d233fdb2bcdf48b53ca382bef65a7b36b687a8f53bf655d29e649b2d800fa6c4551f55cbe0728e92c85501d758
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /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 j.loyolarangel@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,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tictactoe_gto.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jaime
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.
@@ -0,0 +1,51 @@
1
+ # TictactoeGto
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/tictactoe_gto`. 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 'tictactoe_gto'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tictactoe_gto
22
+
23
+ ## Usage
24
+
25
+ Open your IRB console with:
26
+
27
+ $ irb
28
+
29
+ The start the game with the following command:
30
+
31
+ > Tictactoe.start
32
+
33
+ And let the fun begin! (So to say).
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jaimelr/tictactoe_gto. This project is intended to be a safe, welcoming space for collaboration.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the TictactoeGto project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jaimelr/tictactoe_gto/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tictactoe_gto"
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__)
@@ -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,329 @@
1
+ require "tictactoe_gto/version"
2
+
3
+ module TictactoeGto
4
+ class Box
5
+
6
+ attr_accessor :value
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ def display
13
+ print "| #{@value} |"
14
+ end
15
+
16
+ def is_empty?
17
+ if @value == ' '
18
+ return true
19
+ else
20
+ return false
21
+ end
22
+ end
23
+ end
24
+
25
+ class Board
26
+
27
+ attr_reader :array
28
+ attr_reader :size
29
+
30
+ def initialize(size)
31
+ @size = size
32
+ end
33
+
34
+ def fill
35
+ @array = Array.new(@size) do
36
+ Array.new(@size) { Box.new(' ') }
37
+ end
38
+ end
39
+
40
+ def display
41
+ print ' '
42
+ @size.times { |i| print ' ' + i.to_s + ' ' }
43
+ puts "\n"
44
+ @array.each_with_index do |row, index|
45
+ print index.to_s + ' '
46
+ row.each { |box| box.display }
47
+ print "\n"
48
+ end
49
+ end
50
+
51
+ def fill_at(pos, id)
52
+ box = @array[pos['x']][pos['y']]
53
+ if box.is_empty?
54
+ box.value = id
55
+ else
56
+ puts "> That spot is not available. Yo missed your chance :c"
57
+ end
58
+ end
59
+
60
+ def self.get_size
61
+ loop do
62
+ print 'Ingresa el tamaño del tablero: '
63
+ begin
64
+ board_size = gets.chomp
65
+ board_size = Integer(board_size)
66
+ rescue
67
+ print 'Are you sure it is a number? Please try again: '
68
+ retry
69
+ end
70
+ if board_size < 2
71
+ puts 'Board size must be 2X2 or higher, try again.'
72
+ else
73
+ puts 'Get ready!'
74
+ return board_size
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ class Player
81
+
82
+ attr_accessor :id
83
+
84
+ def initialize(id)
85
+ @id = id
86
+ end
87
+
88
+ def customize?(id)
89
+ print "Player #{id}: Would you like to customize your ID? (y/n): "
90
+ loop do
91
+ option = gets.chomp
92
+ 30.times { print '_ ' }
93
+ print "\n"
94
+ if option == 'y'
95
+ puts 'Nice! Here we go!'
96
+ return true
97
+ elsif option == 'n'
98
+ puts 'Nice! Old school!'
99
+ return false
100
+ else
101
+ puts '*Confused* I did not understand your input, could you try again, please?'
102
+ puts "HINT: 'y' is for: 'Hell yeah!' and 'n' is for: 'No, thanks. Maybe later'"
103
+ end
104
+ 30.times { print '_ ' }
105
+ print "\n"
106
+ end
107
+ end
108
+ end
109
+
110
+ class Game
111
+ @winner
112
+
113
+ def check_row(board, value)
114
+ board.array.each do |row|
115
+ if row.all? { |box| box.value == value }
116
+ gameover_message value
117
+ return true
118
+ end
119
+ end
120
+
121
+ return false
122
+ end
123
+
124
+ def check_col(board, value)
125
+ points = 0
126
+ board.size.times do |index|
127
+ board.array.each do |row|
128
+ (row[index].is_empty? || row[index].value != value) ? points = 0 : points += 1
129
+ end
130
+ if points == board.size
131
+ gameover_message value
132
+ return true
133
+ end
134
+ end
135
+
136
+ return false
137
+ end
138
+
139
+ def check_diagonal(board, value)
140
+ indexD1 = 0
141
+ indexD2 = board.size - 1
142
+ pointsD1 = 0
143
+ pointsD2 = 0
144
+ board.array.each do |row|
145
+ pointsD1 += 1 if row[indexD1].value == value
146
+ pointsD2 += 1 if row[indexD2].value == value
147
+ indexD1 += 1
148
+ indexD2 -= 1
149
+ end
150
+
151
+ if pointsD1 == board.size || pointsD2 == board.size
152
+ gameover_message value
153
+ return true
154
+ end
155
+
156
+ return false
157
+ end
158
+
159
+ def over?(board, players)
160
+ players.each_with_index do |player, index|
161
+ if check_row(board, players[index].id) || check_col(board, players[index].id) || check_diagonal(board, players[index].id)
162
+ @winner = players[index].id
163
+ return true
164
+ end
165
+ end
166
+ if draw?(board)
167
+ @winner = nil
168
+ return true
169
+ end
170
+
171
+ return false
172
+ end
173
+
174
+ def draw?(board)
175
+ num = 0
176
+ board.array.each do |row|
177
+ row.each do |box|
178
+ num += 1 if box.value == ' '
179
+ end
180
+ end
181
+
182
+ if num == 0
183
+ gameover_message ' '
184
+ return true
185
+ end
186
+
187
+ return false
188
+ end
189
+
190
+ def move(board, player)
191
+ puts "It's your turn Player #{player.id}!"
192
+ coordinates = get_position(board)
193
+ board.fill_at(coordinates, player.id)
194
+ board.display
195
+
196
+ return board
197
+ end
198
+
199
+ def get_position(board)
200
+ coordinates = {x: 0, y: 0}
201
+ loop do
202
+ print 'Enter the number of the row: '
203
+ begin
204
+ coordinates['x'] = gets.chomp
205
+ coordinates['x'] = Integer(coordinates['x'])
206
+ rescue
207
+ print 'Are you sure it is a number? Please try again: '
208
+ retry
209
+ end
210
+ break if coordinates['x'] < board.size && coordinates['x'] >= 0
211
+ puts 'That row is out of the board! Try again!'
212
+ end
213
+ loop do
214
+ print 'Enter the number of the column: '
215
+ begin
216
+ coordinates['y'] = gets.chomp
217
+ coordinates['y'] = Integer(coordinates['y'])
218
+ rescue
219
+ print 'Are you sure it is a number? Please try again: '
220
+ retry
221
+ end
222
+ break if coordinates['y'] < board.size && coordinates['y'] >= 0
223
+ puts 'That column is out of the board! Try again!'
224
+ end
225
+
226
+ coordinates
227
+ end
228
+
229
+ def play(players, board)
230
+ players.reverse! unless @winner.nil?
231
+ players.each do |player|
232
+ board = move(board, player)
233
+ if over?(board, players)
234
+ return false
235
+ end
236
+ end
237
+
238
+ return true
239
+ end
240
+
241
+ def play_again?
242
+ loop do
243
+ puts 'Nice moves out there!'
244
+ print 'Would you like to play again (y/n): '
245
+ option = gets.chomp
246
+ 30.times { print '_ ' }
247
+ print "\n"
248
+ if option == 'y'
249
+ puts 'Yeah! Here we go! /.__./'
250
+ return true
251
+ elsif option == 'n'
252
+ puts ':( We are sad that you are leaving us. Hope to see you soon!'
253
+ return false
254
+ else
255
+ puts '*Confused* I did not understand your input, could you try again, please?'
256
+ puts "HINT: 'y' is for: 'Hell yeah! Lets play again!' and 'n' is for: 'No, thanks. Maybe later'"
257
+ end
258
+ 30.times { print '_ ' }
259
+ print "\n"
260
+ end
261
+ end
262
+
263
+ def gameover_message(value)
264
+ 50.times { print '-' }
265
+ print "\n"
266
+ if value == ' '
267
+ puts 'DRAW GAME!'
268
+ puts 'No winner this time.'
269
+ else
270
+ puts "Game over, Player #{value} wins."
271
+ end
272
+ 50.times { print '-' }
273
+ print "\n"
274
+ end
275
+
276
+ def customize_id(players)
277
+ players.each_with_index do |player, index|
278
+ if player.customize?(index + 1)
279
+ puts "Player #{index + 1}: Enter any letter or symbol you want (just one): "
280
+ id = gets.chomp
281
+ puts 'I\'ll take just the first letter :D'if id.size > 1
282
+ player.id = id[0]
283
+ end
284
+ end
285
+
286
+ return players
287
+ end
288
+
289
+ def set_player1(players)
290
+ case @last_player
291
+ when players[0].id
292
+ return players.reverse!
293
+ when players[1].id
294
+ return players
295
+ end
296
+ end
297
+
298
+ private :check_col, :check_row, :check_diagonal, :gameover_message, :get_position
299
+ end
300
+
301
+ class Tictactoe
302
+ def self.start
303
+ puts '>>> Tic Tac Toe - Ruby version'
304
+ puts '>> Powered by: @JJaimelr'
305
+ puts '> Welcome'
306
+
307
+ board_size = Board.get_size
308
+ board = Board.new(board_size)
309
+ players = [Player.new('X'), Player.new('O')]
310
+ game = Game.new
311
+
312
+ players = game.customize_id(players)
313
+ board.fill
314
+ board.display
315
+
316
+ loop do
317
+ if !game.play(players, board)
318
+ break unless game.play_again?
319
+ board_size = Board.get_size
320
+ board = Board.new(board_size)
321
+ players = game.set_player1 players
322
+ players = game.customize_id players
323
+ board.fill
324
+ board.display
325
+ end
326
+ end
327
+ end
328
+ end
329
+ end
@@ -0,0 +1,3 @@
1
+ module TictactoeGto
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tictactoe_gto/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tictactoe_gto"
8
+ spec.version = TictactoeGto::VERSION
9
+ spec.authors = ["Jaimelr"]
10
+ spec.email = ["j.loyolarangel@gmail.com"]
11
+
12
+ spec.summary = %q{A modifiy version of the classic TicTacToe game}
13
+ spec.description = %q{This is an improved version of the classic game TicTacToe with new features such as customizable IDs and the posibility to play on boards of any size (bigger than 2)}
14
+ spec.homepage = "https://github.com/jaimelr/tictactoe_gto"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tictactoe_gto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jaimelr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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
+ description: This is an improved version of the classic game TicTacToe with new features
42
+ such as customizable IDs and the posibility to play on boards of any size (bigger
43
+ than 2)
44
+ email:
45
+ - j.loyolarangel@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/tictactoe_gto.rb
59
+ - lib/tictactoe_gto/version.rb
60
+ - tictactoe_gto.gemspec
61
+ homepage: https://github.com/jaimelr/tictactoe_gto
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ allowed_push_host: https://rubygems.org
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.7.6
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A modifiy version of the classic TicTacToe game
86
+ test_files: []