| View previous topic :: View next topic |
| Author |
Message |
eagles358 Expert Cheater
Reputation: 0
Joined: 10 Apr 2007 Posts: 174
|
Posted: Fri Jun 22, 2007 9:53 am Post subject: Some simple C++ games |
|
|
I just started learning C++ yesterday, so dont expect much .
This is a simple Rock Paper Scissors game i made.
| Code: | #include <iostream>
using namespace std;
int main ()
{
int rock = 1;
int paper = 2;
int scissors = 3;
int i = 0;
int Y = 1;
int N = 0;
int A = 1;
cout<<"WELCOME to Eagle358's Rock, Paper, Scissors, Game!\n\n\n\n";
Game:
srand((unsigned)time(0));
int compchoice = ((rand()%3)+1);
cout<<"Enter 1 for rock, 2 for paper, and 3 for scissors.\n\n";
cin>> i;
if ( (i != 1) && (i != 2) && (i != 3) ) {
cout<< "Invalid number, please enter 1, 2, or 3.\n\n";
goto Game;
}
else
switch (i)
{
case 1:
if (compchoice == rock){
cout<<"Rock doesnt beat rock. DRAW!\n\n";}
else if (compchoice == paper){
cout<<"Paper beats rock. LOSE!\n\n";}
else{
cout<<"Rock beats scissors. WIN!\n\n";}
break;
case 2:
if (compchoice == rock) {
cout<< "Paper beats rock. WIN!\n\n";}
else if (compchoice == paper) {
cout<<"Paper doesnt beat paper. DRAW!\n\n";}
else {
cout<<"Scissors beat papaer. LOSE!\n\n";}
break;
case 3:
if (compchoice == rock) {
cout<<"Rock beats scissors. LOSE!\n\n";}
else if (compchoice == paper) {
cout<<"Scissors beat paper. WIN!\n\n";}
else{
cout<<"Scissors dont beat scissors. DRAW!\n\n";}
break;
}
cout<<"Would you like to play Rock Paper Scissors again? 1 for YES and 0 for NO. (1 or 0)\n";
cin>>A;
if (A == 1) {
goto Game; }
else {
system("pause"); }
} |
And here is a number guessing game i made:
| Code: | #include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
srand (time(0));
int number = rand() %100 + 1;
int tries = 0, guess;
cout<<"The Guessing Game! Made by Eagles358\n\n";
do
{
cout<<"Guess a number between 1 and 100.\n\n";
cin>> guess;
++tries;
if (guess > number)
cout<< "Too high! Try again.\n\n";
if (guess < number)
cout<< "Too low! Try again.\n\n";
}
while (guess != number);
cout<<"You got it! It took you "<<tries<<" guesses.\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
|
Both were made with my limited knowledge of C++ so Im sure they could be improved. anyway, if you have any suggestions on ways to improve them or ideas for another beginner program or game that i should try to make next, it would be apprieciated.
Also made temperature converters, these were easy also.
*Edit* i combined them together.
| Code: | #include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
float C;
float F;
int A;
cout<< "Would you like to convert Farenheit to Celcius (1) or Celcius to Farenheit (2) ? (1 or 2)?";
cin>>A;
if (A == 1) {
cout << "What is the Farenheit temperature you want converted into Celcius?";
cin>> F;
C = (F-32)*.55555555555;
cout <<"" <<F<< " degrees Farenheit is equal to "<<C<<" degrees Celcius.\n";
system("PAUSE");
return EXIT_SUCCESS;
}
else if (A==2) {
cout << "What is the Celcius temperature you want converted into Farenheit?";
cin>> C;
F = (C*1.8) + 32;
cout <<"" <<C<< " degrees Celsius is equal to "<<F<<" degrees Farenheit.\n";
system("PAUSE");
return EXIT_SUCCESS;
}
}
|
Last edited by eagles358 on Fri Jun 22, 2007 10:19 am; edited 2 times in total |
|
| Back to top |
|
 |
Icos Grandmaster Cheater
Reputation: 0
Joined: 12 May 2007 Posts: 524
|
Posted: Fri Jun 22, 2007 10:00 am Post subject: |
|
|
| Using labels in a simple program like this? I think thats a bad habit. |
|
| Back to top |
|
 |
ravicus Master Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Fri Jun 22, 2007 10:04 am Post subject: |
|
|
does this compile?
while (guess != number);
cout<<"You got it! It took you "<<tries<<" guesses.\n\n"; _________________
|
|
| Back to top |
|
 |
Icos Grandmaster Cheater
Reputation: 0
Joined: 12 May 2007 Posts: 524
|
Posted: Fri Jun 22, 2007 10:06 am Post subject: |
|
|
| It should compile, although it seems there is something wrong with it. |
|
| Back to top |
|
 |
eagles358 Expert Cheater
Reputation: 0
Joined: 10 Apr 2007 Posts: 174
|
Posted: Fri Jun 22, 2007 10:06 am Post subject: |
|
|
You mean the rock paper and scissors labels? Yea i could have done without them and just used 1 2 and 3. good point.
| ravicus wrote: | does this compile?
while (guess != number);
cout<<"You got it! It took you "<<tries<<" guesses.\n\n"; |
Yes |
|
| Back to top |
|
 |
Icos Grandmaster Cheater
Reputation: 0
Joined: 12 May 2007 Posts: 524
|
Posted: Fri Jun 22, 2007 10:08 am Post subject: |
|
|
I mean this:
|
|
| Back to top |
|
 |
eagles358 Expert Cheater
Reputation: 0
Joined: 10 Apr 2007 Posts: 174
|
Posted: Fri Jun 22, 2007 10:11 am Post subject: |
|
|
Oh.
yea i guess it was a "lazy" way to do it.
How would you suggest doing that? |
|
| Back to top |
|
 |
Icos Grandmaster Cheater
Reputation: 0
Joined: 12 May 2007 Posts: 524
|
Posted: Fri Jun 22, 2007 10:16 am Post subject: |
|
|
int main()
{
while(1)
{
//game routine here
//ask user if they want to quit
//if yes, break
};
return 0;
} |
|
| Back to top |
|
 |
eagles358 Expert Cheater
Reputation: 0
Joined: 10 Apr 2007 Posts: 174
|
Posted: Fri Jun 22, 2007 10:20 am Post subject: |
|
|
| Icos wrote: | int main()
{
while(1)
{
//game routine here
//ask user if they want to quit
//if yes, break
};
return 0;
} |
and if no, how would i get it to loop? |
|
| Back to top |
|
 |
Icos Grandmaster Cheater
Reputation: 0
Joined: 12 May 2007 Posts: 524
|
Posted: Fri Jun 22, 2007 10:24 am Post subject: |
|
|
| you are already looping that with while(1) because it is an infinite loop. |
|
| Back to top |
|
 |
eagles358 Expert Cheater
Reputation: 0
Joined: 10 Apr 2007 Posts: 174
|
Posted: Fri Jun 22, 2007 10:26 am Post subject: |
|
|
ahh, got it.
thanks very much., |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Fri Jun 22, 2007 10:45 am Post subject: |
|
|
| to get rid of the goto, put the whole thing in a for loop. |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Fri Jun 22, 2007 10:50 am Post subject: |
|
|
The improved, platform independent C version of your RPC game.
| Code: |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef enum {
C_TOOLOW,
C_ROCK,
C_SCISSORS,
C_PAPER,
C_TOOHIGH,
C_INITIALIZE
} CRPC, *pCRPC;
typedef enum {
PLAYER_WIN,
COMPUTER_WIN,
BOTH_DRAW,
FATAL_ERROR
} CWIN, *pCWIN;
typedef struct {
CRPC cPlayer;
CRPC cComputer;
CWIN cResult;
} WINSOR, *LPWINSOR;
void CalculateResults(LPWINSOR tc);
int main()
{
char playagain[260]; WINSOR winresults; int fail = 0;
memset(&playagain, 0, sizeof(playagain));
puts("Welcome to the rock, paper, scissors game.");
srand((unsigned)time(0));
winresults.cPlayer = C_INITIALIZE;
winresults.cComputer = ((rand()%3)+1);
winresults.cResult = FATAL_ERROR;
do
{
printf("Enter %d for rock, Enter %d for scissors, Enter %d for paper\n", C_ROCK, C_SCISSORS, C_PAPER);
scanf("%d", &winresults.cPlayer);
} while((winresults.cPlayer <= C_TOOLOW) || (winresults.cPlayer >= C_TOOHIGH));
CalculateResults(&winresults);
switch(winresults.cResult)
{
case PLAYER_WIN:
puts("You win! Congratulations!"); break;
case COMPUTER_WIN:
puts("The computer won! Better luck next time!"); break;
case BOTH_DRAW:
puts("The game was a draw!"); break;
case FATAL_ERROR:
puts("Something went horribly, horribly wrong."); break;
}
do {
printf("Would you like to play again? Yes or no. ");
scanf("%s", &playagain);
if (strlen(playagain) > sizeof(playagain)) {
puts("oh shi.."); exit(1);
} else {
if (!stricmp(playagain, "Yes")) {
main();
} else if (!stricmp(playagain, "No")) {
getchar();
} else {
puts("Sorry, I didn't quite get that.");
memset(&playagain, 0, sizeof(playagain)); continue;
}
}
} while(1>2);
return 0;
}
void CalculateResults(LPWINSOR tc)
{
switch(tc->cPlayer)
{
case C_ROCK:
switch(tc->cComputer)
{
case C_ROCK: tc->cResult = BOTH_DRAW; break;
case C_SCISSORS: tc->cResult = PLAYER_WIN; break;
case C_PAPER: tc->cResult = COMPUTER_WIN; break;
default: tc->cResult = FATAL_ERROR; break;
} break;
case C_SCISSORS:
switch(tc->cComputer)
{
case C_ROCK: tc->cResult = COMPUTER_WIN; break;
case C_SCISSORS: tc->cResult = BOTH_DRAW; break;
case C_PAPER: tc->cResult = PLAYER_WIN; break;
default: tc->cResult = FATAL_ERROR; break;
} break;
case C_PAPER:
switch(tc->cComputer)
{
case C_ROCK: tc->cResult = PLAYER_WIN; break;
case C_SCISSORS: tc->cResult = COMPUTER_WIN; break;
case C_PAPER: tc->cResult = BOTH_DRAW; break;
default: tc->cResult = FATAL_ERROR; break;
} break;
default: tc->cResult = FATAL_ERROR; break;
}
}
|
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Jun 22, 2007 11:00 am Post subject: |
|
|
Where did you learn C++? Book and or site? And which book and or site did you use? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Fri Jun 22, 2007 11:43 am Post subject: |
|
|
improved number guess game
| Code: |
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number, tries = 0, guess = 0;
srand ((unsigned int)time(0));
for(number = rand()%100+1;;tries++)
{
puts("Guess a number between 1 and 100");
scanf("%d", &guess); if (guess == number) break;
guess < number ? printf("Too low") : printf("Too high");
puts(", Try again\n");
}
printf("You got it! And it only took you %d tries! ", tries); getchar();
return 0;
}
|
|
|
| Back to top |
|
 |
|