;Here you play TIC-TAC-TOE against›;the computer. The computer plays›;by picking a random spot on the›;board. If it is used, it picks›;another until it finds an unused›;place to play.›››MODULE››;use a global because the board is›;used by a number of different›;routines and each may or may not›;change it. And we want that change›;to affect the board everywhere.›;›;The ARRAY goes from 1 to 9. If that›;spot has a 0, that means it is unused.›;If it has a 1 that means the player›;has an X there. A 2 means the computer›;placed a O there.››››BYTE ARRAY board(10)››››;This PROC draws the board with the›;X's and O's in their proper places,››PROC print_screen()›› BYTE i,› j,› k›› k=0› PutE()› FOR j=1 TO 3› DO› FOR i=1 TO 3› DO› k = k + 1› IF board(k)=1 THEN› Print("X")› ELSEIF board(k)=2 THEN› Print("O")› ELSE› Print(" ")› FI› IF i<>3 THEN› Print("|")› FI› OD› PutE()› IF j<>3 THEN› PrintE("")› FI› OD›RETURN›››;Here we clear out the board by putting›;0's in all the places››PROC clear_board()› › BYTE i›› FOR i=1 TO 10› DO› board(i)=0› OD›RETURN›››;Print a help screen so the player›;knows how to choose his position››PROC help()›› PutE()› PrintE("The board is set up this:")› PutE()› PrintE(" 1|2|3")› PrintE(" ")› PrintE(" 4|5|6")› PrintE(" ")› PrintE(" 7|8|9")› PutE()› PrintE("Enter 0 for help.")› PutE()›RETURN›››;Here we check all the possible ways to›;win, vertical, horizontal and diagonal›;to see it the player has 3 1's in a row.›;It returns a 1 if the player has won›;and a 0 otherwise.››BYTE FUNC player_won()› › BYTE rval››› rval=0› IF board(1)=1 AND board(2)=1 AND board(3)=1 THEN› rval=1› FI› IF board(4)=1 AND board(5)=1 AND board(6)=1 THEN› rval=1› FI› IF board(7)=1 AND board(8)=1 AND board(9)=1 THEN› rval=1› FI› IF board(1)=1 AND board(4)=1 AND board(7)=1 THEN› rval=1› FI› IF board(2)=1 AND board(5)=1 AND board(8)=1 THEN› rval=1› FI› IF board(3)=1 AND board(6)=1 AND board(9)=1 THEN› rval=1› FI› IF board(1)=1 AND board(5)=1 AND board(9)=1 THEN› rval=1› FI› IF board(3)=1 AND board(5)=1 AND board(7)=1 THEN› rval=1› FI››RETURN(rval)›››;Same thing as the above except it sees›;if the computer has placed 3 2's in›;a row›››BYTE FUNC computer_won()› › BYTE rval››› rval=0› IF board(1)=2 AND board(2)=2 AND board(3)=2 THEN› rval=1› FI› IF board(4)=2 AND board(5)=2 AND board(6)=2 THEN› rval=1› FI› IF board(7)=2 AND board(8)=2 AND board(9)=2 THEN› rval=1› FI› IF board(1)=2 AND board(4)=2 AND board(7)=2 THEN› rval=1› FI› IF board(2)=2 AND board(5)=2 AND board(8)=2 THEN› rval=1› FI› IF board(3)=2 AND board(6)=2 AND board(9)=2 THEN› rval=1› FI› IF board(1)=2 AND board(5)=2 AND board(9)=2 THEN› rval=1› FI› IF board(3)=2 AND board(5)=2 AND board(7)=2 THEN› rval=1› FI››RETURN(rval)›››;Here the computer comes up with a›;random move. If it is unused, it›;returns where it wants to move. If›;it chooses a spot already taken, it›;picks until it finds a empty spot.›;After 30 tries, if it didn't find a›;empty spot, it just thinks the game is›;a tie ("Cat's game") and clears the›;board to start over››BYTE FUNC computer_turn()›› BYTE rnum,› i›› i=0› DO› rnum=Rand(9)+1› i = i + 1› IF i>30 THEN› PutE()› PrintE("Cat's game. We'll start over.")› PutE()› clear_board()› EXIT› FI› UNTIL board(rnum)=0› OD›RETURN(rnum)›››PROC main()›› CHAR ARRAY name(20)› › BYTE move,› winner››;* Set everything up *›› clear_board()› Print("What is your name? ")› InputS(name)› PrintF("%EOk %S, you are X's.%E",name)› help()› winner=0›› DO› print_screen()››;*Get the player's move*›› DO› PrintF("%E%S's turn: ",name)› move=InputB()› UNTIL move<10› OD› ›;*Do they need help?*›› IF move=0 THEN› help()› DO› PrintF("%E%S's turn: ",name)› move=InputB()› UNTIL move<10› OD› FI››;*check to see if the spot is empty or not*›› IF board(move)<>0 THEN› PrintE("That space is already taken!")› ELSE› board(move)=1› FI› print_screen()› ›;*Did the player win?*›› IF player_won() THEN› winner=1› EXIT› FI››;*Did the computer win? If not get it's move*›› IF computer_won() THEN› winner=0› ELSE› move=computer_turn()› board(move)=2› FI› PrintF("%EComputer's turn: %U%E",move)›› UNTIL computer_won()› OD››;*If we get here, either the computer›; won or the player did (if the player›; won we EXIT out of the loop and the›; computer just used an UNTIL loop)*››› print_screen()› IF winner=1 THEN› PrintF("%E%EYou Won!!!!!%E")› ELSE› PrintF("%E%ESorry, the computer beat you.%E")› FI››RETURN››