Business) C++ Array Application Six Wood Questions

Asked 2 years ago, Updated 2 years ago, 21 views

Hello,

I asked the programming related to the array application yesterday, but the code I worked on is not working well, so I'd appreciate it if you could take a look.

Recently, I just learned the arrangement and received the assignment of applying the arrangement. When you start, temporarily set constint N at random, and the picture below is a 2d array with N = 19, for charboard[N][N].

The program is divided into black and white teams, where B (black) starts and enters a space in the given space (as shown in the picture below H14), and W (white) gets two chances, then the test gets two chances, and then fills in the six categories.

"Draw Game!" if the last space is full and there is no neck, or "Black wins/White wins" if not. The limitation is that global variables cannot be used except constint N, which determines the board size, and at least four functions in the program, and at least two of them must contain array variables.

I'd appreciate it if you could help me figure out how to make the chords TT.

The code I just made is:

#include <iostream>
using namespace std;

const int N = 19;


void print(int arr[][N]) {

    cout << "  ";

    for (int i = 0; i < N; i++)
    {
        printf(" %c ", 65 + i);
    }
    printf("\n");

    for (int i = 0; i < N; i++)
    {
        printf(" %2d ", i);
        for (int j = 0; j < N; j++)
        {
            if (arr[i][j] == 1)
                printf(" B ");
            else if (arr[i][j] == 2)
                printf(" W ");
            else
                printf(" . ");
        }

        printf("\n");
    }
}

void input(bool turn, int arr[][N]) {
    char command[255] = { 0 };

    bool invalid = true;
    while (invalid)
    {
        if (turn)
            cout << "W";
        else
            cout << "B";
        cout << "'s turn : ";

        cin >> command;

        if (command[0] >= 65 && command[0] <= 65 + N)
        {
            // That's right
            char c = command[0] - 65;
            int a1 = int(c);

            memmove(&command[0], &command[1], sizeof(command) - 1);

            int a2 = atoi(command);

            if (arr[a2][a1] == 0)
            {
                arr[a2][a1] = turn ? 2 : 1;
                break;
            }
            else
                cout << "Invalid move. Enter again!" << endl;


        }
        else
        {
            // Wrong
            cout << "Invalid move. Enter again!" << endl;
        }
    }
}

void turn(bool* turn, int* count) {
    if (*count == 1) {
        *turn = !(*turn);
        *count = 0;
    }
    else
    {
        (*count)++;
    }
}

bool winner(int arr[][N]) {

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (arr[i][j] == 1 && arr[i][j + 1] == 1 && arr[i][j + 2] == 1 && arr[i][j + 3] == 1 && arr[i][j + 4] == 1 && arr[i][j + 5] == 1) {
                cout << "Black wins!" << endl;
                exit(0);
            }
            else if (arr[j][i] == 1 && arr[j + 1][i] == 1 && arr[j + 2][i] == 1 && arr[j + 3][i] == 1 && arr[j + 4][i] == 1 && arr[j + 5][i] == 1) {
                cout << "Black wins!" << endl;
                exit(0);
            }
            else if (arr[i][j] == 1 && arr[i + 1][j + 1] == 1 && arr[i + 2][j + 2] == 1 && arr[i + 3][j + 3] == 1 && arr[i + 4][j + 4] == 1 && arr[i + 5][j + 5] == 1) {
                cout << "Black wins!" << endl;
                exit(0);
            }
            else if (arr[i][j] == 1 && arr[i - 1][j + 1] == 1 && arr[i - 2][j + 2] == 1 && arr[i - 3][j + 3] == 1 && arr[i - 4][j + 4] == 1 && arr[i - 5][j + 5] == 1) {
                cout << "Black wins!" << endl;
                exit(0);
            }
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (arr[i][j] == 2 && arr[i][j + 1] == 2 && arr[i][j + 2] == 2 && arr[i][j + 3] == 2 && arr[i][j + 4] == 2 && arr[i][j + 5] == 2) {
                cout << "White wins!" << endl;
                exit(0);
            }
            else if (arr[j][i] == 2 && arr[j + 1][i] == 2 && arr[j + 2][i] == 2 && arr[j + 3][i] == 2 && arr[j + 4][i] == 2 && arr[j + 5][i] == 2) {
                cout << "White wins!" << endl;
                exit(0);
            }
            else if (arr[i][j] == 2 && arr[i + 1][j + 1] == 2 && arr[i + 2][j + 2] == 2 && arr[i + 3][j + 3] == 2 && arr[i + 4][j + 4] == 2 && arr[i + 5][j + 5] == 2) {
                cout << "White wins!" << endl;
                exit(0);
            }
            else if (arr[i][j] == 2 && arr[i - 1][j + 1] == 2 && arr[i - 2][j + 2] == 2 && arr[i - 3][j + 3] == 2 && arr[i - 4][j + 4] == 2 && arr[i - 5][j + 5] == 2) {
                cout << "White wins!" << endl;
                exit(0);
            }
        }
    }

}

int main() {
    int arr[N][N] = { 0 };

    bool end = false;
    bool turncheck = 0;
    int turncount = 0;

    while (!end)
    {
        print(arr);
        turn(&turncheck, &turncount);
        input(turncheck, arr);
        end = winner(arr);
    }
}

c++

2022-09-21 10:23

1 Answers

If you write down the questions in detail, it will reduce the effort of the responders and increase the chances of getting answers.

First of all,

It says end=winner(arr), but the winner function does not return the value. If there is no winner, the part that returns false is required.


2022-09-21 10:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.