提问人:Gabriella 提问时间:5/23/2022 更新时间:5/23/2022 访问量:206
c++ Gomoku 板未填充第二种算法的所有位置
c++ Gomoku board not filling all positions of second algorithm
问:
我是编码的新手,也是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_ */
答: 暂无答案
评论
int arr[][15]
std::vector
fieldIndex = rowIndex * columnWidth + columnIndex
movePlayerTwo
randCol(size);
size
rand
rand() % someMaximum
rand() * maximum / RAND_MAX