ruby-tic-tac-toe 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ruby-tic-tac-toe.rb +40 -8
  2. metadata +4 -4
@@ -3,13 +3,6 @@ require "color_text"
3
3
  class Tic
4
4
 
5
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
6
  #map of all places that are possible wins
14
7
  @columns = [
15
8
  [:a1,:a2,:a3],
@@ -34,13 +27,32 @@ class Tic
34
27
  STDOUT.flush
35
28
  @user_name = gets.chomp
36
29
  put_bar
30
+
31
+ @user_score = 0
32
+ @cpu_score = 0
37
33
 
38
- if(@user == 'X')
34
+ start_game(@user == 'X')
35
+ end
36
+
37
+ def start_game(user_goes_first)
38
+ #the tic tac toe slots
39
+ @places = {
40
+ a1:" ",a2:" ",a3:" ",
41
+ b1:" ",b2:" ",b3:" ",
42
+ c1:" ",c2:" ",c3:" "
43
+ }
44
+
45
+ if user_goes_first
39
46
  user_turn
40
47
  else
41
48
  cpu_turn
42
49
  end
43
50
  end
51
+
52
+ def restart_game(user_goes_first)
53
+ (1...20).each { |i| put_line }
54
+ start_game(user_goes_first)
55
+ end
44
56
 
45
57
  def put_line
46
58
  puts ("-" * 80).gray
@@ -52,6 +64,8 @@ class Tic
52
64
  end
53
65
 
54
66
  def draw_game
67
+ puts ""
68
+ puts " Wins: #{@cpu_name}:#{@cpu_score} #{@user_name}:#{@user_score}".gray
55
69
  puts ""
56
70
  puts " #{@cpu_name}: #{@cpu.green}"
57
71
  puts " #{@user_name}: #{@user.green}"
@@ -190,6 +204,8 @@ class Tic
190
204
  puts ""
191
205
  puts " Game Over -- #{@cpu_name} WINS!!!\n".blue
192
206
  game_over = true
207
+ @cpu_score += 1
208
+ ask_to_play_again(false)
193
209
  end
194
210
  # see if user has won
195
211
  if times_in_column(column, @user) == 3
@@ -199,6 +215,8 @@ class Tic
199
215
  puts ""
200
216
  puts " Game Over -- #{@user_name} WINS!!!\n".blue
201
217
  game_over = true
218
+ @user_score += 1
219
+ ask_to_play_again(true)
202
220
  end
203
221
  end
204
222
 
@@ -215,8 +233,22 @@ class Tic
215
233
  put_line
216
234
  puts ""
217
235
  puts " Game Over -- DRAW!\n".blue
236
+ ask_to_play_again(rand() > 0.5)
218
237
  end
219
238
  end
220
239
  end
240
+
241
+ def ask_to_play_again(user_goes_first)
242
+ print " Play again? (Yn): "
243
+ STDOUT.flush
244
+ response = gets.chomp.downcase
245
+ case response
246
+ when "y" then restart_game(user_goes_first)
247
+ when "yes" then restart_game(user_goes_first)
248
+ when "n" then #do nothing
249
+ when "no" then #do nothing
250
+ else ask_to_play_again(user_goes_first)
251
+ end
252
+ end
221
253
 
222
254
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-tic-tac-toe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-01 00:00:00.000000000 Z
12
+ date: 2012-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: color_text
16
- requirement: &70116175839360 !ruby/object:Gem::Requirement
16
+ requirement: &70215224824520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 0.0.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70116175839360
24
+ version_requirements: *70215224824520
25
25
  description: Run 't3' in terminal after installing
26
26
  email: john.isaacks@programming-perils.com
27
27
  executables: