first game help

Started by
5 comments, last by Limitz 17 years, 7 months ago
oi i'm learning c++ and have made my first (simple)game its a rock paper sissors game where 2 players choose their mode and then the computer will see who wins. first question is: when the players input they mode([1]rock [2]paper or [3]sissor) they can se it on screen. now i use cin >> to get the input but is there a function to get the info without showing it onscreen? second question: is the coding any good? here it is

#include "stdafx.h"
#include <iostream>

//player class
class speler {
public:
	speler();      
    ~speler();    
	char naam;
	void zetscore(int);
	int getscore();
private:
	int score;
};

speler::speler(){
score=0;
}

speler::~speler(){
}

void speler::zetscore(int newscore){
score=score+newscore;
}

int speler::getscore(){
return score;
}

//the match function gets the 2 input and gives the name of the winner back
int match(int inputp1,int inputp2){
int winner;		//player1 = 1 player2 = 2

	if (inputp1==1&&inputp2==3) {
	winner=1;
	}else if(inputp1==2&&inputp2==1){
	winner=1;
	}else if(inputp1==3&&inputp2==2){
	winner=1;
	}else if(inputp1==inputp2){
	winner=3;
	}else {
	winner=2;
	}
		if (inputp1>=4||inputp2>=4){
		winner=0;
		}
return winner;
}

void start(){
	printf( "bonjour\n" );
printf( "this is THE rock paper sissors simulator \n" );


}
void choice(){
printf( "the choices are\n" );
printf( "1 = ROCK\n2 = PAPER\n3 = SISSORS\n" );
}

int game(){
system("CLS");
speler player1;
	speler player2;
	player1.naam='a';
	player2.naam='b';

	int inp1;
	int inp2;
	int win;
	int ronde=1;
start();
system("PAUSE");
do{
	
	choice();
	std::cout << "-------RONDE "<< ronde <<"-------\n\n" << std::flush ;
printf( "de keuze van player1 \n" );
std::cin >> inp1;

printf( "de keuze van player2 \n" );
std::cin >> inp2;

win=match(inp1,inp2);
if(win==0){
	std::cout << "an idiot entered the wrong number!!!\nSTART OPNIEUW\n";
}else if(win==1){
	player1.zetscore(1);
	std::cout <<"de winnaar:  player"<< match(inp1,inp2) <<" \n";
}else if(win==2){
	player2.zetscore(1);
	std::cout <<"de winnaar:  player"<< match(inp1,inp2) <<" \n";
}else if(win==3) {
	std::cout << "!!!!!!!!!!DRAW!!!!!!!!!!";
}

ronde++;
printf( "for next round " );
system("pause");
system("CLS");

}while(ronde<=3);

std::cout <<"the endscore is:\nplayer1 has won "<< player1.getscore() << " games\n";
std::cout <<"player2 has won "<< player2.getscore() << " rounds\n";
system("pause");
	return 0;
}





int _tmain()
{
system("CLS");
	int cont=1;
	do{
	int menuid;
	std::cout << "welcome\n";
	std::cout <<"             menu               \n";
	std::cout <<"________________________________\n";
	std::cout <<"|                               |\n";
	std::cout <<"|         [1] start game        |\n";
	std::cout <<"|         [2] stop              |\n";
	std::cout <<"|                               |\n";
	std::cout <<"|_______________________________|\n";

	std::cin >> menuid;
	
	switch (menuid){
	case 1:
		game();
		break;
	case 2:
		cont=0;
		break;
	};


	}while(cont==1);

return 0;	
}





so...
Advertisement
Im not sure about the input not showing on the screen;

The code is good if it gets the job done and understandable. That being said, I'd use a select case instead of the nested ifs.

Goes something like..
Select variable{case 1: //do codebreak;case 2: //do codebreak;//etc etc etcdefault: break;}
Quote:Original post by hjarie
oi
i'm learning c++ and have made my first (simple)game
its a rock paper sissors game where 2 players choose their mode and then the
computer will see who wins.
first question is: when the players input they mode([1]rock [2]paper or [3]sissor)
they can se it on screen. now i use cin >> to get the input but is there a function to get the info without showing it onscreen?

second question: is the coding any good?

here it is
*** Source Snippet Removed ***


For your first question: afaik this isn't possible in console mode :).

Code comments :
you should use an initialization list for your constructor ininstead of setting the variables inside of them. So you get:
speler::speler() : score(0) { }


In c++ most coders use cout instead of printf (as printf is actually c-code), but it's no real error offcourse.

also, your score name is not very perfect (I'm dutch myself), you now use a name "set score", which would actually mean: replace existing score with this one, a better name for example is addPoints. Even better would be, as in this game you'll always add one point: addPoint(), without arguments.

Other points: your code is to much intertwined in my opinion, you should seperate things more (output in one place, game calculation in one place), but that's very normal for your first games :).
You also use a lot of if/else, sometimes (not always) you could replace them with a switch structure.
In my opinion it isn't good to make any member public (except some rare cases where constant use of setters & getters would be unusefull), so I'd make 'naam' private also.

I hope this comment was a bit helpful.
Can't say that I like all of your coding sytle. The format is squished together; it almost looks like you're trying to save lines.

Here's a slightly reformatted part of your code:
int match( int inputp1, int inputp2 ){	int winner;	if( inputp1 == 1 && inputp2 == 3 ){		winner=1;	}	else if( inputp1 == 2 && inputp2 == 1 ){		winner=1;	}	else if( inputp1 == 3 && inputp2 == 2 ){		winner=1;	}	else if( inputp1 == inputp2 ){		winner=3;	}	else{		winner=2;	}	if( inputp1 > = 4 || inputp2 > = 4 ){		winner=0;	}	return winner;}


The advantage of this is that it more accurately shows which lines are subordinate to which lines. Everything inside the function is indented once. Everything inside an if() is indented again. Also, consistently spacing between individual parts of a function call or if test will make it more readable.

And you could output 40 newlines to clear the input. Kinda a cheap way, but it should work for default-size inputs. Of course, they could scroll up and see it, but at least it's something!
thanks all

i will code some more tomorrow to add an one player option an the option to give you own name instead of player1/2 and will clean up the game

thanks again for the comments :D
so...
I once created a console prototype that had movement without displaying the typed keys on screen. I used getch(), and rerendered the screen by flushing cout first (cout << flush;), then resetting the cursor position to 0, 0 with the following function (clear-screen caused too heavy flickering):

void level::gotoxy(int x, int y){	HANDLE hConsoleOutput;	COORD dwCursorPosition;	dwCursorPosition.X = x;	dwCursorPosition.Y = y;	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);}


Just a note on your code: while Nederlands is your native language, it's often hard to interpret code whose variabeles and comments are written in a different language. I prefer writing my code in English simply to avoid language issues. Whatever you choose, at least be consistent. ;)
Create-ivity - a game development blog Mouseover for more information.
I second the "don't use native variable names", i had to update an Indian webshop once, with native variable names, yuk :)

On your code: it looks pretty good. Maybe make rock paper scissors into an Enum for readability and as an excercise. If i would have to make this without showing text on screen, i would include <windows.h> solely for its getAsyncKeyState function. You can then even assign 3 keys for each player and they can press it together at the same time.

int state = 0 ;while (1){  bool p1Rock     = GetAsyncKeyState('Q') != 0;  bool p1Paper    = GetAsyncKeyState('W') != 0;  bool p1Scissors = GetAsyncKeyState('E') != 0;  bool p2Rock     = GetAsyncKeyState('I') != 0;  bool p2Paper    = GetAsyncKeyState('O') != 0;  bool p2Scissors = GetAsyncKeyState('P') != 0;  bool p1PressedAKey = p1Rock || p1Paper || p1Scissors;  bool p2PressedAKey = p2Rock || p2Paper || p2Scissors;  switch (state)  {    case 0:      //loop untill both players released their keys      if (!p1PressedAKey && !p2PressedAKey) {        cout << "Game started\n";        state = 1;      break;        case 1:      //both players pressed a key      if (p1PressedAKey && p2PressedAKey) {        if (p1Rock && p2Paper) p2Wins(); // or whatever etc        state=0; // play a new game      }  }  }


just an idea, not actually tested.
Greetings

This topic is closed to new replies.

Advertisement