iPad 1:08 AM 59% Note: Please post your homework to ICS232 D2L on or before the
ID: 3589762 • Letter: I
Question
iPad 1:08 AM 59% Note: Please post your homework to ICS232 D2L on or before the due date. Pogamming Asignnt 2: The Game of Tic-Tae-Toe ASSEMBLY LANGUAGE Required (100 points): The program should allow two users to play a game of tic-tac-toe. The program should display the board in the top left quadrant of the screen and alternately allow user 1 and user 2 to enter a move. The move can be the digits 1 through 9. The program should make sure the move is legal and inform the users when one of them won or if the game is a draw. The screen can be cleared to allowing drawing of the board by calling the CIrScr The board should be displayed as shown below. Each empty square should display its number and occupied squares should display an X or an O 1X1 3 4101 6 71x19 At least three procedures must be used. displayBoard should clear the screen and display the current board. getNextMove should take as an argument in EAX the player number ( or 2), request the next move to be given, validate the move and update the board based on the move. checkGame should check if the game is over or not. That is, either player 1 or 2 won, a tie game or game is not yet over. The result should be returned in the EAX register. The main loop should call the procedures in the correct order and check the result of checkGame to determine if it should loop or terminate Bonus (Up to 20 points): Allow the option for one user to play against the computer. The user will make a move, then the computer makes the next move. A program does not need to play a good game to get bonus points. A fairly simple algorithm should never allow the computer to lose. You could use some very simplistie algorithms also, such as, always move to first available spot or randomly choose a spot. Submission Requirements: You may either use Microsoft Visual Studio or the DOS command prompt window to assemble, link and execute the program. You need to submit a ZIP file that contains the assembly language source code (ASM), the executable program (.EXE), and sample output from running the program. Please document the program appropriately. Optionally any other documentation you wish to provide that explains the program or problems youExplanation / Answer
Solution :-
The below given assembly code implements a tic-tac-toe game of two players. The program asks user take turn and input the move and the turn goes to second player. If curent player win the game the messsage will displayed. If no player win the game then a tie in the game message is displayed. After ending of one game program asks the user to continue and play another game. If user responds with 'y' then game continues, if user responds with 'n' then the program terminated. This a two player tic-tac-toe game.
The assembly codestarts here.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data segment
grid db 9 dup(0)
player db 0
win db 0
temp db 0
newGameQuest db "Would you like a rematch? (y - yes, any key - no)$"
welcome db "Tic Tac Toe Game$"
separator db "---|---|---$"
enterLoc db "Enter your move by location(1-9)$"
turnMessageX db "Player 1 (X) turn$"
turnMessageO db "Player 2 (O) turn$"
tieMessage db "A tie between the two players!$"
winMessage db "The player who won was player $"
inDigitError db "ERROR!, this place is taken$"
inError db "ERROR!, input is not a digit$"
newline db 0Dh,0Ah,'$'
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
mov ax, data
mov ds, ax
mov es, ax
newGame:
call initiateGrid
mov player, 10b
mov win, 0
mov cx, 9
gameAgain:
call clearScreen
lea dx, welcome
call printString
lea dx, newline
call printString
lea dx, enterLoc
call printString
lea dx, newline
call printString
call printString
call printGrid
mov al, player
cmp al, 1
je p2turn
; last move done by player 2
shr player, 1; 0010b --> 0001b;
lea dx, turnMessageX
call printString
lea dx, newline
call printString
jmp endPlayerSwitch
p2turn:; last move done by player 1
shl player, 1; 0001b --> 0010b
lea dx, turnMessageO
call printString
lea dx, newline
call printString
endPlayerSwitch:
call getMove; BX will point to board position at the end of getMove
mov dl, player
cmp dl, 1
jne p2move
mov dl, 'X'
jmp contMoves
p2move:
mov dl, 'O'
contMoves:
mov [bx], dl
cmp cx, 5 ; before 5th turn no check
jg noWinCheck
call checkWin
cmp win, 1
je won
noWinCheck:
loop gameAgain
;tie, cx = 0 no player win the game
call clearScreen
lea dx, welcome
call printString
lea dx, newline
call printString
call printString
call printString
call printGrid
lea dx, tieMessage
call printString
lea dx, newline
call printString
jmp askForNewGame
won: ; current player win the game
call clearScreen
lea dx, welcome
call printString
lea dx, newline
call printString
call printString
call printString
call printGrid
lea dx, winMessage
call printString
mov dl, player
add dl, '0'
call putChar
lea dx, newline
call printString
askForNewGame:
lea dx, newGameQuest; ask the user for new game
call printString
lea dx, newline
call printString
call getChar
cmp al, 'y'; start new game if 'y' is pressed
jne sof
jmp newGame
sof:
mov ax, 4c00h
int 21h
; Sets ah = 01
; Input char into al;
getChar:
mov ah, 01
int 21h
ret
; Sets ah = 02
; Output char from dlputChar:
mov ah, 02
int 21h
ret
; Sets ah = 09 and Outputs string from dx
; al changes
printString:
mov ah, 09
int 21h
ret
; Clears the screen and ah = 0 at the end
clearScreen:
mov ah, 0fh
int 10h
mov ah, 0
int 10h
ret
; Gets location that can be used
; bx will hold the position (bx[al]) and al will hold the place number(0 - 8)
getMove:
call getChar;
call isValidDigit
cmp ah, 1
je contCheckTaken
mov dl, 0dh
call putChar
lea dx, inError
call printString
lea dx, newline
call printString
jmp getMove
; check if(grid[al] > '9')
contCheckTaken:
lea bx, grid
sub al, '1'
mov ah, 0
add bx, ax
mov al, [bx]
cmp al, '9'
jng finishGetMove
mov dl, 0dh
call putChar
lea dx, inDigitError
call printString
lea dx, newline
call printString
jmp getMove
finishGetMove:
lea dx, newline
call printString
ret
; Set the grid from '1' to '9' and Uses bx, al, cx
initiateGrid:
lea bx, grid
mov al, '1'
mov cx, 9
initNextTa:
mov [bx], al
inc al
inc bx
loop initNextTa
ret
; checks if a char in al is a digit
; DOESN'T include '0' and if is Digit, ah = 1, else ah = 0
isValidDigit:
mov ah, 0
cmp al, '1'
jl sofIsDigit
cmp al, '9'
jg sofIsDigit
mov ah, 1
sofIsDigit:
ret
; Outputs the 3x3 grid and uses bx, dl, dx
printGrid:
lea bx, grid
call printRow
lea dx, separator
call printString
lea dx, newline
call printString
call printRow
lea dx, separator
call printString
lea dx, newline
call printString
call printRow
ret
; Outputs a single row of the grid and bx is the first number in the row
; At the end: dl = third cell on row, bx += 3, for the next row and dx points to newline
printRow:
;First Cell
mov dl, ' '
call putChar
mov dl, [bx]
call putChar
mov dl, ' '
call putChar
mov dl, '|'
call putChar
inc bx
;Second Cell
mov dl, ' '
call putChar
mov dl, [bx]
call putChar
mov dl, ' '
call putChar
mov dl, '|'
call putChar
inc bx
;Third Cell
mov dl, ' '
call putChar
mov dl, [bx]
call putChar
inc bx
lea dx, newline
call printString
ret
; if a player won set 1 in al , 0 for lost and 1 for win
; Changes bx
checkWin:
lea si, grid
call checkDiagonal
cmp win, 1
je endCheckWin
call checkRows
cmp win, 1
je endCheckWin
call CheckColumns
endCheckWin:
ret
checkDiagonal:
;DiagonalLtR
mov bx, si
mov al, [bx]
add bx, 4
cmp al, [bx]
jne diagonalRtL
add bx, 4
cmp al, [bx]
jne diagonalRtL
mov win, 1
ret
diagonalRtL:
mov bx, si
add bx, 2
mov al, [bx]
add bx, 2
cmp al, [bx]
jne endCheckDiagonal
add bx, 2 ;
cmp al, [bx]
jne endCheckDiagonal
mov win, 1
endCheckDiagonal:
ret
checkRows: ;firstRow
mov bx, si
mov al, [bx]
inc bx
cmp al, [bx]
jne secondRow
inc bx
cmp al, [bx]
jne secondRow
mov win, 1
ret
secondRow:
mov bx, si
add bx, 3
mov al, [bx]
inc bx
cmp al, [bx]
jne thirdRow
inc bx
cmp al, [bx]
jne thirdRow
mov win, 1
ret
thirdRow:
mov bx, si
add bx, 6
mov al, [bx]
inc bx
cmp al, [bx]
jne endCheckRows
inc bx
cmp al, [bx]
jne endCheckRows
mov win, 1
endCheckRows:
ret
CheckColumns: ;firstColumn
mov bx, si
mov al, [bx]
add bx, 3
cmp al, [bx]
jne secondColumn
add bx, 3
cmp al, [bx]
jne secondColumn
mov win, 1
ret
secondColumn:
mov bx, si
inc bx
mov al, [bx]
add bx, 3
cmp al, [bx]
jne thirdColumn
add bx, 3
cmp al, [bx]
jne thirdColumn
mov win, 1
ret
thirdColumn:
mov bx, si
add bx, 2
mov al, [bx]
add bx, 3
cmp al, [bx]
jne endCheckColumns
add bx, 3
cmp al, [bx]
jne endCheckColumns
mov win, 1
endCheckColumns:
ret
ends
end start
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.