-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPong.cpp
More file actions
136 lines (106 loc) · 2.73 KB
/
Pong.cpp
File metadata and controls
136 lines (106 loc) · 2.73 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <string>
using namespace std;
#include <fstream>
#include <iomanip>
#include <Windows.h>
#include "Court.h"
#include "Meta.h"
const int SLEEP = 50; // stop the execution during X miliseconds
typedef enum {
black, dark_blue, dark_green, dark_cyan, dark_red,
dark_magenta, dark_yellow, light_gray, dark_gray,
light_blue, light_green, light_cyan, light_red,
light_magenta, light_yellow, white
} tColor;
typedef struct {
tCourt court;
tScore score;
} tGame;
int menu();
void wallLines();
void setColor(tColor color);
void displayCourt(const tCourt &court);
int main() {
int option, roundWinner;
bool finish, wonGame, wonRound;
tGame game;
option = menu();
while (option != 0) {
switch (option) {
case 1:
finish = wonGame = false;
initializeScore(game.score); // Initialize the score to 0
game.court = initializeCourt(); // Set the initial Court
do {
system("cls");
displayScore(game.score);
displayCourt(game.court);
updateCourt(game.court); // //Updates the court after calling the update functions on each component
Sleep(SLEEP); // Waits for the input
wonRound = isThereAWinner(game.court);
wonGame = updateScore(game.score, game.court.roundWinner);
if (wonGame) {
system("cls");
displayScore(game.score);
cout << char(7) << setw(28) << "";
if (game.score.player1 == MAX_ROUNDS)
cout << "The player 1 wons the game";
else
cout << "The player 2 wons the game";
cout << endl;
}
else if ((!wonGame) && (wonRound)) {
game.court = initializeCourt();// Won round, reset the board
}
} while ((!finish) && (!wonGame));
break;
}
option = menu();
}
return 0;
}
int menu() {
int option;
cout << "1. Execute the game" << endl;
cout << "0. Exit" << endl;
cin.sync();
cin >> option;
cin.sync();
cout << endl;
while (option < 0 || option > 1) {
cout << "Invalid number. Try again";
cin >> option;
}
return option;
}
void displayCourt(const tCourt &court) {
setColor(white);
wallLines(); // Top Walls
for (int i = 0; i < COURT_HEIGHT; i++) {
for (int j = 0; j < COURT_WIDTH; j++) {
if (court.board[i][j] == Empty)
cout << char(32);
else if (court.board[i][j] == Ball) {
setColor(light_green);
cout << char(219);
}
else if ((court.board[i][j] == Bat) || (court.board[i][j] == Net)) {
setColor(white);
cout << char(219);
}
}
}
setColor(white);
wallLines(); //Bottom Walls
}
// Print top and bottom lines on the screen
void wallLines() {
for (int i = 0; i < COURT_WIDTH; i++)
cout << char(196);
}
// Function for coloring the characters
void setColor(tColor color) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
}