五子棋:

#include stdio.h

#include windows.h

#include stdlib.h

#include conio.h

#include iostream

using namespace std;//使用getch()函数

int startchoice,winner,player,Q[50][50]= {0};//Q数组记录旗子

char button;//读入键盘输入的指令,如→

struct Point//点坐标的结构体

{

int x,y;

} point,game_xy;

struct Piece//棋子的坐标记录

{

struct Point coord;

//struct Piece *fore;

};

struct Piece *p;

void goto_xy(int x, int y)          //光标移动函数

{

COORD c;

c.X=x*2;

c.Y=y;

SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);

}

void Init()//初始化函数,将记录棋子的数组初始化

{

for(int i=0; i<50; i++)

for(int j=0; j<50; j++)

Q[i][j]=0;

startchoice=0;

player=1;

p=(struct Piece *)malloc(sizeof(struct Piece));

}

void Welcome()      //欢迎界面

{

system("color 2F");//#include  2背景绿色F字体亮白色

goto_xy(10,3);

printf("—→");

goto_xy(17,1);

printf("Menu");

goto_xy(13,3);

printf("   ***Human VS Human***");

goto_xy(13,4);

printf("      ***EXIT***");

point.x=12;

point.y=3;

goto_xy(0,0);

}

void ShowWho()//显示轮到哪一方下棋

{

goto_xy(10,16);

if(player==0)

printf("Player1,it's your turn");

else

printf("Player2,it's your turn");

goto_xy(point.x,point.y);

}

void Draw()                //绘画游戏界面

{

game_xy.x=10;

game_xy.y=0;

system("cls");

system("color 3F");//3湖蓝色F亮白色

goto_xy(12,1);

printf("Welcome!");

//goto_xy(1,16);

//printf("Go back: b");

goto_xy(1,17);

printf("EXIT:    ESC");

const int i=8;//const定义的数据不可以被改变 而且修改数据比较方便

const int j=16;

const int k=3;

goto_xy(game_xy.x-i,game_xy.y+k);//输出甲方的下棋方法

printf("Player1:    ●");

goto_xy(game_xy.x-i,game_xy.y+k+2);

printf("LEFT←");

goto_xy(game_xy.x-i,game_xy.y+k+4);

printf("RIGHT→");

goto_xy(game_xy.x-i,game_xy.y+k+6);

printf("PUSH DOWN:Enter");

goto_xy(game_xy.x+j,game_xy.y+k);//输出乙方的下棋方法

printf("Player2:    ○");

goto_xy(game_xy.x+j,game_xy.y+k+2);

printf("LEFT←");

goto_xy(game_xy.x+j,game_xy.y+k+4);

printf("RIGHT→");

goto_xy(game_xy.x+j,game_xy.y+k+6);

printf("PUSH DOWN:Enter");

for(int k1=0; k1<50; k1++)//初始化棋子记录,在第二局时有明确的作用

for(int k2=0; k2<50; k2++)

Q[k1][k2]=0;

for(int i=0; i<=7; i++)//画棋盘

{

if(i==0)//画第一行

{

goto_xy(10,i+3);

printf("┌┬┬┬┬┬┬┬┐");

}

if(i!=0&&i!=7)//画出中间16行

{

goto_xy(10,i+3);

printf("├┼┼┼┼┼┼┼┤");

}

if(i==7)//画最后一行

{

goto_xy(10,i+3);

printf("└┴┴┴┴┴┴┴┘");

}

}

point.x=17;

point.y=4;

goto_xy(17,4);

}

void Clean()//清除—→运动的轨迹

{

goto_xy(10,3);

printf("     ");

goto_xy(24,3);

printf("     ");

goto_xy(10,4);

printf("     ");

goto_xy(24,4);

printf("     ");

}

void menu_choose(char press)//选择游戏还是退出

{

if(press==72)//↑的ASCLL码

{

if(point.y==3)

point.y=4;

else

point.y=3;

Clean();

goto_xy(10,point.y);

printf("—→");

}

if(press==80)//↓的ASCLL码

{

if(point.y==4)

point.y=3;

else

point.y=4;

Clean();

goto_xy(10,point.y);

printf("—→");

}

if(press==13)// 13:回车键的ASCLL码

{

startchoice=point.y-2;//startchoice为1或2

}

}

/*void go_back(int x1,int y1)//悔棋函数

{

goto_xy(x1,y1);

if(x1==10)

{

if(y1==3)

{

printf("┌");

}

else if(y1==20)

{

printf("└");

}

else

{

printf("├");

}

}

else if(x1==27)

{

if(y1==3)

{

printf("┐");

}

else if(y1==20)

{

printf("┘");

}

else

{

printf("┤");

}

}

else

{

if(y1==3)

{

printf("┬");

}

else if(y1==20)

{

printf("┴");

}

else

{

printf("┼");

}

}

Q[point.x][point.y]=0;//在数组中将弹出的棋子对应的数据设为0

goto_xy(x1,y1);

}*/

void Record()              //记录棋子的情况

{

p->coord.x=point.x;

p->coord.y=point.y;

// ptr=p;

p=(struct Piece *)malloc(sizeof(struct Piece));

// p->fore=ptr;

ShowWho();

Q[point.x][point.y]=player+1;

if(player)

{

player=0;

return;

}

player=1;

goto_xy(point.x,point.y);

}

void PutDown()//显示落子函数

{

int i=0;

for(int a=4;a<=9;a++)

{

if (Q[point.x][a]==0){

i=1;

point.y=a;

}

}

if(i)//先判断该位置是否有棋子

{

if(player)

{

goto_xy(point.x,point.y);

printf("●");

printf("\a");//'\a'表示蜂鸣声

Record();

}

else

{

goto_xy(point.x,point.y);

printf("○");

printf("\a");

Record();

}

goto_xy(point.x,4);

}

}

void Play(char ch)//键盘的操作 移动光标 下棋和悔棋操作

{

if(ch==75)//←的ASCLL码    光标左移

{

if(point.x<=11)

point.x=17;

else

point.x--;

goto_xy(point.x,4);

}

if(ch==77)//→的ASCLL码  光标右移

{

if(point.x>=17)

point.x=11;

else

point.x++;

goto_xy(point.x,4);

}

if(ch==13)//回车键的ASCLL码下棋

{

PutDown();

}

/*if(button=='b'||button=='B')       //悔棋的操作

{

ptr1=p;

if(p!=head)

{

p=p->fore;

free(ptr1);

point.x=p->coord.x;

point.y=p->coord.y;

go_back(point.x,point.y);

}

}*/

}

int Judge()

{

int count=0;

int pp=player==0?2:1;//三目运算

for(int c=0; c<50; c++)

{

for(int r=0; r<50; r++)

{

if(Q[r][c]!=pp)

continue;

//检查列

int rr=r;

int cc=c;

while(--cc>=4 &&Q[rr][cc]==pp)

count++;

cc=c;

while(++cc<9 &&Q[rr][cc]==pp)

count++;

cc=c;

if(count>=3)

return pp;

//检查行

count=0;

while(--rr>=11 &&Q[rr][cc]==pp)

count++;

rr=r;

while(++rr<17 &&Q[rr][cc]==pp)

count++;

rr=r;

if(count>=3)

return pp;

//检查反斜边

count=0;

cc--;

rr--;

while((cc>=4||rr>=11) &&Q[rr][cc]==pp)

{

count++;

cc--;

rr--;

}

rr=r;

cc=c;

cc++;

rr++;

while((cc<9||rr<17) &&Q[rr][cc]==pp)

{

count++;

cc++;

rr++;

}

rr=r;

cc=c;

if(count+1>=4)

return pp;

//检查正斜边

count=0;

cc++;

rr--;

while((cc<9||rr>=11) &&Q[rr][cc]==pp)

{

count++;

cc++;

rr--;

}

rr=r;

cc=c;

cc--;

rr++;

while((cc>=4||rr<17) &&Q[rr][cc]==pp)

{

count++;

cc--;

rr++;

}

rr=r;

cc=c;

if(count+1>=4)

return pp;

count=0;

}

}

return 0;

}

int main(void)

{

system("color 4E");

printf("\nWelcome Puissance4!");

goto_xy(15,3);

printf("designer:zt and hx");

goto_xy(17,5);

//printf(" HE•Honda");

goto_xy(17,7);

//printf(" ZSSB");

Sleep(2000);

system("cls");

while(1)

{

Init();//初始化

int winner=0;

Welcome();//欢迎界面

while(1)//读取菜单选项

{

char choice=getch();

menu_choose(choice);

if(startchoice!=0)

break;

}

if(startchoice==1)//选择人人对战

{

Draw();

goto_xy(10,16);

printf("Player1:it's your turn");

goto_xy(point.x,point.y);

while(1)

{

button=getch();

Play(button);      //读取键盘的操作 移动光标 下棋和悔棋操作

if(button==27)//ESC的ASCLL码

{

if(MessageBox(NULL,TEXT("Are you sure to exit?"),TEXT(""),MB_ICONQUESTION|MB_OKCANCEL)==IDOK)

{

system("cls");

printf("\nThank you!\n");

return 0;

}

}

if(button==13)//回车键ASCLL码按下后开始判断输赢

winner=Judge();

if(winner!=0)

{

goto_xy(10,18);

if(winner==2)

{

cout<<"Congratulations! player1"<

}

if(winner==1)

{

cout<<"Congratulations! player2"<

}

}

if(winner!=1&&winner!=2)

{

continue;

}

goto_xy(10,20);

printf("Continue to play?(Y/N):    ");

while(1)

{

button=getch();

if(button=='n'||button=='N'||button=='y'||button=='Y')

break;

}

if(button=='n'||button=='N')

{

if(MessageBox(NULL,TEXT("  Are you sure to exit?"),TEXT(""),MB_ICONQUESTION|MB_OKCANCEL)==IDOK)

{

system("cls");

printf("\nThank you!\n");

return 0;

}

}

if(button=='y'||button=='Y')

{

winner=0;

system("cls");

break;

}

}

}

if(startchoice==2)

{

if(MessageBox(NULL,TEXT("  Are you sure to exit?"),TEXT(""),MB_ICONQUESTION|MB_OKCANCEL)==IDOK)

{

printf("\n\nThank you!\n");

return 0;

}

}

}

return 0;

}

你可能感兴趣的:(五子棋:)