set display mode 1024,768,32 show mouse:sync on:backdrop on:color backdrop 0 size=80 linewidth=10 dim gridx(3) dim gridy(3) dim board(3,3) dim player$(2) dim cx(9) dim cy(9) dim ox(9) dim oy(9) player$(0)="":player$(1)="X":player$(2)="O" for i=1 to 3 gridx(i)=(size+linewidth)*(i-1) gridy(i)=(size+linewidth)*(i-1) next i for i=1 to 9 read cx(i),cy(i) next i for i=1 to 9 read ox(i),oy(i) next i data 2,2,1,1,1,3,3,1,3,3,1,2,3,2,2,3,2,1,2,2,1,2,3,2,2,3,2,1,1,1,1,3,3,1,3,3 player=1 computer=2 moves=0 while not escapekey() ink rgb(255,255,255),0 box gridx(1),gridy(1),gridx(3)+size,gridy(3)+size mposx=0:mposy=0 set text font "arial":set text size size:set text to bold for x=1 to 3 for y=1 to 3 ink 0,0 box gridx(x),gridy(y),gridx(x)+size,gridy(y)+size ink rgb(0,0,255),0 text gridx(x)+linewidth,gridy(y)+linewidth,player$(board(x,y)) if mousex()>gridx(x) and mousex()<gridx(x)+size and mousey()>gridy(y) and mousey()<gridy(y)+size mposx=x:mposy=y endif next y if board(x,1)>0 and board(x,1)=board(x,2) and board(x,2)=board(x,3) then win=3-player if board(1,x)>0 and board(1,x)=board(2,x) and board(2,x)=board(3,x) then win=3-player next x if board(1,1)>0 and board(1,1)=board(2,2) and board(2,2)=board(3,3) then win=3-player if board(1,3)>0 and board(1,3)=board(2,2) and board(2,2)=board(3,1) then win=3-player if computer=player and play=0 and win=0 gosub computer_move board(mposx,mposy)=player player=3-player inc moves endif if mouseclick()=1 and play=0 and win=0 and player<>computer if mposx>0 and mposy>0 if board(mposx,mposy)=0 board(mposx,mposy)=player play=1:player=3-player inc moves if moves=9 then win=3 endif endif endif if mouseclick()=0 then play=0 if win>0 set text size 16 set cursor gridx(3)+200,0 if win=3 print "Draw" else print player$(win);" wins" endif set cursor gridx(3)+200,40 print "Press ENTER to play again" endif if win>0 and returnkey() gosub reset_board endif sync endwhile end reset_board: for i=1 to 3 for j=1 to 3 board(i,j)=0 next j next i win=0:moves=0:player=1 return computer_move: mposx=0:mposy=0:opp3=0:opp4=0:me3=0:me4=0:posx3=0:posx4=0:posy3=0:posy4=0 for x=1 to 3 opp1=0:opp2=0:me1=0:me2=0:posy1=0:posx2=0 for y=1 to 3 if board(x,y)=3-player then inc opp1 if board(x,y)=player then inc me1 if board(x,y)=0 then posy1=y if board(y,x)=3-player then inc opp2 if board(y,x)=player then inc me2 if board(y,x)=0 then posx2=y next y if me1=2 and opp1=0 mposx=x:mposy=posy1 else if me2=2 and opp2=0 mposx=posx2:mposy=x endif endif if mposx=0 and mposy=0 if opp1=2 and me1=0 mposx=x:mposy=posy1 else if opp2=2 and me2=0 mposx=posx2:mposy=x endif endif endif if board(x,x)=3-player then inc opp3 if board(x,x)=player then inc me3 if board(x,x)=0 then posx3=x:posy3=x if board(x,(4-x))=3-player then inc opp4 if board(x,(4-x))=player then inc me4 if board(x,(4-x))=0 then posx4=x:posy4=4-x next x if me3=2 and opp3=0 mposx=posx3:mposy=posy3 else if me4=2 and opp4=0 mposx=posx4:mposy=posy4 endif endif if mposx=0 and mposy=0 if opp3=2 and me3=0 mposx=posx3:mposy=posy3 else if opp4=2 and me4=0 mposx=posx4:mposy=posy4 endif endif endif if mposx=0 and mposy=0 if board(2,2)=player for x=1 to 9 if board(ox(x),oy(x))=0 mposx=ox(x):mposy=oy(x) x=9 endif next x else for x=1 to 9 if board(cx(x),cy(x))=0 mposx=cx(x):mposy=cy(x) x=9 endif next x endif endif return