Computer Science Canada

Turing Connect Four Stacking Problem

Author:  Michael3176 [ Mon Dec 31, 2012 11:20 pm ]
Post subject:  Turing Connect Four Stacking Problem

What is it you are trying to achieve?
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

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

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


Author:  Panphobia [ Mon Dec 31, 2012 11:38 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Make an array of integers, and add one to each column until it is full, and then if counter[a] > whatever then give him a message to choose an empty column, or just dont process anything if it is over, and just return to the current persons turn

Author:  Insectoid [ Tue Jan 01, 2013 12:08 am ]
Post subject:  RE:Turing Connect Four Stacking Problem

I suggest you create a 2D array of int to represent the game board. When coordinate x, y is selected, you set that position in the array to 1 or 2 depending on the player that selected it. If a player tries to select a position that is anything but 0, force him to pick a new spot.

Author:  Raknarg [ Tue Jan 01, 2013 11:57 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

http://compsci.ca/v3/viewtopic.php?t=14333

Author:  Michael3176 [ Wed Jan 02, 2013 10:53 am ]
Post subject:  RE:Turing Connect Four Stacking Problem

Could you give me a code example, Im new to turing and dont quite understand, can arrays use (x,y)coordinates?

Author:  Panphobia [ Wed Jan 02, 2013 1:16 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Arrays don't specifically store just xy coords, but if you make a 2D array of integers in each slot you can store an x or a y coord, like I would make a 2D array of how many slots there are in connect four in one column times 2 so like counter(6)(0)=x counter(6)(1)=y

Author:  Raknarg [ Wed Jan 02, 2013 9:41 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

Ok. Lets say I wanted to make a map of 25 tiles. I could do something like this:

Turing:

var map : array 1 .. 5, 1 .. 5 of int


Lets assume that all the values are at zero (they're not, we're assuming we already set their values). Now if we draw the map like a coordinated plane, we would end up with something like this:

code:

(y)

 5 | 0 0 0 0 0
 4 | 0 0 0 0 0
 3 | 0 0 0 0 0
 2 | 0 0 0 0 0
 1 | 0 0 0 0 0
     1 2 3 4 5 (x)


cool. Now lets make this a little more relatable. Lets say this is connect four, and I dropped a black token in the centre. Lets say that on this map, 0 is empty, 1 is black and 2 is red. We now have this:

code:

(y)

 5 | 0 0 0 0 0
 4 | 0 0 0 0 0
 3 | 0 0 0 0 0
 2 | 0 0 0 0 0
 1 | 0 0 1 0 0
     1 2 3 4 5 (x)


The next turn, red wants to drop it directly to the left. Now we have this:

code:

(y)

 5 | 0 0 0 0 0
 4 | 0 0 0 0 0
 3 | 0 0 0 0 0
 2 | 0 0 0 0 0
 1 | 0 2 1 0 0
     1 2 3 4 5 (x)


Looks good. Now here comes the part you're having trouble with. Lets say black wants to put another token in the middle. It wont simply drop to the bottom. It's got to land on top of the old black piece. But how will the problem know?

It's easy!

5 | 0 0 0 0 0

Looks like there's nothing on this line. Maybe there's another space down.

4 | 0 0 0 0 0

Nope. Lets keep going.

3 | 0 0 0 0 0

Nope.

2 | 0 0 0 0 0

Nope again.

1 | 0 2 1 0 0

Look now. The center space is taken up. Therefore, the next piece would land on top of it.

Now taking that information into account, we have this:

code:

(y)

 5 | 0 0 0 0 0
 4 | 0 0 0 0 0
 3 | 0 0 0 0 0
 2 | 0 0 1 0 0
 1 | 0 2 1 0 0
     1 2 3 4 5 (x)


Hope that makes more sense now.

Author:  Michael3176 [ Thu Jan 03, 2013 11:03 am ]
Post subject:  RE:Turing Connect Four Stacking Problem

so how would i set up a 2d array of the board using code? Then i could indicate everytime the user would choose a coloumn, indicate that the coodinate was chosen using an array right, and if that coordinate was chosen, if they choose that coloumn again, place it ontop, but how would i now recognize when they have chosen a coloumn twice, would i make a "tries" variable and keep adding one, and if it was two, then locate above it?

Author:  Raknarg [ Thu Jan 03, 2013 11:22 am ]
Post subject:  RE:Turing Connect Four Stacking Problem

You set up the array basically how I just did, however many slots you want as x, and however many slots as y. Basically the player is just choosing which x coordinate they want to put their token in.

So all you have to do is use a for loop to search through the array. At anytime if the slot at the x of the players choice is not zero, then you know you need to place a token above it. If it's the top of the row, then they need to make another choice.

Author:  Michael3176 [ Thu Jan 03, 2013 4:11 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

ok so i did a setup proc like this

procedure setup
for x : 1 .. 7
for y : 1 .. 6
board (x, y) := 0
end for
end for
end setup

and in my processing i did

if coloumn = "1" then
if player = 1 then
drawfilloval (65, 35, 20, 20, red)
board (1, 1) := 1
else
drawfilloval (65, 35, 20, 20, blue)
board (1, 1) := 2
end if
now im stuck i dont no what to write to make them stack

Author:  Raknarg [ Thu Jan 03, 2013 5:36 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

First o all, do all your drawing outside the loop. You usually want to have them all together, so that your main program flows like this:

code:

loop
     input ()
     processing ()
     output/drawing ()
end loop


It would be easier if you rewrote it, but we dont necessarily have to do that...

so now when you get to this part, you have an x coordinate, but you need to find your y coordinate. We can do exactly what we talked about.

so in each statement, you have a for loop to search the column.

Turing:

if coloumn = "1" then
     y := search_column()
          if player = 1 then
          drawfilloval (65, 35, 20, 20, red)
          board (1, y) := 1
     else
          drawfilloval (65, 35, 20, 20, blue)
          board (1, y) := 2
          end if


Now you might be thinking there should be an easier way to do this, and you would be correct. This is set up poorly and could be done much easier.

Author:  Raknarg [ Thu Jan 03, 2013 6:25 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Or another neat way is using another array to keep track of the next slot open in each column. So like an array 1 .. 7, and all the elements start out at one. Each selection, you set the y coordinate of the token the open slot number of that array.

so

map (x, slotOpen (x)) := player
slotOpen (x) += 1

Something like that

Author:  Michael3176 [ Thu Jan 03, 2013 11:10 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

So in the first idea you had, what would the for loop look like in code that your talking about, and I don't quite understand how the second one would work

Author:  jr5000pwp [ Fri Jan 04, 2013 3:54 am ]
Post subject:  Re: Turing Connect Four Stacking Problem

So I wanted to make an example to show you a similar implementation of a 2D array, but I might have went slightly overboard.
This is a column based basin filler. You can set the rows and columns, as well as the maximum amount of water per cell. It fills by randomly picking columns and filling a random amount from 0 to MaxLevel*2, that's why it slows down near the end.
Hopefully you can learn something from this.
Turing:
%Only updates the screen when View.Update is called to provide smooth animation
setscreen ("offscreenonly, nocursor")
%Number of columns, beware of odd output from too many
const numColumns : int := 20
%Number of rows, beware of odd output from too many
const numRows : int := 24
%The maximum level a cell can reach
const maxLevel : real := 99
%A 16x16 array of real numbers representing the water levels of different cells
var basin : array 1 .. numColumns, 1 .. numRows of real
%The array representing if a column is full
var columnFull : array 1 .. numColumns of boolean
%The amount of digits in the maxLevel, used for formatting
var numDigits : int := 1

%This procedure initializes the basin array
procedure initArray
    %Loop through all the columns
    for x : 1 .. numColumns
        %Set the column to not-full
        columnFull (x) := false
        %Loop through all the rows
        for y : 1 .. numRows
            %Initialize the cell to 0
            basin (x, y) := 0.0
        end for
    end for
end initArray

%Determines the number of digits in the maxLevel for formatting
procedure setNumDigits
    %Starts off with numbers with 1 digit (Less than 10)
    var digitsCounter : int := 1
    var digits : int := 10
    %Loop until you find the number of digits
    loop
        %If the maxlevel is less than the current digits counter then we've found the number of digits
        if maxLevel < digits then
            numDigits := digitsCounter
            exit
        end if
        %Multiplying by 10 will add a 0 on to the end of the number
        digits *= 10
        %Increment the number of digits
        digitsCounter += 1
    end loop
end setNumDigits

%This function checks if all cells have been filled.
function isFull : boolean
    %Loop through all columns
    for x : 1 .. numColumns
        %If the column isn't full, then the basin isn't full
        if not columnFull (x) then
            result false
        end if
    end for
    %If no columns are not full, then they are all full and the basin is full
    result true
end isFull

%This function takes an amount and a column and attempts to add that amount to the column, it returns the amoun that couldn't be added.
function addWater (amount : real, x : int) : real
    %If the column is already full then just result the amount input
    if columnFull (x) then
        result amount
    else
        %The variable representing the remaining fluid, used to partially fill cells
        var amountToAdd : real := amount
        %Loop through each row of the column
        for i : 1 .. numRows
            %If there is still room left in the cell
            if basin (x, i) not= maxLevel then
                %Determine how much we can add to the cell, if there is enough room for all of the water, do so, otherwise fill the cell to the max
                var amountIncrement : real := min (amountToAdd, maxLevel - basin (x, i))
                %Add the amount to the cell
                basin (x, i) += amountIncrement
                %Remove the amount from the remaining fluid
                amountToAdd -= amountIncrement
                %Once you have used all the remaining fluid, there is no point to continue on the column, so exit
                exit when amountToAdd = 0.0
            end if
        end for
        %If there is still water remaining this column is now identified as full
        if amountToAdd not= 0.0 then
            columnFull (x) := true
        end if
        %Result the remaining amount of water
        result amountToAdd
    end if
end addWater

%This procedure draws the basin in text
procedure drawBasin
    %Loop through all columns
    for x : 1 .. numColumns
        %Loop through all rows
        for y : 1 .. numRows
            %Set the cursor position to be of this tile
            locate (y, (x - 1) * (2 + numDigits) + 1)
            %Print the amount in the basin rounded to the nearest 10th. There's probably a better way to do this
            put round (basin (x, numRows - y + 1) * 10) / 10
        end for
    end for
end drawBasin

%This is the main procedure, it will gradually fill the basin
procedure fillBasin
    %Loop until the basin is full
    loop
        %Choose a random column
        var x : int := Rand.Int (1, numColumns)
        %Call the function to add the water and store the remaining water in a variable in case we want to use it later
        var amountResulted : real := addWater (Rand.Real * maxLevel * 2, x)
        %Draw the basin
        drawBasin
        %Update the screen
        View.Update
        %When the basin is full, stop running the program
        exit when isFull
        %Clear the screen
        cls
    end loop
end fillBasin

%Initialize the arrays first to avoid errors
initArray
%Determine the number of digits used for formatting
setNumDigits
%Fill the basin
fillBasin

Author:  Raknarg [ Fri Jan 04, 2013 11:23 am ]
Post subject:  RE:Turing Connect Four Stacking Problem

Turing:

for decreasing i : 6 . . 0
   if i = 0 then
      slots (choicex, 1) = player
      valid_move = true
      exit
   if slots (choicex, i) not= 0 then
      if i =  6 then
         valid_move = false
         exit
      else
         slots (choicex, i + 1) = player
         valid_move = true
         exit
      end if
   end if
end for


This is the first idea. You go through the column. If the slot is empty, move on. If it reaches a filled slot, and its the top slot, no moves are possible. Otherwise, set the token to the slot above. If you reach 0 and it's empty, put it in the first slot.

Author:  Michael3176 [ Fri Jan 04, 2013 9:45 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Ok I'll try it out and hopefully it will help me, thanks ill get back to you

Author:  Michael3176 [ Sat Jan 05, 2013 3:11 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

ok so do i make a function called searchColoumn,that includes the for loop you gave me, and then use that in every if statement in processing

Author:  Michael3176 [ Sat Jan 05, 2013 3:13 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

this would be my function,
Turing:

function searchColoumn (y : int) : int
    for decreasing i : 6 .. 0
        if i = 0 then
            board (coloumn, 1) := player
            validMove := true
            exit
        elsif board (coloumn, i) not= 0 then
            if i = 6 then
                validMove := false
                exit
            else
                board (coloumn, i + 1) := player
                validMove := true
                exit
            end if
        end if
    end for
end searchColoumn


Mod Edit:

Please 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 ]

Author:  Michael3176 [ Sat Jan 05, 2013 3:15 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

and this is a bit of proccessing but its not working

Turing:

proc processing
    setup
    y := searchColoumn
    if coloumn = 1 then
        if player = 1 then
            drawfilloval (65, 35, 20, 20, red)
            board (1, y) := 1
        else
            drawfilloval (65, 35, 20, 20, blue)
            board (1, y) := 2
        end if
    elsif coloumn = 2 then

Author:  GuCCIP [ Sat Jan 05, 2013 3:42 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem


Author:  Michael3176 [ Sat Jan 05, 2013 4:39 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

I made a procedure like this

proc searchColoumn
setup
for decreasing i : 6 .. 0
if i = 0 then
board (coloumn, 1) := player
validMove := true
exit
elsif board (coloumn, i) not= 0 then
if i = 6 then
validMove := false
exit
else
board (coloumn, i + 1) := player
validMove := true
exit
end if
end if
end for
end searchColoumn

and in processing did this

proc processing
if coloumn = 1 then
if player = 1 then
searchColoumn
drawfilloval (65, 35, 20, 20, red)

but how would i make an oval be drawn ontop now? I tried doing if coloumn = 1 then
if player = 1 then
searchColoumn
display, and in display i wrote if board (1,1) then drawoval but it did not work. it said if must be boolean type

Author:  Raknarg [ Sat Jan 05, 2013 6:19 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

You need to draw your ovals differently. You're doing it the hard way atm. Think about it, you already have a map that tells you where everything is. just make it bigger, then draw it on the screen.

for instance, look at this program.

Turing:

var map : array 0 .. 5, 0 .. 5 of int

for i : 0 .. 5
    for j : 0 .. 5
        map (i, j) := Rand.Int (0, 255)
        Draw.FillBox (i * 25, j * 25, i * 25 + 25, j * 25 + 25, map (i, j))
    end for
end for


This is the thing you want to do. But instead of map (i, j), you want to draw it red or black according to the player who placed it. Do you get what I'm saying?

Author:  Michael3176 [ Sat Jan 05, 2013 8:02 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

ok if i drew it like this
for i : 1 .. 7
for j : 1 .. 6
Draw.Oval (i * 50, j * 50, 20, 20, 1)
end for
end for

it would draw all the ovals at once? so how would the game work

Author:  Insectoid [ Sat Jan 05, 2013 8:07 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Only draw the ones you need. Look in each spot in your array, and if there's a chip in that spot, draw it.

Author:  Michael3176 [ Sat Jan 05, 2013 9:18 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

ok i got something like this now

proc searchColoumn
setup
for i : 6 .. 0
if i = 0 then
board (coloumn, 1) := player
validMove := true
exit
elsif board (coloumn, i) not= 0 then
if i = 6 then
validMove := false
exit
else
board (coloumn, i + 1) := player
validMove := true
exit
end if
end if
Draw.FillOval ((coloumn) * 50, i * 50, 20, 20, red)
end for
end searchColoumn

the stacking problem is still there, and what ever coloumn i enter, the whole row is filled up

Author:  Raknarg [ Sat Jan 05, 2013 9:43 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Except you dont need to draw it there either. Like you can, it's just better orgranized if all the drawing is done together and at once

Author:  Michael3176 [ Sat Jan 05, 2013 9:46 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

so how and where would i draw it? and do i use both variables "i" and "column" when drawing ?

Author:  Raknarg [ Sat Jan 05, 2013 9:48 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

in a sense. You can have a procedure for drawing if you want. It's basically just what I put in that program before, with some minor differences

Author:  Michael3176 [ Sat Jan 05, 2013 9:52 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

ok but i dont understand why its not drawing where i put it to, because its giving the coloumn, and you said the for statement "for i : 6 .. 0
if i = 0 then
board (coloumn, 1) := player
validMove := true
exit
elsif board (coloumn, i) not= 0 then
if i = 6 then
validMove := false
exit
else
board (coloumn, i + 1) := player
validMove := true
exit
end if
end if
end for"
would make the height stack, correct? but maybe that for statement is wrong?

Author:  jr5000pwp [ Sat Jan 05, 2013 9:52 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

Can you please, please, please use the
code:
[syntax="turing"][/syntax]
formatiing tags? It would make it a lot easier for us to read and help you with.

As for your filling problem, think about what your doing with your searchColumn function, why do you have it, what is it's purpose, and most of all, what line doesn't belong? Hint: You aren't actually filling the column atm.

Author:  Michael3176 [ Sat Jan 05, 2013 10:07 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Ok i will next time sorry, and Well I think the proc is there to trap anything full columns and allow me to place a token where there is open space, but I don't know identify when a column was chosen twice or 3 times, and as to which doesn't belong , I guess it would be the draw command considering you said I'm not filling it it in here ?

Author:  Raknarg [ Sat Jan 05, 2013 10:25 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Sorry, you mind clarifying your question?

Author:  Michael3176 [ Sat Jan 05, 2013 10:33 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Im not sure how to get my program to now draw the counters when the user chooses a coloumn, and have them stack one upon the other using the
searchColumn procedure, so i am wondering what code would i now put in my display procedure to make the counters display stacked upon eachother like connect four would run.

Author:  Raknarg [ Sat Jan 05, 2013 10:35 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

The same way you were trying to do it before, thats the correct way, just instead of the searching procedure put it in the display prodecure

Author:  Michael3176 [ Sat Jan 05, 2013 10:45 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

when i do that it says, "i" has not been declared, and also, it does not display properly, the counters do not stack one at a time, all the counters will stack up to 6 in the column chosen, and i do not know why, i can post the code of the game again if you are confused?

Author:  Raknarg [ Sat Jan 05, 2013 10:46 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

sure, lets see what you have now

Author:  Michael3176 [ Sat Jan 05, 2013 10:51 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

code:
Turing:


import GUI
var mainWin := Window.Open ("position:300;300, graphics:660;500")
var coloumn : int
var player : int := 1
var done, validMove : boolean
var board : array 1 .. 7, 1 .. 6 of int
forward proc mainMenu

procedure setup
    for x : 1 .. 7
        for y : 1 .. 6
            board (x, y) := 0
        end for
    end for
end setup

proc searchColoumn
    setup
    for decreasing i : 6 .. 0
        if i = 0 then
            board (coloumn, 1) := player
            validMove := true
            exit
        elsif board (coloumn, i) not= 0 then
            if i = 6 then
                validMove := false
                exit
            else
                board (coloumn, i + 1) := player

                validMove := true

                exit
            end if
        end if
if player = 1 then
            Draw.FillOval ((coloumn + 2) * 50, i * 50, 20, 20, red)
        else
            Draw.FillOval ((coloumn + 2) * 50, i * 50, 20, 20, blue)
        end if
    end for
end searchColoumn

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
    elsif whatdotcolour (150, 35) = red and whatdotcolour (240, 35) = red and whatdotcolour (330, 35) = red and whatdotcolour (420, 35) = red then
        locate (2, 1)
        put "Player one has won."
        done := true
    elsif whatdotcolour (150, 35) = blue and whatdotcolour (240, 35) = blue and whatdotcolour (330, 35) = blue and whatdotcolour (420, 35) = blue then
        locate (2, 1)
        put "Player two has won."
        done := true
    elsif whatdotcolour (240, 35) = red and whatdotcolour (330, 35) = red and whatdotcolour (420, 35) = red and whatdotcolour (505, 35) = red then
        locate (2, 1)
        put "Player one has won."
        done := true
    elsif whatdotcolour (240, 35) = red and whatdotcolour (330, 35) = red and whatdotcolour (420, 35) = red and whatdotcolour (505, 35) = red then
        locate (2, 1)
        put "Player two has won."
        done := true
    elsif whatdotcolour (330, 35) = red and whatdotcolour (420, 35) = red and whatdotcolour (505, 35) = red and whatdotcolour (595, 35) = red then
        locate (2, 1)
        put "Player one has won."
        done := true
    elsif whatdotcolour (330, 35) = blue and whatdotcolour (420, 35) = blue and whatdotcolour (505, 35) = blue and whatdotcolour (595, 35) = blue then
        locate (2, 1)
        put "Player two has won."
        done := true
    else
        done := false
    end if
end checkWin
proc processing
    searchColoumn

    switch
end processing

proc display

end display
proc userInput
    title
    display
    loop
        locate (2, 1)
        put "Player ", player, ",please enter a coloumn number: " ..
        get coloumn
        if coloumn < 1 or coloumn > 7 then
            locate (2, 1)
            put "Player ", player, ",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 Michael Bartella."
    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

Author:  jr5000pwp [ Sat Jan 05, 2013 11:05 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Turing:
for decreasing i : 6 .. 0
loops through all rows in a column, right? And
Turing:
if player = 1 then
            Draw.FillOval ((coloumn + 2) * 50, i * 50, 20, 20, red)
        else
            Draw.FillOval ((coloumn + 2) * 50, i * 50, 20, 20, blue)
        end if
is inside that loop, what do you think is gonna happen?

Author:  Michael3176 [ Sat Jan 05, 2013 11:12 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

I know but i dont know where else to put it, because thats the only place in my program where i has been declared.

Author:  Insectoid [ Sat Jan 05, 2013 11:15 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

You don't have to use i. You can use a new for loop. You could use p if you wanted. Or q. Or gfefreqwrewhr (though I don't recommend it).

Author:  Michael3176 [ Sat Jan 05, 2013 11:30 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

i know, but thats where im getting at, i dont know what to put in this for loop or how to use it with the draw command

Author:  Michael3176 [ Sun Jan 06, 2013 9:50 am ]
Post subject:  RE:Turing Connect Four Stacking Problem

So any idea on what code I should use?

Author:  Raknarg [ Sun Jan 06, 2013 1:20 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Yes we do, but we're trying to get you to see things for yourself

Author:  Michael3176 [ Sun Jan 06, 2013 3:42 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

ive been trying for a while now and have no idea, i made put the draw command in a display procedure, i had a for loop in that of 1..6 and all the counters showed up at once again, i have no idea what to do.

Author:  Michael3176 [ Sun Jan 06, 2013 10:09 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

?

Author:  GuCCIP [ Mon Jan 07, 2013 1:11 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

I'm trying to fix your whole program in a bit just wait. As for everyone else i think actually posting the code could help him more than just explain to him why it works.

Author:  Insectoid [ Mon Jan 07, 2013 1:14 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

No, it really doesn't. It's actually against the rules. At any rate, I think OP has bitten off more than he can chew in this case and should have picked an easier project.

If you post the code, OP will still have no idea how it works. He'll just have code he can't read and doesn't understand.

Author:  GuCCIP [ Mon Jan 07, 2013 1:26 pm ]
Post subject:  Re: Turing Connect Four Stacking Problem

Would it be against the rules if someone posts the correct code and explains it to the user?

Author:  jr5000pwp [ Mon Jan 07, 2013 2:05 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Snippets, yes, finish his code no, especially since this appears to be a school project.

Author:  Raknarg [ Mon Jan 07, 2013 9:19 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

Understanding a concept is really more important than the code itself.

Author:  Michael3176 [ Mon Jan 07, 2013 10:58 pm ]
Post subject:  RE:Turing Connect Four Stacking Problem

I finished my code it works now thanks


: