用C++写的连连看 源代码发一份 谢谢?

 我来答
周越杰happy
2013-07-17
知道答主
回答量:22
采纳率:0%
帮助的人:7.9万
展开全部
这位同志,很抱歉我有的连连看极易溢出,修好了给你。
我可以给你一个贪吃蛇的c++程序:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
typedef struct snake
{
int a;
int b;
struct snake *u;
struct snake *n;
}snake,*snake1;
typedef struct food
{
int a;
int b;
}food;
int main()
{
char c,c0 = 'd';
int i,j,k,n=1,t,at;
snake p,q;
snake *dd,*dd0,*dd1,*dd2;
food f;
srand(time(NULL));
p.u = NULL;
p.n = &q;
p.a = 5;p.b = 6;q.a = 5;q.b = 5;
q.u = &p;q.n = NULL;
dd=dd2= &q;
f.a=(rand()%15+1);
f.b=(rand()%15+1);
while(1)
{
srand(time(NULL));
system("cls");
for(i = 0;i < 17;i ++)
{
for(j = 0; j < 17;j++)
{

if(i == 0 )
printf("锟絰");
else if(i == 16)
printf("锟斤拷");
else if(j == 0)
printf("锟斤拷");
else if(j == 16)
printf("锟斤拷");
else if(i == p.a && j == p.b)
printf("锟斤拷");
else if(i == f.a && j == f.b)
printf("锟斤拷");
else
{
t = 0;
dd = dd2;
for(k = 0; k < n ;k++)
{
if(i == dd->a && j == dd->b)
{
printf("锟斤拷");
t = 1;
break;
}

dd = dd->u;
}
if(t == 0)
printf(" ");
}

}printf("\n");
}
at = 0;
dd =dd2;
for(i=0;i<n;i++)
{
if(p.a == dd->a && p.b == dd->b)
{
printf("game over!!\n");
for (int iii=0;iii<1000;iii++) {};
exit(0);
}
dd = dd->u;
}
if(p.a == f.a && p.b == f.b)
{
dd = dd2;
at =1;
f.a = (rand()%15+1);
f.b = (rand()%15+1);
for(i=0;i<n;i++)
{
if(f.a == dd->a && f.b == dd->b)
{
f.a = dd2->a;
f.b = dd2->b;
break;
}
}
n++;
}
if(kbhit())
{
c = getch();
dd = dd2;
if(c == 'w' && c0 != 's')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a = (p.a-1)%15;
}
else if(c == 's' && c0 != 'w')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a = (p.a%15)+1;
}
else if(c == 'a' && c0 != 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.b == 1)
p.b = 15;
else
p.b = (p.b-1)%15;
}
else if(c == 'd' && c0 != 'a')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b = (p.b%15)+1;
}
else
{
goto qq;
}
c0 = c;
}
else
{
qq: if(c0 == 'r')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a=(p.a-1)%15;
}
else if(c0 == 'f')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a=(p.a%15)+1;
}
else if(c0 == 'd')
{

if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.b == 1)
p.b = 15;
else
p.b=(p.b-1)%15;
}
else if(c0 == 'g')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b=(p.b%15)+1;

}
}
fflush(stdin);
dd = &q;
_sleep(0);
}
}
这是五子棋的程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int array[21][21];
int focus_x;
int focus_y;
int player1;
int player2;
int winner;
int main()
{
void startGame();
startGame();
return 0;
}
void init()
{
int i, j;
for (i=0; i<21; i++)
{
for (j=0; j<21; j++)
{
array[i][j] = 0;
}
}
player1 = 1;
player2 = 0;
focus_x = 10;
focus_y = 10;
}
void startGame()
{
void init();
void printChessBoard();
void control();
int checkGameOver();

init();
while (winner == 0)
{
printChessBoard();
control();
checkGameOver();
}
printChessBoard();
printf("\nPlayer%d is the winner!\n\n", winner);
system("pause");
}
void printChessBoard()
{
int i, j;
system("cls");
printf("游戏提示:\nwsad为上下左右,每个操作需按Enter确认(比如上要按w再按Enter可以连续进\n行单子的移动),走棋为k\n");
for (i=0; i<21; i++)
{
for (j=0; j<21; j++)
{
if (i==focus_x && j==focus_y)
{
printf("\b[%d] ", array[i][j]);
}
else
{
printf("%-3d", array[i][j]);
}
}
printf("\n");
}
if (winner == 0)
{
if (player1 == 1)
{
printf("Player1: ");
}
else
{
printf("Player2: ");
}
}
}
void control()
{
void up();
void down();
void left();
void right();
void go();
char input;
input = getchar();
if (input == 'w')
{
up();
}
if (input == 's')
{
down();
}
if (input == 'a')
{
left();
}
if (input == 'd')
{
right();
}
if (input == 'k')
{
go();
}
}
void up()
{
if (focus_x <= 0)
{
focus_x = 0;
}
else
{
focus_x--;
}
}
void down()
{
if (focus_x >= 20)
{
focus_x = 20;
}
else
{
focus_x++;
}
}
void left()
{
if (focus_y <= 0)
{
focus_y = 0;
}
else
{
focus_y--;
}
}
void right()
{
if (focus_y >= 20)
{
focus_y = 20;
}
else
{
focus_y++;
}
}
void go()
{
int chessman = array[focus_x][focus_y];
if (chessman == 0)
{
if (player1 == 1)
{
array[focus_x][focus_y] = 1;
player1 = 0;
player2 = 1;
}
else
{
array[focus_x][focus_y] = 2;
player1 = 1;
player2 = 0;
}
}
}
int checkGameOver()
{
int i, j;
int judge(int x, int y);

for (i=0; i<21; i++)
{
for (j=0; j<21; j++)
{
if (array[i][j] != 0)
{
if(judge(i, j) == 1)
{
return 1;
}
}
}
}
return 0;
}
int judge(int x, int y)
{
int horizontal(int x, int y);
int vertical(int x, int y);
int minus_diagonal(int x, int y);
int plus_diagonal(int x, int y);
if (horizontal(x, y) == 1)
{
return 1;
}
if (vertical(x, y) == 1)
{
return 1;
}
if (minus_diagonal(x, y) == 1)
{
return 1;
}
if (plus_diagonal(x, y) == 1)
{
return 1;
}
return 0;
}
int horizontal(int x, int y)
{
int i, chess;
chess = 1;
if (y <= 16)
{
for (i=1; i<5; i++)
{
if (array[x][y] == array[x][y+i])
{
chess++;
}
}
}
if (chess >= 5)
{
winner = array[x][y];
return 1;
}
return 0;
}
int vertical(int x, int y)
{
int i, chess;
chess = 1;
if (x <= 16)
{
for (i=1; i<5; i++)
{
if (array[x][y] == array[x+i][y])
{
chess++;
}
}
}
if (chess >= 5)
{
winner = array[x][y];
return 1;
}
return 0;
}
int minus_diagonal(int x, int y)
{
int i, chess;
chess = 1;
if (x<=16 && y<=16)
{
for (i=1; i<5; i++)
{
if (array[x][y] == array[x+i][y+i])
{
chess++;
}
}
}
if (chess >= 5)
{
winner = array[x][y];
return 1;
}
return 0;
}
int plus_diagonal(int x, int y)
{
int i, chess;
chess = 1;
if (x>=4 && y<=16)
{
for (i=1; i<5; i++)
{
if (array[x][y] == array[x-i][y+i])
{
chess++;
}
}
}
if (chess >= 5)
{
winner = array[x][y];
return 1;
}
return 0;
}
打开.exe即可,望有用!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
木石水每
2013-07-17 · 超过37用户采纳过TA的回答
知道小有建树答主
回答量:206
采纳率:0%
帮助的人:111万
展开全部
邮箱发来..........
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式