我遇到了一个采访问题“建议你将用于蛇和梯子游戏的数据结构?”
我会使用2D数组(与国际象棋相同)来设计每个游戏块。但是有可能用一维阵列设计它吗?许多人建议这样做,但没有人解释如何做到这一点。
答案 0 :(得分:14)
Snake and Ladder 的规则是:
我们只传递一个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;
}
}
}
}
}
答案 1 :(得分:4)
数组
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
即
r = r + s + board[r+s-1])
@Jan Dvorak, “锯齿状阵列不是2D阵列”
答案 2 :(得分:1)
在我博客的implementation中,我使用了一对简单的链表来存储蛇和梯子。列表中的每个元素都有一对正方形,“从”正方形和“到”正方形;如果你降落在任何“从”广场,你的作品被重新安置到“到”广场。我发现最小游戏长度为7回合,平均游戏长度为33回合。你可以交替使用一维数组,其中数组的索引表示“from”方形,数组中的值表示“to”方形,除了在蛇的开头或者蛇的开头,它与索引相同梯子。
答案 3 :(得分:0)
由于蛇/滑道和梯子的移动通常是单向的,而不是国际象棋中可能的多个方向,因此一维阵列或列表绝对可以使用。
要表示蛇和梯子,你可以将每个列表元素的内容设置为一个整数,告诉游戏当你降落时,你的计数器前进或后退多远。例如,在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
答案 4 :(得分:0)
是的,有可能:每个2D数组都可以表示为一维数组。
在一维数组中一个接一个地表示2D数组的所有行。这样做,2d[i][j]
变为1d[i * rowLength + j]
。除非你没有别的选择,否则它通常不是一件好事,因为它变得不那么容易使用。
答案 5 :(得分:0)
使用数组:
int SNL[100];
此数组的每个元素都包含一个根据以下规则的整数:
x
到x+l
的梯子,那么SNL[x]=l;
x
有蛇咬并且x-s
离开,SNL[x]=-s;
,则为SNL[x]=0;
答案 6 :(得分:0)
当然我们可以使用1D阵列解决问题,因为板上标记的数字是1到100.我们可以初始化两个1D阵列:蛇和梯子都是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;
}
答案 7 :(得分:0)
为什么没有人建议使用Map作为数据结构。如果没有蛇或梯子,我们会将整数映射到自身。在蛇的情况下,我们将蛇的头部映射到它的尾部,同样也用于梯子。我们可以为每个玩家使用整数变量。