好友
阅读权限 10
听众
最后登录 1970-1-1
x582
发表于 2015-5-31 21:53
注意:要把控制台窗口默认值从“简体中文”改成“美国”
代码放这里,应用程序在下面
希望高手多多指正
snake.zip
(12.12 KB, 下载次数: 37)
接下来是代码了
思路都是自己的
//global.h
#ifndef global_h
#define global_h
wchar_t a[23][43];
int hl = 0;
int hr = 0;
int fl = 22;
int fr = 42;
int bl[100];
int br[100];
wchar_t c;
int ju;
int b=0;
int k = 1;//food
FILE *fp;
int kill = 0;
#endif
//menu.h
#ifndef menu_h
#define menu_h
void out();
void model();
void start();
void food();
void control(int j);
void over();
int initial();
void file();
#endif
//main.cpp
#include<stdio.h>
#include"menu.h"
#include<Windows.h>
#include<conio.h>
#include<time.h>
extern wchar_t a[23][43];
extern int hl;
extern int hr;
extern int bl[100];
extern int br[100];
extern wchar_t c;
extern int ju;
extern int fl;
extern int fr;
extern int kill;
int main()
{
srand((unsigned)time(NULL));
model();
start();
int j = initial();
while(1)
{
kill++;
control(j);
a[22][42] = 0;
file();
//printf("j=%d\n", j);
//getchar();
j = ju;
}
return 0;
//menu.cpp
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include"global.h"
#include<Windows.h>
#include<conio.h>
extern void food();
void out()
{
system("cls");
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 42; j++)
printf("%c", a[j]);
printf("\n");
}
Sleep(100);
}
void model()
{
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 42; j++)
{
if (i == 0 || i == 21 || j == 0 || j == 41)
a[j] = 219;
else
a[j] = 0;
}
}
a[22][42] = 0;
for (int i = 0; i < 100; i++)
{
bl = 22;
br = 42;
}
}
void start()
{
hl = rand() % 10 + 5;
hr = rand() % 20 + 10;
a[hl] = 4;
bl[0] = hl;
br[0] = hr+1;
a[bl[0]][br[0]] = 15;
food();
out();
/*while (1)
{
a[bl[0]][br[0]] = 0;
br[0]--;
hr--;
a[bl[0]][br[0]] = 15;
a[hl] = 4;
out();
if (_kbhit())
{
c = _getch();
if (c == 'w'||c=='a'||c=='s'||c=='d')
{
break;
}
}
}*/
}
int initial()
{
int judge = 0;//1u 2d 3l 4r
if (hl == (bl[0] + 1) && hr == br[0])
judge = 1;
else if (hl == (bl[0] - 1) && hr == br[0])
judge = 2;
else if (hl == bl[0] && hr == (br[0] - 1))
judge = 3;
else if (hl == bl[0] && hr == (br[0] + 1))
judge = 4;
else
printf("wrong\n");
return judge;
}
void control(int j)
{
switch (j)
{
case 1:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
--hl;//most important
a[hl] = 4;
food();//before out
out();
if (_kbhit())
{
c = _getch();
if (c == 'a')
{
ju = 3;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
--hr;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 'd')
{
ju = 4;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
++hr;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
case 2:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
++hl;//most important
a[hl] = 4;
food();
out();
if (_kbhit())
{
c = _getch();
if (c == 'a')
{
ju = 3;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
--hr;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 'd')
{
ju = 4;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
++hr;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
case 3:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i-1;j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
--hr;//most important
a[hl] = 4;
food();
out();
if (_kbhit())
{
c = _getch();
if (c == 'w')
{
ju = 1;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
--hl;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 's')
{
ju = 2;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
++hl;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
case 4:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
++hr;//most important
a[hl] = 4;
food();
out();
if (_kbhit())
{
c = _getch();
if (c == 'w')
{
ju = 1;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
--hl;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 's')
{
ju = 2;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0) break;
}
a[bl[i - 1]][br[i - 1]] = 0;
for (int j = i - 1; j>0; j--)
{
bl[j] = bl[j - 1];
br[j] = br[j - 1];
a[bl[j]][br[j]] = 15;
}
bl[0] = hl;
br[0] = hr;
a[bl[0]][br[0]] = 15;
++hl;//most important
a[hl] = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
}
}
void food()
{
int x = 1;
if (a[fl][fr] == 0)
{
do
{
x = 1;
fl = rand() % 18 + 2;
fr = rand() % 38 + 2;
for (int i = 0; i < 100; i++)
{
if (fl == bl && fr == br)
x++;
}
} while (x != 1);
a[fl][fr] = 3;
}
else if (a[fl][fr] == 4)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a[bl][br] == 0)
break;
}
if (i == 1)
{
if (hl == (bl[0] + 1) && hr == br[0])
{
bl[1] = bl[0] - 1;
br[1] = br[0];
}
else if (hl == (bl[0] - 1) && hr == br[0])
{
bl[1] = bl[0] + 1;
br[1] = br[0];
}
else if (hl == bl[0] && hr == (br[0] - 1))
{
bl[1] = bl[0];
br[1] = br[0] + 1;
}
else if (hl == bl[0] && hr == (br[0] + 1))
{
bl[1] = bl[0];
br[1] = br[0] - 1;
}
else
printf("wrong\n");
}
else if (i > 1)
{
if (bl[i-1] == (bl[i-2] + 1) && br[i-1] == br[i-2])
{
bl = bl[i-1] + 1;
br = br[i-1];
}
else if (bl[i-1] == (bl[i-1] - 1) && br[i-1] == br[i-1])
{
bl = bl[i-1] - 1;
br = br[i-1];
}
else if (bl[i-2] == bl[i-1] && br[i-2] == (br[i-1] - 1))
{
bl = bl[i-1];
br = br[i-1] - 1;
}
else if (bl[i-2] == bl[i-1] && br[i-2] == (br[i-1] + 1))
{
bl = bl[i-1];
br = br[i-1] + 1;
}
else
printf("wrong\n");
}
a[bl][br] = 15;
do
{
x = 1;
fl = rand() % 18 + 2;
fr = rand() % 38 + 2;
for (int i = 0; i < 100; i++)
{
if (fl == bl && fr == br)
x++;
}
} while (x != 1);
a[fl][fr] = 3;
}
}
void file()
{
char *fname;
fname = "text.txt";
if (fopen_s(&fp, fname, "a+") != 0)
if (fopen_s(&fp, fname, "w+") != 0)
printf("file open wrong\n");
printf("the %d control\n",kill);
for (int i = 0; i < 20; i++)
{
fprintf(fp, "%-4d %-4d %d %d\n", bl, br,kill, a[bl][br]);
}
fclose(fp);
}
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。