| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2742 人关注过本帖, 1 人收藏
标题:^_^编的第一个游戏,贪吃蛇(一道acm题)
取消只看楼主 加入收藏
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏(1)
 问题点数:0 回复次数:1 
^_^编的第一个游戏,贪吃蛇(一道acm题)

#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次游戏

搜索更多相关主题的帖子: acm int 贪吃 游戏 main 
2007-11-15 22:57
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏
得分:0 
回复:(huozoo)不是吧,你i定义这么多次能正常用[em3...

能运行,i都有作用域,不过,我想了想,的确没必要定义这么多次,浪费空间
改了下:
#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 i = 0,j = 0;
int a[52][52]; //设置一个50×50大小的地方
for (i =0;i <= 51;i ++)
for (j = 0;j <=51;j ++)
a[i][j] = 0;
for (i = 0,j = 0;j <= 51;j ++)
a[i][j] = -1;
for (i = 51,j = 0;j <= 51;j ++)
a[i][j] = -1;
for (i = 1,j = 0;i <= 50;i ++)
a[i][j] = -1;
for (i = 1,j = 51;i <=50;i ++)
a[i][j] = -1;
for (i = 25,j = 11;j <= 30;j ++)
a[i][j] = 1;


char action[101];
cin >> action;
Node snake[21];
for (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 (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')
{

if (++snake[1].row == 51)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
else if (action[step-1] == 'S')
{

if (--snake[1].row == 0)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
else if (action[step-1] == 'W')
{

if (--snake[1].column == 0)
{
cout << "The worm ran off the board on move " << step <<endl;
break;
}
}
else if (action[step-1] == 'E')
{

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 (i =51;i >= 0;i --)
{
for (j = 0;j <=51;j ++)
cout<<a[i][j];
cout <<endl;
}

if (step == n)
cout << "The worm successfully made all " << step << endl;
}

}
}


2007-11-16 09:02
快速回复:^_^编的第一个游戏,贪吃蛇(一道acm题)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012657 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved