My First Game!

Russian Roulette! My first non-graphic game. This is a 2 player game, so grab a friend and enjoy!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//First Game!
//Coder: FireCoder.
//Date: 9/2/15
//Version 1.3
/*
Cleaned Code.
Now able to add multible players.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>

void checkSurvival(int i); //To print your death message.

int main()
{
    bool dead = false;
    int slot, player = 1, players = 2;
    const int BULLET_POS = 2; //Unchangable do to request.
    
    srand(time(0)); //Changes seed every secound.
    
    std::cout << "How many players?\t"; //Sets players.
    std::cin  >> players;
    std::cin.ignore(); // This is to make sure after you press "Enter" it will not
                              // miss fire.
    while(dead == false) //Game only works while alive.
    {
        slot = (rand() % 6) + 1; //What slot will be fired.
        
        std::cout << "Press 'Enter' to pull trigger...";
        std::cin.ignore(); //We only need a non-spesific input.
        
        if(BULLET_POS != slot) //If bullet was not shot.
        {
            std::cout << "*click*\n";
            checkSurvival(0); 
            if(player != players) //If Player 1 finished last.
            {
                player++;
            }
            else //If player 2 finished last.
            {
                player = 1;   
            }
            
            std::cout << "Player "<< player <<": your turn.\n"; 
        } 
        else //If bullet was shot.
        {
            
            std::cout << "BANG!!!\n";   
            checkSurvival(1);
            dead = true; //Ends game.
        }
    }
    
    std::cout << "Player "<< player <<" died...\n"; //Game ending message.
} 

void checkSurvival(int i) //To print your death message.
{
    if(i == 0) //If survived.
    {
        std::cout << "You survived for now...\n" ;
    }
    else //IF died.
    { 
        std::cout << "R.I.P.\n";
    }
}
Last edited on
Works great! :D I prefer playing as player 2 because it improves my chances of survival.
You should not reset slot and position each iteration. In this case it provides even chances for both players.
@Peter87 Thanks!
@MiiNiPaa The randomization is a sim of the barrel spin in between turns.
There are variations of that game. I am just saying that not-spinning-between-turns is fair version. Another approach would be spinning barrel after each pair of turns.
@MiiNiPaa You have a point. Here is a updated version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//First Game!
//Coder: FireCoder.
//Date: 9/2/15
//Version 1.2
/*
Cleaned Code.
Added comments.
Re-did revolver spin.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>

void checkSurvival(int i) //To print your death message.
{
    if(i == 0) //If survived.
    {
        std::cout << "You survived for now...\n" ;
    }
    else //IF died.
    { 
        std::cout << "R.I.P.\n";
    }
}

int main()
{
    bool dead = false;
    int slot, player = 1;
    const int BULLET_POS = 2; //Unchangable do to request.
    
    srand(time(0)); //Changes seed every secound.
    
    while(dead == false) //Game only works while alive.
    {
        slot = (rand() % 6) + 1; //What slot will be fired.
        
        std::cout << "Press 'Enter' to pull trigger...";
        std::cin.ignore(); //We only need a non-spesific input.
        
        if(BULLET_POS != slot) //If bullet was not shot.
        {
            std::cout << "*click*\n";
            checkSurvival(0); 
            if(player == 1) //If Player 1 finished last.
            {
                player++;
            }
            else //If player 2 finished last.
            {
                player--;   
            }
            
            std::cout << "Player "<< player <<": your turn.\n"; 
        } 
        else //If bullet was shot.
        {
            
            std::cout << "BANG!!!\n";   
            checkSurvival(1);
            dead = true; //Ends game.
        }
    }
    
    std::cout << "Player "<< player <<" died...\n"; //Game ending message.
} 
Last edited on
I like that part :

if(player == 1) //If Player 1 finished last.
{
player++;
}
else //If player 2 finished last.
{
player--;
}
Thanks.
closed account (18hRX9L8)
You should add support for multiple players. That would be fun.
Last edited on
I will post a better version soon with your suggestion!
closed account (E0p9LyTq)
Another improvement could be using one of the C++ random number engines instead of the C generator.
Thanks, I am going to work on something else, but I don't know what.
Topic archived. No new replies allowed.