forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
football-game.cpp
81 lines (65 loc) · 1.8 KB
/
football-game.cpp
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
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to generate a random number between min and max (inclusive)
int random(int min, int max)
{
return rand() % (max - min + 1) + min;
}
// Function to simulate a single kick
bool kick()
{
const int goalSize = 3;
int kickPos = random(1, 10); // Randomly choose where to kick the ball
int goalPos = random(1, 10); // Randomly choose where the goal is
return abs(kickPos - goalPos) <= goalSize; // Determine if the kick was successful
}
// Function to simulate a game
int playGame()
{
const int numKicks = 5;
int score = 0;
cout << "Let's start the game!" << endl;
for (int i = 1; i <= numKicks; i++)
{
cout << "Kick " << i << ": ";
bool success = kick();
if (success)
{
score++;
cout << "GOAL!" << endl;
}
else
{
cout << "Missed." << endl;
}
}
cout << "The game is over!" << endl;
cout << "Your score: " << score << endl;
return score;
}
int main()
{
srand(time(NULL)); // Seed the random number generator with the current time
int totalScore = 0;
int numGames = 0;
while (true)
{
cout << "Welcome to the football game!" << endl;
cout << "Press enter to play, or type 'quit' to exit." << endl;
string input;
getline(cin, input);
if (input == "quit")
{
break;
}
int score = playGame();
totalScore += score;
numGames++;
cout << "Your total score after " << numGames << " games: " << totalScore << endl;
cout << "Average score per game: " << static_cast<double>(totalScore) / numGames << endl;
}
return 0;
}