ruby-tic-tac-toe 0.0.2

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.
Files changed (3) hide show
  1. data/bin/t3 +4 -0
  2. data/lib/ruby-tic-tac-toe.rb +222 -0
  3. metadata +58 -0
data/bin/t3 ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ruby-tic-tac-toe'
4
+ Tic.new
@@ -0,0 +1,222 @@
1
+ require "color_text"
2
+
3
+ class Tic
4
+
5
+ def initialize
6
+ #the tic tac toe slots
7
+ @places = {
8
+ a1:" ",a2:" ",a3:" ",
9
+ b1:" ",b2:" ",b3:" ",
10
+ c1:" ",c2:" ",c3:" "
11
+ }
12
+
13
+ #map of all places that are possible wins
14
+ @columns = [
15
+ [:a1,:a2,:a3],
16
+ [:b1,:b2,:b3],
17
+ [:c1,:c2,:c3],
18
+
19
+ [:a1,:b1,:c1],
20
+ [:a2,:b2,:c2],
21
+ [:a3,:b3,:c3],
22
+
23
+ [:a1,:b2,:c3],
24
+ [:c1,:b2,:a3]
25
+ ]
26
+
27
+ @cpu = rand() > 0.5 ? 'X' : 'O'
28
+ @user = @cpu == 'X' ? 'O' : 'X'
29
+
30
+ @cpu_name = "Ruby"
31
+ put_line
32
+ puts "\n RUBY TIC TAC TOE".purple
33
+ print "\n What is your name? ".neon
34
+ STDOUT.flush
35
+ @user_name = gets.chomp
36
+ put_bar
37
+
38
+ if(@user == 'X')
39
+ user_turn
40
+ else
41
+ cpu_turn
42
+ end
43
+ end
44
+
45
+ def put_line
46
+ puts ("-" * 80).gray
47
+ end
48
+
49
+ def put_bar
50
+ puts ("#" * 80).gray
51
+ puts ("#" * 80).gray
52
+ end
53
+
54
+ def draw_game
55
+ puts ""
56
+ puts " #{@cpu_name}: #{@cpu.green}"
57
+ puts " #{@user_name}: #{@user.green}"
58
+ puts ""
59
+ puts " a b c".gray
60
+ puts ""
61
+ puts " 1 #{@places[:a1].green}|#{@places[:b1].green}|#{@places[:c1].green}".gray
62
+ puts " -----"
63
+ puts " 2 #{@places[:a2].green}|#{@places[:b2].green}|#{@places[:c2].green}".gray
64
+ puts " -----"
65
+ puts " 3 #{@places[:a3].green}|#{@places[:b3].green}|#{@places[:c3].green}".gray
66
+ end
67
+
68
+ def cpu_turn
69
+ move = cpu_find_move
70
+ @places[move] = @cpu
71
+ put_line
72
+ puts " #{@cpu_name} marks #{move.to_s.upcase.green}".neon
73
+ check_game(@user)
74
+ end
75
+
76
+ def cpu_find_move
77
+
78
+ # see if cpu can win
79
+ #see if any columns already have 2 (cpu)
80
+ @columns.each do |column|
81
+ if times_in_column(column, @cpu) == 2
82
+ return empty_in_column column
83
+ end
84
+ end
85
+
86
+ # see if user can win
87
+ #see if any columns already have 2 (user)
88
+ @columns.each do |column|
89
+ if times_in_column(column, @user) == 2
90
+ return empty_in_column column
91
+ end
92
+ end
93
+
94
+ #see if any columns aready have 1 (cpu)
95
+ @columns.each do |column|
96
+ if times_in_column(column, @cpu) == 1
97
+ return empty_in_column column
98
+ end
99
+ end
100
+
101
+ #no strategic spot found so just find a random empty
102
+ k = @places.keys;
103
+ i = rand(k.length)
104
+ if @places[k[i]] == " "
105
+ return k[i]
106
+ else
107
+ #random selection is taken so just find the first empty slot
108
+ @places.each { |k,v| return k if v == " " }
109
+ end
110
+ end
111
+
112
+ def times_in_column arr, item
113
+ times = 0
114
+ arr.each do |i|
115
+ times += 1 if @places[i] == item
116
+ unless @places[i] == item || @places[i] == " "
117
+ #oppisite piece is in column so column cannot be used for win.
118
+ #therefore, the strategic thing to do is choose a dif column so return 0.
119
+ return 0
120
+ end
121
+ end
122
+ times
123
+ end
124
+
125
+ def empty_in_column arr
126
+ arr.each do |i|
127
+ if @places[i] == " "
128
+ return i
129
+ end
130
+ end
131
+ end
132
+
133
+ def user_turn
134
+ put_line
135
+ puts "\n RUBY TIC TAC TOE".purple
136
+ draw_game
137
+ print "\n #{@user_name}, please make a move or type 'exit' to quit: ".neon
138
+ STDOUT.flush
139
+ input = gets.chomp.downcase.to_sym
140
+ put_bar
141
+ if input.length == 2
142
+ a = input.to_s.split("")
143
+ if(['a','b','c'].include? a[0])
144
+ if(['1','2','3'].include? a[1])
145
+ if @places[input] == " "
146
+ @places[input] = @user
147
+ put_line
148
+ puts " #{@user_name} marks #{input.to_s.upcase.green}".neon
149
+ check_game(@cpu)
150
+ else
151
+ wrong_move
152
+ end
153
+ else
154
+ wrong_input
155
+ end
156
+ else
157
+ wrong_input
158
+ end
159
+ else
160
+ wrong_input unless input == :exit
161
+ end
162
+ end
163
+
164
+ def wrong_input
165
+ put_line
166
+ puts " Please specify a move with the format 'A1' , 'B3' , 'C2' etc.".red
167
+ user_turn
168
+ end
169
+
170
+ def wrong_move
171
+ put_line
172
+ puts " You must choose an empty slot".red
173
+ user_turn
174
+ end
175
+
176
+ def moves_left
177
+ @places.values.select{ |v| v == " " }.length
178
+ end
179
+
180
+ def check_game(next_turn)
181
+
182
+ game_over = nil
183
+
184
+ @columns.each do |column|
185
+ # see if cpu has won
186
+ if times_in_column(column, @cpu) == 3
187
+ put_line
188
+ draw_game
189
+ put_line
190
+ puts ""
191
+ puts " Game Over -- #{@cpu_name} WINS!!!\n".blue
192
+ game_over = true
193
+ end
194
+ # see if user has won
195
+ if times_in_column(column, @user) == 3
196
+ put_line
197
+ draw_game
198
+ put_line
199
+ puts ""
200
+ puts " Game Over -- #{@user_name} WINS!!!\n".blue
201
+ game_over = true
202
+ end
203
+ end
204
+
205
+ unless game_over
206
+ if(moves_left > 0)
207
+ if(next_turn == @user)
208
+ user_turn
209
+ else
210
+ cpu_turn
211
+ end
212
+ else
213
+ put_line
214
+ draw_game
215
+ put_line
216
+ puts ""
217
+ puts " Game Over -- DRAW!\n".blue
218
+ end
219
+ end
220
+ end
221
+
222
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-tic-tac-toe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - JD Isaacks
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: color_text
16
+ requirement: &70228231026880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70228231026880
25
+ description: Run 't3' in terminal after installing
26
+ email: john.isaacks@programming-perils.com
27
+ executables:
28
+ - t3
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/ruby-tic-tac-toe.rb
33
+ - bin/t3
34
+ homepage: https://github.com/jisaacks/ruby-tic-tac-toe
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.15
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Play tic tac toe in your terminal!
58
+ test_files: []