Screenres 320,240,32 'global variables globales Dim Shared As Integer b(3,3) 'tablero Dim Shared As Integer mx, my, btns, ox, oy 'prueba para ganar posición '3 victorias horizontales Function TestWin(t As Integer) As Integer Dim As Integer win = 0 If b(0,0)= t And b(1,0)= t And b(2,0)= t Then win = t If b(0,1)= t And b(1,1)= t And b(2,1)= t Then win = t If b(0,2)= t And b(1,2)= t And b(2,2)= t Then win = t '3 en vertical gana If b(0,0)= t And b(0,1)= t And b(0,2)= t Then win = t If b(1,0)= t And b(1,1)= t And b(1,2)= t Then win = t If b(2,0)= t And b(2,1)= t And b(2,2)= t Then win = t 'cruzada gana If b(0,0)= t And b(1,1)= t And b(2,2)= t Then win = t If b(2,0)= t And b(1,1)= t And b(0,2)= t Then win = t Return win End Function Sub InicializarTablero() For j As Integer = 0 To 2 For i As Integer = 0 To 2 b(i,j)=0 Next i Next j End Sub Sub DibujaTablero() Locate 1,1 : Print "+---+---+---+" For j As Integer = 0 To 2 Print "|"; For i As Integer = 0 To 2 If b(i,j) = 0 Then Print " |"; If b(i,j) = 1 Then Print " x |"; If b(i,j) = 2 Then Print " o |"; Next i Print !"\n+---+---+---+" Next j End Sub Function MovimientoHumano() As Integer DibujaTablero() Print !"\n HAZ CLICK CON EL MOUSE" Print "EN LA CASILLA QUE ELIJAS" Dim As Integer opcion = -1 While opcion = -1 Getmouse mx,my,,btns While btns <> 1 'esperar a pulsar botón Getmouse mx,my,,btns Wend mx = (mx-4)\32 my = (my-4)\16 If mx >= 0 And mx < 3 And my >= 0 And my < 3 Then If b(mx,my) = 0 Then opcion = mx+my*3 'Casilla vacía? End If While btns=1 Getmouse mx,my,,btns Wend Wend Return opcion End Function Function MovimientoAleatorio() As Integer Dim As Integer opcion, i, j opcion = Int(Rnd(1)*9) j = Int(opcion/3) i = opcion - Int(opcion/3)*3 While b(i,j) <> 0 Or (opcion > 8 Or opcion < 0) opcion = Int(Rnd(1)*9) j = Int(opcion/3) i = opcion - Int(opcion/3)*3 Wend Return j*3+i End Function Function MovimientoInteligente(t As Integer) As Integer Dim As Integer i, j, opcion, t2 opcion = -1 'opcion aún no seleccionada 'obtener la ficha t2 de los oponentes If t = 1 Then t2 = 2 Else t2 = 1 'prueba para la casilla central If b(1,1) = 0 Then opcion = 4 'prueba para ganar If opcion = -1 Then If b(0,0)= 0 And b(1,0)= t And b(2,0)= t Then opcion = 0 If b(0,0)= t And b(1,0)= 0 And b(2,0)= t Then opcion = 1 If b(0,0)= t And b(1,0)= t And b(2,0)= 0 Then opcion = 2 If b(0,1)= 0 And b(1,1)= t And b(2,1)= t Then opcion = 3 If b(0,1)= t And b(1,1)= 0 And b(2,1)= t Then opcion = 4 If b(0,1)= t And b(1,1)= t And b(2,1)= 0 Then opcion = 5 If b(0,2)= 0 And b(1,2)= t And b(2,2)= t Then opcion = 6 If b(0,2)= t And b(1,2)= 0 And b(2,2)= t Then opcion = 7 If b(0,2)= t And b(1,2)= t And b(2,2)= 0 Then opcion = 8 '3 bloques verticales If b(0,0)= 0 And b(0,1)= t And b(0,2)= t Then opcion = 0 If b(0,0)= t And b(0,1)= 0 And b(0,2)= t Then opcion = 3 If b(0,0)= t And b(0,1)= t And b(0,2)= 0 Then opcion = 6 If b(1,0)= 0 And b(1,1)= t And b(1,2)= t Then opcion = 1 If b(1,0)= t And b(1,1)= 0 And b(1,2)= t Then opcion = 4 If b(1,0)= t And b(1,1)= t And b(1,2)= 0 Then opcion = 7 If b(2,0)= 0 And b(2,1)= t And b(2,2)= t Then opcion = 2 If b(2,0)= t And b(2,1)= 0 And b(2,2)= t Then opcion = 5 If b(2,0)= t And b(2,1)= t And b(2,2)= 0 Then opcion = 8 'bloques cruzados If b(0,0)= 0 And b(1,1)= t And b(2,2)= t Then opcion = 0 If b(0,0)= t And b(1,1)= 0 And b(2,2)= t Then opcion = 4 If b(0,0)= t And b(1,1)= t And b(2,2)= 0 Then opcion = 8 If b(2,0)= 0 And b(1,1)= t And b(0,2)= t Then opcion = 2 If b(2,0)= t And b(1,1)= 0 And b(0,2)= t Then opcion = 4 If b(2,0)= t And b(1,1)= t And b(0,2)= 0 Then opcion = 6 End If 'prueba para bloques If opcion = -1 Then If b(0,0)= 0 And b(1,0)= t2 And b(2,0)= t2 Then opcion = 0 If b(0,0)= t2 And b(1,0)= 0 And b(2,0)= t2 Then opcion = 1 If b(0,0)= t2 And b(1,0)= t2 And b(2,0)= 0 Then opcion = 2 If b(0,1)= 0 And b(1,1)= t2 And b(2,1)= t2 Then opcion = 3 If b(0,1)= t2 And b(1,1)= 0 And b(2,1)= t2 Then opcion = 4 If b(0,1)= t2 And b(1,1)= t2 And b(2,1)= 0 Then opcion = 5 If b(0,2)= 0 And b(1,2)= t2 And b(2,2)= t2 Then opcion = 6 If b(0,2)= t2 And b(1,2)= 0 And b(2,2)= t2 Then opcion = 7 If b(0,2)= t2 And b(1,2)= t2 And b(2,2)= 0 Then opcion = 8 '3 bloques verticales If b(0,0)= 0 And b(0,1)= t2 And b(0,2)= t2 Then opcion = 0 If b(0,0)= t2 And b(0,1)= 0 And b(0,2)= t2 Then opcion = 3 If b(0,0)= t2 And b(0,1)= t2 And b(0,2)= 0 Then opcion = 6 If b(1,0)= 0 And b(1,1)= t2 And b(1,2)= t2 Then opcion = 1 If b(1,0)= t2 And b(1,1)= 0 And b(1,2)= t2 Then opcion = 4 If b(1,0)= t2 And b(1,1)= t2 And b(1,2)= 0 Then opcion = 7 If b(2,0)= 0 And b(2,1)= t2 And b(2,2)= t2 Then opcion = 2 If b(2,0)= t2 And b(2,1)= 0 And b(2,2)= t2 Then opcion = 5 If b(2,0)= t2 And b(2,1)= t2 And b(2,2)= 0 Then opcion = 8 'bloques cruzados If b(0,0)= 0 And b(1,1)= t2 And b(2,2)= t2 Then opcion = 0 If b(0,0)= t2 And b(1,1)= 0 And b(2,2)= t2 Then opcion = 4 If b(0,0)= t2 And b(1,1)= t2 And b(2,2)= 0 Then opcion = 8 If b(2,0)= 0 And b(1,1)= t2 And b(0,2)= t2 Then opcion = 2 If b(2,0)= t2 And b(1,1)= 0 And b(0,2)= t2 Then opcion = 4 If b(2,0)= t2 And b(1,1)= t2 And b(0,2)= 0 Then opcion = 6 End If If opcion = -1 Then If b(0,0) = 0 Then opcion = 0 If b(2,0) = 0 Then opcion = 2 If b(0,2) = 0 Then opcion = 6 If b(2,2) = 0 Then opcion = 8 End If 'no hay opción de hacer una elección al azar If opcion = -1 Then opcion = Int(Rnd(1)*9) j = Int(opcion/3) i = opcion - Int(opcion/3)*3 'encontrar una casilla vacía While b(i,j) <> 0 opcion = Int(Rnd(1)*9) j = Int(opcion/3) i = opcion - Int(opcion/3)*3 Wend End If Return opcion End Function InicializarTablero() DibujaTablero() Dim As Integer resultado Dim As Integer jugador = 1 Dim As Integer ContarMovimientos = 0 Dim As Integer ContarPartidas = 0 Dim As Integer movimiento = 0 Dim As Integer i, j Do 'alternar jugadores If jugador = 1 Then jugador = 2 Else jugador = 1 'selecciona tipo de movimiento para cada jugador If jugador = 1 Then movimiento = MovimientoHumano() Else movimiento = MovimientoInteligente(2) End If 'print "movimiento ="; movimiento 'print "jugador ="; jugador 'convertir la elección a las coordenadas del tablero i,j j = Int(movimiento/3) i = movimiento - (j*3) b(i,j) = jugador 'ingrese la ficha de jugador 1 o 2 resultado = TestWin(jugador) 'comprobar si el jugador ha ganado DibujaTablero() ContarMovimientos += 1 '======================================================= 'Comprobar final de partida y/o un resultado de victoria '======================================================= If ContarMovimientos = 9 Or resultado <> 0 Then DibujaTablero() If resultado = 0 Then Print !"\n EMPATE " If resultado = 1 Then Print !"\n x GANA " If resultado = 2 Then Print !"\n o GANA " Print Space(28) Print "PULSA BARRA ESPACIADORA PARA" Print "OTRA PARTIDA, ESC PARA SALIR" Sleep Cls InicializarTablero() 'reiniciar tablero ContarMovimientos = 0 ContarPartidas += 1 End If Loop Until Multikey(&H01) End