简易双人五子棋

    在最初学习了C语言和数据结构后就尝试着写写一个较为高端的程序,这个高端也是相对于当时掌握的只是来说的,于是我便写了这个简易小游戏,双人五子棋。由于水平有限,写的很粗糙,不过不管怎么说也是第一个独自完成的大点的程序,还有就是这个调试环境为DEV C++。下面是这个程序的代码:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define STACK_SIZE 81
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef struct
{
	int qp[15][15];
	int sum[5];
	int ch;
	int yon; 
}data;
typedef struct Node
{
 ElemType data[10];
 int top;
}seqstack; 
void initStack(seqstack *s) {
	s->top = -1;
}
int Empty(seqstack *s) {
	return (s->top == -1);
}
int Full(seqstack *s) {
	return (s->top == STACK_SIZE - 1);
}
int push(seqstack *s, ElemType elem) {
	if(Full(s))
		return FALSE;
	s->top++;
	s->data[s->top] = elem;
	return TRUE;
}
int pop(seqstack *s, ElemType *elem) {

	if(Empty(s))
		return FALSE;
	*elem = s->data[s->top];
	s->top--;
	return TRUE;
}
void wqizi()
{
	printf("●");
} 
void bqizi()
{
	printf("○");
} 
void gotoxy(int x,int y)  //通过坐标获取光标位置 
{
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
} 
 void qipan(  )//棋盘界面 
{   
	int MAX=29; 
	int i,j; 
    for( i= 1; i<= MAX; ++i )
    {
        for( j= 1; j<=MAX; ++j )
        {
            if( i== 1 )
            {
                if( j== 1 )
                    printf( "┏" );  
                else if( j== MAX )
                    printf( "┓\n" );
                else if( j%2 )
                    printf( "┳" ); // 横向占两个坐标位,竖向占一个坐标位 
                else
                    printf( "━" );
            }
            else if( i== MAX )
            {
                if( j== 1 )
                    printf( "┗" );
                else if( j== MAX )
                    printf( "┛\n" );
                else if( j%2 )
                    printf( "┻" );
                else
                    printf( "━" );
            }
            else
            {
                if( j== 1 )
                {
                    if( i% 2 )
                        printf( "┣" );
                    else
                        printf( "┃" );
                }
                else if( j== MAX )
                {
                    if( i% 2 )
                        printf( "┫\n" );
                    else
                        printf( "┃\n" );
                }
                else
                {
                    if( i% 2 )
                    {
                        if( j% 2 )
                            printf( "╋" );
                        else
                            printf( "━" );
                    }
                    else
                    {
                        if( j% 2 )
                            printf( "┃" );
                        else  
                            printf( "  " ); 
                    }
                }
            }
        }
    }
    gotoxy(60,12);
	printf("上下左右分别通");
	gotoxy(60,14);
	printf("过W,S,A,D按键");
	gotoxy(60,16);
	printf("来控制,落子通");
	gotoxy(60,18);
	printf("过按键Y来实现");
}
judgeL(seqstack *le,data *da,int x,int y,int xz)//判断水平方向是否满足获胜条件 
{
	int r1,m;
    r1=x;
	da->sum[1]=0;
  	initStack(le);
	while(da->qp[r1][y]==xz)
	{
	  r1--;
 	}
 	m=r1+1;
	while(da->qp[m][y]==xz)
	{
	  push(le, da->qp[m][y]);
	  m++;
	}
	while(!Empty(le)) {
	int tmp;
	pop(le, &tmp);
	da->sum[1]=da->sum[1]+tmp;
	}
}
judgeV(seqstack *le,data *da,int x,int y,int xz)//判断竖直方向是否满足获胜条件 
{
	int r1,m;
    r1=y;
	da->sum[2]=0;
  	initStack(le);
	while(da->qp[x][r1]==xz)
	{
	  r1--;
 	}
 	m=r1+1;
	while(da->qp[x][m]==xz)
	{
	  push(le, da->qp[x][m]);
	  m++;
	}
	while(!Empty(le)) {
	int tmp;
	pop(le, &tmp);
	da->sum[2]=da->sum[2]+tmp;
	}
}
judge45(seqstack *le,data *da,int x,int y,int xz)//判断向左45度是否满足获胜条件 
{
	int r1,r2,m1,m2;
    r1=x;
    r2=y;
	da->sum[3]=0;
  	initStack(le);
	while(da->qp[r1][r2]==xz)
	{
	  r1--;
	  r2--;
 	}
 	m1=r1+1;
 	m2=r2+1;
	while(da->qp[m1][m2]==xz)
	{
	  push(le, da->qp[m1][m2]);
	  m1++;
	  m2++;
	}
	while(!Empty(le)) {
	int tmp;
	pop(le, &tmp);
	da->sum[3]=da->sum[3]+tmp;
	}
}
judge135(seqstack *le,data *da,int x,int y,int xz)//判断向右45度是否满足获胜条件 
{
	int r1,r2,m1,m2;
    r1=x;
    r2=y;
	da->sum[4]=0;
  	initStack(le);
	while(da->qp[r1][r2]==xz)
	{
	  r1++;
	  r2--;
 	}
 	m1=r1-1;
 	m2=r2+1;
	while(da->qp[m1][m2]==xz)
	{
	  push(le,da->qp[m1][m2]);
	  m1--;
	  m2++;
	}
	while(!Empty(le)) {
	int tmp;
	pop(le, &tmp);
	da->sum[4]=da->sum[4]+tmp; 
	}
}
void weizhi(data *da,int x,int y)//用来判断此位置是否已经落子 
{
	if(da->qp[x][y]==1 || da->qp[x][y]==2)
	{
	  gotoxy(65,10);
	  printf("此位置不合法");
	  Sleep(1000);
	  gotoxy(65,10);
	  printf("            ");
      fflush(stdout);  
	  da->ch=1; 
	}
}
zlyj(seqstack *le,data *da)//让用户选择是否再来一局 
{
	int s=0;
	da->yon=0;
	gotoxy(20,9);
	printf("按Y再来一局,按N退出");
	da->yon=getch();
	while(da->yon!='Y' && da->yon!='y' && da->yon!='N' && da->yon!='n')
	{
		da->yon=getch();
	}
	if(da->yon=='Y' || da->yon=='y'){gotoxy(0,0);qipan();dire(le,da);}
    if(da->yon=='N' || da->yon=='n')da->yon=1;
}
void dire(seqstack *le,data *da)//用来控制棋子的移动及落子操作的实现 
{
	int x=0,y=0,a,b,n=1,pd=2;
	int i,j,m,o[3]={0,0,0};
	gotoxy(65,5);
	printf("执黑子行");
	gotoxy(x,y);
	for(i=0;i<=15;i++)
	for(j=0;j<=15;j++)
	da->qp[i][j]=0;
	da->qp[x][y]=0;
	while(1)
	{
		int sum1,sum2;
		a=getch();
		b=pow(-1,n);
	    if(a=='a' || a=='A')
	    {
	    	if(x<1)
			continue; 
	    	gotoxy(4*(x-1),y*2);
	    	x=x-1;
	    	pd=1;
	    }
	    if(a=='d' || a=='D')
	    {
	    	if(x>13)
	    	continue;
	   	    gotoxy((x+1)*4,y*2);
	    	x=x+1;
	    	pd=1;
	    }
	    if(a=='w' || a=='W')
	    {
	    	if(y<1)
			continue;
	    	gotoxy(x*4,(y-1)*2);
	    	y=y-1;
	    	pd=1;
	    }
	    if(a=='s' || a=='S')
	    {
	    	if(y>13)
	    	continue;
	    	gotoxy(x*4,(y+1)*2);
	    	y=y+1;
	    	pd=1;
	    }
	    if(b==-1 && (a=='y' || a=='Y') && pd==1)
	      { 
			weizhi(da,x,y);
			if(da->ch==1){da->ch=0;continue;}
	      	bqizi();
	      	o[1]++;
	      	n++;
	      	pd=0;
	      	da->qp[x][y]=1;
	      	gotoxy(65,5);
	      	printf("执白子行");
			gotoxy(x*4+2,y*2); 
			judgeL(le,da,x,y,1);
	      	judgeV(le,da,x,y,1);
	      	judge45(le,da,x,y,1);
	      	judge135(le,da,x,y,1);
	      	if(da->sum[1]>5 || da->sum[2]>5 || da->sum[3]>5 || da->sum[4]>5)
	      	{
	      		gotoxy(20,5);
	      		printf("由禁手规则,白棋获胜!"); 
	      		zlyj(le,da);
	      		if(da->yon==1)return 0;
	      	}
	      	if(da->sum[1]==5 || da->sum[2]==5 || da->sum[3]==5 || da->sum[4]==5)
	      	{
	      		gotoxy(25,5);
				printf("黑棋胜!");
	      		zlyj(le,da);
	      		if(da->yon==1)return 0;
	      	}
	      }
	    if(b==1 && (a=='y' || a=='Y') && pd==1)
	      {
			weizhi(da,x,y);
			if(da->ch==1){da->ch=0;continue;}
	      	wqizi();
	      	o[0]++;
	      	n++;
	      	pd=0;
	      	da->qp[x][y]=2;
	      	gotoxy(65,5);
	      	printf("执黑子行");
	      	gotoxy(x*4+2,y*2);
	      	judgeL(le,da,x,y,2);
	      	judgeV(le,da,x,y,2);
	      	judge45(le,da,x,y,2);
	      	judge135(le,da,x,y,2);
	      	if(da->sum[1]>=10 || da->sum[2]>=10 || da->sum[3]>=10 || da->sum[4]>=10)
	      	{
	      	  gotoxy(25,7);
			  printf("白棋胜!");
	      	  zlyj(le,da);
	      	  if(da->yon==1)return 0;
	      	}
	      }
	      if(pd==2 && (a=='y' || a=='Y'))
	      {
	      	bqizi();
	      	o[2]=1;
	      	n++;
	      	pd=0;
	      	da->qp[x][y]=1;
	      	gotoxy(65,5);
	      	printf("执白子行");
	      	gotoxy(x*4+2,y*2);
	      } 
          if(o[0]+o[1]+o[2]==225)
	      {
	      	gotoxy(25,7);
	      	printf("棋局已满");
		    zlyj(le,da);
	      	if(da->yon==1)return 0;
          }
	}
}
int main()
{
    gotoxy(32,10);
    printf("欢迎使用双人对战五子棋系统!");
    Sleep(2000);
    system("cls");
    fflush(stdout);
	seqstack le;
	data da;
	system("mode con cols=80 lines=40");
    qipan();
    dire(&le,&da);
}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值