数据结构用于蛇和梯子游戏

use*_*823 6 algorithm data-structures

我遇到了一个采访问题"建议你将用于蛇和梯子游戏的数据结构?"

我会使用2D数组(与我们在国际象棋中一样)来设计每个游戏块.但是有可能用一维阵列设计它吗?很多人建议这样做,但没有人解释过如何做到这一点.

gee*_*eks 15

蛇与梯的规则是:

  1. 这场比赛有两名球员,棋盘大小为100.(10 X 10)
  2. 投掷骰子的可能结果是1,2,3,4,5,6.
  3. 如果输出为6则当前玩家将有机会再次投掷骰子.
  4. 如果骰子的结果是1,2,3,4,5,6并且玩家位于蛇的嘴上,那么他的当前位置将变为蛇的尾部,并且在他投掷有价值的骰子之前他将不会得到其他机会6.
  5. 如果骰子的结果是1,2,3,4,5,6并且球员位于梯子的下方,那么他的当前位置将变为梯子的最高位置,他将再次获得掷骰子的机会.
  6. 如果玩家的当前位置+滚动> 100,那么请考虑以下因素:if(roll == 6)当前玩家将再次获得机会,否则其他玩家将获得.
  7. 任何比其他玩家更早达到100的玩家将成为赢家,游戏将结束.

我们只传递一个HashMap,它包含当前位置作为键,下一个位置作为值.我认为一个HashMap将完成Ladder and 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;
                }                 
        }
       }
     }
  }
Run Code Online (Sandbox Code Playgroud)