Noughts and Crosses (Tic Tac Toe to our American cousins) by Philip11th Nov 2003 9:40
|
---|
Summary This is a complete Noughts and Crosses programme. No media required. Just a simple example of how to write such a programme. Description Feel free to use this code as you want. If you do use it, a short credit to me would be smashing! Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com Rem Project: noughts Rem Created: 17/06/2003 22:25:22 Rem ***** Main Source File ***** Rem This is a very simple Noughts and Crosses game hide mouse begin: cls set text size = 12 Rem Create the board array for the first time dim board(3,3) for y = 1 to 3 for x = 1 to 3 board(y,x) = 0 next x next y do gosub intro: do gosub ask_move gosub checkgame gosub computer_move gosub checkgame loop loop intro: cls center text 320, 200, "Welcome to Noughts and Crosses" center text 320, 220, "By Philip Young" center text 320, 240, "Please press any key" wait 1000 wait key cls return ask_move: gosub display_board center text 320, 240, "Where would you like to move?" center text 320, 260, "Press the keypad keys from 1 to 9 for your move" center text 320, 280, "Thus:" center text 320, 310, "7 8 9" center text 320, 330, "4 5 6" center text 320, 350, "1 2 3" center text 320, 380, "Or hit ESCAPE to exit" Rem Main key input code begins do if escapekey() = 1 then gosub end if keystate(79) = 1 AND board(3,1) = 0 board(3,1) = 1 return endif if keystate(80) = 1 AND board(3,2) = 0 board(3,2) = 1 return endif if keystate(81) = 1 AND board(3,3) = 0 board(3,3) = 1 return endif if keystate(75) = 1 AND board(2,1) = 0 board(2,1) = 1 return endif if keystate(76) = 1 AND board(2,2) = 0 board(2,2) = 1 return endif if keystate(77) = 1 AND board(2,3) = 0 board(2,3) = 1 return endif if keystate(71) = 1 AND board(1,1) = 0 board(1,1) = 1 return endif if keystate(72) = 1 AND board(1,2) = 0 board(1,2) = 1 return endif if keystate(73) = 1 AND board(1,3) = 0 board(1,3) = 1 return endif loop return checkgame: Rem Check horizontal lines for win or lose first Rem Reset the horizontal variable x = 1 for y = 1 to 4 if (board(y,x) = 1) AND (board(y,x + 1) = 1) AND (board(y,x + 2) = 1) then gosub win if (board(y,x) = 2) AND (board(y,x + 1) = 2) AND (board(y,x + 2) = 2) then gosub lose next y Rem Check vertical lines for win or lose next Rem Reset vertical variable y = 1 for x = 1 to 4 if (board(y,x) = 1) AND (board(y + 1,x) = 1) AND (board(y + 2,x) = 1) then gosub win if (board(y,x) = 2) AND (board(y + 1,x) = 2) AND (board(y + 2,x) = 2) then gosub lose next x Rem Check diagonal lines for win or lose next Rem Forward diagonal first and reset the horizontal and vertical variable x = 1 y = 1 if (board(y,x) = 1) AND (board(y + 1,x + 1) = 1) AND (board(y + 2,x + 2) = 1) then gosub win if (board(y,x) = 2) AND (board(y + 1,x + 1) = 2) AND (board(y + 2,x + 2) = 2) then gosub lose Rem Backward diagonal next if (board(y,x + 2) = 1) AND (board(y + 1,x + 1) = 1) AND (board(y + 2,x) = 1) then gosub win if (board(y,x + 2) = 2) AND (board(y + 1,x + 1) = 2) AND (board(y + 2,x) = 2) then gosub lose gosub check_draw return check_draw: Rem Code to check if a draw has occurred (as it should in every game of Xs and Os) Rem Reset counting variable a = 0 for y = 1 to 3 for x = 1 to 3 if (board(y,x) > 0) a = a + 1 endif next x next y if a >= 9 then gosub draw return computer_move: Rem Code to control the computer player Rem Reset move variable so computer only makes one move per turn move = 1 gosub check_win if move = 1 then gosub block_win if move = 1 then gosub make_move return check_win: Rem Code to let the computer play a winning move, if available to it Rem Check horizontal lines first Rem Reset the horizontal variable x = 1 for y = 1 to 4 if (board(y,x) = 2) AND (board(y,x + 1) = 2) AND (board(y,x + 2) = 0) AND (move = 1) board(y,x + 2) = 2 move = 0 endif if (board(y,x) = 2) AND (board(y,x + 1) = 0) AND (board(y,x + 2) = 2) AND (move = 1) board(y,x + 1) = 2 move = 0 endif if (board(y,x) = 0) AND (board(y,x + 1) = 2) AND (board(y,x + 2) = 2) AND (move = 1) board(y,x) = 2 move = 0 endif next y Rem Check vertical lines for win or lose next Rem Reset vertical variable y = 1 for x = 1 to 4 if (board(y,x) = 2) AND (board(y + 1,x) = 2) AND (board(y + 2,x) = 0) AND (move = 1) board(y + 2,x) = 2 move = 0 endif if (board(y,x) = 2) AND (board(y + 1,x) = 0) AND (board(y + 2,x) = 2) AND (move = 1) board(y + 1,x) = 2 move = 0 endif if (board(y,x) = 0) AND (board(y + 1,x) = 2) AND (board(y + 2,x) = 2) AND (move = 1) board(y,x) = 2 move = 0 endif next x Rem Check diagonal lines for win or lose next Rem Forward diagonal first and reset the horizontal and vertical variable x = 1 y = 1 if (board(y,x) = 2) AND (board(y + 1,x + 1) = 2) AND (board(y + 2,x + 2) = 0) AND (move = 1) board(y + 2,x + 2) = 0 move = 0 endif if (board(y,x) = 2) AND (board(y + 1,x + 1) = 0) AND (board(y + 2,x + 2) = 2) AND (move = 1) board(y + 1,x + 1) = 2 move = 0 endif if (board(y,x) = 0) AND (board(y + 1,x + 1) = 2) AND (board(y + 2,x + 2) = 2) AND (move = 1) board(y,x) = 2 move = 0 endif Rem Backward diagonal next if (board(y,x + 2) = 2) AND (board(y + 1,x + 1) = 2) AND (board(y + 2,x) = 0) AND (move = 1) board(y + 2,x) = 2 move = 0 endif if (board(y,x + 2) = 2) AND (board(y + 1,x + 1) = 0) AND (board(y + 2,x) = 2) AND (move = 1) board(y + 1,x + 1) = 2 move = 0 endif if (board(y,x + 2) = 0) AND (board(y + 1,x + 1) = 2) AND (board(y + 2,x) = 2) AND (move = 1) board(y,x + 2) = 2 move = 0 endif return block_win: Rem Code to let the computer block the human player from winning Rem Check horizontal lines first Rem Reset the horizontal variable x = 1 for y = 1 to 4 if (board(y,x) = 1) AND (board(y,x + 1) = 1) AND (board(y,x + 2) = 0) AND (move = 1) board(y,x + 2) = 2 move = 0 endif if (board(y,x) = 1) AND (board(y,x + 1) = 0) AND (board(y,x + 2) = 1) AND (move = 1) board(y,x + 1) = 2 move = 0 endif if (board(y,x) = 0) AND (board(y,x + 1) = 1) AND (board(y,x + 2) = 1) AND (move = 1) board(y,x) = 2 move = 0 endif next y Rem Check vertical lines next Rem Reset vertical variable y = 1 for x = 1 to 4 if (board(y,x) = 1) AND (board(y + 1,x) = 1) AND (board(y + 2,x) = 0) AND (move = 1) board(y + 2,x) = 2 move = 0 endif if (board(y,x) = 1) AND (board(y + 1,x) = 0) AND (board(y + 2,x) = 1) AND (move = 1) board(y + 1,x) = 2 move = 0 endif if (board(y,x) = 0) AND (board(y + 1,x) = 1) AND (board(y + 2,x) = 1) AND (move = 1) board(y,x) = 2 move = 0 endif next x Rem Check diagonal lines for win or lose next Rem Forward diagonal first and reset the horizontal and vertical variable x = 1 y = 1 if (board(y,x) = 1) AND (board(y + 1,x + 1) = 1) AND (board(y + 2,x + 2) = 0) AND (move = 1) board(y + 2,x + 2) = 0 move = 0 endif if (board(y,x) = 1) AND (board(y + 1,x + 1) = 0) AND (board(y + 2,x + 2) = 1) AND (move = 1) board(y + 1,x + 1) = 2 move = 0 endif if (board(y,x) = 0) AND (board(y + 1,x + 1) = 1) AND (board(y + 2,x + 2) = 1) AND (move = 1) board(y,x) = 2 move = 0 endif Rem Backward diagonal next if (board(y,x + 2) = 1) AND (board(y + 1,x + 1) = 1) AND (board(y + 2,x) = 0) AND (move = 1) board(y + 2,x) = 2 move = 0 endif if (board(y,x + 2) = 1) AND (board(y + 1,x + 1) = 0) AND (board(y + 2,x) = 1) AND (move = 1) board(y + 1,x + 1) = 2 move = 0 endif if (board(y,x + 2) = 0) AND (board(y + 1,x + 1) = 1) AND (board(y + 2,x) = 1) AND (move = 1) board(y,x + 2) = 2 move = 0 endif return make_move: Rem Code to make the computer play a free move Rem First take the middle square if it is not already occupied x = 1 y = 1 if (board(y + 1,x + 1) = 0) AND (move = 1) board(y + 1,x + 1) = 2 move = 0 endif Remstart Now stop the computer from falling into this trap X X O O X Remend x = 1 y = 1 if (board(y + 1,x + 1) = 2) AND (((board(y,x) = 1) AND (board(y + 2,x + 2) = 1)) OR ((board(y,x + 2) = 1) AND (board(y + 2,x) = 1))) AND (board(y,x + 1) = 0) AND (board(y + 1,x) = 0) AND (board(y + 1,x + 2) = 0) AND (board(y + 2,x + 1) = 0) AND (move = 1) board(y,x + 1) = 2 move = 0 endif Rem Make a free move a = 0 for y = 1 to 3 for x = 1 to 3 if (board(y,x) = 0) AND (move = 1) board(y,x) = 2 move = 0 endif next x next y return display_board: cls Rem Show Board status Rem Reset cursor variables cursorx = 300 cursory = 100 for y = 1 to 3 for x = 1 to 3 if board(y,x) = 0 center text cursorx, cursory, "-" endif if board(y,x) = 1 center text cursorx, cursory, "X" endif if board(y,x) = 2 center text cursorx, cursory, "O" endif cursorx = cursorx + 20 next x cursorx = 300 cursory = cursory + 20 next y return win: gosub display_board center text 320, 220, "You win! Press any key to continue" wait 1000 a = 0 do a = scancode() if a > 0 then gosub replay loop return lose: gosub display_board center text 320, 220, "You lose! Press any key to continue" wait 1000 a = 0 do a = scancode() if a > 0 then gosub replay loop return draw: gosub display_board center text 320, 220, "Its a draw! Press any key to continue" wait 1000 a = 0 do a = scancode() if a > 0 then gosub replay loop return end: cls center text 320, 220, "Goodbye! Press any key to quit" wait 1000 wait key end return replay: cls center text 320, 220, "Press Y to play again or N to quit" wait 1000 a = 0 do a = scancode() if a = 21 then gosub begin if a = 49 then end loop return |