Apagada docs

Aprendiendo a programar el pasado

Herramientas de usuario

Herramientas del sitio


en:basic:tres_en_raya
Tres en Raya para Chipmunk Basic

Tic-tac-toe adapted to chipmunk basic for Macintosh. You could probably run it on gwbasic. See tic-tac-toe for a gwbasic version for the IBM PC compatible with CGA graphics. Messages and variable names are in Spanish.

10 rem TRES EN RAYA
20 dim tablero(3,3)
30 dim valor(3,3)
40 dim jugadores(2)
50 for x = 0 to 3 : for y = 0 to 3
60 tablero(x,y) = 0
70 valor(x,y) = 0
80 next y : next x
90 for f = 1 to 2
100 print "¿El jugador ";f;" es Humano o Computador? ";
110 input "(1-HUMANO 0-COMPUTADOR)";jugadores(f)
120 if jugadores(f) <> 1 and jugadores(f) <> 0 then print "Respuesta incorrecta" : goto 100
130 next f
140 randomize timer
150 inicia = rnd(2)+1
160 print "EMPIEZA EL JUGADOR ";inicia
170 print "... PULSE CUALQUIER TECLA ..." : f$ = input$(1)
180 jugact = inicia
190 print "Turno del jugador: ";jugact;" ";
200 if jugadores(jugact) = 0 then print "(Controlado por ordenador)" :  else print "(Humano)"
210 gosub 2000
220 if jugadores(jugact) = 1 then gosub 3000 :  else gosub 4000
230 jugact = jugact+1 : if jugact > 2 then jugact = 1
240 gosub 6000 : rem comprueba si han ganado
250 if ganado = 0 then 190
260 gosub 2000 : rem muestra la jugada ganadora
270 if ganado = -1 then print "*** EMPATE ***" : goto 300
280 print "Ha ganado el jugador ";ganado;
290 if jugadores(ganado) = 0 then print "(Controlado por ordenador)" :  else print "(Humano)"
300 input "Otra partida? (S/N): ";a$
310 if a$ = "S" or a$ = "s" then run
320 if a$ = "N" or a$ = "n" then end
330 goto 300
2000 rem dibujar el tablero
2010 a$ = " XO"
2015 print "    A   B   C"
2020 for y = 1 to 3
2024 print "  +---+---+---+"
2025 print y;"|";
2030 for x = 1 to 3
2040 print " ";mid$(a$,tablero(x,y)+1,1);" |";
2050 next x
2060 print
2070 next y
2075 print "  +---+---+---+"
2080 return
3000 rem Jugador humano
3010 print "Escriba donde quiere poner su ficha: "; : input "Columna (A-C): ";col$
3020 if len(col$) > 1 then goto 3010
3030 if col$ < "a" then col$ = chr$(asc(col$)+asc("a")-asc("A"))
3040 if col$ < "a" or col$ > "c" then goto 3010
3050 columna = asc(col$)-asc("a")+1
3060 print tab (36);"Fila (1-3)";
3065 input fil
3070 if fil < 1 or fil > 3 then goto 3010
3080 print "Colocar pieza en ";col$;fil
3085 if tablero(columna,fil) <> 0 then print "Casilla ocupada! " : goto 3010
3090 tablero(columna,fil) = jugact
3095 rem print "(debug)";columna,fil
3100 return
4000 rem Jugador ordenador
4010 rem cada punto de cruce vale... 
4020 rem +1(hasta 4)por cada l completamente vac que lo cruce
4030 rem +99 por cada línea de 2 puntos del color propio con que esté conectado
4040 rem +4 (hasta 16=4*4) por cada línea de 1 punto de color propio con que esté conectada
4050 rem +17 (hasta 68=17*4) por cada línea de 2 puntos de color ajeno que la cruce.
4060 rem ** Vaciando la tabla de valores
4070 for x = 0 to 3 : for y = 0 to 3
4080 valor(x,y) = 0
4090 next
4100 rem ** Calculando las horizontales
4110 for y = 1 to 3
4120 propios = 0 : enemigo = 0
4130 for x = 1 to 3
4140 if tablero(x,y) = jugact then propios = propios+1 :  else if tablero(x,y) <> 0 then enemigo = enemigo+1
4150 next x
4160 gosub 5000
4170 for x = 1 to 3
4180 if tablero(x,y) = 0 then valor(x,y) = valor(x,y)+puntos :  else valor(x,y) = -99
4190 next x
4200 next y
4210 rem ** Calculando las verticales
4220 for x = 1 to 3
4230 propios = 0 : enemigo = 0
4240 for y = 1 to 3
4250 if tablero(x,y) = jugact then propios = propios+1 :  else if tablero(x,y) <> 0 then enemigo = enemigo+1
4260 next y
4270 gosub 5000
4280 for y = 1 to 3
4290 if tablero(x,y) = 0 then valor(x,y) = valor(x,y)+puntos :  else valor(x,y) = -99
4300 next y
4310 next x
4320 rem * diagonal abajo
4325 propios = 0 : enemigo = 0
4330 for x = 1 to 3
4340 if tablero(x,x) = jugact then propios = propios+1 :  else if tablero(x,x) <> 0 then enemigo = enemigo+1
4350 next x
4360 gosub 5000
4370 for x = 1 to 3
4380 if tablero(x,x) = 0 then valor(x,x) = valor(x,x)+puntos :  else valor(x,x) = -99
4390 next
4400 rem diagonal arriba
4405 propios = 0 : enemigo = 0
4410 for x = 1 to 3 : y = 4-x
4420 if tablero(x,y) = jugact then propios = propios+1 :  else if tablero(x,y) <> 0 then enemigo = enemigo+1
4430 next x
4440 gosub 5000
4445 propios = 0 : enemigo = 0
4450 for x = 1 to 3 : y = 4-x
4460 if tablero(x,y) = 0 then valor(x,y) = valor(x,y)+puntos :  else valor(x,y) = -99
4470 next x
4500 maxx = 0 : maxy = 0
4505 maxv = -1
4510 for x = 1 to 3 : for y = 1 to 3
4520 if tablero(x,y) = 0 then if valor(x,y) > maxv then maxx = x : maxy = y : maxv = valor(x,y)
4525 if tablero(x,y) = 0 then if (valor(x,y) = maxv) and rnd(3) = 1 then maxx = x : maxy = y
4530 next y : next x
4540 print "El jugador ";jugact;" coloca en ";chr$(asc("a")+maxx-1);maxy
4550 tablero(maxx,maxy) = jugact
4560 return
5000 rem *Puntua una lin.
5005 if propios > 2 or enemigo > 2 then print "Error programacion" : stop
5010 if propios = 2 then puntos = 99 : return
5020 if enemigo = 2 then puntos = 17 : return
5030 if propios = 1 and enemigo = 0 then puntos = 4 : return
5040 if propios = 0 and enemigo = 0 then puntos = 1 : return
5050 puntos = 0 : return
6000 rem Averigua si la pieza ha ganado
6010 dim vertical(2) : dim horizontal(2)
6020 ganado = 0 : libres = 0
6030 for x = 1 to 3
6040 vertical(1) = 0 : vertical(2) = 0 : horizontal(1) = 0 : horizontal(2) = 0
6050 for y = 1 to 3
6060 if tablero(x,y) <> 0 then horizontal(tablero(x,y)) = horizontal(tablero(x,y))+1 :  else libres = libres+1
6070 if tablero(y,x) <> 0 then vertical(tablero(y,x)) = vertical(tablero(y,x))+1 :  else libres = libres+1
6080 next y
6090 for f = 1 to 2
6100 if horizontal(f) = 3 or vertical(f) = 3 then ganado = f
6110 next f
6120 next x
6130 vertical(1) = 0 : vertical(2) = 0 : horizontal(1) = 0 : horizontal(2) = 0
6140 for x = 1 to 3 : y1 = x : y2 = 4-x
6150 if tablero(x,y1) <> 0 then horizontal(tablero(x,y1)) = horizontal(tablero(x,y1))+1 :  else libres = libres+1
6160 if tablero(x,y2) <> 0 then vertical(tablero(x,y2)) = vertical(tablero(x,y2))+1 :  else libres = libres+1
6170 next x
6180 for f = 1 to 2
6190 if horizontal(f) = 3 or vertical(f) = 3 then ganado = f
6200 next f
6210 if ganado = 0 and libres = 0 then ganado = -1
6220 return
Este sitio web utiliza cookies. Al utilizar el sitio web, usted acepta almacenar cookies en su computadora. También reconoce que ha leído y entendido nuestra Política de privacidad. Si no está de acuerdo abandone el sitio web.Más información
en/basic/tres_en_raya.txt · Última modificación: 2018/01/07 11:58 por nepenthes