#include <iostream>
using namespace std;
class Node
{
friend int main();
private:
int row; //记录蛇中每一个节点的位置
int column;
};
int main()
{
int n; //要走多少步
while(cin >> n && n!=0)
{
int a[52][52]; //设置一个50×50大小的地方
for (int i =0;i <= 51;i ++)
for (int j = 0;j <=51;j ++)
a[i][j] = 0;
for (int i = 0,j = 0;j <= 51;j ++)
a[i][j] = -1;
for (int i = 51,j = 0;j <= 51;j ++)
a[i][j] = -1;
for (int i = 1,j = 0;i <= 50;i ++)
a[i][j] = -1;
for (int i = 1,j = 51;i <=50;i ++)
a[i][j] = -1;
for (int i = 25,j = 11;j <= 30;j ++)
a[i][j] = 1;
char action[101];
cin >> action;
Node snake[21];
for (int i = 1;i <= 20;i ++) //初始化蛇
{
snake[i].row = 25;
snake[i].column = 30-i+1;
}
int step = 1;
int temprow=snake[1].row,tempcolumn=snake[1].column;
for (;step <= n;step ++)
{
//改变最后一个节点原来所占位置的值,变为0
a[snake[20].row][snake[20].column] = 0;
for (int i = 2;i <=20;i ++)
{
int temprow1 = snake[i].row;
int tempcolumn1 = snake[i].column;
snake[i].row = temprow;
snake[i].column = tempcolumn;
temprow = temprow1;
tempcolumn = tempcolumn1;
cout << snake[i].row << ' ' <<snake[i].column<<endl;
}
temprow = snake[1].row;
tempcolumn = snake[1].column;
if (action[step-1] == 'N')
{
snake[1].row++;
if (snake[1].row == 51)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
else if (action[step-1] == 'S')
{
snake[1].row--;
if (snake[1].row == 0)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
else if (action[step-1] == 'W')
{
snake[1].column--;
if (snake[1].column == 0)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
else if (action[step-1] == 'E')
{
snake[1].column++;
if (snake[1].column == 51)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
if (a[snake[1].row][snake[1].column] == 1)
{
cout << "The worm ran into itself on move " << step << endl;
break;
}
//把这次蛇头占的位置变成1
a[snake[1].row][snake[1].column] = 1;
for (int i =51;i >= 0;i --)
{
for (int j = 0;j <=51;j ++)
cout<<a[i][j];
cout <<endl;
}
if (step == n)
cout << "The worm successfully made all " << step << endl;
}
}
}
^_^:
不会做成图形界面,
游戏规则:
输入:
一次性输入你要走的步数和各步的方向
比如说:
18
NWWWWWWWWWWSESSSWS //第一次游戏
20
SSSWWNENNNNNWWWWSSSS //第二次游戏
30
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE //第三次游戏
13
SWWWWWWWWWNEE //第4次游戏