Revista Comunicación

El juego de la Vida, parte 2, estadísticas

Publicado el 14 agosto 2013 por Dracux @isladelmono

Bueno, esta semana le estuve agregando en mi poco tiempo libre un par de cositas al juego de la vida que estaba haciendo en QBasic. En este caso le hice una muy sencilla lista final de cuantas celdas vivas y muertas habían por turno.

De cualquier manera para la próxima entrega van a ver que esto lo modifiqué ya que hacer el listado final sirve solo si lo hago a modo de log.

Les dejo el video:

Y el código:

SCREEN 12
INPUT "Enter dimentions of the board:", dimension
DIM board(0 TO dimension + 1, 0 TO dimension + 1)
DIM nextboard(0 TO dimension + 1, 0 TO dimension + 1)
INPUT "Enter turns: ", turns
DIM alive(1 TO turns)
REM --------SET BOARD--------

FOR i = 0 TO dimension + 1
    board(i, 0) = 0
    board(0, i) = 0
    board(i, dimension + 1) = 0
    board(dimension + 1, i) = 0
    nextboard(i, 0) = 0
    nextboard(0, i) = 0
    nextboard(i, dimension + 1) = 0
    nextboard(dimension + 1, i) = 0
NEXT
RANDOMIZE TIMER
FOR i = 1 TO dimension
    FOR j = 1 TO dimension
        board(i, j) = INT(RND * 2)
    NEXT
NEXT
alive(1) = countAlive(board(), dimension)
REM ----------DRAW BOARD------------
CALL drawBoard(dimension, board())

actualTurn = 1
DO WHILE turns > actualTurn
    SLEEP 1
    FOR i = 1 TO dimension
        FOR j = 1 TO dimension
            cant = returnAliveNeighbours(board(), i, j, dimension)
            IF board(i, j) = 1 THEN
                IF cant < 2 OR cant > 3 THEN
                    nextboard(i, j) = 0
                END IF
            END IF
            IF board(i, j) = 0 THEN
                IF cant = 3 THEN
                    nextboard(i, j) = 1
                END IF
            END IF
        NEXT
    NEXT
    FOR i = 1 TO dimension
        FOR j = 1 TO dimension
            board(i, j) = nextboard(i, j)
        NEXT
    NEXT
    CALL drawBoard(dimension, board())
    actualTurn = actualTurn + 1
    alive(actualTurn) = countAlive(board(), dimension)
LOOP
CALL returnStatistics(alive(), dimension * dimension)

SUB drawBoard (dimension, board())
FOR i = 1 TO dimension
    FOR j = 1 TO dimension
        IF board(i, j) = 0 THEN
            LINE ((i - 1) * (640 / dimension), (j - 1) * (480 / dimension))-((i - 1) * (640 / dimension) + (640 / dimension), (j - 1) * (480 / dimension) + (480 / dimension)), 0, BF
        ELSE
            LINE ((i - 1) * (640 / dimension), (j - 1) * (480 / dimension))-((i - 1) * (640 / dimension) + (640 / dimension), (j - 1) * (480 / dimension) + (480 / dimension)), 6, BF
        END IF
    NEXT
NEXT
FOR i = 1 TO dimension
    LINE (0, i * (480 / dimension))-(640, i * (480 / dimension)), 4
    LINE (i * (640 / dimension), 0)-(i * (640 / dimension), 480), 4
NEXT
END SUB

FUNCTION returnAliveNeighbours (board(), px, py, dimension)
aliveNeighbours = 0
aliveNeighbours = board(px - 1, py - 1) + board(px, py - 1) + board(px + 1, py - 1) + board(px - 1, py) + board(px + 1, py) + board(px - 1, py + 1) + board(px, py + 1) + board(px + 1, py + 1)
returnAliveNeighbours = aliveNeighbours
END FUNCTION

FUNCTION countAlive (board(), dimension)
contador = 0
FOR i = 1 TO dimension
    FOR j = 1 TO dimension
        IF board(i, j) = 1 THEN
            contador = contador + 1
        END IF
    NEXT
    countAlive = contador
NEXT
END FUNCTION

SUB returnStatistics (alive(), quantity)
REM Showing deads/alive
CLS
PRINT "TURN ALIVE vs. DEAD"
FOR i = 1 TO UBOUND(ALIVE)
    PRINT STR$(i) + STR$(alive(i)) + STR$(quantity - alive(i))
NEXT
END SUB

Espero que les sirva y ya saben que son bienvenidas sugerencias, comentarios, lo que sea.

  • El juego de la Vida, parte 2, estadísticas
El juego de la Vida, parte 2, estadísticas

Volver a la Portada de Logo Paperblog