标题:^_^编的第一个游戏,贪吃蛇(一道acm题)
只看楼主
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
 问题点数:0 回复次数:17 
^_^编的第一个游戏,贪吃蛇(一道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
huozoo
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-6-28
得分:0 

不是吧,你i定义这么多次能正常用?

2007-11-15 23:51
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
得分:0 
以下是引用huozoo在2007-11-15 23:51:16的发言:

不是吧,你i定义这么多次能正常用?

没有错啊



by 雨中飞燕 C/C++学习讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge)论坛:[/url] http://yzfy.org/bbs/
Blog: http://yzfy.org/blog/blog.php?uid=2

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=181314]C++编写的Windows界面游戏[/url]

2007-11-16 00:24
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
得分:0 
2楼在6.0下编译的吧,那是不标准的

Fight  to win  or  die...
2007-11-16 00:29
福尔摩斯
Rank: 5Rank: 5
等 级:贵宾
威 望:12
帖 子:4011
专家分:370
注 册:2006-8-15
得分:0 

定义的变量是有作用域的(这才是标准)


自我放逐。。。
2007-11-16 00:44
醉生梦死
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
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
得分:0 
以下是引用aipb2007在2007-11-16 0:29:35的发言:
2楼在6.0下编译的吧,那是不标准的

难道你认为6.0是标准???



by 雨中飞燕 C/C++学习讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge)论坛:[/url] http://yzfy.org/bbs/
Blog: http://yzfy.org/blog/blog.php?uid=2

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=181314]C++编写的Windows界面游戏[/url]

2007-11-16 10:27
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
得分:0 
aipb2007好像是说6.0是不标准的吧

雁无留踪之意,水无取影之心
2007-11-16 11:08
dongfengaa
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-11-14
得分:0 
怎么有错啊
2007-11-16 11:42
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
得分:0 
以下是引用PcrazyC在2007-11-16 11:08:50的发言:
aipb2007好像是说6.0是不标准的吧

同意


Fight  to win  or  die...
2007-11-16 12:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-186331-1-1.html




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

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