souravchwn

Untitled

Oct 22nd, 2020
1,411
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DATA SEGMENT
  2.  
  3.     JUMP_LINE DB 13,10,"$"
  4.  
  5.     TICTACTOE_BOARD DB "---|---|---$"
  6.  
  7.     MSG_RESTART DB 13,10,"RESTART? Y for (YES) / N for (NO)$"
  8.     MSG_BEGIN DB 13,10,".:: TiC TaC ToE ::.$"
  9.     MSG_POS DB 13,10,"Insert the board position for your play:$"
  10.     MSG_TIE DB 13,10,"Oh,IT WAS A TIE!$"
  11.     MSG_VICTORY DB 13,10,"The winner was: PLAYER $"
  12.     MSG_ERROR1 DB 13,10,"You already choose that! please try again.$"
  13.     MSG_ERROR2 DB 13,10,"Invalid charecter! Try again.$"
  14.     MSG_TURN_PLAYER1 DB 13,10,"Now it is player 1 turn(X)Enter Your position for'X'(1-9)!$"
  15.     MSG_TURN_PLAYER2 DB 13,10,"Now it is player 2 turn(O)Enter Your position for'O'(1-9)!$"
  16.  
  17.     GAME_COUNTER DB 9 DUP(0)
  18.     PLAYER DB 0
  19.     VICTORY DB 0
  20.  
  21. ENDS
  22. STACK SEGMENT
  23.     DW 128 DUP(0)
  24. ENDS
  25.  
  26. CODE SEGMENT
  27. START:
  28.  
  29.     MOV AX, DATA
  30.     MOV DS, AX
  31.     MOV ES, AX
  32.  
  33.     NEW_GAME:
  34.     CALL INITIALIZE_TEMPLATE
  35.  
  36.     MOV PLAYER, 10B
  37.     MOV VICTORY, 0
  38.     MOV CX, 9
  39.  
  40.     RESTART:
  41.         CALL GENERAL_PROC
  42.  
  43.         LEA DX, MSG_POS
  44.         CALL SHOW
  45.  
  46.         MOV AL, PLAYER
  47.         CMP AL, 1
  48.  
  49.         JE TURN_J2
  50.  
  51.         SHR PLAYER, 1
  52.         LEA DX, MSG_TURN_PLAYER1
  53.         CALL SHOW
  54.         LEA DX, JUMP_LINE
  55.         CALL SHOW
  56.         JMP BREAK_TURNS
  57.  
  58.     TURN_J2:
  59.         SHL PLAYER, 1
  60.         LEA DX, MSG_TURN_PLAYER2
  61.         CALL SHOW
  62.         LEA DX, JUMP_LINE
  63.         CALL SHOW
  64.  
  65.     BREAK_TURNS:
  66.         CALL GUESS
  67.         MOV DL, PLAYER
  68.         CMP DL, 1
  69.  
  70.         JNE PLAY_PLAYER2
  71.         MOV DL, "X"
  72.         JMP CONT_MOV
  73.  
  74.     PLAY_PLAYER2:
  75.         MOV DL, "O"
  76.     CONT_MOV:
  77.         MOV [BX], DL
  78.         CMP CX, 5
  79.         JG VERIFY_NEW_VICTORY
  80.         CALL VERIFY_WON
  81.         CMP VICTORY, 1
  82.         JE WON
  83.         VERIFY_NEW_VICTORY:
  84.         LOOP RESTART
  85.  
  86.      CALL GENERAL_PROC
  87.  
  88.      LEA DX, MSG_TIE
  89.      CALL SHOW
  90.      LEA DX, JUMP_LINE
  91.      CALL SHOW
  92.      JMP SHOW_MSG_RESTART
  93.  
  94.      WON:
  95.      CALL GENERAL_PROC
  96.  
  97.      LEA DX, MSG_VICTORY
  98.      CALL SHOW
  99.      MOV DL, PLAYER
  100.      ADD DL, "0"
  101.      CALL SHOW_CHARACTER
  102.      LEA DX, JUMP_LINE
  103.      CALL SHOW
  104.  
  105.     SHOW_MSG_RESTART:
  106.      LEA DX, MSG_RESTART
  107.      CALL SHOW
  108.      LEA DX, JUMP_LINE
  109.      CALL SHOW
  110.      CALL READ_INPUT
  111.      CMP AL, "S"
  112.      JNE END_GAME
  113.      JMP NEW_GAME
  114.  
  115.     END_GAME:
  116.     MOV AX, 4C00H
  117.     INT 21H
  118.  
  119. GENERAL_PROC:
  120.     CALL CLEAN_SCREEN
  121.     LEA DX, MSG_BEGIN
  122.     CALL SHOW
  123.     LEA DX, JUMP_LINE
  124.     CALL SHOW
  125.     CALL SHOW_TEMPLATE
  126.     CALL SHOW
  127.     RET
  128.  
  129. SHOW_CHARACTER:
  130.     MOV AH, 02H
  131.     INT 21H
  132.     RET
  133.  
  134. SHOW:
  135.     MOV AH, 09H
  136.     INT 21H
  137.     RET
  138.  
  139. CLEAN_SCREEN:
  140.     MOV AH, 0FH
  141.     INT 10H
  142.     MOV AH, 0H
  143.     INT 10H
  144.     RET
  145.  
  146. READ_INPUT:
  147.     MOV AH, 01
  148.     INT 21H
  149.     RET
  150.  
  151. GUESS:
  152.     CALL READ_INPUT
  153.     CALL VERIFY_VALID
  154.     CMP AH, 1
  155.     JE VERIFY_POSITION_ALRDY_GSSD
  156.     MOV DL, 0DH
  157.     CALL SHOW_CHARACTER
  158.     LEA DX, MSG_ERROR2
  159.     CALL SHOW
  160.     LEA DX, JUMP_LINE
  161.     CALL SHOW
  162.     JMP GUESS
  163.  
  164.     VERIFY_POSITION_ALRDY_GSSD:
  165.     LEA BX, GAME_COUNTER
  166.     SUB AL, "1"
  167.     MOV AH, 0
  168.     ADD BX, AX
  169.     MOV AL, [BX]
  170.     CMP AL, "9"
  171.     JNG END_PALP
  172.     MOV DL, 0DH
  173.     CALL SHOW_CHARACTER
  174.     LEA DX, MSG_ERROR1
  175.     CALL SHOW
  176.     LEA DX, JUMP_LINE
  177.     CALL SHOW
  178.     JMP GUESS
  179.     END_PALP:
  180.     LEA DX, JUMP_LINE
  181.     CALL SHOW
  182.     RET
  183.  
  184. INITIALIZE_TEMPLATE:
  185.     LEA BX, GAME_COUNTER
  186.     MOV AL, "1"
  187.     MOV CX, 9
  188.     INITIALIZE_PROX_TEMP:
  189.     MOV [BX], AL
  190.     INC AL
  191.     INC BX
  192.     LOOP INITIALIZE_PROX_TEMP
  193.     RET
  194.  
  195. VERIFY_VALID:
  196.     MOV AH, 0
  197.     CMP AL, "1"
  198.     JL END_GAMEISDIGIT
  199.     CMP AL, "9"
  200.     JG END_GAMEISDIGIT
  201.     MOV AH, 1
  202.     END_GAMEISDIGIT:
  203.     RET
  204.  
  205. SHOW_TEMPLATE:
  206.     LEA BX, GAME_COUNTER
  207.     CALL SHOW_LINE
  208.     LEA DX, TICTACTOE_BOARD
  209.     CALL SHOW
  210.     LEA DX, JUMP_LINE
  211.     CALL SHOW
  212.     CALL SHOW_LINE
  213.     LEA DX, TICTACTOE_BOARD
  214.     CALL SHOW
  215.     LEA DX, JUMP_LINE
  216.     CALL SHOW
  217.     CALL SHOW_LINE
  218.     RET
  219.  
  220. SHOW_LINE:
  221.  
  222.     MOV DL, " "
  223.     CALL SHOW_CHARACTER
  224.     MOV DL, [BX]
  225.     CALL SHOW_CHARACTER
  226.     MOV DL, " "
  227.     CALL SHOW_CHARACTER
  228.     MOV DL, "|"
  229.     CALL SHOW_CHARACTER
  230.     INC BX
  231.  
  232.     MOV DL, " "
  233.     CALL SHOW_CHARACTER
  234.     MOV DL, [BX]
  235.     CALL SHOW_CHARACTER
  236.     MOV DL, " "
  237.     CALL SHOW_CHARACTER
  238.     MOV DL, "|"
  239.     CALL SHOW_CHARACTER
  240.     INC BX
  241.  
  242.     MOV DL, " "
  243.     CALL SHOW_CHARACTER
  244.     MOV DL, [BX]
  245.     CALL SHOW_CHARACTER
  246.     INC BX
  247.  
  248.     LEA DX, JUMP_LINE
  249.     CALL SHOW
  250.     RET
  251.  
  252. VERIFY_WON:
  253.     LEA SI, GAME_COUNTER
  254.     CALL VERIFY_DIAG
  255.     CMP VICTORY, 1
  256.     JE BREAK_WON_VERIFY
  257.     CALL VERIFY_LINES
  258.     CMP VICTORY, 1
  259.     JE BREAK_WON_VERIFY
  260.     CALL VERIFY_COLUMNS
  261.     BREAK_WON_VERIFY:
  262.     RET
  263.  
  264. VERIFY_DIAG:
  265.  
  266.     MOV BX, SI
  267.     MOV AL, [BX]
  268.     ADD BX, 4
  269.     CMP AL, [BX]
  270.     JNE VERIFY_DIAGONAL_PLAY
  271.     ADD BX, 4
  272.     CMP AL, [BX]
  273.     JNE VERIFY_DIAGONAL_PLAY
  274.     MOV VICTORY, 1
  275.     RET
  276.  
  277. VERIFY_LINES:
  278.  
  279.     MOV BX, SI
  280.     MOV AL, [BX]
  281.     INC BX
  282.     CMP AL, [BX]
  283.     JNE SEC_LINE
  284.     INC BX
  285.     CMP AL, [BX]
  286.     JNE SEC_LINE
  287.     MOV VICTORY, 1
  288.     RET
  289.  
  290.     SEC_LINE:
  291.     MOV BX, SI
  292.     ADD BX, 3
  293.     MOV AL, [BX]
  294.     INC BX
  295.     CMP AL, [BX]
  296.     JNE THIRD_LINE
  297.     INC BX
  298.     CMP AL, [BX]
  299.     JNE THIRD_LINE
  300.     MOV VICTORY, 1
  301.     RET
  302.  
  303.     THIRD_LINE:
  304.     MOV BX, SI
  305.     ADD BX, 6
  306.     MOV AL, [BX]
  307.     INC BX
  308.     CMP AL, [BX]
  309.     JNE END_VALID_LINES
  310.     INC BX
  311.     CMP AL, [BX]
  312.     JNE END_VALID_LINES
  313.     MOV VICTORY, 1
  314.     END_VALID_LINES:
  315.     RET
  316.  
  317. VERIFY_COLUMNS:
  318.  
  319.     MOV BX, SI
  320.     MOV AL, [BX]
  321.     ADD BX, 3
  322.     CMP AL, [BX]
  323.     JNE COLUMN_TWO
  324.     ADD BX, 3
  325.     CMP AL, [BX]
  326.     JNE COLUMN_TWO
  327.     MOV VICTORY, 1
  328.     RET
  329.  
  330.     COLUMN_TWO:
  331.     MOV BX, SI
  332.     INC BX
  333.     MOV AL, [BX]
  334.     ADD BX, 3
  335.     CMP AL, [BX]
  336.     JNE COLUMN_THREE
  337.     ADD BX, 3
  338.     CMP AL, [BX]
  339.     JNE COLUMN_THREE
  340.     MOV VICTORY, 1
  341.     RET
  342.  
  343.     COLUMN_THREE:
  344.     MOV BX, SI
  345.     ADD BX, 2
  346.     MOV AL, [BX]
  347.     ADD BX, 3
  348.     CMP AL, [BX]
  349.     JNE END_VERIFIC_COLUMNS
  350.     ADD BX, 3
  351.     CMP AL, [BX]
  352.     JNE END_VERIFIC_COLUMNS
  353.     MOV VICTORY, 1
  354.     END_VERIFIC_COLUMNS:
  355.     RET
  356.  
  357. VERIFY_DIAGONAL_PLAY:
  358.     MOV BX, SI
  359.     ADD BX, 2
  360.     MOV AL, [BX]
  361.     ADD BX, 2
  362.     CMP AL, [BX]
  363.     JNE END_VERIF_DIAG
  364.     ADD BX, 2
  365.     CMP AL, [BX]
  366.     JNE END_VERIF_DIAG
  367.     MOV VICTORY, 1
  368.     END_VERIF_DIAG:
  369.     RET
  370.  
  371. ENDS
  372. END START
  373.  
RAW Paste Data