Posted: 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 forwardproc mainMenu
proc title
cls locate(1, 36) put"Connect Four" put"" end title
proc switch
if player =1then
player :=2 else
player :=1 endif 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 ifwhatdotcolour(65, 35)=redandwhatdotcolour(150, 35)=redandwhatdotcolour(240, 35)=redandwhatdotcolour(330, 35)=redthen locate(2, 1) put"Player one has won."
done :=true elsif whatdotcolour(65, 35)=blueandwhatdotcolour(150, 35)=blueandwhatdotcolour(240, 35)=blueandwhatdotcolour(330, 35)=bluethen locate(2, 1) put"Player two has won."
done :=true else
done :=false endif end checkWin
proc processing
if coloumn ="1"then if player =1then drawfilloval(65, 35, 20, 20, red) %draw the rest of ovals ontop else drawfilloval(65, 35, 20, 20, blue) endif elsif coloumn ="2"then if player =1then drawfilloval(150, 35, 20, 20, red) %draw the rest of ovals ontop else drawfilloval(150, 35, 20, 20, blue) endif elsif coloumn ="3"then if player =1then drawfilloval(240, 35, 20, 20, red) else drawfilloval(240, 35, 20, 20, blue) endif elsif coloumn ="4"then if player =1then drawfilloval(330, 35, 20, 20, red) else drawfilloval(330, 35, 20, 20, blue) endif elsif coloumn ="5"then if player =1then drawfilloval(420, 35, 20, 20, red) else drawfilloval(420, 35, 20, 20, blue) endif elsif coloumn ="6"then if player =1then drawfilloval(505, 35, 20, 20, red) else drawfilloval(505, 35, 20, 20, blue) endif else if coloumn ="7"then if player =1then drawfilloval(595, 35, 20, 20, red) else drawfilloval(595, 35, 20, 20, blue) endif endif endif
switch
end processing
proc display
for x :10.. 310by50 drawline(20, x, 635, x, black) endfor for x :20.. 640by88 drawline(x, 10, x, 310, black) endfor 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
endif exitwhen done =true endloop end userInput
proc goodBye
title
put"Made by the." delay(1300) Window.Close(mainWin) end goodBye
bodyproc 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
Posted: 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
Posted: 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
Posted: Tue Jan 01, 2013 11:57 pm Post subject: RE:Turing Connect Four Stacking Problem
Posted: 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
Posted: 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
Posted: 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 :array1.. 5, 1.. 5ofint
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:
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:
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:
Posted: 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
Raknarg
Posted: 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
Posted: 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
Posted: 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 =1then drawfilloval(65, 35, 20, 20, red)
board (1, y):=1 else drawfilloval(65, 35, 20, 20, blue)
board (1, y):=2 endif
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
Posted: 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
Posted: 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
Posted: 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 :array1.. numColumns, 1.. numRows ofreal %The array representing if a column is full var columnFull :array1.. numColumns ofboolean %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 endfor endfor 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 endif %Multiplying by 10 will add a 0 on to the end of the number
digits *=10 %Increment the number of digits
digitsCounter +=1 endloop 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 ifnot columnFull (x)then resultfalse endif endfor %If no columns are not full, then they are all full and the basin is full resulttrue 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 exitwhen amountToAdd =0.0 endif endfor %If there is still water remaining this column is now identified as full if amountToAdd not= 0.0then
columnFull (x):=true endif %Result the remaining amount of water result amountToAdd
endif 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 putround(basin (x, numRows - y + 1)*10) / 10 endfor endfor 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 exitwhen isFull
%Clear the screen cls endloop end fillBasin
%Initialize the arrays first to avoid errors
initArray
%Determine the number of digits used for formatting
setNumDigits
%Fill the basin
fillBasin
Raknarg
Posted: Fri Jan 04, 2013 11:23 am Post subject: RE:Turing Connect Four Stacking Problem
Turing:
for decreasing i :6. . 0 if i =0then
slots (choicex, 1)= player
valid_move =true exit if slots (choicex, i)not= 0then if i =6then
valid_move =false exit else
slots (choicex, i + 1)= player
valid_move =true exit endif endif endfor
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.