#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#define KEY_UP 0x48
#define KEY_LEFT 0x4B
#define KEY_RIGHT 0x4D
#define KEY_DOWN 0x50
#define XIAZI 32
int x=320,y=230;
int who=2;
int zuobiao[10][10];
int xi=5,yi=5;
int ifover=1;
char player1[20],player2[20];
void initgr(void) /* BGI³õʼ»¯ */
{
int gd=DETECT,gm=0; /* ºÍgd=VGA,gm=VGAHIÊÇͬÑùЧ¹û */
registerbgidriver(EGAVGA_driver);/* ×¢²áBGIÇý¶¯ºó¿ÉÒÔ²»ÐèÒª.BGIÎļþµÄÖ§³ÖÔËÐÐ */
initgraph(&gd,&gm,"");
}
void drawmat(char *mat,int matsize,int x,int y,int color)
/*ÒÀ´Î£º×ÖÄ£Ö¸Õë¡¢µãÕó´óС¡¢Æðʼ×ø±ê(x,y)¡¢ÑÕÉ«*/
{int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /*²âÊÔΪ1µÄλÔòÏÔʾ*/
putpixel(x+i*8+k,y+j,color);
}
char wu24[]={
/* ÒÔÏÂÊÇ 'Îå' µÄ 24µãÕóËÎÌå ×ÖÄ££¬72 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x18,0x1F,0xFF,0xE0,0x00,
0x30,0x00,0x00,0x30,0x00,0x00,0x20,0x00,
0x00,0x20,0x00,0x00,0x20,0x00,0x00,0x20,
0x40,0x0F,0xFF,0xE0,0x00,0x60,0x40,0x00,
0x60,0x40,0x00,0x40,0x40,0x00,0x40,0x40,
0x00,0x40,0x40,0x00,0x40,0xC0,0x00,0xC0,
0xC0,0x00,0xC0,0xC0,0x00,0xC0,0xC4,0x7F,
0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,
};
char zi24[]={
/* ÒÔÏÂÊÇ '×Ó' µÄ 24µãÕóËÎÌå ×ÖÄ££¬72 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0x0F,0xFF,0xF0,0x00,0x00,0x60,0x00,
0x00,0x80,0x00,0x01,0x00,0x00,0x16,0x00,
0x00,0x18,0x00,0x00,0x08,0x00,0x00,0x08,
0x0C,0x7F,0xFF,0xFE,0x00,0x08,0x00,0x00,
0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,
0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,
0x00,0x00,0x08,0x00,0x00,0x18,0x00,0x00,
0xF8,0x00,0x00,0x30,0x00,0x00,0x00,0x00,
};
char qi24[]={
/* ÒÔÏÂÊÇ 'Æå' µÄ 24µãÕóËÎÌå ×ÖÄ££¬72 byte */
0x00,0x00,0x00,0x04,0x00,0x00,0x06,0x0C,
0x30,0x04,0x08,0x20,0x04,0x08,0x20,0x04,
0x08,0x2C,0x04,0xFF,0xF0,0x3F,0xC8,0x20,
0x04,0x08,0x20,0x04,0x0F,0xE0,0x0F,0x08,
0x20,0x0D,0x88,0x20,0x0C,0x88,0x20,0x14,
0x8F,0xE0,0x14,0x08,0x20,0x24,0x08,0x24,
0x24,0xF7,0xD8,0x44,0x00,0x00,0x04,0x0C,
0x60,0x04,0x18,0x30,0x04,0x30,0x1C,0x04,
0x60,0x0C,0x04,0x80,0x04,0x00,0x00,0x00,
};
void welcome(){
int i,j;
initgr(); /* BGI³õʼ»¯ */
outtextxy(10,10,"welcome to use v1.0");
outtextxy(200,30,"welcome");
setcolor(GREEN);
setbkcolor(DARKGRAY);
drawmat(wu24,24,150,125,YELLOW);
drawmat(zi24,24,180,125,YELLOW);
drawmat(qi24,24,210,125,YELLOW);
rectangle(149,125,235,150);
outtextxy(250,110,"player1");
outtextxy(320,110,"player2");
circle(280,140,20);
circle(350,140,20);
floodfill(280,130,GREEN);
setfillstyle(1,BLUE);
floodfill(350,130,GREEN);
moveto(230,260);
outtext("intruction");
outtextxy(180,275,"1.press up to up");
outtextxy(180,285,"2.press lift to lift");
outtextxy(180,295,"3.press dowm to dowm");
outtextxy(180,305,"4.press right to right");
outtextxy(180,315,"5.press space to input and esc to exit");
outtextxy(180,325,"6.player1 use the white");
outtextxy(180,335,"7.player2 use the blue");
/***********draw line*******************/
rectangle(130,20,500,460);
getch(); /* ÔÝͣһϣ¬¿´¿´Ç°Ãæ»æͼ´úÂëµÄÔËÐнá¹û */
}
void qipan(){
int i,j;
cleardevice();
setcolor(BROWN);
setbkcolor(DARKGRAY);
setviewport(50,50,700,550,1);
for(i=120;i<520;i=i+50){
for(j=30;j<430;j=j+50){ /* »Ò»¸ö8*8µÄÆåÅÌ */
rectangle(i,j,i+50,j+50);
}
}
}
void xiazi(){
int temp=BROWN;
switch(getch()){
case 27:
exit(0);
break;
case KEY_DOWN: /*ÅжϷ½ÏòÏÂ*/
if(y!=430){
if(zuobiao[xi][yi]==1)
{ temp=BLUE;}
if(zuobiao[xi][yi]==2)
{temp=WHITE; }
setcolor(temp);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
y=y+50;
yi=yi+1;
setcolor(GREEN);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
}
break;
case KEY_UP: /*ÅжϼüÅÌÏòÉÏ*/
if(y!=30){
if(zuobiao[xi][yi]==1)
{ temp=BLUE;}
if(zuobiao[xi][yi]==2)
{temp=WHITE; }
setcolor(temp);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
y=y-50;
yi=yi-1;
setcolor(GREEN);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
}
break; /*ÅжϼüÅÌÏò×ó*/
case KEY_LEFT:
if(x!=120){
if(zuobiao[xi][yi]==1)
{ temp=BLUE;}
if(zuobiao[xi][yi]==2)
{temp=WHITE; }
setcolor(temp);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
x=x-50;
xi=xi-1;
setcolor(GREEN);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
}
break;
case KEY_RIGHT: /*ÅжϼüÅÌÏòÓÒ*/
if(x!=520){
if(zuobiao[xi][yi]==1)
{ temp=BLUE;}
if(zuobiao[xi][yi]==2)
{temp=WHITE; }
setcolor(temp);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
x=x+50;
xi=xi+1;
setcolor(GREEN);
line(x,y-10,x,y+10);
line(x-10,y,x+10,y);
}
break;
case XIAZI:
if(zuobiao[xi][yi]==0){ /**/
/*ÅжÏËÏÂ×Ó*/
if(who==1){
who=who+1;
setcolor(BLUE);
circle(x,y,18);
setfillstyle(1,BLUE); /*»Ò»¸ö°ë¾¶Îª18µÄlanÉ«µÄµã*/
floodfill(x,y,BLUE);
zuobiao[xi][yi]=1;
ifwin();
break;
}
if(who==2){
who=who-1;
setcolor(WHITE);
circle(x,y,18);
setfillstyle(1,WHITE); /*»Ò»¸ö°ë¾¶Îª18µÄlvÉ«µÄµã*/
floodfill(x,y,WHITE);
zuobiao[xi][yi]=2;
ifwin();
break;
}
}
else
break;
}
}
void over(){
cleardevice();
circle(100,100,50);
circle(75,70,10);
circle(120,70,10);
putpixel(75,70,GREEN);
putpixel(120,70,GREEN);
setcolor(GREEN);
line(80,110,115,100);
moveto(200,200);
printf(" game over");
if(who==1){
printf(" %s win",player1);
}
else
printf(" %s win",player2);
getch();
}
/*ÅжÏÊÇ·ñʤÀû*/
int ifwin(){
int i=0;
int num=1;
for(i=1;i<10;i++){
if(xi+i<=10||yi+i<=10){
if(zuobiao[xi][yi]==zuobiao[xi+i][yi+i])
num=num+1;
}
if(xi-i>=1||yi-i>=1){
if(zuobiao[xi][yi]==zuobiao[xi-i][yi-i]) /*ÅжÏÊÇ·ñ5×ÓÁ¬ÔÚÒ»Æð*/
num=num+1;
}
}
if(num==5){
over();
ifover=0;
return;
}
num=1;
for(i=1;i<10;i++){
if(xi+i<=10){
if(zuobiao[xi][yi]==zuobiao[xi+i][yi])
num=num+1;
}
if(xi-i>=1){
if(zuobiao[xi][yi]==zuobiao[xi-i][yi])
num=num+1;
}
}
if(num==5){
over();ifover=0;return;
}
num=1;
for(i=1;i<10;i++){
if(yi+i<=10){
if(zuobiao[xi][yi]==zuobiao[xi][yi+i])
num=num+1;
}
if(yi-i>=1){
if(zuobiao[xi][yi]==zuobiao[xi][yi-i])
num=num+1;
}
}
if(num==5){
over();ifover=0; return;
}
num=1;
for(i=1;i<10;i++){
if(xi+i<=10||yi-i>=1){
if(zuobiao[xi][yi]==zuobiao[xi+i][yi-i])
num=num+1;
}
if(xi-i>=1||yi+i<=10){
if(zuobiao[xi][yi]==zuobiao[xi-i][yi+i])
num=num+1;
}
}
if(num==5){
over();ifover=0; return;
}
}
void player(){
int i;
printf("verson 1.0");
window(10,5,70,20); /*»Ò»¸ö·½¿ò±³¾°ÑÕÉ«ÊÇÂÌÉ«*/
textcolor(BLACK);
textbackground(LIGHTGRAY);
clrscr();
gotoxy(15,4);
cputs("Input your name");
gotoxy(15,7);
cputs("player1:");
gotoxy(15,9);
cputs("player2:");
gotoxy(24,7);
scanf("%s",player1);
gotoxy(24,9);
scanf("%s",player2);
}
void main(){
int i,j;
player();
welcome(); /*µ÷Óû¶Ó½çÃæ*/
while(1){
for(i=0;i<10;i++){
for(j=0;j<10;j++){
zuobiao[i][j]=0;
}
}
ifover=1;
clrscr();
qipan(); /*µ÷ÓóöÆåÅ̵ÄͼÏó*/
moveto(x,y);
setcolor(GREEN);
line(x,y-10,x,y+10); /*Ê×Ïȹ涨³ö¸Õ¿ªÊ¼¹â±êµÄλÖÃ*/
line(x-10,y,x+10,y);
do{
if(who==2)
{
setcolor(RED);
rectangle(120,6,270,18);
setfillstyle(1,YELLOW); /*ÅжϸÃÄĸöÓû§ÏÂ×Ó*/
floodfill(121,9,RED);
setcolor(GREEN);
outtextxy(125,8,player1);
}
else
{setcolor(RED);
rectangle(120,6,270,18);
setfillstyle(1,YELLOW);
floodfill(121,9,RED);
setcolor(GREEN);
outtextxy(125,8,player2);
}
xiazi(); /*ÅжϼüÅ̵ÄÊäÈëÇé¿ö*/
}while(ifover);
}
}
参考资料:http://post.baidu.com/f?kz=109233471
本回答被网友采纳