#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阵列(游戏板)。当前代码存在两个问题。在玩家移动空间之后,他们之前留下的空间仍然标记着玩家。此外,一旦它穿过整行,它就不会前进到下一行。感谢您的帮助。
注意:我删除了大量代码,使其与问题更相关,因此现在可能有错误。