First Game [Archive] - C Board

PDA

View Full Version : First Game


CheesyMoo
01-26-2003, 11:13 AM
I know you probably all think I'm a dumb n00b, but oh well.
I made my first game(it doesn't really count) it was a mad lib game, it was really easy but, I want to make a game like tic-tac-toe or something... I'm lost I really have no idea on how to start, I've read tutorials and all they say is stuff like you can't start out making quake 3 start small... that really doesn't help, does any body example code for a tic-tac-toe game or other VERY simple game... thanks.

Travis Dane
01-26-2003, 11:19 AM
What's mad lib?
Well Tic-Tac-Toe is about the easiest thing i can think of,
imagin an array


int spot[10]; // 10 instead of 9 because 10 doesnt count


you ask the input from a player and proces it


if(key_pressed=="1")
spot[1]==current_player; // current player is an int holding the....


and you print it


cout << spot[1] << spot[2] << spot[3] << endl;
cout << spot[4] << spot[5] << spot[6] << endl;
cout << spot[7] << spot[8] << spot[9] << endl;


That just about covers the basics.

Eibro
01-26-2003, 12:38 PM
No, you'd still use spot[9]...
0-8 = 9 elements, while 9th holds the NULL terminator.

CheesyMoo
01-26-2003, 12:55 PM
Is goto statement for C++ like in BASIC? I'm trying to think of a way to have it ask for the input again and go back to the switch statement without writing it over and over alot.

Vber
01-26-2003, 12:56 PM
TicTacToe - you can have a 2d array

char board[3][3];


And then make a function to print the board,
another function to get data and put in the array, another to check if we have a winner and so on...

Not too hard.
Hmm A nice game is: "The Game of the life".
If you wan't some info about him, tell.

CheesyMoo
01-26-2003, 12:59 PM
Nevermind, I fixed it.

CheesyMoo
01-26-2003, 03:25 PM
I've been workin' for a while and I got it to run and the only problem is, it asks for only player ones input, but I think I can figure it out, I just need a rest thanks for the help! I think in a couple of days I'll have my first working game!

Magos
01-26-2003, 03:42 PM
Originally posted by Eibro
No, you'd still use spot[9]...
0-8 = 9 elements, while 9th holds the NULL terminator.
Only strings have a Null terminator, not 'normal' arrays.

Travis Dane
01-26-2003, 03:45 PM
Originally posted by CheesyMoo
Is goto statement for C++ like in BASIC?

....there is....BUT YOU MUST NOT USE IT!!!! NEVER!!!
Use while,do or for loops instead

CheesyMoo
01-27-2003, 04:38 PM
I finished it, got out most of the bugs and here it is, I think I can upload it... I don't really know how but give me your thoughts on it and any bugs you find thanks.

Travis Dane
01-27-2003, 04:56 PM
I stumbled among some problems:

You're using the old <iostream.h>, The new one is <iostream>

You don't have 'using namespace std' but you don't specify them
with std functions either.

You're using the Boreland clscr(); command, i replacement is '
system("cls");'.

The variable 'uno' doesn't seem to be initilized.

CheesyMoo
01-27-2003, 05:03 PM
My compiler won't recognize namespace std... everytme I type it in it says, bad initilazation or something like that...

Travis Dane
01-27-2003, 05:05 PM
Originally posted by CheesyMoo
My compiler won't recognize namespace std... everytme I type it in it says, bad initilazation or something like that...

You changed <iostream.h> to <iostream>?
You must use 'using namespace std'.

CheesyMoo
01-27-2003, 05:11 PM
I type in #include <iostream>
using namespace std;

It says, can't find IOSTREAM
Bad syntax,declaration error(for using namespace)

Travis Dane
01-27-2003, 05:14 PM
Hm, specify your compiler.

CheesyMoo
01-27-2003, 05:19 PM
Borland Tubro C++ 4.5, maybe I don't have a setting turned on or something.

Travis Dane
01-27-2003, 05:21 PM
It might be possible you have an old compiler, if so your program
cannot compile without few changes on other newer compilers.
But after changing some things it worked fine.

-=SoKrA=-
02-02-2003, 07:18 AM
You should check the if the user input is a number. I typed quit (dunno why I just felt like it) and the program went into an infinite loop telling me choose another number.

Travis Dane
02-02-2003, 08:38 AM
Originally posted by -=SoKrA=-
You should check the if the user input is a number. I typed quit (dunno why I just felt like it) and the program went into an infinite loop telling me choose another number.

Another good alternative i like to do is writing my own CIN,
It really isn't that hard. Sokra how about saying how to filter
out numbers? (i don't use console apps, so i dont know too much
about them).

-=SoKrA=-
02-02-2003, 09:02 AM
Originally posted by Travis Dane
Sokra how about saying how to filter
out numbers? (i don't use console apps, so i dont know too much
about them).
http://cboard.cprogramming.com/showthread.php?s=&threadid=31654
this is a thread about this but if you want more specific...
Ok, here's some code to check the input of numbers:

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
char input;
cout<<"Please input a number (or q to quit): ";
cin>>input;
if(input == 'q')
return 0;
else if(isdigit(input))
cout<<"Thankyou for typing the number "<<input<<endl;
else
cout<<"I said a number or q. Go back to school!"<<endl;

return 0;
}

You can use this code as a guideline. You can assing input's value to an int var and use it without any problems.