-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfrog-jumping-game.cpp
137 lines (118 loc) · 2.27 KB
/
frog-jumping-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
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>
#include <vector>
using namespace std;
const int BOARD_WIDTH = 10;
const int BOARD_HEIGHT = 10;
// Struct to represent a frog
struct Frog
{
int x;
int y;
};
// Function to print the game board
void printBoard(const vector<vector<char>> &board)
{
for (int y = 0; y < BOARD_HEIGHT; y++)
{
for (int x = 0; x < BOARD_WIDTH; x++)
{
cout << board[y][x];
}
cout << endl;
}
}
// Function to move the frog up
void moveUp(Frog &frog)
{
if (frog.y > 0)
{
frog.y--;
}
}
// Function to move the frog down
void moveDown(Frog &frog)
{
if (frog.y < BOARD_HEIGHT - 1)
{
frog.y++;
}
}
// Function to move the frog left
void moveLeft(Frog &frog)
{
if (frog.x > 0)
{
frog.x--;
}
}
// Function to move the frog right
void moveRight(Frog &frog)
{
if (frog.x < BOARD_WIDTH - 1)
{
frog.x++;
}
}
// Function to update the game board
void updateBoard(vector<vector<char>> &board, Frog &frog)
{
for (int y = 0; y < BOARD_HEIGHT; y++)
{
for (int x = 0; x < BOARD_WIDTH; x++)
{
if (y == frog.y && x == frog.x)
{
board[y][x] = 'F';
}
else
{
board[y][x] = '.';
}
}
}
}
// Function to play the game
void playGame()
{
vector<vector<char>> board(BOARD_HEIGHT, vector<char>(BOARD_WIDTH, '.'));
Frog frog = {4, 9};
board[frog.y][frog.x] = 'F';
while (true)
{
printBoard(board);
cout << "Use the w/a/s/d keys to move the frog. Press Q to quit." << endl;
char input;
cin >> input;
if (input == 'Q' || input == 'q')
{
break;
}
switch (input)
{
case 'w':
case 'W':
moveUp(frog);
break;
case 'a':
case 'A':
moveLeft(frog);
break;
case 's':
case 'S':
moveDown(frog);
break;
case 'd':
case 'D':
moveRight(frog);
break;
}
updateBoard(board, frog);
}
cout << "Thanks for playing!" << endl;
}
int main()
{
playGame();
return 0;
}