RonStar

Life

Jun 3rd, 2020
360
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IDEAL
  2. MODEL small
  3. STACK 100h
  4. DATASEG
  5.     Index dw 0
  6.     neighborsCounter db 0
  7.     GameArray db 70 dup (3)
  8.     Picture    db'oooooo                          ooooooo           o                       '
  9.                  db'o         oo   o    o oooooo    o     o oooooo    o       o oooooo oooooo   '
  10.                  db'o        o  o  oo  oo o         o     o o         o       o o      o        '
  11.                db'o  oooo o    o o oo o ooooo     o     o ooooo     o       o ooooo  ooooo  '
  12.                  db'o     o oooooo o    o o         o     o o         o       o o      o        '
  13.                  db'o     o o    o o    o o         o     o o         o       o o      o        '
  14.                  db'ooooo o o    o o    o oooooo    ooooooo o         ooooooo o o      oooooo   ',10,13
  15.                  db '                                                                           ', 10,13
  16.                  db 'Welcome to the game "life". Here are the rules:$',10,13
  17.                  
  18.                  SelectedEnter db 'Press any key to continue...$',10,13
  19.                   Rules db '- The universe of Game of Life is an infinite,two-dimensional' ,10,13
  20.                  db ' consists of square cells.',10,13
  21.                    db '- Each cell can be either alive or dead',10,13
  22.                  db '- Every cell interacts with its eight neighbours',10,13
  23.                    db '- Which are the cells that are horizontally,vertically,or ',10,13
  24.                  db ' diagonally adjacent. At each step in time, the following transitions occur:',10,13
  25.                    db '- Any live cell with fewer than two live neighbours dies',10,13
  26.                  db ' as if by underpopulation.',10,13
  27.                    db '- Any live cell with two or three live neighbours lives',10,13
  28.                  db '- On to the next turn.',10,13
  29.                    db '- Any live cell with more than three live neighbours dies',10,13
  30.                  db '- As if by overpopulation.',10,13
  31.                    db '- Any dead cell with exactly three live neighbours becomes a live cell',10,13
  32.                  db '- As if by reproduction.$'
  33.  
  34.     x dw ?
  35.     y dw ?
  36.    
  37.     color db ?
  38.    
  39.     startX dw ?
  40.     endX dw ?
  41.    
  42.     startY dw ?
  43.     endY dw ?
  44.    
  45.     CounterLines db 0
  46.     CounterColumns db 0
  47.  
  48.  
  49.     IndexX dw ?
  50.     IndexY dw ?
  51.  
  52.     RectLength db 20
  53.  
  54.     ArrayNum dw ?
  55.    
  56.  
  57. ;-------
  58.  
  59. CODESEG
  60.  
  61. proc PrintPicture
  62. push ax
  63. push dx
  64.     mov dx, offset Picture
  65.     mov ah, 9h
  66.     int 21h
  67. pop dx
  68. pop ax
  69.     call PrintRules
  70.     ret
  71. endp PrintPicture
  72.  
  73.  
  74. proc PrintRules ;prints the rules of the game
  75. push ax
  76. push dx
  77.     mov ah,2h
  78.     mov dl, 10
  79.     int 21h
  80.     mov dl, 13
  81.     int 21h
  82.     mov dx, offset Rules
  83.     mov ah, 9h
  84.     int 21h
  85.     mov dx, offset SelectedEnter
  86.     mov ah, 9h
  87.     int 21h
  88.     mov ah, 01h
  89.     int 21h
  90.     cmp al, 13 ;enter
  91.     call SwitchToGraphicsMode
  92.  
  93.  
  94. pop dx
  95. pop ax
  96.     ret
  97. endp PrintRules
  98.  
  99. proc CheckInput ;checks if you press r or enter
  100. push ax
  101. OneMoreInput:
  102.     mov ah, 7
  103.     int 21h
  104.     cmp al, 114 ;r
  105.     je PrintRules
  106.     cmp al, 13 ;enter
  107.     jne OneMoreInput
  108.     call SwitchToGraphicsMode
  109. pop ax
  110.     ret
  111. endp CheckInput
  112.  
  113.  
  114. proc SwitchToGraphicsMode ;switches to graphics mode
  115. push ax
  116.     mov ax, 13h
  117.     int 10h
  118. pop ax
  119.     ret
  120. endp SwitchToGraphicsMode
  121.  
  122.  
  123. proc SwitchToTextMode ;switches totext mode
  124. push ax
  125.     mov ax, 2
  126.     int 10h
  127. pop ax
  128.     ret
  129. endp SwitchToTextMode
  130.  
  131.  
  132. proc DisplayDot
  133. push ax
  134. push bx
  135. push cx
  136. push dx
  137.     mov al, [color] ;color
  138.     mov bl, 0 ;page number - always 0
  139.    
  140.     mov cx, [x] ;x value
  141.     mov dx, [y] ;y value
  142.    
  143.     mov ah, 0Ch
  144.     int 10h
  145. pop dx
  146. pop cx
  147. pop bx
  148. pop ax
  149.     ret
  150. endp DisplayDot
  151.  
  152.  
  153. proc DisplayLine
  154. push cx
  155.     mov cx, [startX] ;x position
  156.     mov [x], cx
  157.    
  158. DisplayDotLoop:
  159.     call DisplayDot
  160.     inc [x]
  161.    
  162.     mov cx, [endX]
  163.     inc cx
  164.     cmp [x], cx
  165.    
  166.     jne DisplayDotLoop
  167. pop cx
  168.     ret
  169. endp DisplayLine
  170.  
  171.  
  172. proc DisplayLine2 ;horizontal line
  173. push cx
  174.     mov cx, [startY] ;y position
  175.     mov [y], cx
  176.    
  177. DisplayDotLoop2:
  178.     call DisplayDot
  179.     inc [y]
  180.    
  181.     mov cx, [endY]
  182.     inc cx
  183.     cmp [y], cx
  184.    
  185.     jne DisplayDotLoop2
  186. pop cx
  187.     ret
  188. endp DisplayLine2
  189.  
  190.  
  191. proc DisplayRect
  192. push dx
  193.     mov dx, [startY]
  194.     mov [y], dx
  195.    
  196. DisplayLineLoop:
  197.     call DisplayLine
  198.     inc [y]
  199.    
  200.     mov dx, [endY]
  201.     inc dx
  202.     cmp [y], dx
  203.     jne DisplayLineLoop
  204. pop dx
  205.     ret
  206. endp DisplayRect
  207.  
  208.  
  209. proc AliveRectGraphics ;White color as parameter
  210. push [endY]
  211. push [startY]
  212. push [endX]
  213. push [startX]
  214.     add [endY], 4
  215.     sub [startY], 4
  216.     add [endX], 4
  217.     sub [startX], 4
  218.     call DisplayRect
  219. pop [startX]
  220. pop [endX]
  221. pop [startY]
  222. pop [endY]
  223.     ret
  224. endp AliveRectGraphics
  225.  
  226.  
  227. proc NewRectGraphics ;Yellow color as parameter
  228. push [endY]
  229. push [startY]
  230. push [endX]
  231. push [startX]
  232.     add [endY], 4
  233.     sub [startY], 4
  234.     add [endX], 4
  235.     sub [startX], 4
  236.     call DisplayRect
  237. pop [startX]
  238. pop [endX]
  239. pop [startY]
  240. pop [endY]
  241.     ret
  242. endp NewRectGraphics
  243.  
  244.  
  245. proc DeadRectGraphics ;Red color as parameter
  246. push [endY]
  247. push [startY]
  248. push [endX]
  249. push [startX]
  250.     add [endY], 4
  251.     sub [startY], 4
  252.     add [endX], 4
  253.     sub [startX], 4
  254.     call DisplayRect
  255. pop [startX]
  256. pop [endX]
  257. pop [startY]
  258. pop [endY]
  259.     ret
  260. endp DeadRectGraphics
  261.  
  262.  
  263. proc DisplayBoard ;creates the board of the game
  264.     mov [y], 20
  265. DisplayLines:
  266.     mov [color], 1Eh
  267.     mov [startX], 60
  268.     mov [endX], 260
  269.     call DisplayLine
  270.     add [Y], 20
  271.     inc [CounterLines]
  272.     cmp [CounterLines],8
  273.     jne DisplayLines
  274.     mov [x], 60
  275.  
  276. DisplayColumns:
  277.     mov [color], 1Eh
  278.     mov [startY], 20
  279.     mov [endY], 160
  280.     call DisplayLine2
  281.     add [X], 20
  282.     inc [CounterColumns]
  283.     cmp [CounterColumns],11
  284.     jne DisplayColumns
  285.     ret
  286. endp DisplayBoard
  287.  
  288. proc WaitForKey
  289. push ax
  290.     mov ah, 0
  291.     int 16h
  292. pop ax
  293.     ret
  294. endp WaitForKey
  295.  
  296.  
  297. proc MoveToken ;moves the gray token in all directions
  298.  
  299. push ax
  300.     xor ax,ax
  301.     mov [Index],ax
  302.     mov [color], 7h
  303.     mov [startX], 65    
  304.     mov [endX], 75
  305.     mov [startY], 25
  306.     mov [endY], 35
  307.     call DisplayRect
  308. ReadKeyboardLoop:  
  309.     mov ah, 0
  310.     int 16h
  311.    
  312.     cmp ah, 4Dh ;right arrow
  313.     jne Left
  314.     call MoveRight
  315.     jmp ReadKeyboardLoop
  316. Left:
  317.     cmp ah, 4Bh ;left arrow
  318.     jne Up
  319.     call MoveLeft
  320.     jmp ReadKeyboardLoop
  321. Up:
  322.     cmp ah, 48h ;up arrow
  323.     jne Down
  324.     call MoveUp
  325.     jmp ReadKeyboardLoop
  326. Down:
  327.     cmp ah, 50h ;down arrow
  328.     jne Space
  329.     call MoveDown
  330.     jmp ReadKeyboardLoop
  331. Space:
  332.     cmp ah, 39h ;space
  333.     jne EnterCheck
  334.     call ColorSlot
  335.     mov [color], 7h
  336.     call DisplayRect
  337.     jmp ReadKeyboardLoop
  338. EnterCheck:
  339.     cmp al, 13 ;enter
  340.     jne Eraser
  341.     jmp EndMoveToken
  342. Eraser: ;erase the slot
  343.     cmp ah,0Eh
  344.     jne EscCheck
  345.     call EraseSlot
  346.     mov [color], 7h
  347.     call DisplayRect
  348.     jmp ReadKeyboardLoop
  349. EscCheck:;Press esc to exit
  350.     cmp ah,1b ;esc
  351.     jne ReadKeyboardLoop   
  352. EndMoveToken:
  353. pop ax
  354.     ret
  355. endp MoveToken
  356.  
  357. proc MoveRight
  358. push bx
  359.     cmp [startX], 240 ;board limit
  360.     jae EndMoveRight
  361.     mov [color], 0
  362.     mov bx,offset GameArray
  363.     add bx,[index]
  364.     cmp [byte ptr bx],1
  365.     jne SquareR
  366.     mov [color],1Eh
  367. SquareR:
  368.     call DisplayRect ;draw a black or white square
  369.    
  370.     add [startX], 20
  371.     add [endX], 20
  372.     mov [color], 7h
  373.     call DisplayRect ;draw a small gray square
  374.     inc [index]
  375. EndMoveRight:
  376. pop bx
  377.     ret
  378. endp MoveRight
  379.  
  380. proc MoveLeft
  381. push bx
  382.     cmp [startX], 80
  383.     jbe EndMoveLeft
  384.     mov [color], 0
  385.     mov bx,offset GameArray
  386.     add bx,[Index]
  387.     cmp [byte ptr bx],1
  388.     jne SquareL
  389.     mov [color],1Eh
  390. SquareL:
  391.     call DisplayRect
  392.    
  393.     sub [startX], 20
  394.     sub [endX], 20
  395.     mov [color], 7h
  396.     call DisplayRect
  397.     dec [Index]
  398. EndMoveLeft:
  399. pop bx
  400.     ret
  401. endp MoveLeft
  402.  
  403. proc MoveUp
  404. push bx
  405.     cmp [startY], 40
  406.     jbe EndMoveUp
  407.     mov [color], 0h
  408.     mov bx,offset GameArray
  409.     add bx,[Index]
  410.     cmp [byte ptr bx],1
  411.     jne SquareU
  412.     mov [color],1Eh
  413. SquareU:
  414.     call DisplayRect
  415.    
  416.     sub [startY], 20
  417.     sub [endY], 20
  418.     mov [color], 7h
  419.     call DisplayRect
  420.     sub [Index],10
  421. EndMoveUp:
  422. pop bx
  423.     ret
  424. endp MoveUp
  425.  
  426.  
  427. proc MoveDown
  428. push bx
  429.     cmp [startY], 140
  430.     jae EndMoveDown
  431.     mov [color], 0h
  432.     mov bx,offset GameArray
  433.     add bx,[index]
  434.     cmp [byte ptr bx],1
  435.     jne SquareD
  436.     mov [color],1Eh
  437. SquareD:
  438.     call DisplayRect
  439.    
  440.     add [startY], 20
  441.     add [endY], 20
  442.     mov [color], 7h
  443.     call DisplayRect
  444.     add [Index],10
  445. EndMoveDown:
  446. pop bx
  447.     ret
  448. endp MoveDown
  449.  
  450.  
  451. proc EraseSlot
  452. push bx
  453.     mov [color], 0
  454.     call AliveRectGraphics
  455.     mov bx,offset GameArray
  456.     add bx,[index]
  457.     mov [byte ptr bx],0
  458. pop bx
  459.     ret
  460. endp EraseSlot
  461.  
  462.  
  463. proc ColorSlot
  464. push bx
  465.     mov [color], 1Eh
  466.     call AliveRectGraphics
  467.     mov bx, offset GameArray
  468.     add bx, [Index]
  469.     mov [byte ptr bx], 1
  470. pop bx
  471.     ret
  472. endp ColorSlot
  473.  
  474.  
  475. proc CheckIfColored
  476.     mov [Index],0
  477.     mov bx,offset GameArray
  478.    
  479.     mov [startX], 65    
  480.     mov [endX], 75
  481.     mov [startY], 25
  482.     mov [endY], 35
  483.    
  484. StartCheck:
  485.     mov [neighborsCounter],0
  486.     mov bx,offset GameArray
  487.     add bx,[Index]
  488.     cmp [byte ptr bx],3
  489.     jne AfterCheck
  490.     cmp [Index],10
  491.     jb AfterUpCheck
  492.     call UpCheck
  493. AfterUpCheck:
  494.     cmp [Index],59
  495.     ja AfterDownCheck
  496.     call DownCheck
  497. AfterDownCheck:
  498.     xor ax,ax
  499.     mov ax,[index]
  500.     inc ax
  501.     mov dl,10
  502.     div dl
  503.     cmp ah,0
  504.     je AfterRightCheck
  505.     push 1
  506.     call CheckNeighbors
  507. AfterRightCheck:
  508.     xor ax,ax
  509.     mov ax,[Index]
  510.     mov dl,10
  511.     div dl
  512.     cmp ah,0
  513.     je AfterLeftCheck
  514.     push -1
  515.     call CheckNeighbors
  516. AfterLeftCheck:
  517.     cmp [neighborsCounter],3
  518.     jne AfterCheck
  519.     mov [color],44
  520.     call AliveRectGraphics
  521. AfterCheck:
  522.     inc [Index]
  523.     cmp [Index],80
  524.     je EndCheckNeighbors
  525.     mov dl,10
  526.     mov ax,[Index]
  527.     div dl
  528.     cmp ah,0
  529.     je DownLine
  530.     add [startX], 20
  531.     add [endX], 20
  532.     jmp StartCheck
  533. DownLine:
  534.     mov [startX], 65    
  535.     mov [endX], 75
  536.     add [startY], 20
  537.     add [endY], 20
  538.     jmp StartCheck
  539. EndCheckNeighbors:
  540.     ret
  541. endp CheckIfColored
  542.  
  543. proc ColorSign
  544.     mov [color],0
  545.     cmp [neighborsCounter],3
  546.     jne EndColorSign
  547.     mov [color],0Eh
  548. EndColorSign:
  549.     ret
  550. endp ColorSign
  551.  
  552. proc UpCheck
  553.     push -10
  554.     call CheckNeighbors
  555.     mov ax,[index]
  556.     mov dl,10
  557.     div dl
  558.     cmp ah,0
  559.     je AfterLeftUpCheck
  560.     push -11
  561.     call CheckNeighbors
  562. AfterLeftUpCheck:
  563.     xor ax,ax
  564.     mov ax,[Index]
  565.     inc ax
  566.     mov dl,10
  567.     div dl
  568.     cmp ah,0
  569.     je AfterRightUpCheck
  570.     push -9
  571.     call CheckNeighbors
  572. AfterRightUpCheck:
  573.     ret
  574. endp UpCheck
  575.  
  576. proc DownCheck
  577.     push 10
  578.     call CheckNeighbors
  579.     mov ax,[Index]
  580.     mov dl,10
  581.     div dl
  582.     cmp ah,0
  583.     je AfterLeftDownCheck
  584.     push 9
  585.     call CheckNeighbors
  586. AfterLeftDownCheck:
  587.     xor ax,ax
  588.     mov ax,[Index]
  589.     inc ax
  590.     mov dl,10
  591.     div dl
  592.     cmp ah,0
  593.     je AfterRightDownCheck
  594.     push 11
  595.     call CheckNeighbors
  596. AfterRightDownCheck:
  597.     ret
  598. endp DownCheck
  599.  
  600.  
  601. Proc CheckNeighbors
  602. push bp
  603.     mov bp,sp
  604. push ax
  605. push bx
  606.     mov ax,[bp+4]
  607.     mov bx,offset GameArray
  608.     add bx,[Index]
  609.     add bx,ax
  610.     cmp [byte ptr bx],1
  611.     jne EndCheck
  612.     inc [neighborsCounter]
  613. EndCheck:
  614. pop bx
  615. pop ax
  616. pop bp
  617.     ret 2
  618. endp CheckNeighbors
  619.  
  620.  
  621. start:
  622.     mov ax,@data
  623.     mov ds,ax
  624.  
  625. Preview:
  626.     call SwitchToTextMode
  627.     call PrintPicture
  628.     call DisplayBoard
  629.    
  630.    
  631. Game:
  632.     call MoveToken
  633.     call CheckIfColored
  634.     ;jmp Game
  635.    
  636.    
  637. Exit:
  638.     mov ax, 4c00h
  639.     int 21h
  640. END start
RAW Paste Data