Link Bar

Home | Games | Programming | Tutorials | Donate | Legal (These are mostly empty right now. Placeholders!)

warning code

This website contains adult content.

Wednesday, March 7, 2012

Day 18: A game, at last!

It's not much, but I have the rudiments of a game here, now.  Thanks to Zachary's superior tutelage, I now have a game with a functioning victory condition, failure condition, and means of quitting.  You can't save, or talk to anyone, or do anything besides win/lose/quit... but still!  I have something functional here, and now it's just a matter of scaling it up until I get something worth distributing.

For those of you playing along at home, this is how Noob Game works (so far):
#include "stdafx.h"
#include "HelloWorld.h" //this header is empty at the moment, but I have a feeling I'll be using it eventually
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) //this is the main function!
/* If I'm reading it right, the main function is simply an integer (albeit a really, really big one in binary).  What this means is that any possible game state could also be represented as an integer, Library of Babel-style, such that there is a set of integers which in aggregate represent all possible game states.  Whoah. */
{
cout<<"One Man Show"<<std::endl;
cout<<"Welcome to Noob Game.  Enter 1 to win, 2 to lose, or 3 to quit."<<std::endl;
char choiceOne[100];
std::cin.getline(choiceOne,100);
if(choiceOne [0]=='1'){
std::cout<<"You win!  Thank you for playing!"<<std::endl;
}
else {
if(choiceOne [0]=='2'){
std::cout<<"You lose!  Please play another time."<<std::endl;
}
else {
if(choiceOne [0]=='3')[
std::cout<<"You quit!  Fuckin' quitter."<<std::endl;
}}}
std::cout<<"The game is over.  Enter anything to close."<<std::endl;
std::cin.getline(choiceOne,100);
;
return 0;
}
And that's it!  I have extra comments in there & stuff, but I don't really care about that on such a small program.  I compiled it and ran the .exe on my computer many times!  It works!  Well, entering "4" or anything else besides 1, 2, or 3 will simply advance to ending the game, but as far as I'm concerned right now, entering unacceptable input is a failure condition.

I'm working on a replay module right now; I'll probably have to learn something else to make it work the way I want to, since I can't use "goto" any more.  Or at least shouldn't...

Anyway, even that was the result of an embarrassingly long session of poking, prodding, and just trying different things.  I'm going to go back to doing that now; just wanted to share that I've actually got something!  The simple fact that I'm using a program I created myself, no matter how rudimentary, is giving me a bit of a rush.

1 comment:

  1. pretty good! minor stylistic notes:

    generally you want [X] bits right next to the variable they're referencing; i.e. "choiceOne[0]", not "choiceOne [0]". This is because the [] operator is literally performing an operation on the variable (dereferencing the pointer and shifting forward by N bytes, where N = sizeof(variableType)*X.

    fun backend bit: arrays are simply a pre-calculated block of memory determined by the size of the array and the size of the object being specified. int choiceOne[100]; initializes a block of memory that is 100*sizeof(int) bytes long. When you reference an item in that array, it knows to find it really quickly by going to the memory location identified by the base memory location (i.e. choiceOne[0], which is actually a pointer reference) and moving forwards by sizeof(int)*N, where N is the reference point. This is why you get overflow errors when you enter in too big a number; it's just doing some addition math.

    Generally, it's good to add spaces between operators to improve readability. so std::cout << "blah" << std::endl; instead of std::cout<<"blah"<<std::endl;

    Finally, you're misreading. a function definition is as follows:

    ReturnType functionName(paramType paramName1, paramType paramName2);

    The first part is just the return type. Main returns an integer because the OS itself can track return types and determine if an error was caused.

    argc is a reference to the "argument count", so when you run something by the commandline you can count how many arguments were passed. argv is the list of arguments that were passed in. it's an array of arrays, or in another way of thinking, an array of strings.

    If you want to have it ONLY do something on 1, 2, or 3 you can wrap your cin and if check in a "while" loop, and for the while condition set it so that it only loops while choiceOne[0] is neither 1, 2, or 3.

    Finally, it's not super important, but when you know you only are looking at one character worth of input, it's not necessary to initialize an array of 100 characters. It's a waste of memory.

    ReplyDelete