gpt4 book ai didi

c++ - NxN 尺寸棋盘上的井字游戏赢家

转载 作者:行者123 更新时间:2023-11-30 03:43:47 28 4
gpt4 key购买 nike

有一个 N×N 的井字棋盘,有 2 个玩家。这个游戏不同于普通的井字游戏,因为只有当棋盘被完全填满时游戏才会结束。连续三个获得一分,连续四个获得两分,连续五个获得三分,并且很快。 “成一行”可以表示水平、垂直或对角线。给定一个完整的棋盘,尽可能高效地根据得分最多的人确定获胜者。

这是我完成的代码。我想知道是否有更好的方法来做到这一点。只需将顶部全局变量中的“N”更改为一个数字,您就应该能够在任何电路板尺寸下运行它。

const int boardSize = N;

//used for calculating all points except for diagonals with a negative slope
void counter(vector<<vector>> &ticTacToe, int &score1, int &score2, int wid, bool horz, bool diag) {
int count = 0, hei = 0;
if (horz != diag)////allows us to traverse the board vertically instead of horizontally
swap(wid, hei);
while (hei < boardSize && wid < boardSize) {
while (hei < boardSize && wid < boardSize && ticTacToe[wid][hei]) {
count++;
if (horz) {
wid++;
if (diag)//if traversing diagonally, need to increment both width & height
hei++;
}
else {
hei++;
if (diag)//if traversing diagonally, need to increment both width & height
wid++;
}
}
if (count > 2) {
score1 += (count - 2);
}
count = 0;

//only difference from above is ticTacToe[wid][hei]
while (hei < boardSize && wid < boardSize && !ticTacToe[wid][hei]) {
count++;
if (horz) {
wid++;
if (diag)
hei++;
}
else {
hei++;
if (diag)
wid++;
}
}
if (count > 2) {
score2 += (count - 2);
}
count = 0;
}
if (horz !& diag) { //should change?
swap(wid, hei);
}
}


//used for calculating diagonal points with a diagonal slope
void negSlopeDiagonal(vector<vector<int>> &ticTacToe, int &score1, int &score2, int wid, bool horz) {
int count = 0, hei = boardSize - 1;
if (!horz) {
hei -= wid;
wid = 0;
}
while (wid < boardSize && hei >= 0) {
while (wid < boardSize && hei >= 0 && ticTacToe[wid][hei]) {
count++;
hei--;
wid++;
}
if (count > 2) {
score1 += (count - 2);
}
count = 0;
while (wid < boardSize && hei >= 0 && !ticTacToe[wid][hei]) {
count++;
hei--;
wid++;
}
if (count > 2) {
score_P2 += (count - 2);
}
count = 0;
}
}


bool winner(vector<vector<int>> ticTacToe) {
int score_P1 = 0, score_P2 = 0;

//count points vertically
for (int wid = 0; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, false, false);
}

//count points horizontally
for (int wid = 0; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, true, false);
}

//counts diagonal points sloping up going vertical
for (int wid = 0; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, false, true);
}

//counts diagonal points sloping up going horizontal
for (int wid = 1; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, true, true);
}

//counts diagonal points sloping down going horizonatal
for (int wid = 0; wid < boardSize; wid++) {
negSlopeDiagonal(ticTacToe, score_P1, score_P2, wid, true);
}

//counts diagonal points sloping down going vertical
for (int wid = 1; wid < boardSize; wid++) {
negSlopeDiagonal(ticTacToe, score_P1, score_P2, wid, false);
}
cout << "P1: " << score_P1 << endl << "P2: " << score_P2 << endl;
return (score_P1 > score_P2);
}


void printBoard(const vector<vector<int>> gameboard) {
assert(gameboard.size() == boardSize && gameboard[0].size() == boardSize);
for (int j = boardSize - 1; j >= 0; j--) {
for (int i = 0; i < boardSize; i++) {
cout << gameboard[i][j] << " ";
}
cout << endl;
}
}

int main() {
vector<int> empt; //empty vector for initialization
vector<vector<int>> board(boardSize, empt);

for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
board[i].push_back((bool)(rand() % 2));
}
}
printBoard(board);
if(winner(board)) {
cout << "P1 wins!\n";
}
else {
cout << "P2 wins!\n";
}
return 0;
}

最佳答案

您可以使用方向 vector 来遍历棋盘。例如:

int traverse_and_calculate(const vector< vector<int> > &board, int start_row, int start_column, int inc_row, int inc_col, int length)
{
for (int row = start_row, column = start_column, step = 0; step < length; row += inc_row, col += inc_col)
{

}
}

并且通过这种方式,您可以轻松地在一个或两个循环中处理所有情况。

关于c++ - NxN 尺寸棋盘上的井字游戏赢家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35883649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com