Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tic-Tac-Toe(UNBEATABLE COMPUTER IF YOU BEAT TELL ME!)
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Keerigan




PostPosted: Mon Nov 21, 2005 12:50 pm   Post subject: Tic-Tac-Toe(UNBEATABLE COMPUTER IF YOU BEAT TELL ME!)

Arrow Make your own X's and O's
code:
setscreen ("graphics:905;605,position:center;center,nobuttonbar")
var u : array 1 .. 5 of int %the array that holds all of the players moves
var c : array 1 .. 5 of int %the array that holds all of the computers moves
var win : int := 0 %counter for how many wins the user obtains(but won't get any)
var lose : int := 0 %counter for how many loses the user obtains
var cat : int := 0 %counter for how many cat games were played

proc Draw_Screen
%procedure that draws the Tic-Tac_Toe lines and
%the counters
cls
drawbox (5, 5, 600, 600, green)
drawline (205, 5, 205, 600, green)
drawline (405, 5, 405, 600, green)
drawline (5, 205, 600, 205, green)
drawline (5, 405, 600, 405, green)
locate (34, 77)
put "WINS:"
locate (34, 85)
put win
locate (35, 77)
put "LOSES:"
locate (35, 86)
put lose
locate (36, 77)
put "CAT GAMES:"
locate (36, 90)
put cat
end Draw_Screen

proc Initialize
%resets the global values to "0" and
%used if another game will be played without closing the window
for i : 1 .. 5
u (i) := -999
c (i) := -999
end for
end Initialize

function User_Win : boolean
%checks to see if 3 of the users moves equals 15
%which will make the user win
if u (1) + u (2) + u (3) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1 %adds to the win counter
locate (34, 85)
put win %puts the win counter
result true
elsif u (2) + u (3) + u (4) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (3) + u (4) + u (5) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (1) + u (3) + u (4) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (1) + u (4) + u (5) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (1) + u (2) + u (4) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (1) + u (3) + u (5) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (2) + u (4) + u (5) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
elsif u (2) + u (3) + u (5) = 15 then
locate (1, 77)
put "You Win!"
win := win + 1
locate (34, 85)
put win
result true
end if
result false
end User_Win

function Computer_Win : boolean
%to check if 3 of the computers move will equal 15
%which will make the computer win
if c (1) + c (2) + c (3) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1 %adds to the lose counter
locate (35, 86)
put lose %puts the lose counter
result true
elsif c (2) + c (3) + c (4) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (3) + c (4) + c (5) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (1) + c (3) + c (4) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (1) + c (4) + c (5) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (1) + c (2) + c (4) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (1) + c (3) + c (5) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (2) + c (4) + c (5) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
elsif c (2) + c (3) + c (5) = 15 then
locate (1, 77)
put "You Lose!"
lose := lose + 1
locate (35, 86)
put lose
result true
end if
result false
end Computer_Win

function taken (x : int) : boolean
%checks to see if the move selected has already been taken
%if true then another move will have to be made
for i : 1 .. 5
if x = u (i) or x = c (i) then
result true
end if
end for
result false
end taken


function Get_User_Move : int
%gets the users move by clicking with the mouse in the
%desired location
var x, y, button : int
loop
Mouse.Where (x, y, button)
if button = 1 then
if x >= 5 and x < 205 and y >= 5 and y <= 205 then
if not (taken (6)) then


result 6
end if
elsif x >= 205 and x < 405 and y >= 205 and y <= 405 then
if not (taken (5)) then


result 5
end if
elsif x >= 405 and x < 600 and y >= 405 and y <= 600 then
if not (taken (4)) then


result 4
end if
elsif x >= 5 and x < 205 and y >= 205 and y <= 405 then
if not (taken (7)) then


result 7
end if
elsif x >= 5 and x < 205 and y >= 405 and y <= 600 then
if not (taken (2)) then


result 2
end if
elsif x >= 205 and x < 405 and y >= 5 and y <= 205 then
if not (taken (1)) then


result 1
end if
elsif x >= 205 and x < 405 and y >= 405 and y <= 600 then
if not (taken (9)) then


result 9
end if
elsif x >= 405 and x < 600 and y >= 5 and y <= 206 then
if not (taken (8)) then


result 8
end if

elsif x >= 405 and x < 600 and y >= 205 and y <= 405 then
if not (taken (3)) then


result 3
end if
end if
end if
end loop
end Get_User_Move

function Can_Computer_Win : int
%adds 2 of the computers move together and will move the
%spot that equals the sum of the 2 moves - 15 and that number will be made
for i : 1 .. 4
for j : i + 1 .. 5
if c (i) + c (j) = 11 then
if not (taken (4)) then

result 4
end if
elsif c (i) + c (j) = 6 then
if not (taken (9)) then

result 9
end if
elsif c (i) + c (j) = 13 then
if not (taken (2)) then

result 2
end if
elsif c (i) + c (j) = 9 then
if not (taken (6)) then

result 6
end if
elsif c (i) + c (j) = 8 then
if not (taken (7)) then

result 7
end if
elsif c (i) + c (j) = 7 then
if not (taken (8)) then

result 8
end if
elsif c (i) + c (j) = 10 then
if not (taken (5)) then

result 5
end if
elsif c (i) + c (j) = 12 then
if not (taken (3)) then

result 3
end if
elsif c (i) + c (j) = 14 then
if not (taken (1)) then

result 1
end if
end if
end for
end for
result 0
end Can_Computer_Win

function Block_User : int
%adds 2 of users move together and moves the computer into the
%spot need for the user to get 15
for i : 1 .. 4
for j : i + 1 .. 5
if u (i) + u (j) = 11 then
if not (taken (4)) then

result 4
end if
elsif u (i) + u (j) = 6 then
if not (taken (9)) then

result 9
end if
elsif u (i) + u (j) = 13 then
if not (taken (2)) then
result 2
end if
elsif u (i) + u (j) = 9 then
if not (taken (6)) then

result 6
end if
elsif u (i) + u (j) = 8 then
if not (taken (7)) then
result 7
end if
elsif u (i) + u (j) = 7 then
if not (taken (8)) then

result 8
end if
elsif u (i) + u (j) = 10 then
if not (taken (5)) then

result 5
end if
elsif u (i) + u (j) = 12 then
if not (taken (3)) then

result 3
end if
elsif u (i) + u (j) = 14 then
if not (taken (1)) then

result 1
end if
end if
end for
end for
result 0
end Block_User

function Get_Computer_Move (turn : int) : int
%uses the Can_Computer_Win and the Block_User and
%checks possible combination moves that the user can make
%to trick the computer and then moves accordingly
var move : int
loop
if Can_Computer_Win = 0 then %if the computer can win it will go for the win else it will check for a black
if Block_User = 0 then %if the computer can block it will go for the block else it will take the middle spot
if taken (5) then %goes for the middle but if taken then
for i : 1 .. 5
for j : 1 .. 5
if u (i) = 2 and u (j) = 8 then
if not (taken (9)) then
result 9
end if
end if
if u (i) = 4 and u (j) = 6 then
if not (taken (3)) then
result 3
end if
end if
if u (i) = 2 and u (j) = 1 then
if not (taken (6)) then
result 6
end if
end if
if u (i) = 2 and u (j) = 3 then
if not (taken (4)) then
result 4
end if
end if
if u (i) = 4 and u (j) = 1 then
if not (taken (8)) then
result 8
end if
end if
if u (i) = 4 and u (j) = 7 then
if not (taken (2)) then
result 2
end if
end if
if u (i) = 6 and u (j) = 9 then
if not (taken (2)) then
result 2
end if
end if
if u (i) = 6 and u (j) = 3 then
if not (taken (8)) then
result 8
end if
end if
if u (i) = 8 and u (j) = 7 then
if not (taken (6)) then
result 6
end if
end if
if u (i) = 8 and u (j) = 9 then
if not (taken (4)) then
result 4
end if
end if
if u (i) = 5 and u (j) = 2 then
if not (taken (6)) then
result 6
end if
end if
if u (i) = 5 and u (j) = 6 then
if not (taken (2)) then
result 2
end if
end if
if u (i) = 5 and u (j) = 4 then
if not (taken (8)) then
result 8
end if
end if
if u (i) = 5 and u (j) = 8 then
if not (taken (4)) then
result 4
end if
end if
if u (i) = 7 and u (j) = 9 then
if not (taken (2)) then
result 2
end if
end if
if u (i) = 7 and u (j) = 1 then
if not (taken (6)) then
result 6
end if
end if
if u (i) = 1 and u (j) = 3 then
if not (taken (8)) then
result 8
end if
end if
if u (i) = 3 and u (j) = 9 then
if not (taken (4)) then
result 4
end if
end if
end for
end for
for i : 1 .. 5
if u (i) = 5 then
randint (move, 1, 4)
if move = 1 then
if not (taken (2)) then
result 2
end if
elsif move = 2 then
if not (taken (4)) then
result 4
end if
elsif move = 3 then
if not (taken (6)) then
result 6
end if
elsif move = 4 then
if not (taken (8)) then
result 8
end if
end if
end if
end for
else
result 5
end if
else
result Block_User
end if
else
result Can_Computer_Win
end if
randint (move, 1, 9)
if not (taken (move)) then
result move
end if

end loop
end Get_Computer_Move
%|2|9|4
%|7|5|3
%|6|1|8
proc Plot_Move (player : string (1), var move : int)
var picID : int
if player = "c" then
if move = 1 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 206, 6, picCopy)

elsif move = 2 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 6, 406, picCopy)

elsif move = 3 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 406, 206, picCopy)

elsif move = 4 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 406, 406, picCopy)

elsif move = 5 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 206, 206, picCopy)

elsif move = 6 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 6, 6, picCopy)

elsif move = 7 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 6, 206, picCopy)

elsif move = 8 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 406, 6, picCopy)

elsif move = 9 then
picID := Pic.FileNew ("H:/o.bmp")
Pic.Draw (picID, 206, 406, picCopy)

end if
else
if move = 1 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 206, 6, picCopy)

elsif move = 2 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 6, 406, picCopy)

elsif move = 3 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 406, 206, picCopy)

elsif move = 4 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 406, 406, picCopy)

elsif move = 5 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 206, 206, picCopy)

elsif move = 6 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 6, 6, picCopy)

elsif move = 7 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 6, 206, picCopy)

elsif move = 8 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 406, 6, picCopy)

elsif move = 9 then
picID := Pic.FileNew ("H:/x.bmp")
Pic.Draw (picID, 206, 406, picCopy)
end if
end if
end Plot_Move


% main program
loop
var trans : int
var again : string (1)
Initialize
var user : string := "u"
var comp : string := "c"
var turn : int
Draw_Screen
for i : 1 .. 5
turn := i
locate (1, 77)
put "It is your turn."
trans := Get_User_Move
u (i) := trans
Plot_Move (user, trans)
if User_Win then
exit
end if
if i = 5 then
cat := cat + 1
locate (36, 90)
put cat
end if
exit when i = 5
locate (1, 77)
put "It is now the computer's turn."
delay (1000)
trans := Get_Computer_Move (i)
c (i) := trans
Plot_Move (comp, trans)
if Computer_Win then
exit
end if
delay (100)
end for
locate (2, 77)
put "would you like to play again?"
locate (3, 77)
getch (again)
if again = "y" then
else
exit
end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: