Algorithm 用于蛇梯游戏的数据结构

Algorithm 用于蛇梯游戏的数据结构,algorithm,data-structures,Algorithm,Data Structures,我在采访中遇到了一个问题“建议您在snake&ladder游戏中使用的数据结构?” 我会使用2D数组(和国际象棋一样)来设计游戏的每个区块。但是否有可能设计成一维阵列?很多人都提出了这一点,但没有人解释如何做到这一点。因为蛇/斜槽和梯子的运动通常是单向的,而不是象棋中可能的多个方向,所以1D数组或列表肯定会起作用 为了表示蛇和梯子,您可以将每个列表元素的内容设置为整数,告诉游戏当您降落到计数器上时,计数器向前或向后跳过多远。例如,在Python中: # create a 5x5 board bo

我在采访中遇到了一个问题“建议您在snake&ladder游戏中使用的数据结构?”


我会使用2D数组(和国际象棋一样)来设计游戏的每个区块。但是否有可能设计成一维阵列?很多人都提出了这一点,但没有人解释如何做到这一点。

因为蛇/斜槽和梯子的运动通常是单向的,而不是象棋中可能的多个方向,所以1D数组或列表肯定会起作用

为了表示蛇和梯子,您可以将每个列表元素的内容设置为整数,告诉游戏当您降落到计数器上时,计数器向前或向后跳过多远。例如,在Python中:

# create a 5x5 board
board = [0 for i in range(25)]
# put a ladder in square 3, that moves you to square 10
board[2] = 7
# put a snake in square 14, that moves you to square 9
board[13] = -5

是的,这是可能的:每个二维数组都可以表示为一维数组

在一维数组中逐个表示二维数组的所有行。这样,
2d[i][j]
就变成了
1d[i*rowLength+j]
。除非你别无选择,只能使用一维数组,否则这通常不是一件好事,因为它的可读性和易用性都会降低。

在我的博客中,我使用了一对简单的链表来存储蛇和梯子。列表中的每个元素都有一对正方形,“from”正方形和“to”正方形;如果你降落在任何“从”广场上,你的作品将被重新定位到“到”广场。我发现最低游戏长度为7圈,平均游戏长度为33圈。您也可以使用一维数组,其中数组的索引表示“from”平方,数组中的值表示“to”平方,这与索引相同,但在蛇或阶梯的开头除外。

Vakh是正确的。
“是的,这是可能的:每个二维数组都可以表示为一维数组。”

阵列

int board[100] =
{
     0,  0,  0, 10,  0,  0,  0,  0, 22,  0,
     0,  0,  0,  0,  0,  0,-10,  0,  0, 18,
     0,  0,  0,  0,  0,  0,  0, 56,  0,  0,
     0,  0,  0,  0,  0,  0,  0,  0,  0, 19,
    17,  0,  0,-20,  0,  0,  0,  0,  0,  0,
     0,-43, 18, -4,  0,  0,  0,  0,  0,  0,
    20,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,  0,  0,  0,  0,  0,-63,  0,  0,
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,-20,  0,-20,  0,  0,  0,-21,  0
};
我能撑梯子 4->14, 9->13, 20->38, 28->84, 40->59, 51->67, 63->81, 71->91 还有蛇 17->7,54->34,62->19,64->60,87->24,93->73,95->75,99->78

如果红色位于位置2(即r=2)且得分为2(即s=2),则红色的新位置为

    2+2+board[2+2-1] = 14
i、 e

@简·德沃夏克, “锯齿阵列不是二维阵列”

使用阵列:

int SNL[100];
根据以下规则,此数组的每个元素都包含一个整数:

  • 如果存在从
    x
    x+l
    的阶梯,则
    SNL[x]=l
  • 如果在
    x
    处有蛇咬伤,并将您留在
    x-s
    ,则
    SNL[x]=-s,否则
    SNL[x]=0

  • 当然,我们可以使用1D数组解决这个问题,因为板上标记的数字是从1到100。我们可以初始化两个1D数组:snake和ladder,这两个数组的大小都是100。 如果玩家在蛇头(假设56岁),那么它必须移动到尾巴(假设6岁)。然后snake[56]=6(根据规则),玩家将移动到标记为6的方块。梯形图阵列也是如此。我已经讲述了一个案例,玩家在蛇头处导航到它的尾巴,发现了一个梯子,反之亦然。 伪代码是:

    int snake[101], ladder[101];
    void SnakeAndLadder()
    {
        int player_1=1, player_2=1, turn_player_1 = 1, a;
        while(1)
        {
            a = rand()%6 +1;
            if(turn_i==1)
            {
                turn_player_1 = 0;
                player_1 = takeStep(player_1+a);
                if(player_1==100)
                {
                    cout<<"Player 1 won";
                    break;
                }
            }
            else
            {
                turn_player_1 = 1;
                player_2 = takeStep(player_2+a);
                if(player_2==100)
                {
                    cout<<"Player 2 won";
                    break;
                }
            }
        }
    }
    
    int takeStep(int i)
    {
        if(i<100)
        {
        if(snake[i]!=0 && i!=100)
        {
            i = snake[i];
        }
        if(ladder[i]!=0 && i!=100)
        {
            i = ladder[i];
            if(snake[i]!=0 && i!=100)
            {
                i= snake[i];
            }
        }
        }
    return i;
    }
    
    intsnake[101],梯形图[101];
    虚空蛇梯()
    {
    int player_1=1,player_2=1,turn_player_1=1,a;
    而(1)
    {
    a=rand()%6+1;
    如果(圈数i==1)
    {
    把_玩家_1=0;
    玩家_1=takeStep(玩家_1+a);
    如果(玩家1==100)
    {
    
    cout为什么没有人建议使用Map作为数据结构。如果没有蛇或梯子,我们将把整数映射到它自己。如果是蛇,我们将把蛇头映射到它的尾巴,同样地,也映射到梯子。我们可以为每个玩家使用整数变量。

    蛇和梯子的规则是:

  • 游戏中有两名玩家,棋盘大小为100。(10 X 10)
  • 掷骰子的可能结果是1,2,3,4,5,6
  • 如果输出为6,则当前玩家将有机会再次掷骰子
  • 如果骰子的结果是1,2,3,4,5,6,并且玩家位于蛇的嘴上,那么他的当前位置将变为蛇的尾巴,并且在他投掷一个值为6的骰子之前,他不会获得其他机会
  • 如果骰子的结果是1,2,3,4,5,6,并且玩家位于梯子的下方,那么他的当前位置将更改为梯子的最上方位置,他将获得再次掷骰子的机会
  • 如果玩家的当前位置+掷骰>100,则考虑以下因素 i、 如果(掷骰==6),当前玩家将再次获得该机会,否则其他玩家将获得该机会
  • 任何比其他玩家早到100的玩家都将是赢家,游戏将结束
  • 我们只传递一个HashMap,其中包含当前位置作为键,下一个位置作为值。我认为一个HashMap将完成梯形图和Snake的所有要求

    int playSnakeAndLadder(HashMap<Integer, Integer> hashMap){
        int player1=1, player2=1;// Initial position of players
        int chance=0;// This value show the change of players if chance is odd then player1 will play
                     // if chance is even then player2 will play
        while(1){
            if((player1==100)||(player2==100))// if any of player's position is 100 return chance;
                return chance;// Here chance shows who win the game, if chance is even player1 wins other //wise player2 wins
        int roll=Randon(6);// this will generate random number from 1 to 6.
        if(chance%2==0){
             int key=roll+player1;// new position of player1             
             boolean isLadder=false;// This is for checking the turn current player if againTurn is ture 
             // then the current player will player again.
             if(hashMap.contains(Key)){
                 player1=hashMap.getValue(Key);
                 // Here player current position will automatically update according to the hashMap.
                 // if there is a snake the key value is greater than it mapping value.
                 // if there is a ladder then key value is less than it mapping value. 
                 if(Key<hashMap.getValue(Key))
                     isLadder=true;// Here player gets ladder.
                 if(isLadder==true && roll==6 || isLadder==true)
                   chance=chance;
                 else
                   chance=(chance+1)%2;
             }
             else if(player1+roll>100 && roll!=6)
                   chance=(chance+1)%2;
                else if(player1+roll>100 && roll==6)
                   chance=chance;
                else if(roll==6){
                   player1=player1+roll;
                   chance=chance;
                }
                else{
                   player1=player1+roll;
                   chance1=(chance1+1)%2;
                }                 
           }
    
    
        else{// Now similarly for player2
                  {
                 int key=roll+player2;// new position of player2             
                 boolean isLadder=false;// This is for checking the turn current player if againTurn is ture 
                 // then the current player will player again.
                 if(hashMap.contains(Key)){
                     player2=hashMap.getValue(Key);
                     // Here player current position will automatically update according to the hashMap.
                     // if there is snake the key value is greater than it mapping value.
                     // if there is ladder then key value is less than it mapping value. 
                     if(Key<hashMap.getValue(Key))
                         isLadder=true;// Here player gets ladder.
                     if(isLadder==true && roll==6 || isLadder==true)
                       chance=chance;
                     else
                       chance=(chance+1)%2;
                 }
                 else if(player2+roll>100 && roll!=6)
                       chance=(chance+1)%2;
                    else if(player2+roll>100 && roll==6)
                       chance=chance;
                    else if(roll==6){
                       player2=player2+roll;
                       chance=chance;
                    }
                    else{
                       player2=player2+roll;
                       chance=(chance+1)%2;
                    }                 
            }
           }
         }
      }
    
    int playSnakeAndLadder(HashMap HashMap){
    int player1=1,player2=1;//玩家的初始位置
    int chance=0;//此值显示玩家的变化如果chance为奇数,则玩家1将进行游戏
    //如果机会均等,那么玩家2将出战
    而(1){
    if((player1==100)| |(player2==100))//如果任何玩家的位置是100回球机会;
    return chance;//这里chance表示谁赢了游戏,如果chance是偶数,则player1赢其他//明智的player2赢
    int roll=Randon(6);//这将生成从1到6的随机数。
    如果(机会%2==0){
    int key=roll+player1;//player1的新位置
    boolean isLadder=false;//如果againtrn为真,则用于检查当前玩家的回合
    //然后当前玩家将再次播放。
    if(hashMap.contains(键)){
    player1=hashMap.getValue(键);
    //在这里,玩家的当前位置将根据hashMap自动更新。
    //如果有蛇,则键值大于它的映射值。
    //如果存在梯形图,则键值小于它的映射值。
    
    int playSnakeAndLadder(HashMap<Integer, Integer> hashMap){
        int player1=1, player2=1;// Initial position of players
        int chance=0;// This value show the change of players if chance is odd then player1 will play
                     // if chance is even then player2 will play
        while(1){
            if((player1==100)||(player2==100))// if any of player's position is 100 return chance;
                return chance;// Here chance shows who win the game, if chance is even player1 wins other //wise player2 wins
        int roll=Randon(6);// this will generate random number from 1 to 6.
        if(chance%2==0){
             int key=roll+player1;// new position of player1             
             boolean isLadder=false;// This is for checking the turn current player if againTurn is ture 
             // then the current player will player again.
             if(hashMap.contains(Key)){
                 player1=hashMap.getValue(Key);
                 // Here player current position will automatically update according to the hashMap.
                 // if there is a snake the key value is greater than it mapping value.
                 // if there is a ladder then key value is less than it mapping value. 
                 if(Key<hashMap.getValue(Key))
                     isLadder=true;// Here player gets ladder.
                 if(isLadder==true && roll==6 || isLadder==true)
                   chance=chance;
                 else
                   chance=(chance+1)%2;
             }
             else if(player1+roll>100 && roll!=6)
                   chance=(chance+1)%2;
                else if(player1+roll>100 && roll==6)
                   chance=chance;
                else if(roll==6){
                   player1=player1+roll;
                   chance=chance;
                }
                else{
                   player1=player1+roll;
                   chance1=(chance1+1)%2;
                }                 
           }
    
    
        else{// Now similarly for player2
                  {
                 int key=roll+player2;// new position of player2             
                 boolean isLadder=false;// This is for checking the turn current player if againTurn is ture 
                 // then the current player will player again.
                 if(hashMap.contains(Key)){
                     player2=hashMap.getValue(Key);
                     // Here player current position will automatically update according to the hashMap.
                     // if there is snake the key value is greater than it mapping value.
                     // if there is ladder then key value is less than it mapping value. 
                     if(Key<hashMap.getValue(Key))
                         isLadder=true;// Here player gets ladder.
                     if(isLadder==true && roll==6 || isLadder==true)
                       chance=chance;
                     else
                       chance=(chance+1)%2;
                 }
                 else if(player2+roll>100 && roll!=6)
                       chance=(chance+1)%2;
                    else if(player2+roll>100 && roll==6)
                       chance=chance;
                    else if(roll==6){
                       player2=player2+roll;
                       chance=chance;
                    }
                    else{
                       player2=player2+roll;
                       chance=(chance+1)%2;
                    }                 
            }
           }
         }
      }