c++ Gomoku 板未填充第二种算法的所有位置

c++ Gomoku board not filling all positions of second algorithm

提问人:Gabriella 提问时间:5/23/2022 更新时间:5/23/2022 访问量:206

问:

我是编码的新手,也是stackoverflow的新手。我正在创建一个 gomoku 游戏,需要连续获得五块才能赢得比赛。我已经初始化了电路板,2d 数组,一开始就全部归零。电路板尺寸基于输入文件。我创建了两种算法,第一种是随机的,第二种是放置一个棋子,距离它之前的移动一个街区。第二种是“更智能”的算法。我还打印了数组并进行了大量“测试”/“移动”,以查看正在采取哪条路径。 出于某种原因,第二种算法似乎没有一直放置棋子,因此当玩最大回合数以填充棋盘时,会导致出现零。 我也用过一门课。我也意识到,我最终需要将我在课堂上的职能设为私有,而不是公开。

这是输入的代码

6

这是 int main

#include <iostream>
#include <fstream>
#include "game.h"
#include <iomanip>
#include <ctime>
using namespace std;



int main() {
    string inputSize = "input.txt";
    int size =0;
    srand(time(0));

    int arr[15][15];

    game Gomuko;

    size = Gomuko.getSize(inputSize, arr);

    Gomuko.initZero(arr, size);

    Gomuko.switchPlayers(arr, size);

    Gomuko.printArr(arr,size);




    return 0;
}

班级.cpp

#include "game.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
using namespace std;

game::game() {
    // TODO Auto-generated constructor stub

}

int game::getSize(string inputFileName, int dataArray[15][15]) {
    ifstream inputData;

    int size = 0;
    int counter = 0;

    inputData.open(inputFileName);

    if (!inputData) {
        cout << "Cannot open the file \"" << inputFileName << "\"" << endl;
    }
    while (inputData >> size) {
        counter++;
    }
    cout << "There are " << counter << " board sizes in the inputFile" << endl;
    cout << "The size of the board is " << size << "x" << size << endl << endl;
    return size;
}

void game::initZero(int arr[][15], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            arr[i][j] = 0;
        }
    }
}

void game::printArr(int arr[][15], int size) {

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    cout << endl << endl;
}

int game::randRow(int size) {

    int randNoRow = 0;
    randNoRow = rand() % size;
    return randNoRow;
}

int game::randCol(int size) {

    int randNoCol = 0;
    randNoCol = rand() % size;
    return randNoCol;
}
void game::movePlayerOneFirstMove(int arr[][15], int size) {
    int randNoRow = 0;
    int randNoCol = 0;
    int counter2 = 0;

    randNoRow = randRow(size);
    cout << "randNoRow = " << randNoRow << endl;

    randNoCol = randCol(size);
    cout << "randNoCol = " << randNoCol << endl;

    cout << endl;

    arr[randNoRow][randNoCol] = 1;
    counter2++;
}

int game::findFirstMoveRow(int arr[][15], int size) {
    int positionRow;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (arr[i][j] == 1) {
                positionRow = i;
            }
        }
    }

    return positionRow;
}

int game::findFirstMoveCol(int arr[][15], int size) {
    int positionCol;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (arr[i][j] == 1) {
                positionCol = j;
            }
        }
    }

    return positionCol;
}

int game::PlayerOneSurroundRow(int arr[][15], int size) {

    int positionRow = game::findFirstMoveRow(arr, size);
    cout << "The old row is " << positionRow << endl;

    int oldRow = positionRow;
    int randChoice = 0;
    int newRow = 0;

    randChoice = (rand() % 3);
    cout << "randChoice Row case:  " << randChoice << endl;

    switch (randChoice) {
    case 0:
        newRow = oldRow - 1;
        break;
    case 1:
        newRow = oldRow;
        break;
    case 2:
        newRow = oldRow + 1;
        break;
    }
    cout << "Test2" << endl << endl;
    if (newRow > size - 1) {
        cout << "Row too big as Row is " << newRow << endl;
        newRow = newRow - 1;
        cout << "Row is now " << newRow << endl;

    }
    if (newRow < 0) {
        cout << "Row too small as row is " << newRow << endl;
        newRow = newRow + 1;
        cout << "Row is now " << newRow << endl;

    }

    cout << "The new row is: " << newRow << endl;

    return newRow;
}

int game::PlayerOneSurroundCol(int arr[][15], int size) {

    int positionCol = findFirstMoveCol(arr, size);
    cout << "The old col is " << positionCol << endl;

    int oldCol = positionCol;
    int randChoice = 0;
    int newCol = 0;

    randChoice = (rand() % 3);
    cout << "randChoice Col case: " << randChoice << endl;

    switch (randChoice) {
    case 0:
        newCol = oldCol - 1;
        break;
    case 1:
        newCol = oldCol;
        break;
    case 2:
        newCol = oldCol + 1;
        break;
    }
    cout << "Test2" << endl << endl;
    if (newCol > size - 1) {
        cout << "Col too big as is " << newCol << endl;
        newCol = newCol - 1;
        cout << "Col is now " << newCol << endl;
    }
    if (newCol < 0) {
        cout << "Col too small as is " << newCol << endl;
        newCol = newCol + 1;
        cout << "Col is now " << newCol << endl;

    }

    cout << "The new col is: " << newCol << endl;

    return newCol;
}

void game::movePlayerOne(int arr[][15], int size, int counter2) {

    int newRow = 0;
    int newCol = 0;
    int oldRow = 0;
    int oldCol = 0;
    int count3 = 0;

    if (counter2 == 0) {
        game::movePlayerOneFirstMove(arr, size);
        cout << "Test1" << endl << endl << endl;
        counter2++;
    }

    else {

        cout << "Test 3" << endl;

        newRow = game::PlayerOneSurroundRow(arr, size);
        newCol = game::PlayerOneSurroundCol(arr, size);

        if (arr[newRow][newCol] == 0 && (newRow < size) && (newCol < size)
                && (newRow > 0) && (newCol > 0)) {
            arr[newRow][newCol] = 1;
            cout << "Test4" << endl;
            cout << "randNoRow = " << newRow << endl;
            cout << "randNoCol = " << newCol << endl;

            oldRow = newRow;
            oldCol = newCol;
        }

        else if ((arr[newRow][newCol] == 1) || (arr[newRow][newCol] == 2)
                || (newRow > size) || (newCol > size) || (newRow < 0)
                || (newCol < 0)) {
            cout
                    << "There has been a match, or even out of bounds, going again. "
                    << endl << endl;

            while ((arr[newRow][newCol] == 1) || (arr[newRow][newCol] == 2)) {

                cout << "Test5" << endl;
                newRow = game::PlayerOneSurroundRow(arr, size);
                newCol = game::PlayerOneSurroundCol(arr, size);

                cout << "randNoRow = " << newRow << endl;
                cout << "randNoCol = " << newCol << endl;

                count3++;
                if ((arr[newRow][newCol] != 1 && arr[newRow][newCol] != 2)
                        && (newRow < size) && (newCol < size) && (newRow >= 0)
                        && (newCol >= 0)) {
                    arr[newRow][newCol] = 1;
                    cout << "Test6" << endl;

                    cout << "randNoRow = " << newRow << endl;
                    cout << "randNoCol = " << newCol << endl;

                    oldRow = newRow;
                    oldCol = newCol;
                    break;
                }

                if (count3++ > 3) {
                    cout << "Test 7" << endl;
//                  newRow = randRow(size);
//                  newCol = randCol(size);
                    while (arr[newRow][newCol] == 1 || arr[newRow][newCol] == 2) {
                        newRow = randRow(size);
                        newCol = randCol(size);
                        cout << "Test 8" << endl;
                        cout << "randNoRow = " << newRow << endl;
                        cout << "randNoCol = " << newCol << endl;

                        if (arr[newRow][newCol] == 0) {
                            arr[newRow][newCol] = 1;
                            cout << "Test 9" << endl;
                            cout << "randNoRow = " << newRow << endl;
                            cout << "randNoCol = " << newCol << endl;
                            oldRow = newRow;
                            oldCol = newCol;
                            break;
                        }
                    }

                    break;
                }
            }

        }

//  else {
//      arr[newRow][newCol] = 1;
//  }
    }
}

void game::movePlayerTwo(int arr[][15], int size) {

    int randNoRow = 0;
    int randNoCol = 0;

    randNoRow = randRow(size);
    cout << "randNoRow = " << randNoRow << endl;

    randNoCol = randCol(size);
    cout << "randNoCol = " << randNoCol << endl;

    cout << endl;

    if ((arr[randNoRow][randNoCol] == 1) || (arr[randNoRow][randNoCol] == 2)) {
        //cout << "There has been a match, going again. " << endl << endl;
        while ((arr[randNoRow][randNoCol] == 1)
                || (arr[randNoRow][randNoCol] == 2)) {

            int randNoRow = 0;
            int randNoCol = 0;

            randNoRow = randRow(size);
            //cout << "randNoRow = " << randNoRow << endl;

            randNoCol = randCol(size);
            //cout << "randNoCol = " << randNoCol << endl;

            //cout << endl;

            if (arr[randNoRow][randNoCol] == 0) {
                arr[randNoRow][randNoCol] = 2;
                break;
            }
        }

    } else {
        arr[randNoRow][randNoCol] = 2;
    }
}

void game::switchPlayers(int arr[][15], int size) {
    int counter = 0;
    for (int i = 0; i < 36; i++) {
        cout << "This is turn " << counter + 1 << endl;
        if (counter % 2 == 0) {
            cout << "PlayerOne: " << endl;
            game::movePlayerOne(arr, size, counter);
        }
        if (counter % 2 == 1) {
            cout << "PlayerTwo:" << endl;
            game::movePlayerTwo(arr, size);
        }

        counter++;

    }

}

以及类的头文件

#ifndef GAME_H_
#define GAME_H_

#include <string>
#include <ctime>
using namespace std;

class game {
public:
    game();
    int getSize(string inputFileName, int dataArray[15][15]);
    void initZero(int arr[][15], int size);
    void printArr(int arr[][15], int size);
    void movePlayerOne(int arr[][15], int size, int counter2);
    void movePlayerOneFirstMove(int arr[][15], int size);
    void movePlayerTwo(int arr[][15], int size);
    void switchPlayers(int arr[][15], int size);
    int PlayerOneSurroundRow(int arr[][15],int size);
    int PlayerOneSurroundCol(int arr[][15],int size);
    int findFirstMoveRow(int arr[][15], int size);
    int findFirstMoveCol(int arr[][15], int size);
private:

    int randRow(int size);
    int randCol(int size);
};

#endif /* GAME_H_ */
C++ 算法 Gomoku

评论

0赞 Aconcagua 5/23/2022
只是绊倒了:将你的板固定为正好 15 列——这对我来说似乎不是一个好主意。与原始数组相比,您可能更喜欢 ,这样可以让您从任何手动内存管理中解脱出来。但是,向量的向量效率相当低(就像指针的原始数组一样),您可能更喜欢一维向量并手动 de 索引计算 ()。int arr[][15]std::vectorfieldIndex = rowIndex * columnWidth + columnIndex
0赞 Aconcagua 5/23/2022
在表达中是错误的,但 15...movePlayerTworandCol(size);size
0赞 Aconcagua 5/23/2022
您的代码非常复杂 - 更简单的是单个调用,其值范围为 0 到 7(含 0) - 然后 0 可能分别对应于少一行、少一列、少一行、同一列、少 2 到一行、多一列、3 和 4 同一行、少一列或多一列, 最后 5、6 和 7 多了一行,与 0、1、2 的列分配类似......rand
0赞 Aconcagua 5/23/2022
题外话:顺便说一句,不会产生良好的分布——只要不会发生溢出,那么会产生更好的结果。作为 C++,您可能希望首选新的 C++ 伪随机数工具(从 C++ 11 开始)。rand() % someMaximumrand() * maximum / RAND_MAX
0赞 Aconcagua 5/23/2022
只是随机选择任何场地(或任何邻居),然后重新选择,如果被占用,可能会导致太多的碰撞,使棋盘越满,游戏速度就越慢。推荐一种新的字段选择方法: 1.随机:记住有多少行有空闲字段,并使用该行数作为最大随机值的输入,然后遍历行数,直到达到随机值,完全忽略不再有空闲字段的行。然后,您可以类似地选择列,忽略占用的字段。

答: 暂无答案