forked from cristianbgp/tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic Tac Toe.rb
More file actions
105 lines (102 loc) · 3.48 KB
/
Tic Tac Toe.rb
File metadata and controls
105 lines (102 loc) · 3.48 KB
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
require 'colorize'
#Variables generales
array_opciones=(1..9).to_a
array_no_opciones = []
array_jugadas = Array.new(9,"")
step_jugadas = 0
def creation_board (arr)
num_arr=arr.length - 1
tablero = ""
for i in (0..num_arr)
posicion = " " + (i+1).to_s
case arr[i]
when "1"
posicion = " x".blue
when "0"
posicion = " o".red
end
if i < num_arr
if i == 2 || i == 5
posicion += "\n-----------\n"
else
posicion += " |"
end
end
tablero += posicion
end
tablero
end
#jugadas a ganar
#posicion 0,1,2 - 3,4,5 - 6,7,8 - 0,4,8 - 2,4,6 - 0,3,6 - 1,4,7 - 2,5,8
def validate_winner (arr, last_player)
if (arr[0..2] == Array.new(3,"1") || arr[0..2] == Array.new(3,"0")) ||
(arr[3..5] == Array.new(3,"1") || arr[3..5] == Array.new(3,"0")) ||
(arr[6..8] == Array.new(3,"1") || arr[6..8] == Array.new(3,"0")) ||
([arr[0],arr[4],arr[8]] == Array.new(3,"1") || [arr[0],arr[4],arr[8]] == Array.new(3,"0")) ||
([arr[2],arr[4],arr[6]] == Array.new(3,"1") || [arr[2],arr[4],arr[6]] == Array.new(3,"0")) ||
([arr[0],arr[3],arr[6]] == Array.new(3,"1") || [arr[0],arr[3],arr[6]] == Array.new(3,"0")) ||
([arr[1],arr[4],arr[7]] == Array.new(3,"1") || [arr[1],arr[4],arr[7]] == Array.new(3,"0")) ||
([arr[2],arr[5],arr[8]] == Array.new(3,"1") || [arr[2],arr[5],arr[8]] == Array.new(3,"0"))
return last_player
end
return ""
end
#Juego
puts "Bievenido al juego #️⃣ 🐱 !!\n\n"
puts "----------------------------------"
msj_inicio = "Elige un número del tablero para hacer tu jugada\n\n"
board = " 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9\n\n"
puts msj_inicio + board
jugada = 0
loop do
jugada = gets.chomp.to_i
break if jugada > 0 && jugada <10
puts "Ingrese un valor correcto\n" + msj_inicio + board
end
array_jugadas[jugada-1] = "1"
array_no_opciones.push(jugada)
board = creation_board (array_jugadas)
step_jugadas += 1
puts "\n" + board + "\n\n"
msj_computador = "El computador esta jugando\n\n"
msj_persona = "Continua tu jugada, elige un número del tablero\n\n"
for n in (1..((array_opciones.length - 1)/2))
system "clear"
if step_jugadas >= 5
result = validate_winner(array_jugadas, "1")
if result != ""
puts "El ganador eres tú!!! 🎉 🙌 🙌 🎉"
break
end
end
puts msj_computador
jugada_computador = (array_opciones-array_no_opciones).sample
array_jugadas[jugada_computador-1] = "0"
array_no_opciones.push(jugada_computador)
board = creation_board (array_jugadas)
step_jugadas += 1
puts "\n" + msj_persona + board + "\n\n"
if step_jugadas >= 5
result = validate_winner(array_jugadas, "0")
if result != ""
puts "El ganador es el computador!!! 😓 😓 Intentalo la proxima vez"
break
end
end
jugada = 0
loop do
jugada = gets.chomp.to_i
if array_no_opciones.include?(jugada)
puts "Esa posición esta usada\n"
elsif jugada > 0 && jugada <10
break
end
puts "Ingrese un valor correcto\n" + msj_persona + board
end
array_jugadas[jugada-1] = "1"
array_no_opciones.push(jugada)
board = creation_board (array_jugadas)
step_jugadas += 1
puts "\n" + board + "\n\n"
end
puts "Empataron!! 🤷♂️ 🤷♂️" if step_jugadas == 9