Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts



Genetic Algorithm Experiment #1 Phase 1


I read about genetic algorithms a while ago and I've wanted to try them for a while, but didn't have the right platform to do so in.
But luckily this year I'm taking AP Computer Science and we're using a case study called GridWorld, which is a perfect way to test things.

Without further ado, here's my write up of my experiment.

---

Time to start work on the first experiment of my first genetic algorithm experiments.
Here's my setup:
Gridworld critters
Four genomes:
G1: Atk 1, Def 1, Con 1
G2: Atk 2, Def 1, Con 1
G3: Atk 1, Def 2, Con 1
G4: Atk 1, Def 1, Con 2

Attack is amount of damage, defence is how much damage is negated when attacked, regenerates 1 point of def every (two?) steps. Con is how many hitpoints it has. Regenerates 1 HP per step if not attacking. Whoever gets to their step first will regen first, tough luck if a genome isn't regened when it gets attacked. Def regeneration isn't cumulative, a genome must spend two full turns regernating one point of Def.

All genomes attack each other.
If another genome is within range (1 square) of another it determines randomly, at this point, which to attack. Then attacks, it gets first strike, so it does its' damage first then the other genome does its' damage.
First experiment, only four bugs with genomes.

I think that one of the 3 stronger ones are going to prevail simply because they have higher stats at this point. Of those three, I would have to say that G2 is probably going to win because of how hard it hits.



Basic Ciphering Techniques Part 1


I had the idea for this post while writing the walkthrough for the Sophos Puzzle, and I think it's a good idea. Essentially, I'm going to outline some basic ciphering techniques that I've learned participating mainly in ARGs.

Firstly, you need to understand what a cipher is. Wikipedia, naturally, has a good article on it: http://en.wikipedia.org/wiki/Cipher. As is normal for Wikipedia, the article has a bit too much information for a beginner, although it's not as bad as some I've seen. Good grief. But anyway, a cipher is pretty much just a disguised way of writing some, or a secret code. I'm sure most of you used or made up some codes when you were kids, it's the exact same principle, except most ciphers are a bit more complicated than the ones you made up in gradeschool. (Although some aren't, you'd be surprised, actually.)
Wikipedia defines cipher and encryption in an interesting way. It says that you encrypt something with a cipher, which I suppose is indeed correct, although that's not really how I view the definition. I view encrypting and enciphering something as two different things. Mainly, enciphering something is less secure and easier to decipher than something you've encrypted. But maybe that's just me, I don't know.

Now that you know what a cipher is, here are some basic and fairly common ciphers:



Sophos Puzzle Walkthrough


One of my friends told me about this puzzle, and I'm quite thankful to him for that. This was a very fun puzzle to do and I learned a lot.
Essentially Sophos.com hosted a puzzle that's giving out three Lego Mindstorms in celebration of the release of The Girl With The Dragon Tattoo. You can find the original page here. So far as I know everything is going to stay there, which is awesome, because other people can solve it.
Fair warning, this spoils the entire puzzle, so if you want to try it yourself first, which I highly recommend, visit the link above and give it your best shot and come back here if you get stuck. Or just read the whole thing if you're lazy. :p

So, this is a two-part puzzle, the second part being harder, obviously.
The first part, as found on the page above, is this:

Turn =ImYndmbn1ieiBnLmJWdjJmZ into a URL.

Seems simple enough, right? It actually is pretty easy.

You can tell hacking really looks like this, can't you?
If you're familiar with the normal ciphering methods, you'll probably recognize this as Base64, just in reverse. (If you're not familiar with basic ciphering, I'll write a post on that eventually.) A normal Base64 string looks like this: SGVsbG8gd29ybGQ=
Luckily this string has an equals sign at the end, which some strings don't. Some have two (==) or one or none. Look it up on Wikipedia if you want to know more.



Plains Terrain: Part 3 - I'm a Moron


I am a moron. Let me explain. Last time I was trying to stitch the terrain together by averaging the vertex data itself, but I use a heightmap to get all of my vertex data. So why not just stitch the heightmaps together before I generate the vertex position data? It'll be faster, and I won't have to keep wrestling with my arrays. It's also very easy to store heightmaps, they're just images, if I ever want to make a save system of some kind.

But first, check out the heightmap. It looks weird.



It shouldn't look like that, it should look more like this.



Well, I fixed it. I'm not completely sure why it was doing that, but whatever. Now it looks like it should, and the terrain isn't completely odd.



Now, let's try stitching the heightmaps together as I originally thought.



Sweet! It looks perfect. Well, mostly perfect. It's not smoothed or anything, but the vertices meet up in exactly the same places. The next step would be to just have the averaging of the heightmaps go deeper in to make the transition smoother. But I think I'm going to stop here for now. It's been almost a month since I wrote this code that I'm doing this write up, and I've lost interest for the moment. I'll probably come back to it later, but for now this project is going on the shelf.



Plains Terrain: Part 2 - How not to Stitch


We last left off having just figured out how to render the squares of terrain in a proper grid. Now we have a different problem. If you look at what our terrain looks like now, you'll see that it's almost as though there is a border of vertices around each of the squares of terrain. I need these vertices to stitch the terrain together, and I need to figure out why they're way down at value zero.

Turns out my heightmap wasn't big enough by one pixel. That was an easy fix, now there are no random vertices floating out of place. Yet.

I'd like my terrain to be mostly uniform as it changes from place to place, instead of what I have now where everything looks completely disjointed. So, I added a global heightmap. When the program generates each individual heightmap, it uses the bumpiness value of the global heightmap in that specific location, and it looks awesome. Unfortunately, I forgot to take a screenshot here for some reason.

Even though it apparently looks good, it's predictable, so I'm gonna try and make the heightmap smaller, so there is more variation. That..kind of works, but it didn't actually change that much. I might try another noise generator other than Perlin noise, but not right now. For now, let's try some stitching.

I'm going to try and average the vertices in the top of one square and the bottom of another, so they should meet in the middle. I do that and...nothing happens. What is going on here?



Plains Terrain: Part 1 - Starting Out


So I've been looking for a new procedural project (after my OpenGl maze and basic terrain generator.) I want to, eventually, make something like Project Frontier eventually, but I haven't had enough practice with OpenGl and I'm not good enough at programming yet.

So, for now, I'm going to make a Plains terrain generator. That is, I'm going to make a program that will generate one biome of terrain, namely the savanna, or something close to it.

My goal isn't to make something that's realistic, just something that looks very pretty and is procedurally generated. I'm not going to generate my own textures, at last most of them. I also want to have lighting, a sky, some kind of sunset/rise as well. Maybe trees, if I'm feeling adventurous.

Let's get started.

I'm gonna start by pulling in code from my old terrain project, which, at this point, doesn't actually work. It also loads the vertex and texture data into a straight float array, which I don't like, it looks ugly and it's hard to work with. So I'm going to switch it over to a more object oriented method.

For example, this is what I'm using.

for (int ix = 0; ix < TileData.terrainWidth; ix++)
            {
                for (int iy = 0; iy < TileData.terrainHeight; iy++)
                {

                    int curL = curLoc * 72;
                    int curT = curLoc * 8;

                    TileData.verts[curL] = ix * scaleFactor; // first 0, 0
                    TileData.verts[curL + 1] = heightByte[ix, iy] * heightFactor;
                    TileData.verts[curL + 2] = iy * scaleFactor;

                    TileData.verts[curL + 3] = ix * scaleFactor;
                    TileData.verts[curL + 4] = heightByte[ix, iy + 1] * heightFactor;
                    TileData.verts[curL + 5] = (1 + iy) * scaleFactor; // second 0, 1

                    TileData.verts[curL + 6] = (1 + ix) * scaleFactor;
                    TileData.verts[curL + 7] = heightByte[1 + ix, 1 + iy] * heightFactor;
                    TileData.verts[curL + 8] = (1 + iy) * scaleFactor; // 1, 1

                    TileData.verts[curL + 9] = (1 + ix) * scaleFactor; // 1, 0
                    TileData.verts[curL + 10] = heightByte[1 + ix, iy] * heightFactor;
                    TileData.verts[curL + 11] = iy * scaleFactor;

                    TileData.NumVertex += 4;

                    TileData.texVerts[curT] = 0;
                    TileData.texVerts[curT + 1] = 0.25f;

                    TileData.texVerts[curT + 2] = 0;
                    TileData.texVerts[curT + 3] = 0;

                    TileData.texVerts[curT + 4] = 0.25f;
                    TileData.texVerts[curT + 5] = 0;

                    TileData.texVerts[curT + 6] = 0.25f;
                    TileData.texVerts[curT + 7] = 0.25f;
                    curLoc++;
               
                }
            }
And this is what I'd rather use.




Changes to Blog Layout


As you may have noticed, I updated the layout of the blog and added some features.
I really like Blogger, the fact that I can do all of that is really pretty nice. Of course, I had to learn how they handled their variables, and I'd never worked with XML on the web before, but that was no big deal.

The way Blogger handles the layouts is very interesting. It's incredibly object-orientated so you don't have to go into the code to change the layout basically, like changing the color of the fonts, and everything else you can do through the web panel.

Of course, what I wanted to do (which, by the way, I pretty much completely stole from Shamus Young's site) wasn't possible with the standard editor. Luckily Blogger makes it pretty darn easy to edit the templates.

Here's an example of some of the code I added:



Mandelbrot Generation Part 2: Generation Success


Alrighte, it's been two or so days since I've had time to sit down and do any programming at all, as I'm pretty

busy at the moment. We'll see how much I remember what I've been doing. :p
So I left off where I had drawn a 10x10 pixel texture and figured out how to set pixel color-by pixel. So, let's try and port the C++ code that I found into C# and see if it works.



...Well then. That worked really well. Now to find out how the hell it works. XD
I pretty much copied and pasted the code over, so I'm not really sure how it works, so let's go through and add spaces and stuff.



Mandelbrot Generation Part 1: XNA Problems


Yeah, so I decided to try and make a Mandelbrot fractal generator, and while waiting for the C# download to finish I figured I might as well try and write it up just because.
So yeah. I don't have a degree in math or programming, I just like fractals, math and programming.
Okay, after reading the Wikipedia article I found this bit of pesudo-code: