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

Username:   Password: 
 RegisterRegister   
 Connect Four error trap help needed as well as correct looping.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Michael3176




PostPosted: Sun Dec 30, 2012 4:07 pm   Post subject: Connect Four error trap help needed as well as correct looping.

Connect Four error trap help needed as well as correct looping and simplified a bit.


What is the problem you are having?
The game will not reset and start over when it is done, error traps are not working and seems to be over complicated.

Here is the code, please help





Turing:

import GUI
var mainWin := Window.Open ("position:300;300, graphics:660;500")
var board : array 1 .. 8, 1 .. 8 of string (25)
var counterType : array 1 .. 2 of string (5)
var choice : array 1 .. 8 of int
var player, coloumn, counter, winRow, xCord : int
var gameover, ok : boolean

proc setup
    %Sets up the board.
    for x : 1 .. 8
        for y : 1 .. 8
            board (x, y) := "     "
        end for
    end for
    %This loop allows the user to choose a coloumn.
    for j : 1 .. 8
        choice (j) := 8
    end for
    player := 1
    color (red)
    counterType (1) := "  X  "
    color (blue)
    counterType (2) := "  O  "
end setup

procedure CheckWin
    for j : 1 .. 8
        winRow := 0
        for k : 1 .. 8
            if board (j, k) = counterType (player) then
                winRow := winRow + 1
                if winRow = 4 then
                    gameover := true
                end if
            else
                winRow := 0
            end if
        end for
    end for
    for j : 1 .. 8
        winRow := 0
        for k : 1 .. 8
            if board (k, j) = counterType (player) then
                winRow := winRow + 1
                if winRow = 4 then
                    gameover := true
                end if
            else
                winRow := 0
            end if
        end for
    end for
    counter := 0
    winRow := 0
    for d : 1 .. 5
        counter := counter + 1
        xCord := 0
        for j : counter .. 8
            xCord := xCord + 1
            if board (j, xCord) = counterType (player) then
                winRow := winRow + 1
                if winRow = 4 then
                    gameover := true
                end if
            else
                winRow := 0
            end if
        end for
    end for
    winRow := 0
    counter := 9
    for d : 1 .. 5
        counter := counter - 1
        xCord := 9
        for decreasing j : counter .. 1
            xCord := xCord - 1
            if board (j, xCord) = counterType (player) then
                winRow := winRow + 1
                if winRow = 4 then
                    gameover := true
                end if
            else
                winRow := 0
            end if
        end for
    end for
    counter := 0
    winRow := 0
    for d : 1 .. 5
        counter := counter + 1
        xCord := 9
        for j : counter .. 8
            xCord := xCord - 1
            if board (xCord, j) = counterType (player) then
                winRow := winRow + 1
                if winRow = 4 then
                    gameover := true
                end if
            else
                winRow := 0
            end if
        end for
    end for
    winRow := 0
    counter := 9
    for d : 1 .. 5
        counter := counter - 1
        xCord := 0
        for decreasing j : counter .. 1
            xCord := xCord + 1
            if board (j, xCord) = counterType (player) then
                winRow := winRow + 1
                if winRow = 4 then
                    gameover := true
                end if
            else
                winRow := 0
            end if
        end for
    end for
end CheckWin

forward proc processing
forward proc mainMenu
proc title
    cls
    locate (1, 36)
    put "Connect Four"
    put ""
end title

proc pauseProgram
    var reply : string (1)
    put ""
    put "Press any key to continue...." ..
    getch (reply)
end pauseProgram

proc intro
    title
    var menuButton := GUI.CreateButton (295, 50, 0, "Main Menu", mainMenu)
end intro



proc instructions
    title
    put
        "Connect Four is a two player game on which playes take turns dropping tokens into a 7x6 grid falling vertically. Players win by getting four tokens in a row in any straight direction. Click on one of the top cirlces to drop a token."
    var menuButton := GUI.CreateButton (295, 50, 0, "Main Menu", mainMenu)
end instructions

body proc processing
    %Error Traps
    ok := true
    loop
        if coloumn > 8 or coloumn < 1 then
            ok := false
            locate (24, 1)
            put "Player ", player, " please input numbers between 1-8 only."
        elsif ok then
            board (choice (coloumn), coloumn) := counterType (player)
            choice (coloumn) := choice (coloumn) - 1
        else
            ok := false
            locate (24, 1)
            put "Choose a column that has room."
        end if
        exit when ok
    end loop
    %SWITCHES TURNS
    if player = 1 then
        player := 2
    else
        player := 1
    end if
end processing



proc display
    title
    %Draws board
    for j : 1 .. 8
        for k : 1 .. 8
            if k < 8 then
                put board (j, k), "|" ..
            else
                put board (j, k)
            end if
        end for
        if j < 8 then
            put "-----------------------------------------------"
        end if
    end for
    locate (19, 1)
    put "  1     2     3     4     5     6     7     8"
end display
proc userInput
    title
    gameover := false
    display
    loop
        locate (20, 1)
        put "Player ", player, " input a coloumn number: " ..
        get coloumn
        processing
        display
        CheckWin
        exit when gameover
    end loop
    mainMenu
end userInput

proc goodBye
    title
    put "Made by ."
    delay (1300)
    Window.Close (mainWin)
end goodBye

body proc mainMenu
    title
    var intrusctionsButton := GUI.CreateButton (295, 300, 100, "Instructions", instructions)
    var newGameButton := GUI.CreateButton (295, 200, 100, "New Game", userInput)
    var exitB := GUI.CreateButtonFull (295, 100, 100, "Exit", GUI.Quit, 0, KEY_ESC, false)
end mainMenu
intro
setup
loop
    exit when GUI.ProcessEvent
end loop
goodBye


Mod Edit:

Pleas wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code:

[syntax="turing"] ... code ... [/syntax]

[code] ... code ... [/code ]
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sun Dec 30, 2012 7:29 pm   Post subject: RE:Connect Four error trap help needed as well as correct looping.

What is suppose to happen, and what is exactly happening?

To "replay" a section of code. You need to reset everything. Variables must be reset to whatever value they are required to be when the game starts. Another thing you probably overlooked is that you never actually clean up any of the buttons you created (probably because you were never taught to).

Ask yourself what code is called the first time through, that isn't when you "restart" the game.

Click the Main Menu button, the main menu button "disappears" and 3 other buttons are created. The Main Menu button is no longer visible (as you drew over it), but that does not mean it's not there. Try clicking where the button was.

When creating a button (and other objects) you need to remember to destroy them (and free up the memory they take up). You can do this with GUI.Dispose(widgetID : int). There's an example of it's usage in the link.

An alternate way to do what you wish would be to create all the buttons at the start, all of them disabled (Gui.Disable). You'd then enable then when you enter a screen, and disable them when you leave the screen.
Michael3176




PostPosted: Mon Dec 31, 2012 9:59 am   Post subject: Re: Connect Four error trap help needed as well as correct looping.

Yes you are correct about the buttons, but that is not my main concern, what is suppose to happen is that the game would reset whenever new game is pressed, but whenever i press new game it is simply continuing the game. I understand i need to reset variables, but i have no idea which ones to reset. Also my other problem is that the game does not exit to the mainMenu until after the turn of the winner. For ex: lets say player one gets four in a row, it will not exit until player two takes there turn, and vice verse
Insectoid




PostPosted: Mon Dec 31, 2012 11:05 am   Post subject: RE:Connect Four error trap help needed as well as correct looping.

Quote:
I understand i need to reset variables, but i have no idea which ones to reset.


You wrote the game, so you should know which ones need to be reset. It's probably safe to assume you'll have to reset most, if not all of them. You already have a setup procedure. Why not modify it so it resets the game whenever it's called?

Unrelated, but the way your game is organized right now, after playing several games, you're going to have a userinput inside a mainmenu inside a userinput inside a mainmenu, etc. That's not ideal. Your program will end up consuming more and more RAM until there's no RAM left, and it crashes (though I think Turing will crash before that point). Instead of calling mainmenu at the end of userinput, you should keep everything inside mainmenu() in a loop, and just let userinput exit when it finishes. That way, when you finish a game, it quits the procedure and the original mainmenu resumes (currently, you'll get multiple mainmenus and userinputs running inside each other).
Michael3176




PostPosted: Mon Dec 31, 2012 11:22 pm   Post subject: RE:Connect Four error trap help needed as well as correct looping.

Ok thanks, i altered my program and now
I do not know how to make counters stack on top of the other, for example if the user chooses i column twice, i do not know how to recognize that on Turing.
I am trying make my connect four error traps and make it so the user cannot choose a full coloumn.

What is the problem you are having?
My error traps dont work properly and i dont know how to make counters stack on top of the other

Turing:


import GUI
var mainWin := Window.Open ("position:300;300, graphics:660;500")
var coloumn : string
var player : int := 1
var done : boolean
forward proc mainMenu
proc title
    cls
    locate (1, 36)
    put "Connect Four"
    put ""
end title
proc switch
    if player = 1 then
        player := 2
    else
        player := 1
    end if
end switch
proc pauseProgram
    var reply : string (1)
    put ""
    put "Press any key to continue...." ..
    getch (reply)
end pauseProgram

proc intro
    title
    var menuButton := GUI.CreateButton (295, 50, 0, "Main Menu", mainMenu)
end intro


proc instructions
    title
    put
        "Connect Four is a two player game on which playes take turns dropping tokens into a 7x6 grid falling vertically. Players win by getting four tokens in a row in any straight direction. Click on one of the top cirlces to drop a token."
    var menuButton := GUI.CreateButton (295, 50, 0, "Main Menu", mainMenu)
end instructions
proc checkWin
    done := false
    if whatdotcolour (65, 35) = red and whatdotcolour (150, 35) = red and whatdotcolour (240, 35) = red and whatdotcolour (330, 35) = red then
        locate (2, 1)
        put "Player one has won."
        done := true
    elsif
            whatdotcolour (65, 35) = blue and whatdotcolour (150, 35) = blue and whatdotcolour (240, 35) = blue and whatdotcolour (330, 35) = blue then
        locate (2, 1)
        put "Player two has won."
        done := true
    else
        done := false
    end if
end checkWin
proc processing
    if coloumn = "1" then
        if player = 1 then
            drawfilloval (65, 35, 20, 20, red)
            %draw the rest of ovals ontop
        else
            drawfilloval (65, 35, 20, 20, blue)
        end if
    elsif coloumn = "2" then
        if player = 1 then
            drawfilloval (150, 35, 20, 20, red)
            %draw the rest of ovals ontop
        else
            drawfilloval (150, 35, 20, 20, blue)
        end if
    elsif coloumn = "3" then
        if player = 1 then
            drawfilloval (240, 35, 20, 20, red)
        else
            drawfilloval (240, 35, 20, 20, blue)
        end if
    elsif coloumn = "4" then
        if player = 1 then
            drawfilloval (330, 35, 20, 20, red)
        else
            drawfilloval (330, 35, 20, 20, blue)
        end if
    elsif coloumn = "5" then
        if player = 1 then
            drawfilloval (420, 35, 20, 20, red)
        else
            drawfilloval (420, 35, 20, 20, blue)
        end if
    elsif coloumn = "6" then
        if player = 1 then
            drawfilloval (505, 35, 20, 20, red)
        else
            drawfilloval (505, 35, 20, 20, blue)
        end if
    else
        if coloumn = "7" then
            if player = 1 then
                drawfilloval (595, 35, 20, 20, red)
            else
                drawfilloval (595, 35, 20, 20, blue)
            end if
        end if
    end if
    switch
end processing

proc display
    for x : 10 .. 310 by 50
        drawline (20, x, 635, x, black)
    end for
    for x : 20 .. 640 by 88
        drawline (x, 10, x, 310, black)
    end for
end display
proc userInput
    title
    display
    loop
        locate (2, 1)
        put "Please enter a coloumn number: " ..
        get coloumn
        if coloumn not= "1" and coloumn not= "2" and coloumn not= "3" and coloumn not= "4" and coloumn not= "5" and coloumn not= "6" and coloumn not= "7" then
            put "Please enter coloums 1-9 only: " ..
            get coloumn
        else
            processing
            display
            checkWin
        end if
        exit when done = true
    end loop
end userInput

proc goodBye
    title
    put "Made by the."
    delay (1300)
    Window.Close (mainWin)
end goodBye

body proc mainMenu
    title
    var intrusctionsButton := GUI.CreateButton (295, 300, 100, "Instructions", instructions)
    var newGameButton := GUI.CreateButton (295, 200, 100, "New Game", userInput)
    var exitB := GUI.CreateButtonFull (295, 100, 100, "Exit", GUI.Quit, 0, KEY_ESC, false)
end mainMenu

intro
loop
    exit when GUI.ProcessEvent
end loop
goodBye

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  [ 5 Posts ]
Jump to:   


Style:  
Search: