C 蛇和梯子:可视化和更新玩家位置

C 蛇和梯子:可视化和更新玩家位置,c,C,游戏从左到右从>>开始,沿行向下移动,直到>>结束。以下是印制板的图片,以供参考: 我想能够更新和打印一个球员的位置后,他们立即投骰子 我不明白我是如何“限制”位置变量的,这样当它到达一行的结束列时,它会自动转移到下一行。另一件我正在努力的事情是编辑印刷电路板,因为我已经将它制作成一个函数,所以//table[pos1][0]=219被编码出来,因为它没有意义,而且它什么也不做,因为这样的数组在main中不存在 到目前为止,我已经成功地创建了一个伪随机骰子卷,同时打印起始位置板并跟踪每个骰子卷

游戏从左到右从>>开始,沿行向下移动,直到>>结束。以下是印制板的图片,以供参考:

我想能够更新和打印一个球员的位置后,他们立即投骰子

我不明白我是如何“限制”位置变量的,这样当它到达一行的结束列时,它会自动转移到下一行。另一件我正在努力的事情是编辑印刷电路板,因为我已经将它制作成一个函数,所以//table[pos1][0]=219被编码出来,因为它没有意义,而且它什么也不做,因为这样的数组在main中不存在

到目前为止,我已经成功地创建了一个伪随机骰子卷,同时打印起始位置板并跟踪每个骰子卷。请注意,219和176是我在Player1和Player2中使用的字符

我已经用switch()语句看到了这个问题的解决方案,但我不太确定,这并不能解决我更新电路板和打印新位置的问题

#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

void printBoard();

int main(){
    //Counters
    //int i,k,p;
    //Keeps track of position
    int pos1=0;
    int pos2=0;
    //Keeps track of turns
    int rollA=0;
    int rollB=0;

    int playerTurn;
    int rRoll;
    bool gameOn;
    gameOn=true;
    srand(time(0));

    printf("Starting Position:\n");
    printBoard();

    while(gameOn==true){
        printf("Enter player number to roll dice\n");
        scanf("%d",&playerTurn);
        rRoll=(rand() % 6) + 1;

        if(playerTurn==1){
            pos1=pos1+rRoll;
            //table[pos1][0]=219;
            rollA++;
            printf("You rolled a %d \nPlayed 1: %d times   Player 2: %d times\n",rRoll, rollA, rollB);
            printBoard();

        } else if(playerTurn==2){
            pos2=pos2+rRoll;
            rollB++;
            printf("You rolled a %d \nPlayed 1: %d times   Player 2: %d times\n",rRoll, rollA, rollB);
            printBoard();

            } else {
            printf("Please choose a valid Player\n");
            }

        /*gameOn=false;
        if(gameOn==false){
            printf("Congratulations Player %d wins! The game will now terminate. \n",playerWin);

            }*/
    }
    return 0;
}

void printBoard(){
    int i,k,p;
    char table[10][10];
    for(i=0; i<10; i++){
        for(k=0; k<10; k++){
            table[i][k]=46;
                for(p=2;p<8;p++){
                    table[p][3]=186;
                }
            table[4][9]=245;
            table[5][8]=245;
            table[6][7]=245;
            table[7][6]=245;
            table[8][5]=245;

            table[2][1]=219;
            table[1][2]=176;

            printf("%c",table[i][k]);
        }
        printf("\n");
    }
}
#包括
#包括
#包括
作废印刷板();
int main(){
//计数器
//inti,k,p;
//跟踪位置
int pos1=0;
int pos2=0;
//跟踪转弯
int-rollA=0;
int-rollB=0;
int playerTurn;
内尔;
布尔加梅;
gameOn=true;
srand(时间(0));
printf(“起始位置:\n”);
印制板();
while(gameOn==true){
printf(“输入掷骰子的玩家编号\n”);
scanf(“%d”、&playerTurn);
罗尔=(兰德()%6)+1;
如果(playerTurn==1){
pos1=pos1+rRoll;
//表[pos1][0]=219;
罗拉++;
printf(“您滚动了%d次\n滚动1:%d次玩家2:%d次\n”,滚动,滚动,滚动b);
印制板();
}否则如果(playerTurn==2){
pos2=pos2+rRoll;
rollB++;
printf(“您滚动了%d次\n滚动1:%d次玩家2:%d次\n”,滚动,滚动,滚动b);
印制板();
}否则{
printf(“请选择一个有效的播放器\n”);
}
/*gameOn=false;
如果(gameOn==false){
printf(“祝贺玩家%d获胜!游戏现在将终止。\n”,playerWin);
}*/
}
返回0;
}
作废印制板(){
inti,k,p;
字符表[10][10];

对于(i=0;i这是@rykker提供的解决方案的“完成”版本。我成功地让两名玩家都朝着正确的方向前进(从左到右,而不是从上到下),设置游戏中的障碍,并创建结束游戏的条件来停止游戏,这当然不是那么具有挑战性。我计划在周四更新这个答案

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>

//Improves code readability
#define POSITIONS (100 - 1) //assign 99 positions from 0-99
#define PLYR_ICON_1 219
#define PLYR_ICON_2 176
#define SNAKE 245
#define SNAKE_BEGIN 49
#define LADDER 186
#define LADDER_END 73
#define DOT 46
#define ROWS 10
#define COLS 10
#define START_END 175
#define ZERO 48
#define ONE 49
#define TWO 50

//Creates a global enumeration PLAYER - Improves code readability
enum {
    PLAYER1,    //Enumerator; state=0
    PLAYER2,    //Enumerator; state=1
    MAX_PLAYERS //Enumerator; state=2
}PLAYER;        //enum variable

//Creates a global struct POS containing an integer member position
typedef struct {
    int position;
}POS;

//Initializes function container that can pass position values for all players
void printBoard(POS *ptr);

int main(void){

    //local position initialized at 0
    int pos1=0;//local player1 position
    int pos2=0;//local player2 position

    //bounds check variable initialized at 0
    int check_position = 0;

    //turn counter initialized at 0
    int rollA=0;
    int rollB=0;

    POS *pos_global = calloc(MAX_PLAYERS, sizeof(*pos_global)); //creates a new pointer instance of the struct with enough space for MAX_PLAYERS(or 2) elements, 4bytes (the size of a pointer) each
    //used to pass information via function argument
    //free(pos_global) when done

    char input[10]={0}; //initializing array for user input for player turn
    int rRoll;          //random roll
    bool gameOn=true;   //bool utilized in while loop
    srand(time(0));     //pseudo-random seed generator for rand() depending on time(0);


    printf("Starting Position:\n");
    //Sets both positions to 0
    pos_global[PLAYER1].position = 0;   //equivalent to pos_global[0].position = 0
    pos_global[PLAYER2].position = 0;   //equivalent to pos_global[1].position = 0
    printBoard(pos_global);             //calls printBoard function passing pos_global for both players as 0, enabling the function to set both player tags in the starting position


    while(gameOn==true) //main game loop
    {
        printf("Enter player number to roll dice\n");
        fgets(input, sizeof(input), stdin); //stores user input as characters in the array input[]
        if((isdigit(input[0]))==0){         //checks if digit is entered; isdigit returns 0 if the 1st element of the array storing the user input isn't a digit, providing some input boundaries
            printf("Incorrect input please enter digits 0, 1 or 2 \n");
        }
        else{//code to be executed if a digit is entered for the first character

        rRoll=(rand() % 6) + 1; //random dice roll: any numbers remainder when divided by 6 will be between 0-5, adding one give 1-6 random dice roll

        if(input[0]==ONE){      //code to be executed if 1 is entered: PLAYER1's TURN
            pos1=pos1+rRoll;    //new position=old position+dice roll
            rollA++;            //adds one to the turn counter
            printf("You rolled a %d \n",rRoll);

            if (pos1>POSITIONS) //winning end-game condition (if new position is bigger than 99 (last position/end of board), end the game)
            {
                printf(" *** Game over. Player 1 wins! *** \n");
                free(pos_global); //deallocates the earlier allocated memory by calloc();
            }

            check_position = POSITIONS - pos1;
            if(check_position < 0) pos1 = pos1 - POSITIONS; //if pos>99, subtract the difference to pos, providing some bounds

            //Switch used to make ladder/snake functional
            switch (pos1)
                {
                case 23:
                case 33:
                case 43:
                case 53:
                case 63:
                    pos1=LADDER_END; //whenever PLAYER1 lands on any of the ladder positions, set his position at the LADDER_END (73th position)
                    break;

                case 85:
                case 76:
                case 67:
                case 58:
                case 69:
                    pos1=SNAKE_BEGIN; //whenever PLAYER1 lands on any of the snake positions, set his position at the SNAKE_BEGIN (49th position)
                    break;
                }

                pos_global[PLAYER1].position = pos1;    //updates the pointer instance with the new position for PLAYER1
                printBoard(pos_global);                 //prints the board with newly updated pointer
                printf("Player 1: %d times   Player 2: %d times\n",rollA, rollB);
        }
        else if(input[0]==TWO)  //code to be executed if 1 is entered: PLAYER1's TURN
        {
            pos2=pos2+rRoll;    //new position=old position+dice roll
            rollB++;            //adds one to the turn counter
            printf("You rolled a %d \n",rRoll);

            if (pos2>POSITIONS)
            {
                printf(" *** Game over. Player 2 wins! *** \n");
                free(pos_global); //deallocates the earlier allocated memory by calloc();
            }

            check_position = POSITIONS - pos2;
            if(check_position < 0) pos2 = pos2 - POSITIONS; //if pos>99, subtract the difference to pos, providing some bounds

            //Switch used to make ladder/snake functional
            switch (pos2){
                case 23:
                case 33:
                case 43:
                case 53:
                case 63:
                    pos2=LADDER_END;   //whenever PLAYER2 lands on any of the ladder positions, set his position at the LADDER_END (73th position)
                    break;

                case 85:
                case 76:
                case 67:
                case 58:
                case 69:
                    pos2=SNAKE_BEGIN;  //whenever PLAYER2 lands on any of the ladder positions, set his position at the LADDER_END (49th position)
                    break;
                }

            pos_global[PLAYER2].position = pos2;    //updates the pointer instance with the new position for PLAYER2
            printBoard(pos_global);                 //prints the board with newly updated pointer
            printf("Player 1: %d times   Player 2: %d times\n",rollA, rollB);

        }
            else if(input[0]==ZERO)//exit program if 0 is entered
            {
                free(pos_global); //deallocates the earlier allocated memory by calloc();
                break;
            }
            else{
                printf("Please choose a valid Player\n");
            }

        }
    }
    free(pos_global); //deallocates the earlier allocated memory by calloc() effectively terminating the program;
    return 0;
}

void printBoard(POS *ptr)
{
    //Counters
    int i,k,p;
    //position stored by digit 64; x=6 y=4;
    int x=0,y=0;
    char table[ROWS][COLS] = {0};   //initialize array table[10][10] before use
    for(i=0; i<ROWS; i++){          //for loop #1 executing 10 times from i=0 to i=9
        for(k=0; k<COLS; k++){      //for loop #2 executing 10 times from j=0 to j=9, nested within #1 to allow the whole 2d array to be
            table[i][k]=DOT;        //filled with dots (ascii 46)
                for(p=2;p<8;p++){   //fills table[][] with LADDERs
                    table[p][3]=LADDER;
                }
            table[4][9]=SNAKE;      //fills the SNAKE on the board
            table[5][8]=SNAKE;
            table[6][7]=SNAKE;
            table[7][6]=SNAKE;
            table[8][5]=SNAKE;
            table[0][0]=START_END;  //characters used in the beginning and ending of game
            table[9][9]=START_END;
           //position digit breakdown PLAYER1
           y = ptr[PLAYER1].position%COLS; //stores the 2nd digit of the position computed by applying a modulo argument to ptr[0].position (64%10=4)
           x = ptr[PLAYER1].position/COLS; //stores the 1st digit of position computed by dividing ptr[0].position by 10
           table[x][y]=PLYR_ICON_1;        //assigns PLAYER1 icon to it's new position
           //position digit breakdown PLAYER2
           y = ptr[PLAYER2].position%COLS; //equivalent to y = ptr[1].position%10
           x = ptr[PLAYER2].position/COLS; //equivalent to x = ptr[1].position/10
           table[x][y]=PLYR_ICON_2;

            printf("%c",table[i][k]);      //prints every character stored in all the array space
        }
        printf("\n");                      //inserts a new line after every row of 10 spaces
    }
}
#包括
#包括
#包括
#包括
#包括
//提高代码可读性
#定义位置(100-1)//从0-99分配99个位置
#定义PLYR_图标_1 219
#定义PLYR_图标_2 176
#定义SNAKE 245
#定义SNAKE_开始49
#定义梯形图186
#定义梯形图U端73
#定义点46
#定义第10行
#定义COLS 10
#定义起点和终点175
#定义零48
#定义一个49
#定义两个50
//创建全局枚举播放器-提高代码可读性
枚举{
PLAYER1,//枚举器;状态=0
PLAYER2,//枚举器;状态=1
最大值//枚举数;状态=2
}PLAYER;//枚举变量
//创建包含整数成员位置的全局结构POS
类型定义结构{
内部位置;
}POS;
//初始化可以为所有玩家传递位置值的函数容器
无效印刷板(POS*ptr);
内部主(空){
//本地位置初始化为0
int pos1=0;//本地播放器1位置
int pos2=0;//本地播放器2位置
//边界检查变量初始化为0
int check_位置=0;
//翻转计数器初始化为0
int-rollA=0;
int-rollB=0;
POS*POS_global=calloc(MAX_PLAYERS,sizeof(*POS_global));//创建结构的一个新指针实例,该实例有足够的空间容纳MAX_PLAYERS(或2个)元素,每个元素4字节(指针大小)
//用于通过函数参数传递信息
//完成后免费(pos_global)
字符输入[10]={0};//初始化玩家回合用户输入的数组
int-rRoll;//随机滚动
bool gameOn=true;//while循环中使用的bool
srand(time(0));//根据时间(0)生成rand()的伪随机种子生成器;
printf(“起始位置:\n”);
//将两个位置都设置为0
位置全局[PLAYER1]。位置=0;//相当于位置全局[0]。位置=0
位置全局[PLAYER2]。位置=0;//相当于位置全局[1]。位置=0
printBoard(pos_global);//调用printBoard函数,将两个播放器的pos_global传递为0,使该函数能够将两个播放器标签都设置在起始位置
while(gameOn==true)//主游戏循环
{
printf(“输入掷骰子的玩家编号\n”);
fgets(input,sizeof(input),stdin);//将用户输入作为字符存储在数组input[]
if((isdigit(input[0]))==0){//检查是否输入了数字;如果存储用户输入的数组的第一个元素不是数字,isdigit返回0,提供一些输入边界
printf(“输入错误,请输入数字0、1或2\n”);
}
else{//如果为第一个字符输入了数字,则要执行的代码
rRoll=(rand()%6)+1;//随机掷骰:任何数字被6除后的余数都将在0-5之间,加上1将得到1-6个随机掷骰
if(输入[0]==1){//输入1时执行的代码:轮到玩家1
pos1=pos1+rRoll;//新位置=旧位置+掷骰子
rollA++;//向转向计数器添加一个
printf(“您滚动了%d\n”,错误);
if(pos1>位置)//获胜结束游戏条件(如果新位置大于99(最后位置/棋盘结束),结束游戏)
{
printf(