Just got introduced to C++ yesterday, and after experimenting with it for an hour or two I decided to try and make a small game using C++. I do enjoy it more than C, but it probably wouldn't have been half as enjoyable had I not learnt C first.
'Lizard, Spock' are not implemented, it's just plain 'Rock, Paper, Scissors'.
My code is not the most optimized, but it works. I'm just going to put it past me and continue to work on other C++ projects.
#include <iostream>
#include <string>
using namespace std;
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main(void)
{
string user_rps;
string answer;
int rock = 0;
int paper = 0;
int scissors = 0;
int times_played = 0;
cout << "Ready to play rock paper scissors?\n";
getline(cin, answer);
while(answer == "yes")
{
srand(time(0));
cout << "Rock...\n";
sleep(1);
cout << "Paper...\n";
sleep(1);
cout << "Scissors!\n";
sleep(1);
cout << "Okay, I've picked. Now you pick yours:\n";
getline(cin, user_rps);
times_played++;
cout << "Yours was " << user_rps << ".\n";
if(user_rps == "rock")
{
rock += 1;
}
if(user_rps == "paper")
{
paper += 2;
}
if(user_rps == "scissors")
{
scissors += 3;
}
cout << "Mine was ";
int rps = 0;
rps = rand() % 3 + 1;
switch(rps)
{
case 1:
cout << "Rock!\n";
break;
case 2:
cout << "Paper!\n";
break;
case 3:
cout << "scissors\n";
break;
}
//PC Win
if((rock == 1 && rps == 2) || (paper == 2 && rps == 3) || (scissors == 3 && rps == 1))
{
cout << "Haha! I win!\n\n";
scissors = 0;
rock = 0;
paper = 0;
}
//PC Lose
if((rock == 1 && rps == 3) || (paper == 2 && rps == 1) || (scissors == 3 && rps == 2))
{
cout << "Aww, you win!\n\n";
scissors = 0;
rock = 0;
paper = 0;
}
//Tie
if(rock || paper || scissors == rps)
{
cout << "Cat's game!\n\n";
scissors = 0;
rock = 0;
paper = 0;
}
cout << "Wanna play again?\n";
getline(cin, answer);
}
cout << "We played " << times_played << " time(s)!\n";
sleep(2);
return 0;
}
CetiX
Wednesday, January 11, 2012
Random Walk Generator in C Code
Code to generate a dunk man walking. The percentage (as stated in the commented code) for the drunk to take a step in any of the four directions are as follows:
10% chance to step backwards.
20% chance to step left or right.
50% chance to step forward.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*Stupid little function to check and change a word to plural*/
void plural_function(int number);
int main(void)
{
srand(time(0));
/*Probability of step direction in array is as follows:*/
/*50% chance of forward step. 20% of left or right step. 10% chance of backward step*/
/*1 is foward*/ /*2 is left*/ /*3 is right*/ /*4 is backward*/
int probability_array[10] = {1,1,1,1,1,2,2,3,3,4};
int player_choice = 0;
int random = 0;
int i;
int steps = 0;
printf("Press 1 to take a step. (0=quit)\n");
scanf("%i", &player_choice);
while(player_choice)
{
random = probability_array[rand() % 10 + 1];
switch(random)
{
case 1:
steps++;
printf("he stepped forward...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
case 2:
steps++;
printf("he stepped left...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
case 3:
steps++;
printf("he stepped right...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
case 4:
steps++;
printf("he stepped backward...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
}
printf("Play again? (0=quit)\n");
scanf("%i", &player_choice);
}
return 0;
}
void plural_function(int number)
{
if(number > 1)
{
printf(" %i steps ", number);
}
else
{
printf(" %i step ", number);
}
}
10% chance to step backwards.
20% chance to step left or right.
50% chance to step forward.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*Stupid little function to check and change a word to plural*/
void plural_function(int number);
int main(void)
{
srand(time(0));
/*Probability of step direction in array is as follows:*/
/*50% chance of forward step. 20% of left or right step. 10% chance of backward step*/
/*1 is foward*/ /*2 is left*/ /*3 is right*/ /*4 is backward*/
int probability_array[10] = {1,1,1,1,1,2,2,3,3,4};
int player_choice = 0;
int random = 0;
int i;
int steps = 0;
printf("Press 1 to take a step. (0=quit)\n");
scanf("%i", &player_choice);
while(player_choice)
{
random = probability_array[rand() % 10 + 1];
switch(random)
{
case 1:
steps++;
printf("he stepped forward...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
case 2:
steps++;
printf("he stepped left...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
case 3:
steps++;
printf("he stepped right...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
case 4:
steps++;
printf("he stepped backward...\nHe has taken");
plural_function(steps);
printf("in total\n\n");
break;
}
printf("Play again? (0=quit)\n");
scanf("%i", &player_choice);
}
return 0;
}
void plural_function(int number)
{
if(number > 1)
{
printf(" %i steps ", number);
}
else
{
printf(" %i step ", number);
}
}
Labels:
Code in C
Countdown Timer in C Code
Countdown timer written in the C language. Works only in command prompt. I'm an amateur programmer, so I'm not 100% sure of this, but I believe that it will not work in a graphics prompted window. This is because the code refreshes the entire command prompt to simulate a continuous countdown.
#include <stdio.h>
#include <windows.h>
/*Total time desired for countdown*/
#define MINUTES 2;
int main(void)
{
int i;
int total_minute = MINUTES;
printf("%02i:00\r", total_minute);
fflush(stdout);
total_minute--;
for(i = 59; i >= 0; i--)
{
printf("%02i:%02i\r", total_minute, i);
Sleep(1000);
fflush(stdout);
if((total_minute == 0) && (i == 0))
{
break;
}
if(i == 0)
{
total_minute--;
i = 60;
}
}
return 0;
}
#include <stdio.h>
#include <windows.h>
/*Total time desired for countdown*/
#define MINUTES 2;
int main(void)
{
int i;
int total_minute = MINUTES;
printf("%02i:00\r", total_minute);
fflush(stdout);
total_minute--;
for(i = 59; i >= 0; i--)
{
printf("%02i:%02i\r", total_minute, i);
Sleep(1000);
fflush(stdout);
if((total_minute == 0) && (i == 0))
{
break;
}
if(i == 0)
{
total_minute--;
i = 60;
}
}
return 0;
}
Labels:
Code in C
Subscribe to:
Posts (Atom)