Guessing game and score keeping

Need suggestions on how to keep score in a guessing game

Page 1 of 1

1 Replies - 3415 Views - Last Post: 01 December 2009 - 11:28 AM Rate Topic: -----

#1 pdxstudent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-November 09

Guessing game and score keeping

Posted 30 November 2009 - 07:07 PM

Okay, so I am looking for a way to keep score with this guessing game and I really have no idea where to start. Any suggestions would be appreciated.
#include <iostream>
#include <string>
using namespace std;
//Robin Brust
//CS 161/Homework 5



void welcome()
{
	 cout<<"Welcome to Robin's guessing game."<<endl;
	 cout<<"The object of the game is to be the user with the"<<endl;
	 cout<<"least points. Player 1 will enter a word that"<<endl; 
	 cout<<"player 2 must guess. For every incorrect guess 1 point will"<<endl;
	 cout<<"be added to player ones score. For every correct guess a point"<<endl;
	 cout<<"will be added to player twos score. If you think you know the"<<endl;
	 cout<<"solution you may enter it at anytime. Good luck"<<endl<<endl;  
	 cin.get(); 
} 

string player_one()
{ 
	char player_one[20];
	cout<<"Player 1 please enter your name"<<endl;
	cin>>player_one;
	cin.get();
	return (player_one);
}


	
  
void game_play()
{
	char word[25];//Creates memory for the word that is to be guessed
	char blank[25];//creates blank spaces
	char guess;
	cout<<player_one()<<" please enter a word or phrase with 25 or less characters"<<endl;
	int counter=0; //simple counter
	int correct=0;//1=correct answer, 0=wrong answer
	cin.getline (word,25);
	strlen(word);
	cout<<"The word is "<<strlen(word)<<" characters long!"<<endl;
	int length=strlen(word); //stores the length of the word or phrase
	for (counter = 0; counter < length; counter++)//converts the letters to uppercase
		{		
		   word[counter] = toupper(word[counter]);
	   	}
	strcpy(blank,word);//Copies the word to the 'blank' array
	for (counter = 0; counter < length; counter++) 
	{			
		   if (isalnum(word[counter])) blank[counter] = '_';	//converts characters to _'s to represent blanks 	
   		   else blank[counter] = word[counter]; 
	}
   	char player_two[20];
	cout<<"Player 2 please enter your name"<<endl;
	cin>>player_two;
	cin.get();
	
		  //play game until the 'blank' puzzle becomes the 'right' answer 	
	   do	{
		   cout<<"The current blank puzzle is: "<<blank<<"."<<endl;	
	   	   cout<<player_two<<" enter a guess."<<endl;		
		   cin>>guess;		
		   guess = toupper(guess);				  
		   for (counter = 0; counter <= length; counter++) //check guess
		   { 				
			  if (guess == word[counter]) 
			   { 				
				  blank[counter] = guess;		//fill in the puzzle with the letter 			
	   		   } 		
		   }			
					  
		}
		while (strcmp (word,blank) != 0);	 					   	
	cout<<"Winner!"<<endl;
   	cin.get();
}  
	

		 	
bool play_again()
{
	 char again;
	 cout<<"Would you like to play again? y/n "<<endl;
	 cin>>again;		 
	 cin.get();
	 return (again == 'y' || again == 'Y');
}
		
int main()
{
	welcome();
	do{
	   game_play();
	  }while (play_again());		  
	return 0;
}
 
	 



Is This A Good Question/Topic? 0
  • +

Replies To: Guessing game and score keeping

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Guessing game and score keeping

Posted 01 December 2009 - 11:28 AM

Since you want:
For every incorrect guess 1 point will
be added to player ones score. For every correct guess a point
will be added to player twos score

You can just declare two variables for keeping score like:
int playerOneScore = 0, playerTwoScore = 0;

//then when a correct guess is made (you should know where that occurs in your code) increment player 2's score:
playerTwoScore++;

//when an incorrect guess is made, increment player 1's score:
playerOneScore++;


Don't forget to reset each score to zero when they play again. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1