What Is Wrong!

closed account (jyU4izwU)
I Dont Understand!
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<windows.h>
using namespace std;

class Ship
{
    public:
    int s_horizontal; //the ships horizontal factor
    int s_vertical; //the ships vertical factor

    char s_image; //the ship image

    Ship();
};

Ship::Ship()
{
    s_vertical = 4;
    s_horizontal = 8;
    s_image = 'M';
}

class Bomb
{
    public:
    int b_horizontal; //the bombs horizontal factor
    int b_vertical; //the bombs vertical factor

    char b_image; //the bombs image

    Bomb();

    protected:
    int setHorizontal(); //sets the horizontal factor
    int setVertical(); //sets the vertical factor
};

int Bomb::setHorizontal()
{
    int tempHorizontal = (rand() % 9);
    return tempHorizontal;
}

int Bomb::setVertical()
{
    srand(time(0));

    int tempVert = (rand() % 9);
    return tempVert;
}

Bomb::Bomb()
{
    b_horizontal = setHorizontal();
    b_vertical = setVertical();

    b_image = 'X';
}

class GOLD : public Bomb
{
   public:
    GOLD();
};

GOLD::GOLD()
{
    b_image = 'G';
    b_horizontal = setHorizontal();
    b_vertical = setVertical();
}

void displayBoard(char board[9][9])
{
    for(int i = 0; i < 9; ++i)
    {
        for(int j = 0; j < 9; ++j)
        cout << board[i][j];
        cout << endl;
    }
}

void moveBomb(Bomb &theBomb, char array[9][9])
{
    int newHorizontal = (rand() % 3);
    int newVertical = (rand() % 3);

    array[theBomb.b_horizontal][theBomb.b_vertical] = '.'; //sets old position as '.'

    theBomb.b_horizontal += newHorizontal;
    theBomb.b_vertical += newVertical;

    if( (theBomb.b_horizontal > 8) || (theBomb.b_vertical > 8) )
    {
        theBomb.b_horizontal = newHorizontal;
        theBomb.b_vertical = newVertical;
    }

   array[theBomb.b_horizontal][theBomb.b_vertical] = theBomb.b_image;
}

bool checkHit(Ship theShip, Bomb theBomb)
{
    if( (theShip.s_horizontal == theBomb.b_horizontal) && (theShip.s_vertical == theBomb.b_vertical) )
        return false;

    return true;
}

bool checkWinner(Ship theShip, GOLD WinningPeice)
{
    if( (theShip.s_horizontal == WinningPeice.b_horizontal) && (theShip.s_vertical == WinningPeice.b_vertical) )
       return true;

    return false;
}

void instructions()
{
    cout << "\tGOLD MINE!\n";

    cout << "The goal of this game is to grab the the Gold 'G' before the\n";
    cout << "keeper gets you, he's moving fast and he's quick so try to keep your distance." << endl;
    cout << "The keeper looks like 'X', and you are a 'M' ";

    cout << "GOOD LUCK !" << endl;

    Sleep(5000);
}

int main()
{
    Ship tempPeice;
    Bomb theBomb;
    GOLD GoldPeice;

    char array[9][9] { {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'} };

    array[theBomb.b_horizontal][theBomb.b_vertical] = theBomb.b_image;

    array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;

    bool continueGame = true;

    instructions();

    while(continueGame)
    {
        array[GoldPeice.b_horizontal][GoldPeice.b_vertical] = GoldPeice.b_image;

        system("cls");
        displayBoard(array);

        if(checkWinner(tempPeice, GoldPeice))
        {
            cout << "Congradulations ! You WON !" << endl;
            break;
        }

        moveBomb(theBomb, array);

        if(GetAsyncKeyState(VK_RIGHT))
        {
            tempPeice.s_vertical += 1;
            if(tempPeice.s_vertical > 8)
            {
                tempPeice.s_vertical = 8;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
            else
            {
                tempPeice.s_vertical -= 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = '.';
                tempPeice.s_vertical += 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
        }

        if(GetAsyncKeyState(VK_LEFT))
        {
            tempPeice.s_vertical -= 1;
            if(tempPeice.s_vertical < 0)
            {
                tempPeice.s_vertical = 0;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
            else
            {
                tempPeice.s_vertical += 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = '.';
                tempPeice.s_vertical -= 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
        }

        if(GetAsyncKeyState(VK_UP ))
        {
            tempPeice.s_horizontal -= 1;
            if(tempPeice.s_horizontal < 0)
            {
                tempPeice.s_horizontal = 0;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
            else
            {
                tempPeice.s_horizontal += 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = '.';
                tempPeice.s_horizontal -= 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
        }

        if(GetAsyncKeyState(VK_DOWN))
        {
            tempPeice.s_horizontal += 1;

            if(tempPeice.s_horizontal > 8)
            {
                tempPeice.s_horizontal = 8;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
            else
            {
                tempPeice.s_horizontal -= 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = '.';
                tempPeice.s_horizontal += 1;
                array[tempPeice.s_horizontal][tempPeice.s_vertical] = tempPeice.s_image;
            }
        }

        continueGame = checkHit(tempPeice, theBomb);

        Sleep(200);
    }

    if(continueGame == false)
    {
        cout << "You got hit by the BOMB !" << endl;
    }

    system("pause");
    return 0;
}

C:\Users\Eleve\Downloads\new.cpp In function `int main()':
139 C:\Users\Eleve\Downloads\new.cpp expected primary-expression before "char"
139 C:\Users\Eleve\Downloads\new.cpp expected `;' before "char"
149 C:\Users\Eleve\Downloads\new.cpp `array' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
It compiled just fine for me on Windows/Code::Blocks-Mingw
Try
1
2
3
4
5
6
7
8
9
char array[9][9] = { {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'},
                       {'.', '.', '.', '.', '.', '.', '.', '.', '.'} };
closed account (jyU4izwU)
Thank You naraku9333 It Worked
Topic archived. No new replies allowed.