/
tic_tac_toe.rb
156 lines (144 loc) · 4.18 KB
/
tic_tac_toe.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class Game
def initialize(p1, p2)
@p1 = p1
@p2 = p2
@line_1 = ['1', '2', '3']
@line_2 = ['4', '5', '6']
@line_3 = ['7', '8', '9']
@mark = 'X'
@turn_count = 0
end
def whose_turn
if @mark == 'X'
puts "Now it\'s #{@p2}\'s turn."
@mark = 'O'
else
puts "Now it\'s #{@p1}\'s turn."
@mark = 'X'
end
puts ''
end
def input(x)
if x >= 1 and x <= 3 and @line_1[x - 1] == x.to_s
@line_1[x - 1] = @mark
elsif x >= 4 and x <= 6 and @line_2[x - 4] == x.to_s
@line_2[x - 4] = @mark
elsif x >= 7 and x <= 9 and @line_3[x - 7] == x.to_s
@line_3[x - 7] = @mark
else
puts "You have to input a valid position."
new_x = gets.chomp
new_x = new_x.to_i
self.input(new_x)
end
@turn_count += 1
end
def show_table
print @line_1
puts ''
print @line_2
puts ''
print @line_3
puts ''
end
def victory_check
if @turn_count >= 5
if @line_1[0] == @line_1[1] and @line_1[1] == @line_1[2]
if @line_1[0] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_2[0] == @line_2[1] and @line_2[1] == @line_2[2]
if @line_2[0] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_3[0] == @line_3[1] and @line_3[1] == @line_3[2]
if @line_3[0] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_1[0] == @line_2[0] and @line_2[0] == @line_3[0]
if @line_1[0] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_1[1] == @line_2[1] and @line_2[1] == @line_3[1]
if @line_1[1] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_1[2] == @line_2[2] and @line_2[2] == @line_3[2]
if @line_1[2] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_1[0] == @line_2[1] and @line_2[1] == @line_3[2]
if @line_1[0] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @line_1[2] == @line_2[1] and @line_2[1] == @line_3[0]
if @line_1[2] == 'X'
puts "Winner: #{@p1}"
return 1
else
puts "Winner: #{@p2}"
return 1
end
elsif @turn_count == 9
puts "It is a draw."
return 1
end
end
end
end
print "Name for player 1: "
player_1 = gets.chomp
print "Name for player 2: "
player_2 = gets.chomp
playing = true
match = Game.new(player_1, player_2)
puts 'Tic-Tac-Toe - Ruby GUIless Edition'
puts ''
puts 'The game always starts with the first player marking X and the second marking 0 on the table.'
puts 'Have fun.'
puts ''
puts "#{player_1}, choose a position on the table."
puts ''
match.show_table
puts ''
while playing
play = gets.chomp
puts ''
play = play.to_i
match.input(play)
match.show_table
puts ''
if match.victory_check == 1
playing = false
else
match.whose_turn
end
end