通过2D阵列C 移动锯齿ZAG模式的数字

Moving a number in a zig zag pattern through a 2D array C++

本文关键字:ZAG 模式 数字 2D 阵列 移动 通过      更新时间:2023-10-16
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std; 
//function protypes 
void gameBoard(); //prints board
void diceRoll();
void move();
//global variables
int roll;
int playerOne = 0; 
int playerTwo = 99;
int board[5][8] = {{33,34,35,36,37,38,39,40},
           {32,31,30,29,28,27,26,25},
           {17,18,19,20,21,22,23,24},
           {16,15,14,13,12,11,10, 9},
           { 1, 2, 3, 4, 5, 6, 7, 8}};
void diceRoll() { //asks player to input a charater to roll a dice 
    srand(time(0));
    roll = ((rand() % 6) + 1); //sets num to a random number between 1 and 6
   }
void gameBoard(){ // prints game board
for (int i = 0; i < 5; ++i){
    for (int j = 0; j < 8; ++j){
        cout << board[i][j] << "  ";
        if (board[i][j] <= 8){
            cout << " ";
        }
    }
    cout << endl << endl;
}
} 
void move(int player, int& colPos, int& rowPos){
int tempCol; 
int tempRow;
int previous;
for (int i = 0; i <= 2; i++){
    if(i % 2 == 1){
        tempCol = colPos + roll;
        colPos = tempCol; 
        tempCol = colPos - roll;
        if(colPos > 7){
            colPos = 7;
            rowPos--;
        }
    board[rowPos][colPos] = player; 
}
}
} 

int main() {    
int turn = 1;
int colPos1 = 0;
int rowPos2 = 4;
int colPos1 = 0;
int rowPos2 = 0; 
 while(winner == false){
    if(turn == 1){
        turn = 2;                                  //allows to switch back and forth bewteen turns
        diceRoll(player1);                        //rolls die
        move(playerOne, colPos1, rowPos2);
        gameBoard();                              //prints game board
    }else{
        turn = 1;                                   //allows to switch back and forth bewteen turns
        diceRoll(player2);                         //rolls die
        move(playerTwo, colPos2, rowPos2);
        gameBoard();                              //prints game board
                }
}

return 0;
}

因此,上面的代码适用于滑道和梯子游戏。上面的程序应在没有任何错误的情况下运行。我几乎已经完成了此代码,但是在移动功能上有问题。在这里,我试图在每个玩家掷骰子时尝试通过2D阵列(游戏板)。当前代码有两个问题。玩家将一个在移动的空间之后,他们先前留下的空间仍然标有玩家的标记。此外,一旦它穿过整个行,就不会迈向下一行。感谢您提前的帮助。

注意:我删除了很多代码以使其与问题更相关,因此现在可能会有错误。

"在玩家移动一个空间后,他们先前留下的空间仍在播放器上标记为

move(int player, int& colPos, int& rowPos)中,您已经知道您要更新的播放器以及当前的行和列位置,因此您可以将其用作删除当前板上件上的标记并将其设置回其默认值的机会,例如。一旦您进入move调用void ResetBoardPositition(int colPos, int rowPos)之类的东西,然后使用播放器标记更新新位置(或者,您根本无法更改游戏板,并且只需在绘制阶段打印玩家位置,因为您仍然存储他们的位置)

"一旦穿过整个行,就不会前进到下一行"

您的代码没有采取任何行动来考虑您每次更改行时都需要扭转方向的事实。每次经过第7列时,您只需将列设置为7并减少行即可。这意味着您的下一个卷将立即将您再次超过列阈值,将其设置为7并减少行计数。您需要使用某种形式的方向修改器进行卷,因此您有类似的东西:

int direction = 1;
if (rowPos % 2 == 1)
    direction = -1;
tempCol = colPos + (roll * direction);
colPos = tempCol;
tempCol = colPos - (roll * direction);

您对Colpos的支票也需要考虑到这一点

if (colPos > 7) {
    colPos = 7;
    rowPos--;
} else if (colPos < 0) {
    colPos = 0;
    rowPos--;
}

还有一个问题,您在这里不考虑完整卷)您只将1个空间移至下一行,然后停在那里。

希望这些回答您的主要问题,但我认为这里还有其他逻辑问题。

要回答您的问题,以前空间中仍然有该播放器名称的原因是因为该值在玩家离开后永远不会重置。您可以做的是在玩家到达那里之前为当前位置创建一个INT,将播放器移至新位置,然后将旧位置设置为您创建的INT。我看到您的移动功能具有以前的int,所以只需使用它。

第二,当我运行它时,玩家将通过第一行,然后向上移动最后一列。从我所看到的,您的移动功能只能操纵tempcol和colpos,但不能操纵temprow或rowpos。移动这些将帮助玩家在行之间移动。

另外,在附带注意事项上,我看到了一些供您注意的东西。您将功能称为

void diceRoll();

但是,当您定义它时,您会这样写:

void diceRoll(string name) { //asks player to input a charater to roll a dice 

我建议宁愿添加您的参数,或者声明它或仅声明并立即定义它。

第二,您可能没有太多搞砸了,但是checkwinner()函数不会改变赢家的价值。

if (pos1 || pos2 != board[0][7]) {
    winner == false;
}
else {
    winner == true;
}

使用==是用于检查值,而不是为分配,因此取出其中一个。