ticktacktoe 0.1.0

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. checksums.yaml +7 -0
  2. data/lib/ticktacktoe.rb +172 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c94840de2279d9bc054031b351d36c57c06d014
4
+ data.tar.gz: daaf690749938ea2fbe2112cb140e0a312fa8a1e
5
+ SHA512:
6
+ metadata.gz: 9eab8e57dfa3f2a3c4ce8fc2a938fe0cb34248fc598f18db170049adce44b526dafbf31211a5ca067540c9d6719e1ed8c6303540ba6eec3ccc9446b4ade1de9b
7
+ data.tar.gz: 7795efd3cf82499ceae45e283c51c7964d6a2a530d77a72613e6d5459afcd38177fc49c5cffd2aa960999e3a0ac2ffe22b1be2f1f5ec62f7b58607f86f28f355
@@ -0,0 +1,172 @@
1
+ class Ticktacktoe
2
+ def tablero(tamaño)
3
+ tab = Array.new(tamaño){Array.new(tamaño)}
4
+ for i in 0...tamaño
5
+ for j in 0...tamaño
6
+ tab[i][j] = " "
7
+ end
8
+ end
9
+ jugar(tamaño, tab)
10
+ return tab
11
+ end
12
+ def muestraTab(tamaño, tab)
13
+ system("cls")
14
+ puts "Tick Tack Toe"
15
+ col = " "
16
+ for k in 1...(tamaño+1)
17
+ col = col + k.to_s + " "
18
+ end
19
+ puts col
20
+ for i in 0...tamaño
21
+ ren = ""
22
+ number = i + 1
23
+ for j in 0...tamaño
24
+ if (j + 1) < tamaño
25
+ ren = ren + " " + tab[i][j] + " " + "|"
26
+ else
27
+ ren = ren + " " + tab[i][j] + " "
28
+ end
29
+ end
30
+ puts number.to_s + ren
31
+ ren = ""
32
+ for k in 0...tamaño
33
+ ren = ren + " " + "---"
34
+ end
35
+ if (i + 1) < tamaño
36
+ puts ren
37
+ end
38
+ end
39
+ end
40
+ def jugar (tamaño, tab)
41
+ fin = 0
42
+ mov = 0
43
+ turno = "O"
44
+ while fin != "2"
45
+ if turno == "O"
46
+ turno = "X"
47
+ else
48
+ turno = "O"
49
+ end
50
+ r = ""
51
+ c = ""
52
+ loop do
53
+ muestraTab(tamaño, tab)
54
+ puts "Jugador " + turno
55
+ puts "Renglon:"
56
+ r = gets.chomp
57
+ if r.to_i == 0 || r.to_i > tamaño
58
+ puts "Posicion no valida"
59
+ gets.chomp
60
+ else
61
+ puts "Columna:"
62
+ c = gets.chomp
63
+ if c.to_i == 0 || c.to_i > tamaño
64
+ puts "Posicion no valida"
65
+ gets.chomp
66
+ else
67
+ break if tab[(r.to_i - 1)][(c.to_i - 1)] == " "
68
+ puts "Lugar ya ocupado"
69
+ gets.chomp
70
+ muestraTab(tamaño, tab)
71
+ puts "Se lleno o.o"
72
+ end
73
+ end
74
+ end
75
+ mov = mov + 1
76
+ tab[(r.to_i - 1)][(c.to_i - 1)] = turno
77
+ gana = valGana(tamaño, tab, r.to_i - 1, c.to_i - 1, turno)
78
+ if gana != " "
79
+ muestraTab(tamaño, tab)
80
+ puts "Gano el " + gana.to_s
81
+ gets.chomp
82
+ if turno == "O"
83
+ turno = "X"
84
+ else
85
+ turno = "O"
86
+ end
87
+ end
88
+ loop do
89
+ if mov == (tamaño ** 2) or gana != " "
90
+ puts "¿Seguir jugando?"
91
+ puts "1: Si"
92
+ puts "2: No"
93
+ fin = gets.chomp
94
+ if (fin.to_i <= 0 and fin.to_i >= 3)
95
+ puts "opcion no valida"
96
+ muestraTab(tamaño, tab)
97
+ else
98
+ if fin == "1"
99
+ mov = 0
100
+ tab = tablero(tamaño)
101
+ end
102
+ break
103
+ end
104
+ else
105
+ break
106
+ end
107
+ end
108
+ muestraTab(tamaño, tab)
109
+ end
110
+ end
111
+
112
+ def valGana(tamaño, tab, r, c, turno)
113
+ i = val = 0
114
+ while i < 4
115
+ case i
116
+ when 0
117
+ rMov = -1
118
+ cMov = 0
119
+ rlim = 1
120
+ clim = 0
121
+ when 1
122
+ rMov = 0
123
+ cMov = -1
124
+ rlim = 0
125
+ clim = 1
126
+ when 2
127
+ rMov = -1
128
+ cMov = -1
129
+ rlim = 1
130
+ clim = 1
131
+ else
132
+ rMov = -1
133
+ cMov = 1
134
+ rlim = 1
135
+ clim = -1
136
+ end
137
+ for mov in 0...3
138
+ if (r + (rlim*mov)) < tamaño and (c + (clim*mov)) < tamaño and tab[(r + (rlim*mov))][(c + (clim*mov))] == turno
139
+ r2 = r + (rlim*mov)
140
+ c2 = c + (clim*mov)
141
+ for j in 1...3
142
+ if (r2+(j*rMov) >= 0 and c2+(j*cMov) >= 0) and (r2+(j*rMov) < tamaño and c2+(j*cMov) < tamaño) and tab[r2+(j*rMov)][c2+(j*cMov)] == turno
143
+ val = val + 1
144
+ else
145
+ break
146
+ end
147
+ end
148
+ if val == 2
149
+ return "Jugador " + turno
150
+ else
151
+ val = 0
152
+ end
153
+ end
154
+ end
155
+ i += 1
156
+ end
157
+ return " "
158
+ end
159
+ end
160
+
161
+ tamaño = ""
162
+ while tamaño.to_i < 3
163
+ puts "Tamaño del tablero"
164
+ tamaño = gets.chomp
165
+ if tamaño.to_i < 3
166
+ puts "Tamaño no valido"
167
+ gets.chomp
168
+ system("cls")
169
+ end
170
+ end
171
+ juego = Ticktacktoe.new
172
+ tablero = juego.tablero(tamaño.to_i)
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ticktacktoe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luis Daniel Parga Cruz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Juego de gato
14
+ email: iscluisdaniel@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ticktacktoe.rb
20
+ homepage:
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Primer gema
44
+ test_files: []