Got some good parts of FreeCell finished
by J Sears · 11/20/2006 (10:11 pm) · 6 comments
Well for the possibly 3 people that read my blog I added this part as a remark to my last post. After struggling early on the other day with how to best create games for FreeCell I ended up with a random shuffler as I call it. I made an array with all the cards in it (specifically their image map names in it) I then used a random number call and a for loop to shuffle the cards in the array. I called that array in another for loop to make one long string out of it and then wrote that string to a file. Since I was going to write a lot of these (eventually plan on getting to 2 million) I added a readfile check that stores each line in a variable called holder which I compare to the current string if none of them match I write the new string and then make another random game.
After getting this programmed and testing it a few ways I let it loose. The screen stayed black. I figured I had messed up so I forced windows to close the program and checked my levels.cs and it already had a thousand or so levels in it and I realised that since I had that stuff as the first part of my program nothing else would show till it was done so I cleared those levels and gave it a fresh start. About 25 minutes into it TGB finally had enough and closed on error, I figure the way I programmed it must be leaving something in memory each cycle so it finally ran out. I checked the file and I ended up with 37501 unique games one per line. Not as many as I'd hoped but certainly enough to do for now.
Now I had to write code to read the file and set up the game. This was actually tougher then I thought it would be. Well it wasn't actually tough but my idiot self coding through it too fast in excitement from my level creations mistyped a couple small things that didn't show up as errors and took me a while to notice. My array for instance that kept track of where to mount the next card was called columns[] but when I put it in the loop that took apart the level string and mounted the cards I called it column[] thus creating 2 different arrays so my game was not ending up right. Then when I was sure I had it all worked out no cards were showing up I realised I had to move where I called the function down further in game.cs. So all that wasted some time but was a good learning experience on keeping good track of names and planning ahead on certain things.
I also added right mouse to show what card was stacked under other cards like microsoft freecell has. Was simple enough brought the object to layer 0 on right mouse down return it to it's previous layer on right mouse up.
Then having those levels I needed someway to access them until I made my full blown start up gui so I added a popup window that loads first you enter level number and it loads the level, also pretty easy and was looking forward to more gui experience since I think being able to make good guis can incredibly mean the difference between a good 2d game and a bad one so need all the practice I can get.
Fixed a couple small bugs I had made in my earlier parts of the program that finally surfaced and I think that covers the day. My current goal is to work on the ability to move columns of correct cards at a time which is more involved the more I think about it so I will try it one step at a time with first step being keeping track of open free cells and open columns (a column will allow for double the amount of freecells + 1 to be moved due to the nature of freecell).
Possible addition I am thinking of adding to my code is a double click on a card will move it to a freecell, since I don't think there is a double click function already in TGB and I don't yet want to mess with source code I will probably have to make it as something of on first mousedown start a timer and set a variable of first click to true then on second mouse down check to see if first click is true and if timer is
Oh ya and I wanted to add a multiplayer feature to my game to get some work with the net code aspect, and I'm starting to really wonder how that will work since I use a few globals to do my code. Could result in some unfortunate code rewrites
After getting this programmed and testing it a few ways I let it loose. The screen stayed black. I figured I had messed up so I forced windows to close the program and checked my levels.cs and it already had a thousand or so levels in it and I realised that since I had that stuff as the first part of my program nothing else would show till it was done so I cleared those levels and gave it a fresh start. About 25 minutes into it TGB finally had enough and closed on error, I figure the way I programmed it must be leaving something in memory each cycle so it finally ran out. I checked the file and I ended up with 37501 unique games one per line. Not as many as I'd hoped but certainly enough to do for now.
Now I had to write code to read the file and set up the game. This was actually tougher then I thought it would be. Well it wasn't actually tough but my idiot self coding through it too fast in excitement from my level creations mistyped a couple small things that didn't show up as errors and took me a while to notice. My array for instance that kept track of where to mount the next card was called columns[] but when I put it in the loop that took apart the level string and mounted the cards I called it column[] thus creating 2 different arrays so my game was not ending up right. Then when I was sure I had it all worked out no cards were showing up I realised I had to move where I called the function down further in game.cs. So all that wasted some time but was a good learning experience on keeping good track of names and planning ahead on certain things.
I also added right mouse to show what card was stacked under other cards like microsoft freecell has. Was simple enough brought the object to layer 0 on right mouse down return it to it's previous layer on right mouse up.
Then having those levels I needed someway to access them until I made my full blown start up gui so I added a popup window that loads first you enter level number and it loads the level, also pretty easy and was looking forward to more gui experience since I think being able to make good guis can incredibly mean the difference between a good 2d game and a bad one so need all the practice I can get.
Fixed a couple small bugs I had made in my earlier parts of the program that finally surfaced and I think that covers the day. My current goal is to work on the ability to move columns of correct cards at a time which is more involved the more I think about it so I will try it one step at a time with first step being keeping track of open free cells and open columns (a column will allow for double the amount of freecells + 1 to be moved due to the nature of freecell).
Possible addition I am thinking of adding to my code is a double click on a card will move it to a freecell, since I don't think there is a double click function already in TGB and I don't yet want to mess with source code I will probably have to make it as something of on first mousedown start a timer and set a variable of first click to true then on second mouse down check to see if first click is true and if timer is
Oh ya and I wanted to add a multiplayer feature to my game to get some work with the net code aspect, and I'm starting to really wonder how that will work since I use a few globals to do my code. Could result in some unfortunate code rewrites
About the author
Recent Blogs
• <edit>• My thoughts, and my farewell
• some progress on cards
• teaching my computer to play cards
• I'm not dead
#2
11/21/2006 (11:46 am)
ahh that is easier, but how close together do those clicks have to be?
#3
the math problem is this available open spaces (from now on OS) is = the number of FreeCells(FC) * 2 +1 for each open column(OC) so for 4 FCs and 1 OC it works like this OS=4 * 2 + 1 = 9 now if you have 2 OCs it does this OS=(4*2 +1) * 2 + 1 = 19 and so on for more open columns and the only way I can figure to do this is to make a function that calculates OS and works like this
function calcOpenSpaces(%freecells, %columns)
{
%openSpaces = %freecells;
for(%i=0;%i<%columns;%i++)
{
%openSpaces = %openSpaces * 2 + 1;
}
return %openSpaces;
}
now I haven't put this in my program yet but I'm about to I was just wondering if there was a way to set up a math formula to do this instead.
11/21/2006 (1:58 pm)
don't know if anyone who is reading this is a math whiz but I'm too stupid to figure out a good formula for something so I'm about to write it up with a for loop. the math problem is this available open spaces (from now on OS) is = the number of FreeCells(FC) * 2 +1 for each open column(OC) so for 4 FCs and 1 OC it works like this OS=4 * 2 + 1 = 9 now if you have 2 OCs it does this OS=(4*2 +1) * 2 + 1 = 19 and so on for more open columns and the only way I can figure to do this is to make a function that calculates OS and works like this
function calcOpenSpaces(%freecells, %columns)
{
%openSpaces = %freecells;
for(%i=0;%i<%columns;%i++)
{
%openSpaces = %openSpaces * 2 + 1;
}
return %openSpaces;
}
now I haven't put this in my program yet but I'm about to I was just wondering if there was a way to set up a math formula to do this instead.
#4
And as to the function for calculating the number of open spaces - if I understand you correctly, you need to add up add the OS values for each column, where the OS value is computed using the OS=2(FC) + 1. If you were making a math formula, you would use summation, which translates into a for loop in code anyway. If my understanding above was correct, then I'd write the function like this:
Then again, I have no idea how freecell works, so that might not be it at all ;)
Edit: Actually - I definitely don't understand you. What roles do columns play in this? Is the number of freecells different for each column? If it is, you need to make sure the %freecells value in the loop is changed each time to properly reflect that particular column's value. Or maybe the %freecell value is just a certain value of the game and independent of the number of columns and so the loop isn't needed at all and the total openSpaces is just columns*(%freecells * 2 + 1)...
11/21/2006 (2:18 pm)
Quote:ahh that is easier, but how close together do those clicks have to be?I believe that it just uses the OS settings, but it might be a Pref.
And as to the function for calculating the number of open spaces - if I understand you correctly, you need to add up add the OS values for each column, where the OS value is computed using the OS=2(FC) + 1. If you were making a math formula, you would use summation, which translates into a for loop in code anyway. If my understanding above was correct, then I'd write the function like this:
%openSpaces = 0;
function calcOpenSpaces(%freecells, %columns)
{
for(%i=0;%i<%columns;%i++)
{
%openSpaces += %freecells * 2 + 1;
}
return %openSpaces;
}Then again, I have no idea how freecell works, so that might not be it at all ;)
Edit: Actually - I definitely don't understand you. What roles do columns play in this? Is the number of freecells different for each column? If it is, you need to make sure the %freecells value in the loop is changed each time to properly reflect that particular column's value. Or maybe the %freecell value is just a certain value of the game and independent of the number of columns and so the loop isn't needed at all and the total openSpaces is just columns*(%freecells * 2 + 1)...
#5
So people start to build on the columns a stack of cards in order like red 8 black 7 red 6 black 5 red 4 black 3, so the program allows for a super move to move the 7 - 3 to either an open column or a black 9. So let's say there's 2 free cells and an open column and you want those 5 cards off say an A that you need up top. you would move the cards as such. The 4 to a freecell, the 5 to a free cell, the 6 to the open column, the 5 onto the 6, the 4 onto the 5, the 7 up to the freecell, the 8 up to the freecell and now the A is free.
Now most programs will see there's 5 moves possible and 5 cards to move and move all the cards in that order for you in a second as opposed to you moving each one by hand since those moves are common.
so as you see having any freecell is just equal to one open spot since nothing can be stacked on cards in freecells, but having an open column with the freecells allows for 2 X freecells + 1 moves=os, and having another column open will have 2 x OS + 1 and so on for each additional column.
Now granted once you have 4 freecells and one free column you have more moves then a person can have possible in one stack, but since I'd rather keep an accurate count of all the moves possible for when they fill in spots I need an accurate count. The for loop is working exactly right I was just wondering if a single math formula would allow for it so I didn't need a function.
11/21/2006 (4:18 pm)
freecell gets dealt out in 8 columns and there's 4 free cells at the top and 4 suit stacks the goal is to get all the card stacked from A to K in their suit stacks. the cards are all dealt out into 8 columns 4 columns have 1 more card then the other 4. The cards can only be moved either to the suit stack a free cell a free column or moved onto a card of opposite color of one higher value. So people start to build on the columns a stack of cards in order like red 8 black 7 red 6 black 5 red 4 black 3, so the program allows for a super move to move the 7 - 3 to either an open column or a black 9. So let's say there's 2 free cells and an open column and you want those 5 cards off say an A that you need up top. you would move the cards as such. The 4 to a freecell, the 5 to a free cell, the 6 to the open column, the 5 onto the 6, the 4 onto the 5, the 7 up to the freecell, the 8 up to the freecell and now the A is free.
Now most programs will see there's 5 moves possible and 5 cards to move and move all the cards in that order for you in a second as opposed to you moving each one by hand since those moves are common.
so as you see having any freecell is just equal to one open spot since nothing can be stacked on cards in freecells, but having an open column with the freecells allows for 2 X freecells + 1 moves=os, and having another column open will have 2 x OS + 1 and so on for each additional column.
Now granted once you have 4 freecells and one free column you have more moves then a person can have possible in one stack, but since I'd rather keep an accurate count of all the moves possible for when they fill in spots I need an accurate count. The for loop is working exactly right I was just wondering if a single math formula would allow for it so I didn't need a function.
#6
If you really want to get to 2 million levels -- I would be willing to work with you to auto-generate the level file (s).
I have a fairly extensive development background, and wouldn't mind taking your torquescript code and converting it into something that can handle that large of an array and producing the file for you using your random calculation code.
Email me at higginsd at zoulcreations dot com if you'd like to get together on the level file -- I honestly do not believe that TorqueScript or TGB can handle that, so another direction will most likely be necessary. You can also message me on AIM as 'Foobastic' or Yahoo as 'moz08132' --
11/21/2006 (8:13 pm)
@J, If you really want to get to 2 million levels -- I would be willing to work with you to auto-generate the level file (s).
I have a fairly extensive development background, and wouldn't mind taking your torquescript code and converting it into something that can handle that large of an array and producing the file for you using your random calculation code.
Email me at higginsd at zoulcreations dot com if you'd like to get together on the level file -- I honestly do not believe that TorqueScript or TGB can handle that, so another direction will most likely be necessary. You can also message me on AIM as 'Foobastic' or Yahoo as 'moz08132' --

Associate Tom Eastman (Eastbeast314)