tictactoe 0.0.1 → 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.
- data/lib/tictactoe.rb +45 -43
- metadata +1 -1
data/lib/tictactoe.rb
CHANGED
@@ -2,11 +2,13 @@ require "color_text"
|
|
2
2
|
|
3
3
|
class Tictactoe
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
def initialize
|
6
|
+
@moves = (0..8).to_a
|
7
|
+
@computer_move = 4
|
8
|
+
@playing = true
|
9
|
+
@message = "Please enter your move".blue
|
10
|
+
@continue = true
|
11
|
+
end
|
10
12
|
|
11
13
|
def play_game
|
12
14
|
play_tictactoe
|
@@ -25,23 +27,23 @@ class Tictactoe
|
|
25
27
|
def play_tictactoe
|
26
28
|
display_board
|
27
29
|
@start_game = interactive
|
28
|
-
while
|
30
|
+
while @continue
|
29
31
|
print "\e[2J\e[f" # clear screen
|
30
32
|
case @start_game
|
31
33
|
when 'y'
|
32
34
|
display_board
|
33
35
|
competitor_plays
|
34
|
-
break if
|
36
|
+
break if !@continue
|
35
37
|
computer_plays
|
36
|
-
break if
|
38
|
+
break if !@continue
|
37
39
|
when 'q'
|
38
40
|
break
|
39
41
|
when 'n'
|
40
42
|
computer_plays
|
41
43
|
display_board
|
42
|
-
break if
|
44
|
+
break if !@continue
|
43
45
|
competitor_plays
|
44
|
-
break if
|
46
|
+
break if !@continue
|
45
47
|
else
|
46
48
|
display_board
|
47
49
|
puts 'Please enter appropriate options'.red
|
@@ -56,18 +58,18 @@ class Tictactoe
|
|
56
58
|
end
|
57
59
|
|
58
60
|
def computer_plays
|
59
|
-
if
|
61
|
+
if @playing
|
60
62
|
computer_moves
|
61
|
-
|
63
|
+
@continue = continue_playing
|
62
64
|
end
|
63
65
|
end
|
64
66
|
|
65
67
|
def competitor_plays
|
66
|
-
if
|
67
|
-
puts
|
68
|
+
if @continue
|
69
|
+
puts @message
|
68
70
|
move = gets.chomp
|
69
71
|
play(move)
|
70
|
-
|
72
|
+
@continue = continue_playing
|
71
73
|
end
|
72
74
|
end
|
73
75
|
|
@@ -81,52 +83,52 @@ class Tictactoe
|
|
81
83
|
|
82
84
|
def play(move)
|
83
85
|
if move.size > 1
|
84
|
-
|
85
|
-
|
86
|
-
puts
|
87
|
-
elsif
|
88
|
-
|
89
|
-
|
90
|
-
puts
|
86
|
+
@playing = false
|
87
|
+
@message = "Please enter appropriate move".red
|
88
|
+
puts @message
|
89
|
+
elsif @moves.values_at(move.to_i).first == "X" || @moves.values_at(move.to_i).first == "O"
|
90
|
+
@playing = false
|
91
|
+
@message = "Please choose another move, it's already taken".red
|
92
|
+
puts @message
|
91
93
|
elsif !is_numeric(move)
|
92
|
-
|
93
|
-
|
94
|
-
puts
|
94
|
+
@playing = false
|
95
|
+
@message = "Please enter correct move".red
|
96
|
+
puts @message
|
95
97
|
else
|
96
|
-
position =
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
position = @moves.index(move.to_i)
|
99
|
+
@playing = true
|
100
|
+
@message = "Please enter your move".blue
|
101
|
+
@moves[position] = "O"
|
100
102
|
end
|
101
103
|
end
|
102
104
|
|
103
105
|
def computer_moves
|
104
|
-
temp_moves =
|
106
|
+
temp_moves = @moves.clone
|
105
107
|
temp_moves.delete("O")
|
106
108
|
temp_moves.delete("X")
|
107
|
-
if temp_moves.size ==
|
108
|
-
|
109
|
+
if temp_moves.size == @moves.size
|
110
|
+
@moves[4] = "X"
|
109
111
|
elsif computer_best_move
|
110
|
-
|
111
|
-
|
112
|
+
@computer_move = computer_best_move
|
113
|
+
@moves[computer_best_move] = "X"
|
112
114
|
elsif competitor_best_move
|
113
|
-
|
114
|
-
|
115
|
+
@computer_move = competitor_best_move
|
116
|
+
@moves[competitor_best_move] = "X"
|
115
117
|
else
|
116
118
|
indexes = temp_moves.inject([]) do |result, element|
|
117
|
-
result <<
|
119
|
+
result << @moves.index(element)
|
118
120
|
result
|
119
121
|
end
|
120
122
|
position_index = rand(0...indexes.size)
|
121
123
|
values_position = indexes.values_at(position_index).first
|
122
|
-
|
123
|
-
|
124
|
+
@computer_move = values_position
|
125
|
+
@moves[values_position] = "X"
|
124
126
|
end
|
125
127
|
end
|
126
128
|
|
127
129
|
def display_board
|
128
130
|
board = ""
|
129
|
-
|
131
|
+
@moves.each_with_index do |move, index|
|
130
132
|
case move
|
131
133
|
when 'X'
|
132
134
|
color_moves = "#{move}".purple
|
@@ -157,7 +159,7 @@ class Tictactoe
|
|
157
159
|
end
|
158
160
|
|
159
161
|
def play_until_draw
|
160
|
-
unmoved =
|
162
|
+
unmoved = @moves.select do |move|
|
161
163
|
(0..10).include?(move)
|
162
164
|
end
|
163
165
|
!(unmoved.any?)
|
@@ -165,14 +167,14 @@ class Tictactoe
|
|
165
167
|
|
166
168
|
def get_values
|
167
169
|
score_values.inject([]) do |result, v|
|
168
|
-
result <<
|
170
|
+
result << @moves.values_at(v[0], v[1], v[2]).uniq
|
169
171
|
result
|
170
172
|
end
|
171
173
|
end
|
172
174
|
|
173
175
|
def best_move(check_moves_of, replace_with)
|
174
176
|
score_values.each do |v|
|
175
|
-
result =
|
177
|
+
result = @moves.values_at(v[0], v[1], v[2])
|
176
178
|
if result.include?("#{check_moves_of}") && result.count("#{check_moves_of}") == 2
|
177
179
|
result.delete("#{check_moves_of}")
|
178
180
|
if result.first == "#{replace_with}"
|