注册 登录
编程论坛 C++教室

^_^编的第一个游戏,贪吃蛇(一道acm题)

醉生梦死 发布于 2007-11-15 22:57, 2746 次点击

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

17 回复
#2
huozoo2007-11-15 23:51

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

#3
雨中飞燕2007-11-16 00:24
以下是引用huozoo在2007-11-15 23:51:16的发言:

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

没有错啊



by 雨中飞燕 C/C++学习讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge)论坛:[/url] 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" target="_blank">https://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]

#4
aipb20072007-11-16 00:29
2楼在6.0下编译的吧,那是不标准的
#5
福尔摩斯2007-11-16 00:44

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

#6
醉生梦死2007-11-16 09:02
回复:(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;
}

}
}

#7
雨中飞燕2007-11-16 10:27
以下是引用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/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" target="_blank">https://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]

#8
PcrazyC2007-11-16 11:08
aipb2007好像是说6.0是不标准的吧
#9
dongfengaa2007-11-16 11:42
怎么有错啊
#10
aipb20072007-11-16 12:39
以下是引用PcrazyC在2007-11-16 11:08:50的发言:
aipb2007好像是说6.0是不标准的吧

同意

#11
PcrazyC2007-11-16 12:43
aipb2007,你准备换aipb2008了,你好像有50000积分了
#12
aipb20072007-11-16 12:44
可以改ID?我还不知道,呵呵,但是2008被我注册了,重名了可以不?
#13
PcrazyC2007-11-16 12:46

你想个办法让老静把你那号封了不就得了,封号的条件好多的

#14
aipb20072007-11-16 12:48
有道理,哈哈!
#15
雨中飞燕2007-11-16 18:19
误会了,不好意思~~~~~~



by 雨中飞燕 C/C++学习讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge)论坛:[/url] 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" target="_blank">https://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]
#16
kakawei2007-11-16 19:02
6。0是不标准的,dev好象也不是很标准,比如在定义静态数组时可以将中括号中的数定义为一个非 常量变量。
#17
zjl1382007-11-16 19:41
我还看不懂这程序.

[此贴子已经被作者于2007-11-16 19:42:27编辑过]


#18
phb7112007-11-18 13:08
在g++下编译是会报错地
1