第一次打貪喫蛇的時候,花了好多時間,打出的代碼如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char map[12][12] = { '\0' };
int i, j,k=0,e=0,a,b;
int sx[10] = { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, sy[10] = { 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 };
int gameover(char x);
int main(void)
{
char c, d[10];
srand(time(NULL));
for (i = 0; i <= 11; i++){
map[0][i] = '*';
map[i][0] = '*';
map[11][i] = '*';
map[i][11] = '*';
}
map[1][1] = 'X';
map[1][2] = 'X';
map[1][3] = 'X';
map[1][4] = 'X';
map[1][5] = 'H';
for (i = 0; i <= 11; i++){
for (j = 0; j <= 11; j++){
if (map[i][j] == '\0'){
printf(" ");
}
else{
printf("%c", map[i][j]);
}
}
printf("\n");
}
scanf("%s", d);
c = d[0];
while (gameover(c) == 0){
for (i = 0; i <= 11; i++){
for (j = 0; j <= 11; j++){
map[i][j] = '\0';
}
}
if ((e % 10) == 0){
a = rand() % 10 + 1;
b= rand() % 10 + 1;
map[a][b] = '$';
}
map[a][b] = '$';
for (i = 0; i <= 11; i++){
map[0][i] = '*';
map[i][0] = '*';
map[11][i] = '*';
map[i][11] = '*';
}
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= 9 && sx[i] != 0 && sy[i] != 0; i++){
map[sx[i]][sy[i]] = 'X';
}
for (i = 0; i <= 11; i++){
for (j = 0; j <= 11; j++){
if (map[i][j] == '\0'){
printf(" ");
}
else{
printf("%c", map[i][j]);
}
}
printf("\n");
}
scanf("%s", d);
c = d[0];
e++;
}
printf("Game over!\n");
return 0;
}
int gameover(char x){
switch (x){
case 'A':
if (map[sx[0]][sy[0] - 1] == '$'){
for (i = 8; i >=0; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sy[0]--;
return 0;
}
else{
for (i = 9; i >= 1; i--){
if (sx[i] != 0){
sx[i] = sx[i - 1], sy[i] = sy[i - 1];
}
}
sy[0]--;
for (i = 1; i <= 9; i++){
if (sx[i] == sx[0] && sy[i] == sy[0]){
return 1;
}
}
if (sy[0] == 0){
return 1;
}
else{
return 0;
}
}
break;
case 'D':
if (map[sx[0]][sy[0] + 1] == '$'){
for (i = 8; i >=0; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sy[0]++;
return 0;
}
else{
for (i = 9; i >= 1; i--){
if (sx[i] != 0){
sx[i] = sx[i - 1], sy[i] = sy[i - 1];
}
}
sy[0]++;
for (i = 1; i <= 9; i++){
if (sx[i] == sx[0] && sy[i] == sy[0]){
return 1;
}
}
if (sy[0] == 11){
return 1;
}
else{
return 0;
}
}
break;
case 'W':
if (map[sx[0]-1][sy[0]] == '$'){
for (i = 8; i >=0; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sx[0]--;
return 0;
}
else{
for (i = 9; i >= 1; i--){
if (sx[i] != 0){
sx[i] = sx[i - 1], sy[i] = sy[i - 1];
}
}
sx[0]--;
for (i = 1; i <= 9; i++){
if (sx[i] == sx[0] && sy[i] == sy[0]){
return 1;
}
}
if (sx[0] == 0){
return 1;
}
else{
return 0;
}
}
break;
case 'S':
if (map[sx[0]+1][sy[0]] == '$'){
for (i = 8; i >=0; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sx[0]++;
return 0;
}
else{
for (i = 9; i >= 1; i--){
if (sx[i] != 0){
sx[i] = sx[i - 1], sy[i] = sy[i - 1];
}
}
sx[0]++;
for (i = 1; i <= 9; i++){
if (sx[i] == sx[0] && sy[i] == sy[0]){
return 1;
}
}
if (sx[0] == 11){
return 1;
}
else{
return 0;
}
}
break;
}
}
可以看出,bug相當多,而且食物的產生完全是錯的,還有沒有清屏代碼。參考了一些大神的代碼之後,終於完成了優化的貪喫蛇,代碼如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char map[12][13] = {
"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"
};
int sx[100] = {1,1,1,1,1}, sy[100] = {5,4,3,2,1};
int foodnum = 0, zhixing = 1, len = 5;
int i, foodx, foody;
void move(char a);
int main(void)
{
char s[2], c;
srand(time(NULL));
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while (map[foodx][foody] != ' '){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
}
map[foodx][foody] = '$';
foodnum = 1;
for (i = 0; i <= 11; i++){
printf("%s\n", map[i]);
}
while (zhixing){
scanf("%s", s);
c = s[0];
move(c);
system("cls");
for (i = 0; i <= 11 && zhixing == 1; i++){
printf("%s\n", map[i]);
}
}
printf("Game over!\n");
return 0;
}
void move(char a)
{
int j1=sx[0], j2=sy[0];
if (foodnum == 0){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while (map[foodx][foody] != ' '){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
}
map[foodx][foody] = '$';
foodnum = 1;
}
switch (a){
case 'A':case 'a':
sy[0]--;
break;
case 'D':case 'd':
sy[0]++;
break;
case 'W':case 'w':
sx[0]--;
break;
case 'S':case 's':
sx[0]++;
break;
}
if (map[sx[0]][sy[0]] != ' '&&map[sx[0]][sy[0]] != '$'){
zhixing = 0;
return;
}
if (map[sx[0]][sy[0]] == ' '){
map[sx[len - 1]][sy[len - 1]] = ' ';
for (i = len - 2; i >= 1; i--){
sx[i + 1] = sx[i];
sy[i + 1] = sy[i];
}
sx[1] = j1, sy[1] = j2;
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= len - 1; i++){
map[sx[i]][sy[i]] = 'X';
}
}
if (map[sx[0]][sy[0]] == '$'){
foodnum = 0;
len++;
for (i = len - 2; i >= 1; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sx[1] = j1, sy[1] = j2;
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= len - 1; i++){
map[sx[i]][sy[i]] = 'X';
}
}
}
這個比起上一個就好多了,這周又要求做智能蛇,也就是說,做一個會自己走的蛇,我的實現方法是相當白癡的,下面是我的wheregonext函數:
char wheregonext(void)
{
int heng, shu;
heng = abs(sy[0] - foody);
shu = abs(sx[0] - foodx);
if (heng >= shu){
if (sy[0] - foody > 0){
return 'a';
}
else{
return 'd';
}
}
if (heng < shu){
if (sx[0] - foodx > 0){
return 'w';
}
else{
return 's';
}
}
}
雖說白癡,但是好歹可以找到方向,下面是完整代碼:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<Windows.h>
char map[12][13] = {
"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"
};
int sx[100] = { 1, 1, 1, 1, 1 }, sy[100] = { 5, 4, 3, 2, 1 };
int foodnum = 0, zhixing = 1, len = 5;
int i, foodx, foody;
void move(char a);
char wheregonext(void);
int main(void)
{
char c;
srand(time(NULL));
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while (map[foodx][foody] != ' '){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
}
map[foodx][foody] = '$';
foodnum = 1;
for (i = 0; i <= 11; i++){
printf("%s\n", map[i]);
}
while (zhixing){
c = wheregonext();
move(c);
system("cls");
Sleep(100);
for (i = 0; i <= 11 && zhixing == 1; i++){
printf("%s\n", map[i]);
}
}
printf("Game over!\n");
return 0;
}
void move(char a)
{
int j1 = sx[0], j2 = sy[0];
if (foodnum == 0){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while (map[foodx][foody] != ' '){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
}
map[foodx][foody] = '$';
foodnum = 1;
}
switch (a){
case 'A':case 'a':
sy[0]--;
break;
case 'D':case 'd':
sy[0]++;
break;
case 'W':case 'w':
sx[0]--;
break;
case 'S':case 's':
sx[0]++;
break;
}
if (map[sx[0]][sy[0]] != ' '&&map[sx[0]][sy[0]] != '$'){
zhixing = 0;
return;
}
if (map[sx[0]][sy[0]] == ' '){
map[sx[len - 1]][sy[len - 1]] = ' ';
for (i = len - 2; i >= 1; i--){
sx[i + 1] = sx[i];
sy[i + 1] = sy[i];
}
sx[1] = j1, sy[1] = j2;
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= len - 1; i++){
map[sx[i]][sy[i]] = 'X';
}
}
if (map[sx[0]][sy[0]] == '$'){
foodnum = 0;
len++;
for (i = len - 2; i >= 1; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sx[1] = j1, sy[1] = j2;
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= len - 1; i++){
map[sx[i]][sy[i]] = 'X';
}
}
}
char wheregonext(void)
{
int heng, shu;
heng = abs(sy[0] - foody);
shu = abs(sx[0] - foodx);
if (heng >= shu){
if (sy[0] - foody > 0){
return 'a';
}
else{
return 'd';
}
}
if (heng < shu){
if (sx[0] - foodx > 0){
return 'w';
}
else{
return 's';
}
}
}
這是windows下的智能蛇,用linux時,要改不少東西。
頭文件Windows.h在linux中沒有,要用unistd.h。還有linux清屏要用VT100終端標準中的printf(“\033[2J”)清屏。
總之,最後看見蛇跑起來,還是挺開心的。