10 rem tic tac toe - c64 basic 20 rem learning edition 30 rem by openai 40 rem 50 rem initialize the game board 60 dim board(3,3) 70 for row=1 to 3 80 for col=1 to 3 90 board(row,col)=" " 100 next col 110 next row 120 rem 130 rem display the game board 140 print "tic tac toe": print 150 print " 1 | 2 | 3 " 160 print "---+--+--" 170 print " 4 | 5 | 6 " 180 print "---+--+--" 190 print " 7 | 8 | 9 " 200 rem 210 rem main game loop 220 for turn=1 to 9 230 rem determine who's turn it is 240 if (turn mod 2)=0 then 260 242 print "do you want to play with x or o? (x/o)" 244 input xo 246 if xo="x" then player="x" else player="o" 250 player="x" 260 else 270 player="o" 280 end if 290 rem 300 rem get player's move 310 print "player "; player; " turn. enter move (1-9): "; 320 input move 330 rem 340 rem validate move 350 if move < 1 or move > 9 then print "invalid move. please enter a number between 1 and 9.": goto 310 360 row=int((move-1)/3) 370 col=move-3*(row-1) 380 if board(row,col)<>" " then 310 400 board(row,col)=player 410 rem 420 rem display game board 430 print "tic tac toe": print 440 print " "; board(1,1); " | "; board(1,2); " | "; board(1,3); " " 450 print "---+--+--" 460 print " "; board(2,1); " | "; board(2,2); " | "; board(2,3); " " 470 print "---+--+--" 480 print " "; board(3,1); " | "; board(3,2); " | "; board(3,3); " " 490 rem 500 rem check for win 510 if (board(1,1)=player and board(2,2)=player and board(3,3)=player) or _ 520 (board(1,3)=player and board(2,2)=player and board(3,1)=player) or _ 530 (board(1,1)=player and board(1,2)=player and board(1,3)=player) or _ 540 (board(2,1)=player and board(2,2)=player and board(2,3)=player) or _ 550 (board(3,1)=player and board(3,2)=player and board(3,3)=player) or _ 580 (board(1,2)=player and board(2,2)=player and board(3,2)=player) then 590 print "player "; player; " wins!" 600 goto 620 610 end if 620 rem 630 next turn 640 rem 650 rem check for draw 660 if turn = 9 then 670 print "it's a draw!" 680 end if 690 rem 700 rem add learning feature for board 710 rem store the previous move in a list 720 dim moves(9,3) 730 for i=1 to 9 740 moves(i,1)=i 750 next i 760 rem 770 rem store the outcome of the previous games in a list 780 dim outcomes(9) 790 rem 800 rem update the list after each game 810 outcome=-1 820 if turn=9 then outcome=0 830 if board(1,1)=player and board(2,2)=player and board(3,3)=player then outcome=1 840 if board(1,3)=player and board(2,2)=player and board(3,1)=player then outcome=1 850 if board(1,1)=player and board(1,2)=player and board(1,3)=player then outcome=1 860 if board(2,1)=player and board(2,2)=player and board(2,3)=player then outcome=1 870 if board(3,1)=player and board(3,2)=player and board(3,3)=player then outcome=1 880 if board(1,2)=player and board(2,2)=player and board(3,2)=player then outcome=1 890 rem 900 rem add the outcome to the list of outcomes 910 for i=8 to 1 step -1 920 outcomes(i)=outcomes(i) 930 next i 940 outcomes(1)=outcome 950 rem 960 rem make the computer player smarter by learning from its mistakes 970 if player="o" then 980 rem check the outcomes of the previous moves 990 bestmove=0 1000 bestoutcome=-1 1010 for i=1 to 9 1020 if board(int((moves(i,1)-1)/3),moves(i,1)-3*(int((moves(i,1)-1)/3)1))=" " then 1030 if outcomes(i)>bestoutcome then 1040 bestmove=moves(i,1) 1050 bestoutcome=outcomes(i) 1060 end if 1070 end if 1080 next i 1090 move=bestmove 1100 end if 1110 rem 1120 rem repeat the main game loop 1130 goto 220 1140 rem 1150 end
Teemu1.huusko