tic_tac_doh 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/tic_tac_doh/version.rb +3 -0
- data/lib/tic_tac_doh.rb +174 -0
- data/tic_tac_doh.gemspec +23 -0
- metadata +83 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 1432bf6b0e89bb2ef38b66f21550d7a7d52e9b7c
|
|
4
|
+
data.tar.gz: 54b098d8bfe2e4f98e5d600976397f51ca8259e1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 79ff8724bf978182c2db1f31503617466d586a0bca732109495810376854bda6fc38cf73ddf5811f40bffc60d619175cac345d189ecd6e8639fb3741c3dd4349
|
|
7
|
+
data.tar.gz: 3ab8dc72ce96c4bd0cf4538b9c4bd47e39a76b141aed65a786b71df5ce1fe6fa5d47c1e4f8fded919ee1753443c6bca5e017cdc6a8c099d8d0503b0b55f08e99
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 David Cuadra
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# TicTacDoh
|
|
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/tic_tac_doh`. 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 'tic_tac_doh'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install tic_tac_doh
|
|
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 `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` to 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
|
+
1. Fork it ( https://github.com/[my-github-username]/tic_tac_doh/fork )
|
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "tic_tac_doh"
|
|
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
|
data/bin/setup
ADDED
data/lib/tic_tac_doh.rb
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
require "tic_tac_doh/version"
|
|
2
|
+
|
|
3
|
+
module TicTacDoh
|
|
4
|
+
class Game
|
|
5
|
+
# X starts
|
|
6
|
+
# Previous winner starts new game
|
|
7
|
+
# or which turn would have followed if draw
|
|
8
|
+
|
|
9
|
+
attr_reader :players, :grid, :turn
|
|
10
|
+
|
|
11
|
+
def initialize(args={})
|
|
12
|
+
@players = []
|
|
13
|
+
@turn = 1
|
|
14
|
+
@players << args[:player1] << args[:player2]
|
|
15
|
+
args.fetch(:size, 3).times do
|
|
16
|
+
@grid = Array.new(3) {}
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def play
|
|
21
|
+
# puts grid.length
|
|
22
|
+
# puts players.count
|
|
23
|
+
prepare_grid()
|
|
24
|
+
player_turn ||= 0
|
|
25
|
+
until game_over
|
|
26
|
+
clear_screen
|
|
27
|
+
print_grid
|
|
28
|
+
# puts "turn: #{@turn}"
|
|
29
|
+
turn(players[player_turn])
|
|
30
|
+
player_turn = player_turn == (players.count - 1) ? 0 : player_turn + 1
|
|
31
|
+
@turn = @turn + 1 unless game_over
|
|
32
|
+
end
|
|
33
|
+
clear_screen
|
|
34
|
+
print_grid
|
|
35
|
+
puts "Game Over"
|
|
36
|
+
# puts @turn
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def prepare_grid(size=grid.length)
|
|
42
|
+
@grid = []
|
|
43
|
+
array = []
|
|
44
|
+
for cell in 0...size
|
|
45
|
+
for cell2 in cell * size...(cell * size + size)
|
|
46
|
+
array << cell2
|
|
47
|
+
end
|
|
48
|
+
@grid << array
|
|
49
|
+
array = []
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def game_over
|
|
54
|
+
# Winner
|
|
55
|
+
if symbol_winner?
|
|
56
|
+
return true
|
|
57
|
+
end
|
|
58
|
+
# No more free cells
|
|
59
|
+
@grid.each do |row|
|
|
60
|
+
row.each do |cell|
|
|
61
|
+
return false if cell.is_a? Numeric
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
clear_screen
|
|
65
|
+
print_grid
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def symbol_winner?
|
|
70
|
+
winner_mark = nil
|
|
71
|
+
grid.length.times do |n|
|
|
72
|
+
grid.length.times do |m|
|
|
73
|
+
winner_mark ||= secuential_mark(grid[n][m], n,0,0,1) # horizontal search
|
|
74
|
+
winner_mark ||= secuential_mark(grid[m][n], 0,n,1,0) # vertical search
|
|
75
|
+
winner_mark ||= secuential_mark(grid[m][n], m,n,1,1) # diagonal +
|
|
76
|
+
winner_mark ||= secuential_mark(grid[m][n], m,n,-1,1) # diagonal -
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
return winner_mark
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def secuential_mark(mark, row, col, row_step, col_step, counter=1)
|
|
83
|
+
return nil unless in_range?(row, col)
|
|
84
|
+
if grid[row][col] == mark
|
|
85
|
+
return grid[row][col] if counter == 3
|
|
86
|
+
return secuential_mark(mark, row + row_step, col + col_step, row_step, col_step, counter+1)
|
|
87
|
+
else
|
|
88
|
+
return nil
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def in_range? (arg, col)
|
|
93
|
+
if arg.is_a? Hash
|
|
94
|
+
return true if arg[:row] < grid.length && arg[:col] < grid.length if arg[:row] >= 0 && arg[:col] >= 0
|
|
95
|
+
else
|
|
96
|
+
return true if arg < grid.length && col < grid.length if arg >= 0 && col >= 0
|
|
97
|
+
end
|
|
98
|
+
false
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def print_grid
|
|
102
|
+
grid.each do |row|
|
|
103
|
+
puts row.join('|')
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def turn(player)
|
|
108
|
+
player_action player
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def player_action (player)
|
|
112
|
+
valid_move = false
|
|
113
|
+
until valid_move == true do
|
|
114
|
+
position = calculate_cell(player_move player)
|
|
115
|
+
valid_move = insert_player_move_in_grid(position, player_mark(player))
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def clear_screen
|
|
120
|
+
system 'clear' or system 'cls'
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def calculate_cell(cell_number)
|
|
124
|
+
row = 0
|
|
125
|
+
for n in 0..cell_number
|
|
126
|
+
row += 1 if (n % grid.size) == 0 unless n == 0
|
|
127
|
+
end
|
|
128
|
+
column = cell_number - (row * grid.size)
|
|
129
|
+
{row: row, column: column}
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def insert_player_move_in_grid(position, mark)
|
|
133
|
+
return false unless valid_move? position
|
|
134
|
+
@grid[position[:row]][position[:column]] = mark
|
|
135
|
+
true
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def valid_move?(position)
|
|
139
|
+
return false if position[:row] < 0 || position[:row] > (@grid.length) -1
|
|
140
|
+
return false if position[:column] < 0 || position[:column] > (@grid.length) -1
|
|
141
|
+
return false unless @grid[position[:row]][position[:column]].is_a? Numeric
|
|
142
|
+
true
|
|
143
|
+
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def player_move(player)
|
|
147
|
+
player.move()
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def player_mark(player)
|
|
151
|
+
player.mark
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
class Player
|
|
156
|
+
attr_reader :nickname, :score, :mark
|
|
157
|
+
|
|
158
|
+
def initialize(args={})
|
|
159
|
+
@nickname = args.fetch(:nickname, "Anonymous#{rand(100)}")
|
|
160
|
+
@score = 0
|
|
161
|
+
@mark = args[:mark]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def move()
|
|
165
|
+
puts "#{self.mark} turn "
|
|
166
|
+
puts "Pick a position"
|
|
167
|
+
gets.chomp.to_i
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def add_to_score(score)
|
|
171
|
+
@score += score
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
data/tic_tac_doh.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'tic_tac_doh/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "tic_tac_doh"
|
|
8
|
+
spec.version = TicTacDoh::VERSION
|
|
9
|
+
spec.authors = ["David Cuadra Quevedo"]
|
|
10
|
+
spec.email = ["dcuadraq@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Gem for playing Tic Tac Toe.}
|
|
13
|
+
spec.description = %q{Sizeable tic tac toe game.}
|
|
14
|
+
spec.homepage = "https://github.com/dcuadraq/tic_tac_doh"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
17
|
+
spec.bindir = "exe"
|
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tic_tac_doh
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- David Cuadra Quevedo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-04-24 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.9'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.9'
|
|
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: Sizeable tic tac toe game.
|
|
42
|
+
email:
|
|
43
|
+
- dcuadraq@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".gitignore"
|
|
49
|
+
- ".rspec"
|
|
50
|
+
- ".travis.yml"
|
|
51
|
+
- Gemfile
|
|
52
|
+
- LICENSE
|
|
53
|
+
- README.md
|
|
54
|
+
- Rakefile
|
|
55
|
+
- bin/console
|
|
56
|
+
- bin/setup
|
|
57
|
+
- lib/tic_tac_doh.rb
|
|
58
|
+
- lib/tic_tac_doh/version.rb
|
|
59
|
+
- tic_tac_doh.gemspec
|
|
60
|
+
homepage: https://github.com/dcuadraq/tic_tac_doh
|
|
61
|
+
licenses: []
|
|
62
|
+
metadata: {}
|
|
63
|
+
post_install_message:
|
|
64
|
+
rdoc_options: []
|
|
65
|
+
require_paths:
|
|
66
|
+
- lib
|
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - ">="
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '0'
|
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
requirements: []
|
|
78
|
+
rubyforge_project:
|
|
79
|
+
rubygems_version: 2.4.5
|
|
80
|
+
signing_key:
|
|
81
|
+
specification_version: 4
|
|
82
|
+
summary: Gem for playing Tic Tac Toe.
|
|
83
|
+
test_files: []
|