miércoles, 6 de febrero de 2008

Tres en raya para ABAP

Ésta es una implementacion -ociosa- del juego de 3 en raya en ABAP/4, por el momento es muy torpe -juega totalmente aleatorio- y poco robusta, pero es un buen ejemplo de programas interactivos.
El código está listo para pegar poner F8 y jugar.
Prometo implementar la heurística del juego para que no le puedas ganarle, pero eso será en otro post.
Por cierto el generador de aleatorios de ABAP es muy malo, prometo implementar uno y postearlo también.

*&---------------------------------------------------------------------*
*& Autor.........: Carlos Agreda Pérez
*& Descripción...: Juego de 3 en raya
*& Fecha ........: 06/FEB/2008 *
*&---------------------------------------------------------------------*

REPORT ZTMP_3R no standard page heading.
Data: aleatorio type i, posX type i, posY type i.
Data: ganoMaquina type i value 0.
Data: ganoJugador type i value 0.
Data: Begin of estado,
     P1 VALUE '-', P2 VALUE '-', P3 VALUE '-', P4 VALUE '-', P5 VALUE '-',
     P6 VALUE '-', P7 VALUE '-', P8 VALUE '-', P9 VALUE '-',
End of estado.

perform elegirAccionMaquina.
perform mostrarEstado.
 
* CAPTURA DE LA JUGADA-----------------------
AT LINE-SELECTION.
     GET CURSOR LINE posY OFFSET posX.   
     if posX = 10 and posY = 4. estado-P1 = 'o'. endif.
     if posX = 20 and posY = 4. estado-P2 = 'o'. endif.
     if posX = 30 and posY = 4. estado-P3 = 'o'. endif.
     if posX = 10 and posY = 6. estado-P4 = 'o'. endif.
     if posX = 20 and posY = 6. estado-P5 = 'o'. endif.
     if posX = 30 and posY = 6. estado-P6 = 'o'. endif.
     if posX = 10 and posY = 8. estado-P7 = 'o'. endif.
     if posX = 20 and posY = 8. estado-P8 = 'o'. endif.
     if posX = 30 and posY = 8. estado-P9 = 'o'. endif.
     perform verificarGanador.
     if ganoJugador NE 1.
            perform elegirAccionMaquina.
     endif.
     perform mostrarEstado. 
 
* SUBPROGRAMAS ------------------------------------ 
form elegirAccionMaquina.
       perform generarAleatorioLibre.
       if aleatorio = 1. estado-P1 = 'X'. endif.
       if aleatorio = 2. estado-P2 = 'X'. endif.
       if aleatorio = 3. estado-P3 = 'X'. endif.
       if aleatorio = 4. estado-P4 = 'X'. endif.
       if aleatorio = 5. estado-P5 = 'X'. endif.
       if aleatorio = 6. estado-P6 = 'X'. endif.
       if aleatorio = 7. estado-P7 = 'X'. endif.
       if aleatorio = 8. estado-P8 = 'X'. endif.
       if aleatorio = 9. estado-P9 = 'X'. endif.
endform.

form mostrarEstado.
      write: /, /, /.
      write at 11(1): estado-P1. write at 21(1): estado-P2. write at 31(1): estado-P3. write: /.
      write at 11(1): estado-P4. write at 21(1): estado-P5. write at 31(1): estado-P6. write: /.
      write at 11(1): estado-P7. write at 21(1): estado-P8. write at 31(1): estado-P9.
endform.

form generarAleatorioLibre.
      CALL FUNCTION 'QF05_RANDOM_INTEGER'
      EXPORTING
             RAN_INT_MIN = 1
             RAN_INT_MAX = 9
      IMPORTING
             RAN_INT = aleatorio
      EXCEPTIONS
             OTHERS = 1.
      if aleatorio = 1 and estado-P1 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 2 and estado-P2 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 3 and estado-P3 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 4 and estado-P4 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 5 and estado-P5 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 6 and estado-P6 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 7 and estado-P7 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 8 and estado-P8 NE '-'. perform generarAleatorioLibre. endif.
      if aleatorio = 9 and estado-P9 NE '-'. perform generarAleatorioLibre. endif.
endform.

form verificarGanador.
      if estado-P1 = 'X' and estado-P2 = 'X' and estado-P3 = 'X'. ganoMaquina = 1. endif.
      if estado-P4 = 'X' and estado-P5 = 'X' and estado-P6 = 'X'. ganoMaquina = 1. endif.
      if estado-P7 = 'X' and estado-P8 = 'X' and estado-P9 = 'X'. ganoMaquina = 1. endif.
      if estado-P1 = 'X' and estado-P4 = 'X' and estado-P7 = 'X'. ganoMaquina = 1. endif.
      if estado-P2 = 'X' and estado-P5 = 'X' and estado-P8 = 'X'. ganoMaquina = 1. endif.
      if estado-P3 = 'X' and estado-P6 = 'X' and estado-P9 = 'X'. ganoMaquina = 1. endif.
      if estado-P3 = 'X' and estado-P5 = 'X' and estado-P7 = 'X'. ganoMaquina = 1. endif.
      if estado-P1 = 'X' and estado-P5 = 'X' and estado-P9 = 'X'. ganoMaquina = 1. endif.
      if estado-P1 = 'o' and estado-P2 = 'o' and estado-P3 = 'o'. ganoJugador = 1. endif.
      if estado-P4 = 'o' and estado-P5 = 'o' and estado-P6 = 'o'. ganoJugador = 1. endif.
      if estado-P7 = 'o' and estado-P8 = 'o' and estado-P9 = 'o'. ganoJugador = 1. endif.
      if estado-P1 = 'o' and estado-P4 = 'o' and estado-P7 = 'o'. ganoJugador = 1. endif.
      if estado-P2 = 'o' and estado-P5 = 'o' and estado-P8 = 'o'. ganoJugador = 1. endif.
      if estado-P3 = 'o' and estado-P6 = 'o' and estado-P9 = 'o'. ganoJugador = 1. endif.
      if estado-P3 = 'o' and estado-P5 = 'o' and estado-P7 = 'o'. ganoJugador = 1. endif.
      if estado-P1 = 'o' and estado-P5 = 'o' and estado-P9 = 'o'. ganoJugador = 1. endif.  
      if ganoMaquina = 1. Write 'Te gané'.
      elseif ganoJugador = 1. Write 'Ganaste'.
      endif.
endform.

1 comentario:

Alvaro "Blag" Tejada Galindo dijo...

Excelente implementación! Muy buen juego -:) Espero ver más de tus proyectos.

Saludos,

Blag.