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
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TokenHerbz




PostPosted: Sun Oct 02, 2005 12:05 am   Post subject: Tic Tac Toe

I would like to know how much shorter i can make the code, if its possible...

I think i made it as short as i can, but then again i dont think tic tac toe should be this long... Perhaps you can give me a few pointersr to makeing a great code work short?? I have many CLOSELY related Procs, though a few inside the froc change, so i didn't know how to work around that, please any input for me would be lovly...

Heres the code, try the game Smile

code:

%%Tick Tack Toe
setscreen("Graphics:225;300")

%variables
var playerx: boolean                %%which player it is
var TL, TM, TR: boolean := false    %%top lines
var ML, MM, MR: boolean := false    %%mid lines
var BL, BM, BR: boolean := false    %%bot lines
var random: int                     %%to see who goes 1st
var playgame: boolean := true       %%to play game
var endgame: boolean:= false        %%to end the game
var ans: string

%%This is so i can tell which player owns which tiles...
var mtl, mtm, mtr, mml, mmm, mmr, mbl, mbm, mbr: boolean        %%If True - player 1, false = player 2


%%Picks which player goes 1st
randint(random,1,2)
%%Player 1's 1st
if random = 1 then
    put "Player one goes 1st"
    put "Player one is"," 'X'"
    playerx := true
else
%%Player 2's 1st
    put "Player two goes 1st"
    put "Player two is"," 'O'"
    playerx := false
end if
Input.Pause
cls

proc draw
    %%Draws the TicTacToe lines
    for i: 1 .. 5
        drawline(i+75,0,i+75,225,7)
        drawline(i+150,0,i+150,225,7)
        drawline(0,i+75,225,i+75,7)
        drawline(0,i+150,225,i+150,7)
    end for
end draw

proc TopLeft
    if TL = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(20-i,170,60-i,210,7)
                drawline(20-i,210,60-i,170,7)
            end for
            playerx := false        %%player 2 turn
            mtl:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(38,190,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mtl:= false %%player 2 owns it
        end if
        TL := true
    end if
end TopLeft
proc TopMid
    if TM = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(100-i,170,140-i,210,7)
                drawline(100-i,210,140-i,170,7)
            end for
            playerx := false        %%player 2 turn
            mtm:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(113,190,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mtm:= false %%player 2 owns it
        end if
        TM := true
    end if
end TopMid
proc TopRight
    if TR = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(175-i,170,215-i,210,7)
                drawline(175-i,210,215-i,170,7)
            end for
            playerx := false        %%player 2 turn
            mtr:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(188,190,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mtr:= false %%player 2 owns it
        end if
        TR := true
    end if
end TopRight
proc MidLeft
    if ML = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(20-i,95,60-i,135,7)
                drawline(20-i,135,60-i,95,7)
            end for
            playerx := false        %%player 2 turn
            mml:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(38,115,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mml:= false %%player 2 owns it
        end if
        ML := true
    end if
end MidLeft
proc MidMid
    if MM = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(100-i,95,140-i,135,7)
                drawline(100-i,135,140-i,95,7)
            end for
            playerx := false        %%player 2 turn
            mmm:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(113,115,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mmm:= false %%player 2 owns it
        end if
        MM := true
    end if
end MidMid
proc MidRight
    if MR = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(175-i,95,215-i,135,7)
                drawline(175-i,135,215-i,95,7)
            end for
            playerx := false        %%player 2 turn
            mmr:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(188,115,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mmr:= false %%player 2 owns it
        end if
        MR := true
    end if
end MidRight
proc BotLeft
    if BL = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(20-i,20,60-i,60,7)
                drawline(20-i,60,60-i,20,7)
            end for
            playerx := false        %%player 2 turn
            mbl:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(38,38,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mbl:= false %%player 2 owns it
        end if
        BL := true
    end if
end BotLeft
proc BotMid
    if BM = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(100-i,20,140-i,60,7)
                drawline(100-i,60,140-i,20,7)
            end for
            playerx := false        %%player 2 turn
            mbm:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(113,38,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mbm:= false %%player 2 owns it
        end if
        BM := true
    end if
end BotMid
proc BotRight
    if BR = false then             %%hasn't been pict yet
        if playerx = true then      %%1st player
            for i: 1 .. 10          %%to fatten the X up
                drawline(175-i,20,215-i,60,7)
                drawline(175-i,60,215-i,20,7)
            end for
            playerx := false        %%player 2 turn
            mbr:= true  %%player 1 owns it
        else            %%its player 2
            for i: 1 .. 10          %%fatten up the circle
                drawoval(188,38,i+10,i+10,7)
            end for
            playerx := true     %%now player 1 turn
            mbr:= false %%player 2 owns it
        end if
        BR := true
    end if
end BotRight

proc win
    % % % H O R R I Z O N T A L - W I N S % % %
    %%player 1 gets top 3 tiles
    if TL = true and mtl = true and TM = true and mtm = true and TR = true and mtr = true then
        endgame := true        %%to end game
        for i: 1 .. 10      %%draws a line threw the victory
            drawline(0,184+i,225,184+i,7)
        end for
        put "P L A Y E R    1    W O N !"       
    %%player 2 gets top 3 tiles
    elsif TL = true and mtl = false and TM = true and mtm = false and TR = true and mtr = false then
        endgame := true        %%to end game
        for i: 1 .. 10      %%draws a line threw the victory
            drawline(0,184+i,225,184+i,7)
        end for
        put "P L A Y E R    2    W O N !"
    %%player 1 gets mid 3 tiles
    elsif ML = true and mml = true and MM = true and mmm = true and MR = true and mmr = true then
        endgame := true        %%to end game
        for i: 1 .. 10      %%draws a line threw the victory
            drawline(0,110+i,225,110+i,7)
        end for
        put "P L A Y E R    1    W O N !"       
    %%player 1 gets middle 3 tiles
    elsif ML = true and mml = false and MM = true and mmm = false and MR = true and mmr = false then
        endgame := true        %%to end game
        for i: 1 .. 10      %%draws a line threw the vctory
            drawline(0,110+i,225,110+i,7)
        end for
        put "P L A Y E R    2    W O N !"
     %%player 1 gets bot 3 tiles
    elsif BL = true and mbl = true and BM = true and mbm = true and BR = true and mbr = true then
        endgame := true        %%to end game
        for i: 1 .. 10      %%draws a line threw the victory
            drawline(0,34+i,225,34+i,7)
        end for
        put "P L A Y E R    1    W O N !"       
    %%player 1 gets bot 3 tiles
    elsif BL = true and mbl = false and BM = true and mbm = false and BR = true and mbr = false then
        endgame := true        %%to end game
        for i: 1 .. 10      %%draws a line threw the vctory
            drawline(0,34+i,225,34+i,7)
        end for
        put "P L A Y E R    2    W O N !"
       
    % % % V E R T I C L E - W I N S % % %
    %%player 1 gets left row
    elsif TL = true and mtl = true and ML = true and mml = true and BL = true and mbl = true then
        endgame := true
        for i: 1 .. 10      %%Fatten the line
            drawline(34+i,0,34+i,225,7)
        end for
        put "P L A Y E R    1    W O N !"
    %%player 2 gets left row
    elsif TL = true and mtl = false and ML = true and mml = false and BL = true and mbl = false then
        endgame := true
        for i: 1 .. 10      %%Fatten the line
            drawline(34+i,0,34+i,225,7)
        end for
        put "P L A Y E R    2    W O N !"
    %%player 1 gets mid row
    elsif TM = true and mtm = true and MM = true and mmm = true and BM = true and mbm = true then
        endgame := true
        for i: 1 .. 10      %%Fatten the line
            drawline(110+i,0,110+i,225,7)
        end for
        put "P L A Y E R    1    W O N !"
    %%player 2 gets mid row
    elsif TM = true and mtm = false and MM = true and mmm = false and BM = true and mbm = false then
        endgame := true
        for i: 1 .. 10      %%Fatten the line
            drawline(110+i,0,110+i,225,7)
        end for
        put "P L A Y E R    2    W O N !"
    %%player 1 gets right row
    elsif TR = true and mtr = true and MR = true and mmr = true and BR = true and mbr = true then
        endgame := true
        for i: 1 .. 10      %%Fatten the line
            drawline(184+i,0,184+i,225,7)
        end for
        put "P L A Y E R    1    W O N !"
    %%player 2 gets eight row
    elsif TR = true and mtr = false and MR = true and mmr = false and BR = true and mbr = false then
        endgame := true
        for i: 1 .. 10      %%Fatten the line
            drawline(184+i,0,184+i,225,7)
        end for
        put "P L A Y E R    2    W O N !"
       
    % % % S L A N T E D - W I N S % % %
    %%player 1 wins with top left, to bottom right cross win - \
    elsif TL = true and mtl = true and MM = true and mmm = true and BR = true and mbr = true then
        endgame := true
        for i: 1 .. 10 %%fatten up line
            drawline(0+i,225,225+i,0,7)
        end for
        put "P L A Y E R    1    W O N !"
    %%player 2 wins with TL - BR slash - \
    elsif TL = true and mtl = false and MM = true and mmm = false and BR = true and mbr = false then
        endgame := true
        for i: 1 .. 10 %%fatten up line
            drawline(0+i,225,225+i,0,7)
        end for
        put "P L A Y E R    2    W O N !"
    %%player 1 wins with bot left, to top right cross win - /
    elsif TR = true and mtr = true and MM = true and mmm = true and BL = true and mbl = true then
        endgame := true
        for i: 1 .. 10 %%fatten up line
            drawline(0+i,0,225+i,225,7)
        end for
        put "P L A Y E R    1    W O N !"
    %%player 2 wins with TR - BL slash - /
    elsif TR = true and mtr = false and MM = true and mmm = false and BL = true and mbl = false then
        endgame := true
        for i: 1 .. 10 %%fatten up line
            drawline(0+i,0,225+i,225,7)
        end for
        put "P L A Y E R    2    W O N !" 
   
   % % % C A T S - E Y E % % %
   elsif TL = true and TM = true and TR = true and ML = true and MM = true and MR = true and BL = true and BM = true and BR = true then
        endgame := true
        for i: 1 .. 20
            drawoval(125,125,75+i,75+i,7)
            drawline(50+i,50,175+i,175,7)
        end for
        put "    C A T S       E Y E !"
   end if
end win

proc game
    if playerx = true then
        locate(1,1)
        put "It is player One's turn, 'X'"
    elsif playerx = false then
        locate(1,1)
        put "It is player Two's turn, 'O'"
    end if
    var mx,my,mb: int       %%Mouse chords
    Mouse.Where(mx,my,mb)
   
    if mb = 1 then      %%if mouse is clicked
        if mx <= 75 and mx >= 0 and my <= 225 and my >= 175 then  %%Location for top left tile
            TopLeft
        elsif mx <= 150 and mx >= 75 and my <= 225 and my >= 175 then   %%location for top mid tile
            TopMid
        elsif mx <= 225 and mx >= 150 and my <= 225 and my >= 175 then   %%top right location
            TopRight
        elsif mx <= 75 and mx >= 0 and my <= 150 and my >= 75 then  %%Midleft tile location
            MidLeft
        elsif mx <= 150 and mx >= 75 and my <= 150 and my >= 75 then    %%midmid tile loc
            MidMid
        elsif mx <= 225 and mx >= 150 and my <= 150 and my >= 75 then   %%mid right loc
            MidRight
        elsif mx <= 75 and mx >= 0 and my <= 75 and my >= 0 then    %%Bot left tile
            BotLeft
        elsif mx <= 150 and mx >= 75 and my <= 75 and my >= 0 then      %%Mot mid tile
            BotMid
        elsif mx <= 225 and mx >= 150 and my <= 75 and my >= 0 then     %%Bot Right tile loc
            BotRight
        end if
    end if
    draw
    win
end game

%%main loop
loop
    if playgame = true then
        game
    end if
    if endgame = true then
        Input.Pause
        cls
        put "Made By: The Legit Derek!!"
        put ""
        put ""
        put ""
        put ""
        put "Please Tell me errors, etc:"
        put "How to make this better?"
        put "And rate it 1 - 10! :)"
        put "AI COMP SOON TO COME!"
        put ""
        put "       E N J O Y !"
        put ""
        put ""
        put ""
        put "Would you like to play again"
        put "'Y'/'y'   OR   'N'/'n'"
        get ans
        if ans = 'Y' or ans = 'y' then
            playgame:= true
            endgame := false
            TL:= false
            TM:= false
            TR:= false
            ML:= false
            MM:= false
            MR:= false
            BL:= false
            BM:= false
            BR:= false
            cls
        else
            exit
        end if
    end if
end loop
cls
put "Thanks For Playing"
put ""
put "       BYE"
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: