Archives for month: April, 2009

Super Smash Bros. Pong using Win32 and GDI

 
 

Back to the website… Yeah I’ve been working on a pretty big side project. For my Windows Programming class, I had to write a game of Pong, using Win32 and C++. I could not use MFC, DirectX, or any other API besides the Win32 API. The requirements weren’t too difficult, but I wouldn’t want to stop there. I decided to make something much bigger.

Super Smash Brothers Pong

I wanted to have a pong game based on Super Smash Brothers. Mario and a few other Nintendo characters will be playable. There would be a game of pong going on, and while that game was played, the chosen character would have attacks available to send fireballs or boomerangs across the screen that the opponent would also have to block. If the ball or any projectiles hit the opponent’s side of the game, they would lose health. Sounds like a simple concept, but it ended up being much larger than I expected. Plus it doesn’t help that I started on it with under a week before it is due.

Single Player

I was able to implement a single player mode with 10 stages. The difficulty increases through the 10 stages and you are able to fight all 6 characters I implemented. When you begin this single player mode you choose a single character to use and you are able to save your progress through the stages.

Multiplayer

There is a multiplayer mode so 2 people can face each other in Super Smash Brothers Pong. After each player picks one of the six characters they proceed to a stage select screen where they are able to pick from 7 different stages to play their game of pong. Each stage has a different background and paddle to add a little variety to the game.

Character Attacks and Gameplay

Mario
Quick Attack: Fireball
Mid Attack: Mushroom (large paddle)
Power Attack: Star Power
Luigi
Quick Attack: Fireball
Mid Attack: Vacuum Ball
Power Attack: Head Dive
Link
Quick Attack: Boomerang
Mid Attack: Hookshot
Power Attack: Song of Storms
Samus (unfinished) Fox (unfinished) Yoshi (unfinished)

Each player’s attack bar will constantly fill during the game. They can perform a quick attack when the bar is 1/4th full. They can perform a mid attack when it is 1/2 full. A Power Attack is performed when the bar is at full power. The first player to run out of life loses. The end result turns out to be a pretty fun game to play, and it is a great spin on Pong.

What’s Left?

Well I still have quite a bit to do to make this a polished game. I need to finish the attacks for 3 of my 6 characters. I need to add sound to make the game more interesting. Maybe learning FMOD would be a good idea. My how to play screen is very weak. I also need to fix a few bugs that can sometimes ruin gameplay. If you take a look at my code right now, it is a mess. My entire project was created using "just in time" programming.

For setting this up in under a week during classes, and using Win32, I think it was a great project. I learned quite a bit about multithreading and a very effective way at handling collisions, which I will post about.

Download the work in progress game: SSBPong.zip

Boggle – Using Hash Tables

 
 

One of my projects I had to do for my data structures class was a Boggle game in C++. It presented a very neat challenge of cataloging the English dictionary into the program. Throughout our data structures class, we found the best way to store the dictionary was through a hash table.

Hash tables are used to store very large amounts of information for fast searching, inserting, and removing. A hash table is compromised of an array of buckets. Each bucket is used to store a select set of information, usually within a singly linked list. You will want to write your hash table to work along with a singly linked list. In the case of the buckets being a singly linked list, you would use your array you made, and the [] operator, to access the appropriate singly linked list.

Check out how I constructed my Hash Table…

#include "SLList.h"     //I made my own Singly Linked List, check out my download to see more of this

template <typename Type = int>     //The class must be templated to create a proper SLL
class HTable
{
public:
HTable (unsigned int numOfBuckets, unsigned int (*hFunction) (const Type &v));
virtual ~HTable ();
void insert (const Type &v);
bool findAndRemove (const Type &v);
void clear ();
int find (const Type &v);
int numEmpty();     //I only made these next 4 functions to show stats on my Dictionary Stats page
unsigned int getNumBuckets();
unsigned int largestBucket();
unsigned int smallestBucket();


private:
SLList<Type>* arr;     //This is going to be dynamic memory so its going to be pointer
unsigned int (*hFunc) (const Type &v);     //This will store my hash function, I'll get into that soon
unsigned int numBuckets;

};

template <typename Type>
HTable<Type>::HTable(unsigned int numOfBuckets, unsigned int (*hFunction) (const Type &v))
{
arr = new SLList<Type>[numOfBuckets];     //Dynamic memory (our buckets), now we can use arr[ANUMBER] to access our SLL lists
hFunc = hFunction;
numBuckets = numOfBuckets;

}

After constructing a hash table I have all the buckets I need, and I can call my saved hash function to find out where any data sent into find, insert, and remove, would be.

Hash functions are written to find which bucket a specific item should be in. They need to be written in accordance to what type of information you will be storing. That is why you pass in the hash function that you will write alongside your program, rather than writing it within your Hash Table data structure.

Lets see how I need to write my hash function in this situation. I could have stored the entire dictionary within a 26 bucket hash table, with a bucket for each letter of the alphabet. Then if I needed to find if a word the user typed was real, I would grab the first letter of the input, head to the appropriate bucket, and see if it was there. Unfortunately a hash table like I just described would be very slow. There would be thousands of words in some buckets. It would take quite a bit of time to iterate through all of these words.

I needed to write something more efficient as one of my goals for the project. My hash function was completed after quite a bit of trial and error. Once I knew what had to be done it was all about finding which numbers would work out the best. In the end, this is what my hash function looked like…

unsigned int wordHash (const Type &word)
{
unsigned int bucket = 0;
short length = word.size();
for(int i = 0; i < length - 2; i++)
{
bucket += word[i] * word[i+2] + word[i+1];
}
return bucket % 16691;

}

I wanted to generate a large number based on nearly every character in the word in order to make it very unique. I did not want to spend too much time looping through the words so I decided to loop through the length minus 2. I made 16691 buckets to store this information. I chose this number for two reasons:

  • It is a prime number, meaning I can perform a modulus to it and get a number between 0 and 16690
  • It is a good number to evenly distribute 110307 words, leaving about 6 words per bucket on average
  • These are the results…

    I was amazed at how powerful that formula was! Out of 16691 buckets only 30 of them were empty and the largest bucket only held 18. Now my Boggle game can search the dictionary very quickly. It would only take a call to the hash function and iterating through a maximum of 18 words rather than searching through 110307 words.

    The rest of the game consisted of programming the grid and scoring. If you need the source or you need help with programming our own version of Boggle feel free to contact me at mikesprogramming@gmail.com

    Here is a link to the zipped up executable: Boggle.zip

    The Syndicate

     
     

    Last month I had one of my few general education classes, Psychology. It was an okay class overall, I found the content pretty interesting. We were put into groups in order to create a story and a few characters. We would be responsible for creating the psychological interactions between these characters. We were allowed to compile the final project in any medium we wanted, whether it be a powerpoint, word document, etc…

    I wanted to spice things up a bit so I decided to create a website. I only had a few days to get it together but I was up for the challenge.

    Check it out at http://mikesprogramming.com/syndicate

    I programmed the site entirely in PHP and hosted it on my home computer for the submission of the project. Our group credits are inside the website.