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: Please Check!
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xXInsanityXx




PostPosted: Tue Oct 04, 2005 10:11 am   Post subject: Tic Tac Toe: Please Check!

Hi,

We had to use Two Dimensional Arrays in class to make a Tic Tac Toe game. I am posting it here please skim through it and give me some suggestions to simplify it if possible. Thank you very much

Im unable to attatch the file i will post the code instead
code:
% xXInsanityXx - abh -
% Mr.Wong
% Monday, October 3, 2005
% Tic Tac Toe - 2 Players

% This program allows 2 users to play the old but famous game of Tic Tac Toe

setscreen ("graphics")
var count : int := 0

%proc for the graphics of the tic tac toe frame and locations
proc drawBox
    %locations start
    locate (24, 5)
    put "(3,1)"
    locate (24, 15)
    put "(3,2)"
    locate (24, 25)
    put "(3,3)"

    locate (19, 5)
    put "(2,1)"
    locate (19, 15)
    put "(2,2)"
    locate (19, 25)
    put "(2,3)"

    locate (14, 5)
    put "(1,1)"
    locate (14, 15)
    put "(1,2)"
    locate (14, 25)
    put "(1,3)"
    %locations end

    % tic tac toe frame starts
    Draw.ThickLine (90, 10, 90, 250, 10, 255)
    Draw.ThickLine (170, 10, 170, 250, 10, 255)
    Draw.ThickLine (10, 90, 250, 90, 10, 255)
    Draw.ThickLine (10, 170, 250, 170, 10, 255)
    % tic tac toe frame ends
end drawBox


var tic : array 1 .. 3, 1 .. 3 of string

var x, y : int := 0

% Proc for the Layout of the Xs' and Os'
proc graphicsForm
    if x = 1 and y = 1 then
        locate (12, 7)
        put tic (x, y)
        if tic (1, 3) = "X" and tic (1, 2) = "X" or tic (1, 3) = "O" and tic (1, 2) = "O" or tic (1, 3) = "X" and tic (1, 2) = "O" or tic (1, 3) = "O" and tic (1, 2) = "X" then
            locate (12, 17)
            put tic (1, 2)
            locate (12, 27)
            put tic (1, 3)
        elsif tic (1, 2) = "X" or tic (1, 2) = "O" then
            locate (12, 17)
            put tic (1, 2)
        elsif tic (1, 3) = "X" or tic (1, 3) = "O" then
            locate (12, 27)
            put tic (1, 3)
        end if
        drawBox
    elsif x = 1 and y = 2 then
        locate (12, 17)
        put tic (x, y)
        if tic (1, 3) = "X" or tic (1, 3) = "O" then
            locate (12, 27)
            put tic (1, 3)
        end if
        drawBox
    elsif x = 1 and y = 3 then
        locate (12, 27)
        put tic (x, y)
        drawBox
    elsif x = 2 and y = 1 then
        locate (17, 7)
        put tic (x, y)
        if tic (2, 3) = "X" and tic (2, 2) = "X" or tic (2, 3) = "O" and tic (2, 2) = "O" or tic (2, 3) = "X" and tic (2, 2) = "O" or tic (2, 3) = "O" and tic (2, 2) = "X" then
            locate (17, 17)
            put tic (2, 2)
            locate (17, 27)
            put tic (2, 3)
        elsif tic (2, 2) = "X" or tic (2, 2) = "O" then
            locate (17, 17)
            put tic (2, 2)
        elsif tic (2, 3) = "X" or tic (2, 3) = "O" then
            locate (17, 27)
            put tic (2, 3)
        end if
        drawBox
    elsif x = 2 and y = 2 then
        locate (17, 17)
        put tic (x, y)
        if tic (2, 3) = "X" or tic (2, 3) = "O" then
            locate (17, 27)
            put tic (2, 3)
        end if
        drawBox
    elsif x = 2 and y = 3 then
        locate (17, 27)
        put tic (x, y)
        drawBox
    elsif x = 3 and y = 1 then
        locate (22, 7)
        put tic (x, y)
        if tic (3, 3) = "X" and tic (3, 2) = "X" or tic (3, 3) = "O" and tic (3, 2) = "O" or tic (3, 3) = "X" and tic (3, 2) = "O" or tic (3, 3) = "O" and tic (3, 2) = "X" then
            locate (22, 17)
            put tic (3, 2)
            locate (22, 27)
            put tic (3, 3)
        elsif tic (3, 2) = "X" or tic (3, 2) = "O" then
            locate (22, 17)
            put tic (3, 2)
        elsif tic (3, 3) = "X" or tic (3, 3) = "O" then
            locate (22, 27)
            put tic (3, 3)
        end if
        drawBox
    elsif x = 3 and y = 2 then
        locate (22, 17)
        put tic (x, y)
        if tic (3, 3) = "X" or tic (3, 3) = "O" then
            locate (22, 27)
            put tic (3, 3)
        end if
        drawBox
    elsif x = 3 and y = 3 then
        locate (22, 27)
        put tic (x, y)
        drawBox
    end if
end graphicsForm

% proc for the winning statement
proc winningPlayer

    %CROSSES
    if tic (1, 1) = tic (2, 2) and tic (2, 2) = tic (3, 3)
            then
        locate (17, 60)
        put tic (1, 1), " WINS"

    elsif tic (1, 3) = tic (2, 2) and tic (2, 2) = tic (3, 1)
            then
        locate (17, 60)
        put tic (1, 3), " WINS"


        %ACROSSES
    elsif tic (1, 1) = tic (1, 2) and tic (1, 2) = tic (1, 3)
            then
        locate (17, 60)
        put tic (1, 1), " WINS"

    elsif tic (2, 1) = tic (2, 2) and tic (2, 2) = tic (2, 3)
            then
        locate (17, 60)
        put tic (2, 1), " WINS"

    elsif tic (3, 1) = tic (3, 2) and tic (3, 2) = tic (3, 3)
            then
        locate (17, 60)
        put tic (3, 1), " WINS"


        %DOWNS
    elsif tic (1, 1) = tic (2, 1) and tic (2, 1) = tic (3, 1)
            then
        locate (17, 60)
        put tic (1, 1), " WINS"

    elsif tic (1, 2) = tic (2, 2) and tic (2, 2) = tic (3, 2)
            then
        locate (17, 60)
        put tic (1, 2), " WINS"

    elsif tic (1, 3) = tic (2, 3) and tic (2, 3) = tic (3, 3)
            then
        locate (17, 60)
        put tic (1, 3), " WINS"

    else
        locate (17, 60)
        put "TIE"
    end if
end winningPlayer

% proc for the errorTrap sentence
proc prevEntered
    var ch : string (1)
    locate (4, 1)
    put ""
    put ""
    put ""
    locate (2, 1)
    put "" : 3, "This location has previously been taken please enter a DIFFERENT location"
    put ""
    put "" : 23, "Please press any key to continue:" ..
    getch (ch)
    locate (2, 1)
    put ""
    put ""
    put ""
end prevEntered

%playerOne Value
proc playOneValue
    tic (x, y) := "X"
    graphicsForm
end playOneValue

%playerTwon Value
proc playTwoValue
    tic (x, y) := "O"
    graphicsForm
end playTwoValue


% playerOne proc
proc playerOne
    locate (1, 1)
    colorback (54)
    put "" : 35, "PLAYER ONE"
    locate (3, 1)
    colorback (0)
    put "Enter the desired location (x,y) format"
    loop
        locate (4, 1)
        put ""
        put ""
        locate (4, 1)
        get x
        exit when x <= 3 and x > 0
    end loop
    loop
        locate (5, 1)
        put ""
        locate (5, 1)
        get y
        exit when y <= 3 and y > 0
    end loop

    % ERROR TRAP FOR LOCATION
    if tic (x, y) = "X" or tic (x, y) = "O" then
        prevEntered
        playerOne
    elsif tic (x, y) not= "X" or tic (x, y) not= "O"
            then
        playOneValue
    end if
end playerOne

%player two proc
proc playerTwo
    locate (1, 1)
    colorback (41)
    put "" : 35, "PLAYER TWO"
    colorback (0)
    locate (3, 1)
    put "Enter the desired location (x,y) format"
    loop
        locate (4, 1)
        put ""
        put ""
        locate (4, 1)
        get x
        exit when x <= 3 and x > 0
    end loop
    loop
        locate (5, 1)
        put ""
        locate (5, 1)
        get y
        exit when y <= 3 and y > 0
    end loop

    % ERROR TRAP FOR LOCATION
    if tic (x, y) = "X" or tic (x, y) = "O" then
        prevEntered
        playerTwo
    elsif tic (x, y) not= "X" or tic (x, y) not= "O"
            then
        playTwoValue
    end if
end playerTwo

% previously assigned tic Values
tic (1, 1) := "D"
tic (1, 2) := "E"
tic (1, 3) := "F"
tic (2, 1) := "G"
tic (2, 2) := "H"
tic (2, 3) := "I"
tic (3, 1) := "J"
tic (3, 2) := "K"
tic (3, 3) := "L"




% mainloop starts
loop
    count += 1
    drawBox
    playerOne
    % loop exiting winning statements for player one starts
    if tic (1, 1) = tic (2, 2) and tic (2, 2) = tic (3, 3)
            then
        exit

    elsif tic (1, 3) = tic (2, 2) and tic (2, 2) = tic (3, 1)
            then
        exit


        %ACROSSES
    elsif tic (1, 1) = tic (1, 2) and tic (1, 2) = tic (1, 3)
            then
        exit

    elsif tic (2, 1) = tic (2, 2) and tic (2, 2) = tic (2, 3)
            then
        exit

    elsif tic (3, 1) = tic (3, 2) and tic (3, 2) = tic (3, 3)
            then
        exit


        %DOWNS
    elsif tic (1, 1) = tic (2, 1) and tic (2, 1) = tic (3, 1)
            then
        exit

    elsif tic (1, 2) = tic (2, 2) and tic (2, 2) = tic (3, 2)
            then
        exit

    elsif tic (1, 3) = tic (2, 3) and tic (2, 3) = tic (3, 3)
            then
        exit
    else
        exit when count = 5
    end if
    % loop exiting winning statements for player one ends

    % loop exiting winning statements for player two starts
    playerTwo
    if tic (1, 1) = tic (2, 2) and tic (2, 2) = tic (3, 3)
            then
        exit

    elsif tic (1, 3) = tic (2, 2) and tic (2, 2) = tic (3, 1)
            then
        exit


        %ACROSSES
    elsif tic (1, 1) = tic (1, 2) and tic (1, 2) = tic (1, 3)
            then
        exit

    elsif tic (2, 1) = tic (2, 2) and tic (2, 2) = tic (2, 3)
            then
        exit

    elsif tic (3, 1) = tic (3, 2) and tic (3, 2) = tic (3, 3)
            then
        exit


        %DOWNS
    elsif tic (1, 1) = tic (2, 1) and tic (2, 1) = tic (3, 1)
            then
        exit

    elsif tic (1, 2) = tic (2, 2) and tic (2, 2) = tic (3, 2)
            then
        exit

    elsif tic (1, 3) = tic (2, 3) and tic (2, 3) = tic (3, 3)
            then
        exit
    end if
    % loop exiting winning statements for player two ends
end loop
% end of main loop

% Final result
locate (17, 60)
winningPlayer

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: