Xcode 7'这里不允许使用函数定义'

时间:2016-03-12 20:05:32

标签: c++ arrays xcode loops

我的代码如下。我尝试了所有不同的选择。我仍在通过课程学习C ++,但已陷入僵局。 Xcode表示错误在行中有三个直的 *标记。例如:***代码* 任何建议都会很棒,并且正在寻找更好地理解数组的方法。

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

int board [7] [7];

void initBoard (int array1[7][7]) {
srand(time(0));
string position[7][7];
int moves = rand() % 8 + 5;
int r = 0;
int c = 0;
for (c = 0; c < 7; c++) {
    if (c == 0 || c == 6) {
        for (r = 0; r < 7; r++) {
            position[r][c] = "  ";
        }
    }
    else {
        for (r = 0; r < 7; r++) {
            if (r == 0 || r == 6) {
                position[r][c] = "  ";
            }
            else {
                position[r][c] = "_ ";
            }
        }
    }
}

***void plantBombs (int array2[7][7]) {***
r = 0;
c = 0;
int k = (rand() % 5) + 1;
int d = ((rand() % 111) % 5) + 1;
int numBombs = rand() % 4 + 3;
int bombR[numBombs];
int bombC[numBombs];
int i = 0;
while (i < numBombs){
    bombR[i] = (rand() % 71) % 5 + 1;
    bombC[i] = (rand() % 123) % 5 + 1;
    while (bombR[i] == k && bombC[i] == d) {
        bombR[i] = (rand() % 97) % 5 + 1;
        bombC[i] = (rand() % 79) % 5 + 1;
    }
    while ((i > 0) && (bombR[i-1] == bombR[i]) && (bombC[i-1] == bombC[i])){
        bombR[i] = (rand() % 213) % 5 + 1;
        bombC[i] = (rand() % 857) % 5 + 1;
    }
    position[bombR[i]][bombC[i]] = "* ";
    i = i + 1;
}

}

***void placeYou (int array2[7][7]){***
    position[k][d] = "U ";
}

***void movePlayer (int arrary3[7][7]){***
    while (moves != 0) {
        cout << "SURVIVE " << moves << " MORE MOVES!" << endl;
        for (c = 0; c < 7; c++) {
            for (r = 0; r < 7; r++) {
                cout << position[r][c];
            }
            cout << endl;
        }
        cout << "Enter direction (N/S/E/W): ";
        string direction;
        cin >> direction;
        //if (direction == "N")
        if (direction == "N"){
            if (position[k][d-1] == "  "){
                cout << "You fell off the board" << endl;
                cout << "***YOU LOSE***" << endl;
                break;
            }
            else if (position[k][d-1] == "* "){
                cout << "BANG! YOUR DEAD!" << endl;
                cout << "***YOU LOSE!***";
                break;
            }
            else {
                position[k][d] = "* ";
                position[k][d-1] = "U ";
                d = d - 1;
            }
        }

        if (direction == "E"){
            if (position[k+1][d] == "  "){
                cout << "You fell off the board" << endl;
                cout << "***YOU LOSE***" << endl;
                break;
            }
            else if (position[k+1][d] == "* "){
                cout << "BANG! YOUR DEAD!" << endl;
                cout << "***YOU LOSE!***";
                break;
            }
            else {
                position[k][d] = "* ";
                position[k+1][d] = "U ";
                k = k + 1;
            }
        }

        if (direction == "S"){
            if (position[k][d+1] == "  ") {
                cout << "You fell off the board" << endl;
                cout << "***YOU LOSE***" << endl;
                break;
            }
            else if (position[k][d+1] == "* "){
                cout << "BANG! YOUR DEAD!" << endl;
                cout << "***YOU LOSE!***";
                break;
            }
            else {
                position[k][d] = "* ";
                position[k][d+1] = "U ";
                d = d + 1;
            }
        }
        if (direction == "W"){
            if (position[k-1][d] == "  "){
                cout << "You fell off the board" << endl;
                cout << "***YOU LOSE***" << endl;
                break;
            }
            else if (position[k-1][d] == "* "){
                cout << "BANG! YOUR DEAD!" << endl;
                cout << "***YOU LOSE!***";
                break;
            }
            else {
                position[k][d] = "* ";
                position[k-1][d] = "U ";
                k = k - 1;
            }

            moves = moves - 1;
        }
        if (moves == 0){
            cout << "***YOU WIN***";
        }
        else {
            cout << "";
        }

    }
}

}

int main()
{
   initBoard(board);
    ***plantBombs(board);
    placeYou(board);
    movePlayer(board);***

    return 0;

}

0 个答案:

没有答案