• Steam recently changed the default privacy settings for all users. This may impact tracking. Ensure your profile has the correct settings by following the guide on our forums.

My First "Game"

KezraPlanes

Just some dood
After years of talking out my arse, I decided to pick up programming for good.

I've been learning C++ for about three days and this the first game that game out of it xD

I've included the source so more experienced people can help my better my code with tips.

Don't be too harsh though... It's only been three days.
 

KezraPlanes

Just some dood
Err, "gp1" and "gp2". More meaningful names would make it more maintainable.

My identifiers suck I know xD I'll try to fix that..
 

Davee

lolhax
Also:
Code:
		if(again == 'y') return gp1(); 
		else if(again == 'n') return main();

You re-call main and gp1, these are more advanced techniques called "recursion". Recursion is a bad omen, and can lead to a load of problems where you use up all your allocated stack memory.

STACK:
[MAIN]
[GP1]
[MAIN]
[GP1]
[MAIN]
[GP1]
[MAIN]

You see how the stack just builds up there? It is better do have a "while" loop within your main function and terminate out of it when you wish to exit.

Here is pseudo-C
Code:
while (1)
{
	clearscreen();
	displaytext();
	
	int button = getinput();
	
	if (button == gp1)
	{
		gp1();
	}
	else if (button == gp2)
	{
		gp2();
	}
	else if (button == exit)
	{
		break;
	}
	else
	{
		displayinvalid();
		continue;
	}
	
	//Only gets here after execution of gp1 or gp2 
	displayplayagain();
	button = getinput();
	
	if (button != playagain)
	{
		break;
	}
	
	//goes to top of loop
}

//exit

Always a good idea to write a simple pseudo design before coding. Even if you get a pen + paper and draw boxes and arrows.
 

KezraPlanes

Just some dood
Also:
Code:
		if(again == 'y') return gp1(); 
		else if(again == 'n') return main();

You re-call main and gp1, these are more advanced techniques called "recursion". Recursion is a bad omen, and can lead to a load of problems where you use up all your allocated stack memory.

STACK:
[MAIN]
[GP1]
[MAIN]
[GP1]
[MAIN]
[GP1]
[MAIN]

You see how the stack just builds up there? It is better do have a "while" loop within your main function and terminate out of it when you wish to exit.

Here is pseudo-C
Code:
while (1)
{
	clearscreen();
	displaytext();
	
	int button = getinput();
	
	if (button == gp1)
	{
		gp1();
	}
	else if (button == gp2)
	{
		gp2();
	}
	else if (button == exit)
	{
		break;
	}
	else
	{
		displayinvalid();
		continue;
	}
	
	//Only gets here after execution of gp1 or gp2 
	displayplayagain();
	button = getinput();
	
	if (button != playagain)
	{
		break;
	}
	
	//goes to top of loop
}

//exit

Always a good idea to write a simple pseudo design before coding. Even if you get a pen + paper and draw boxes and arrows.

I tried doing a while but unfortunately I would get into an infinite loop of playing again even if you pressed "n" (or any other key for that matter)

So being very inexperienced I decide a simple else if would do the job.

But thanks for the tips :)
 

KezraPlanes

Just some dood
A little more info for us non coders?

EDIT: Never mind file name says it all.

You wanted to know what the game was about? xD
 

MenaceInc

Staff Member
Might wanna add in the OP that it requires MSVCP100.dll and MSVCR100.dll :p


EDIT: Hmm, actually even with those .dll's I'm getting an error.... "The procedure entry point_invalid_parameter_noinfo_noreturn could not be located in the dynamic link library msvcr100.dll"

seems there's something wrong with my system...weird. Anyways, this isn't because of your code so don't be worrying about it :p

---------- Post added at 07:43 PM ---------- Previous post was at 07:32 PM ----------

Just wondering but what version of Visual Studio/Visual C++ did you make this with? You may wanna bundle the msvcp and msvcr dll's with your future packages.


EDIT: I was able to get to compiled with CodeBlocks and didn't need any work done to get it to compile so nice one for staying away from Visual Studio specific stuff. :D
Screenshot,
H8wRs.png
 

KezraPlanes

Just some dood
Might wanna add in the OP that it requires MSVCP100.dll and MSVCR100.dll :p


EDIT: Hmm, actually even with those .dll's I'm getting an error.... "The procedure entry point_invalid_parameter_noinfo_noreturn could not be located in the dynamic link library msvcr100.dll"

seems there's something wrong with my system...weird. Anyways, this isn't because of your code so don't be worrying about it :p

---------- Post added at 07:43 PM ---------- Previous post was at 07:32 PM ----------

Just wondering but what version of Visual Studio/Visual C++ did you make this with? You may wanna bundle the msvcp and msvcr dll's with your future packages.


EDIT: I was able to get to compiled with CodeBlocks and didn't need any work done to get it to compile so nice one for staying away from Visual Studio specific stuff. :D
Screenshot, [qimg]http://imgur.com/H8wRs.png[/qimg]

Whoops D: xD

This was written and compiled in VS C++ 2010 Express so it requires:

Microsoft Visual C++ 2010 Redistributable Package Download ...

this.

xD
 

KezraPlanes

Just some dood
Tried that but didn't work. Just wrapped it up in a CodeBlocks project instead. :p

Any particular reason why you chose VS C++ 2010 Express? I'm not saying it's a bad IDE, just wondering.

I just have a little bit more experience with it (tinkered with it a few times through the years) so it's the one I'm more used to...
 

Hellcat

Contributor
About the while loop, did you add a "reason" why and when it should exit the loop?

Something like this:
Code:
while( again == 'y' )
{
 ...
}
 

KezraPlanes

Just some dood
About the while loop, did you add a "reason" why and when it should exit the loop?

Something like this:
Code:
while( again == 'y' )
{
 ...
}

Can't remember my original code but I think I didn't terminate the first if inside the loop :S
 

MenaceInc

Staff Member
So that's why it won't compile on OS X. :cool:

You've inspired me to do something similar in Java, as i'll be starting my course on it very soon :blush:.

it should compile fine, I couldn't see anything OS dependant. It should be fine in Xcode. :S
 

KezraPlanes

Just some dood
So that's why it won't compile on OS X. :cool:

You've inspired me to do something similar in Java, as i'll be starting my course on it very soon :blush:.

Glad to know I was of use :p Though I thought this source should be OS independent. I'm having great fun coding and right now I find myself wanting to code more than playing actually.

it should compile fine, I couldn't see anything OS dependant. It should be fine in Xcode. :S

I dunno what Xcode is (probably a IDE for OSX I'd wager) but if Menace says it should work, I guess it should xD
 

KezraPlanes

Just some dood
Any reason as to why you used '\n' instead of endl for starting new lines?

Nope... I just prefer \n over endl I guess... Though I used more endl in the beginning, I stopped for some reason.

The disadvantage of \n is that it can make lines and strings confusing though =\
 
Well, I translated gp1 into Java, and it appears to work fine. Only ~20 typos, new personal best. Had to lookup random and user input codes from Google though. :p. I also had it the wrong way around, where the user would be guessing the number. Maybe a diagram isn't such a bad idea after all.

Code:
import java.io.*;
import java.util.*;

//Paul Colusso, 2010
//Java implementation of the gp1 "Number Guesser"

public class gp1
{
    
    String userEntry;

    public static void main(String[] args)
    {
        int userNumber;
        int cpuNumber;
        int triesCounter = 0;
        Random numberGuess = new Random();
        
        System.out.println("Welcome to Guess My Number\n\n");
        System.out.println("Enter your guess (between 100 & 1): ");
        userNumber = getUserInput();
        
        do
        {
            cpuNumber = numberGuess.nextInt(99) + 1;
            triesCounter++;
            if (userNumber < cpuNumber)
            {
                System.out.print("Too High! ");
            } else if (userNumber > cpuNumber) {
                System.out.print("Too Low! ");
            }
        } while (userNumber != cpuNumber);
        
        System.out.println("\n\nThe CPU guessed your number " + userNumber + " in " + triesCounter + " guesses");
    }
    
    public static int getUserInput()
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String userEntry;
        
        try
        {
            userEntry = br.readLine();
        } catch (IOException ioe)
        {
            System.out.println("Error!");
            System.exit(1);
            userEntry = "Failed";
        }
        return Integer.parseInt(userEntry);
    }
}

EDIT: It probably would have compiled fine in the hands of someone more experienced, but every time I see "ld: Symbol(s) not found" I run for the hills.
 

ilyace

Member
Nope... I just prefer \n over endl I guess... Though I used more endl in the beginning, I stopped for some reason.

The disadvantage of \n is that it can make lines and strings confusing though =\

Yeah that's my main reason for using it over '\n'.
 
Top