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 WIN TELL ME!)
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Keerigan




PostPosted: Tue Jun 21, 2005 10:51 am   Post subject: Tic-Tac-Toe(UNBEATABLE COMPUTER IF YOU WIN TELL ME!)

Arrow For Images use your own pictures

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


Martin says: I just added [code] tags. Nothing to see here. Move along.
Sponsor
Sponsor
Sponsor
sponsor
Spitfyre




PostPosted: Thu Jan 05, 2006 12:32 pm   Post subject: (No subject)

how the hell do i play
DIIST




PostPosted: Thu Jan 05, 2006 2:28 pm   Post subject: (No subject)

U sure made my life hard not providing the pictures Laughing .

PS: I won Very Happy and i captured the moment via PrintScreen. I ll PM you the image! Cool
md




PostPosted: Thu Jan 05, 2006 6:38 pm   Post subject: (No subject)

1. USE CODE TAGS! Please! It makes it soooo much easier for the rest of us!
2. Tick-tack-toe is a fairly simple game to make unbeatable, though it is indeed cool... now only if it were in a language I could run...
Spitfyre




PostPosted: Thu Jan 05, 2006 11:08 pm   Post subject: (No subject)

i still dont know how to play after all i am just newbcake
MysticVegeta




PostPosted: Sat Jan 07, 2006 12:04 pm   Post subject: (No subject)

Cornflake wrote:
1. USE CODE TAGS! Please! It makes it soooo much easier for the rest of us!
2. Tick-tack-toe is a fairly simple game to make unbeatable, though it is indeed cool... now only if it were in a language I could run...


3. Provide Images
4. 1 simple implementation of minMax algorithm could make the computer unbeatable.
Spitfyre




PostPosted: Sat Jan 07, 2006 4:35 pm   Post subject: (No subject)

Whateva!!
sylvester-27




PostPosted: Wed Jan 11, 2006 10:44 am   Post subject: (No subject)

yo the program doesn't work. it doesn't recognize the mouse click is there suppossed to be a special button u press or arrow keys or something??

nvm i just figured out that i have to make my own pics. im kinda slow that way. but still if you went through all the effort to provide such a long and tedious code, you could atleast provide the pictures
Sponsor
Sponsor
Sponsor
sponsor
theguru




PostPosted: Thu Jan 12, 2006 5:48 pm   Post subject: (No subject)

Quote:

4. 1 simple implementation of minMax algorithm could make the computer unbeatable.


lol, is that really possible... in my experiences of tictacto you can always stop the other person if you play it smart and do the 'correct' moves, which depend on the current scenario. i'd like to see an unbeatable computer. then i can learn from it and become unbeatable <insert evil laugh here> Smile
chrispminis




PostPosted: Thu Jan 12, 2006 6:44 pm   Post subject: (No subject)

By Unbeatable i think he means, that it won't lose, it will win if your carelss but it won't lose, which isn't hard to do.
Clayton




PostPosted: Sat Jan 14, 2006 9:18 pm   Post subject: (No subject)

chrispminis wrote:
By Unbeatable i think he means, that it won't lose, it will win if your carelss but it won't lose, which isn't hard to do.


i think that that is true because i couldnt beat it but i ended up in a cats game quite a few times
MysticVegeta




PostPosted: Sun Jan 15, 2006 4:06 pm   Post subject: (No subject)

What I meant was if it couldn't beat you, it wont lose to you, it will draw it, but there will never be a scenario where you would win. Since minMax algorithm will sort out every single possible way the game could end and will choose the one where its winning. Since there are less possible combination (for a computer) to perform, it should have no difficulty in sorting it out.

Example:

...
.X.
...
| \ \
0.. .0. ..0 .... and so on...
.X. .X. .X.
... ... ...
|
0X.
.X.
...

and so on. The tree will perform a depth search and choose the path where the computer will win. If no such scenario exists since the player made excellent moves, computer will draw it (notice how I didnt say "try to draw it").
Rasta Fella




PostPosted: Sun Jan 15, 2006 5:33 pm   Post subject: (No subject)

Well, saying it is "UNBEATABLE" is true but all that happens to me is a "tie game". Making it unbeatable is simple..but can you make it so the computer also tries to go for wins sometimes???....I doubt possible..to many rand:int and stuff.
cool dude




PostPosted: Sun Jan 15, 2006 7:21 pm   Post subject: (No subject)

Rasta Fella wrote:
Well, saying it is "UNBEATABLE" is true but all that happens to me is a "tie game". Making it unbeatable is simple..but can you make it so the computer also tries to go for wins sometimes???....I doubt possible..to many rand:int and stuff.


r u joking u use no randint at all!!! u use algoritithms which will calculate the best move to make for the computer. and its actually not hard at all to make the computer to try to go for wins. if it wasn't for exams coming up i might've made a really good tic tac toe to show u that u can make the computer go for wins, and impossible to beat it just to draw it or lose to the computer.
iker




PostPosted: Sun Jan 15, 2006 8:21 pm   Post subject: (No subject)

I played this game when it was first posted and didn't bother replying because I couldn't beat it. But, what if the computer took the first move instead of the 'user'? I would like to see the computer beat me...
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 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: