C语言教程:简易五子棋程序  收集于网络  

/* 纯用 字符 和数组编的五子棋,棋盘也是用字符画的。     编了1上午了,主要是算法跟按键比较烦,发现有bug-- 按键速度过快会产生延时显示,可能是算法不好。      操作:玩家1  a,s,w,d(方向)     空格(落子)           玩家2  上、下、左、右      回车 (落子)           ESC 退出    编译测试环境:TC3.0  */ 

 #include <stdio.h> 

 #include <stdlib.h>  

#include <bios.h>  

#include <conio.h>  

 #define CRRU     0xbf  /*右上角点   197*/  

#define CRLU     0xda  /*左上角点   218*/ 

 #define CRLD     0xc0  /*左下角点   192*/  

#define CRRD     0xd9  /*右下角点   217*/  

#define CRL      0xc3  /*左边       195*/ 

 #define CRR      0xb4  /*右边       190*/  

#define CRU      0xc2  /*上边       194*/ 

 #define CRD      0xc1  /*下边       193*/  

#define CR       0xc5  /*十字交叉点 197*/   

#define size     19   

char a[size][size];  

int i,j;                 //跟踪光标在数组中对应的位置    

     int x=10;  int y=3;                 //光标所在位的坐标  

  int side=1;              //持子方   1为玩家12为玩家2  

int CB=1; int CW=2;      // 棋子图形      

void inita()   

void inits();  

void pressco(int );  

void pressct(int );  

int judge(int);   

int main(){       

 inita();        

inits();        

getch();       

while(1){       

int press=bioskey(0);        

if(press==283)break;        

if(side==1){                   

pressco(press);                  

 if(side==2)if(judge(1)==1) {                  

 gotoxy(1,1);                    

 printf("the play1 win");break;}                           

if(side==2){                  

 pressct(press);                  

 if(side==1)if(judge(2)==1){                   

  gotoxy(1,1);                    

 printf("the play2 win");break;}                 

           

  

getch();  

return 0;    

void inita()             //数组初始化;      

      a[0][0]=CRLU;  

  a[0][size-1]=CRRU;    

a[size-1][0]=CRLD;   

 a[size-1][size-1]=CRRD;  

for(int i=1;i<size-1;i++)         

 a[0][i]=CRU;      

a[size-1][i]=CRD;     

 a[i][0]=CRL;      

a[i][size-1]=CRR;     

 for(int j=1;j<size-1;j++)a[i][j]=CR;       

 return        

void inits()         //界面初     

 for(int i=0;i<size;i++){      

 gotoxy(x,y+i);       

for(int j=0;j<size;j++)putch(a[i][j]);              

gotoxy(x,y);    i=0;j=0;    

return        

 void pressco(int m){      

 switch(m){          case 7777:       //A        

  if(i>0)     {i--;x--;gotoxy(x,y);} break;         

 case 8051:       //S         

 if(j<size-1){j++;y++;gotoxy(x,y);} break;          case 4471:       //w        

  if(j>0)     {j--;y--;gotoxy(x,y);} break;          case 8292:       //D        

  if(i<size-1){i++;x++;gotoxy(x,y);} break;          case 14624:      //空格        

 if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CB;putch(CB);gotoxy(x,y);side=2;}break;       

  default: break;                   

  return                 

 void pressct(int m){       

switch(m){          

case 19200:       //         

 if(i>0)     {i--;x--;gotoxy(x,y);} break;         

 case 20480:       //         

 if(j<size-1){j++;y++;gotoxy(x,y);} break;         

 case 18432:       //        

  if(j>0)     {j--;y--;gotoxy(x,y);} break;         

 case 19712:       //         

 if(i<size-1){i++;x++;gotoxy(x,y);} break;         

 case 7181:        //回车         

if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CW;putch(CW);gotoxy(x,y);side=1;}break;       

  default: break;                         

 return               }

 这是一个通过用C语言编程而做的一个小游戏————五子棋

首先通过以下的C语言程序 绘制出一个简易的棋盘 并规定出一个棋盘的尺寸

#include <stdio.h> 

 #include <stdlib.h>  

#include <bios.h>  

#include <conio.h>  

 #define CRRU     0xbf  /*右上角点   197*/  

#define CRLU     0xda  /*左上角点   218*/ 

 #define CRLD     0xc0  /*左下角点   192*/  

#define CRRD     0xd9  /*右下角点   217*/  

#define CRL      0xc3  /*左边       195*/ 

 #define CRR      0xb4  /*右边       190*/  

#define CRU      0xc2  /*上边       194*/ 

 #define CRD      0xc1  /*下边       193*/  

#define CR       0xc5  /*十字交叉点 197*/   

#define size     19   

char a[size][size];  

int i,j;                 //跟踪光标在数组中对应的位置    

然后通过以下的语句,定义出一个数组,因为是两个玩家一起来玩,通过对数组的使用而定义出两个玩家

 int side=1;              //持子方   1为玩家12为玩家2  

int CB=1; int CW=2;      // 棋子图形      

void inita()   

void inits();  

void pressco(int );  

void pressct(int );  

int judge(int);   

int main(){       

 inita();        

inits();        

getch();       

while(1){       

int press=bioskey(0);        

if(press==283)break;        

if(side==1){                   

pressco(press);                  

 if(side==2)if(judge(1)==1) {                  

 gotoxy(1,1);                    

 printf("the play1 win");break;}                           

if(side==2){                  

 pressct(press);                  

 if(side==1)if(judge(2)==1){                   

  gotoxy(1,1);                    

 printf("the play2 win");break;}                 

           

  

getch();  

return 0;    

最后通过对switch case 这种的选择语句,而规定出两个玩家的不同的操作方式,并且这两个人的按键并不冲突

从而是两个人用一个键盘对战的时候能够更方便的对战 智商的比拼

switch(m){          case 7777:       //A        

  if(i>0)     {i--;x--;gotoxy(x,y);} break;         

 case 8051:       //S         

 if(j<size-1){j++;y++;gotoxy(x,y);} break;          case 4471:       //w        

  if(j>0)     {j--;y--;gotoxy(x,y);} break;          case 8292:       //D        

  if(i<size-1){i++;x++;gotoxy(x,y);} break;          case 14624:      //空格        

 if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CB;putch(CB);gotoxy(x,y);side=2;}break;       

  default: break;                   

  return                 

 void pressct(int m){       

switch(m){          

case 19200:       //         

 if(i>0)     {i--;x--;gotoxy(x,y);} break;         

 case 20480:       //         

 if(j<size-1){j++;y++;gotoxy(x,y);} break;         

 case 18432:       //        

  if(j>0)     {j--;y--;gotoxy(x,y);} break;         

 case 19712:       //         

 if(i<size-1){i++;x++;gotoxy(x,y);} break;         

 case 7181:        //回车         

if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CW;putch(CW);gotoxy(x,y);side=1;}break;       

  default: break;                         

 return               }

上学期虽然已经学了C语言,但是学习到的知识在各个章节比较零散,并没有进行过统一的综合的设计程序的练习,而通过次程序,把选择语句和数组的应用结合了起来,而且数组因为是在上学期最后学的,理解的并不是十分的扎实,通过此个程序,使我对数组的理解更加深了一步,也可以使我在未来在C语言的程序设计上能够得心应手。