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

Username:   Password: 
 RegisterRegister   
 Turing Connect Four Stacking Problem
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Michael3176




PostPosted: 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

Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: 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
Insectoid




PostPosted: 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.
Raknarg




PostPosted: Tue Jan 01, 2013 11:57 pm   Post subject: RE:Turing Connect Four Stacking Problem

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




PostPosted: 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?
Panphobia




PostPosted: 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
Raknarg




PostPosted: 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.
Michael3176




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: 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.
Michael3176




PostPosted: 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
Raknarg




PostPosted: 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.
Raknarg




PostPosted: 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
Michael3176




PostPosted: 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
jr5000pwp




PostPosted: 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
Raknarg




PostPosted: 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.
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 4  [ 51 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: