C Language Implementation Gobang small game _c language

Source: Internet
Author: User
Tags flush flushes function definition

First let's look at a slightly simpler implementation:

#include <stdio.h> #include <stdlib.h> #define N int chessboard[n + 1][n + 1] = {0};

int whoseturn = 0;
void Initgame (void);
void Printchessboard (void);
void playchess (void);

int judge (int, int);

 int main (void) {initgame ();

  while (1) {whoseturn++;
 Playchess ();
return 0;

 } void Initgame (void) {char c;
 printf ("Please input \ ' y\ ' to enter the game:");
 c = GetChar ();

 if (' Y '!= c && ' y '!= c) exit (0);
 System ("CLS");
Printchessboard ();

 } void Printchessboard (void) {int I, J;
   for (i = 0; I <= N; i++) {for (j = 0; J <= N; j +) {if (0 = i) printf ("%3d", j);
   else if (j = = 0) printf ("%3d", I);
   else if (1 = = Chessboard[i][j]) printf ("O");
   else if (2 = = Chessboard[i][j]) printf ("X");
  else printf ("*");
 printf ("\ n");

 } void Playchess (void) {int I, J, winner;
  if (1 = whoseturn% 2) {printf ("Turn to player 1, please input the position:"); scanf ("%d%d", &i, &j);
   while (Chessboard[i][j]!= 0) {printf ("This position has been occupied, please input the position again:");
  scanf ("%d%d", &i, &j);
 } Chessboard[i][j] = 1;
  else {printf ("Turn to player 1, please input the position:");

  scanf ("%d%d", &i, &j);
   while (Chessboard[i][j]!= 0) {printf ("This position has been occupied, please input the position again:");
  scanf ("%d%d", &i, &j);
 } Chessboard[i][j] = 2;
 System ("CLS");

 Printchessboard ();
   if (judge (I, J)) {if (1 = whoseturn% 2) {printf ("Winner is player 1!\n");
  Exit (0);
   else {printf ("Winner is player 2!\n");
  Exit (0);
 an int judge (int x, int y) {int i, J;

 int t = 2-whoseturn% 2; for (i = x-4, j = y; I <= x; i++) {if (I >= 1 && i <= N-4 && t = = Chessboard[i][j] && Amp t = = Chessboard[i + 1][j] && t = = chessboard[i + 2][j] && t = = chessboard[i + 3][j] && t = = CHESSB OArd[i + 4][j]) return 1; for (i = x, j = y-4 J <= y; j +) {if (J >= 1 && J <= N-4 && t = = Chessboard[i][j] &am p;& T = = chessboard[i][j + 1] && t = = chessboard[i][j + 1] && t = = chessboard[i][j + 3] && t =
 = Chessboard[i][j + 4]) return 1; for (i = x-4, j = y-4 I <= x, J <= y; i++, J + +) {if (I >= 1 && i <= N-4 && j) ; = 1 && J <= N-4 && t = chessboard[i][j] && t = chessboard[i + 1][j + 1] && t = = ch
 Essboard[i + 2][j + 2] && t = = chessboard[i + 3][j + 3] && T = = chessboard[i + 4][j + 4]) return 1; for (i = x + 4, j = y-4 I >= 1, j <= y; I--, j + +) {if (I >= 1 && i <= N-4 && J ; = 1 && J <= N-4 && t = chessboard[i][j] && t = = chessboard[i-1][j + 1] && t = = ch Essboard[i-2][j + 2] && t = = chessboard[i-3][j + 3] && t = = CHessboard[i-4][j + 4]) return 1;
return 0;

 }

Demo screenshot

Let's look at a more complicated one.

C Language Implementation Gobang small game # include<stdio.h> # include<stdio.h> # include<stdio.h> # include<stdio.h>// Insert Input Output header file # include<string.h>//character array function definition header file # include<stdlib.h>//stdlib header file that standard
Library standard header File Stdlib header file contains the C, C + + language of the most commonly used system functions, the file contains the C language standard library function definition. # define SPA 0//in a C or C + + language source program allows an identifier to be used to represent a string, called a macro.
"Define" defines a command for a macro. # define man 1 # define COM 2/* Empty location set to 0, the player under the position set to 1, the computer under the position of 2 */#define ORDEX int Qipan[ordex][ordex];
 Its own defined preprocessing function, in the form of two-dimensional array to build the chessboard//15*15 of the chessboard/typedef struct//typedef is defined in c a struct type {int iflag;
int IX, IY;
}scmpiece;
int G_ipiecelen;

Scmpiece g_scmgame1, g_scmgame2; void Start (); /* The main control function of the program/Void Draw (); /* Draw chessboard/int victory (SCMPIECE * pscmpiece);  /* Judgment Victory P Q for the judgment point coordinates/void AI (int *p,int *q);  * * The computer P Q back to the coordinates */int value (int p,int q); /* Compute the value of the null point p Q/int qixing (int n,int p,int q); /* Return the null point P Q in the n direction of the chess type N is 1-8 direction from the right clockwise start number/void Yiwei (int n,int *i,int *j);
 /* in n direction to coordinate I j shift N is 1-8 direction from right clockwise start number/void Initgame () {int i; G_ipiecelEn = Ordex * ORDEX;
 G_scmgame1.ix = 0;
 G_scmgame1.iy = 0;
 G_scmgame1.iflag = COM;
 G_scmgame2.ix = 7;
 G_scmgame2.iy = 7;
 G_scmgame2.iflag = Mans;
for (i=0 i < Ordex*ordex ++i) * ((int*) Qipan + i) = SPA;
 } void Main () {char k;
  do{Initgame ();
  Start (); printf ("Do you want another one?" Enter Y or N: "); GetChar ();
  scanf ("%c", &k);
  while (k!= ' y ' &&k!= ' n ') {printf ("input error, reenter \ n"); scanf ("%c", &k);}
 System ("CLS");
 }while (k== ' y ');
printf ("Thank you for using!\n");
  } void Makepiece (Scmpiece * pscmpiece, int igameflag) {if (Igameflag) {printf ("please:"); while (scanf ("%d%d", &pscmpiece->ix, &pscmpiece->iy)) {if (Pscmpiece->ix < 0 | | pscmpiece-& Gt;ix > ORDEX-1) | |
    (Pscmpiece->iy < 0 | | | pscmpiece->iy > ORDEX-1))
   printf ("Coordinate Error! Please re-enter:");
   else if (Qipan[pscmpiece->ix][pscmpiece->iy]) printf ("This position already has a child!");
  else break;
 }} Qipan[pscmpiece->ix][pscmpiece->iy] = pscmpiece->iflag;
 --g_ipiecelen; System ("CLS ");
 Draw ();
if (igameflag = 0) printf ("Computer under%d%d\n", Pscmpiece->ix, Pscmpiece->iy);
 } void Start () {int choice; Scmpiece ScmGameTemp1, scmgametemp2;/* A1 B1 storage player coordinates C1 d1 Storage computer coordinates/printf ("\t╔═══════════════════════════════╗\n"
 );
 printf ("\t║║\n");
 printf ("\t║ Welcome to use Gobang vs program wish you play a happy challenge without limit ║\n");
 printf ("\t║║\n");     printf ("\t║._______________________. 
 ║\n "); printf ("\t║|     _____________________ |
 ║\n "); printf ("\t║|     I I |
 ║\n "); printf ("\t║|     I I |
 ║\n "); printf ("\t║|     I Gobang I |
 ║\n "); printf ("\t║|     I I |
  ║\n "); printf ("\t║|     i___________________i |
 ║\n ");     printf ("\t║!_______________________!
  ║\n ");       printf ("\t║._[__________]_.
 ║\n ");     printf ("\t║.___|_______________|___.
 ║\n ");      printf ("\t║|::: ____ | 
 ║\n "); printf ("\t║|      ~ ~ ~ ~ [CD-ROM] |
 ║\n ");      printf ("\t║!_____________________!
 ║\n "); Printf ("\t║║\n");
 printf ("\t║║\n");   printf ("\t║ Cold stars Brook Month sparse star Head, flower remnant Hile and White Lotus."
 ║\n ");   printf ("\t║ Rain moon Venus chasing black Jade, Pine Hill new night, the fishy."
 ║\n ");   printf ("\t║ star Moon Long Gorge constant current, White Lotus hanging Qiao Yunpu."
 ║\n ");   printf ("\t║ Black Jade Silver Moon Star, Oblique Moon is a friend."
 ║\n ");   printf ("\t║ 26 innings first abandoned two, pointed at tour stars oblique Comet."
 ║\n ");
 printf ("\t║║\n");
 printf ("\t║║\n");
 printf ("\t║1. Human-Computer vs. 2. Everyone vs. ║\n");
 printf ("\t║║\n");
 printf ("\t╚═══════════════════════════════╝\n");
 printf ("\t\t\t Please enter 1 or 2:");
 while (scanf ("%d", &choice), choice!=1 && choice!=2) printf ("Input error, please re-enter:"); 
 System ("CLS"); if (choice = = 1) {/* man machine mode/printf ("\t\t\t Welcome to use Gobang Man-machine game!") \n\t\t Please enter the coordinates (such as 13 6) separated by a space. 
  \n\n\n ");
  Draw ();
  printf ("Please press 1 first, then press 2 below");
  while (scanf ("%d", &choice), choice!=1 && choice!=2) printf ("Input error, please re-enter:");
  if (choice = = 2) makepiece (&g_scmgame2, 0);
 Choice = 1; else {priNTF ("Welcome to use Gobang for everyone!") Please input coordinates (such as 13 6). Undo Please enter 15 15. 
  \n\n\n ");
 Draw ();
  while (G_ipiecelen) {makepiece (&g_scmgame1, 1); if (Victory (&g_scmgame1)) {printf ("Computer God Horse" is a cloud!!!
   \ n ");
  Return
   }/* Player wins/if (choice = 1) {AI (&g_scmgame2.iy, &g_scmgame2.ix);
   Makepiece (&g_scmgame2, 0);
    if (Victory (&g_scmgame2)) {/* computer win/int ichoice; printf ("Want to undo?")
Please enter Y or n: ");
    Startflage:setbuf (stdin, NULL);
    scanf ("%c", &ichoice);
     Switch ((char) ichoice) {case ' Y ': Case ' y ': G_ipiecelen = 2;
     Qipan[g_scmgame2.ix][g_scmgame2.iy] = SPA;
     Qipan[g_scmgame1.ix][g_scmgame1.iy] = SPA; System ("CLS");
     Draw ();
    Break Case ' n ': Cases ' n ': printf ("The computer is very normal, please do not lose heart!!!
     \ n ");
    Return
     default:printf ("Input error, please re-enter:");
    Goto Startflage;
   }} else {makepiece (&g_scmgame2, 1); if (Victory (&g_scmgame2)) {printf ("Computer God Horse" is a cloud!!!
    \ n "); RetuRn
}} printf ("Draw \ n");
 } void Draw ()/* Draw chessboard/{int i,j;
 Char p[15][15][4];
   for (j=0;j<15;j++) for (i=0;i<15;i++) {if (QIPAN[J][I]==SPA) strcpy (p[j][i), "n");
   if (Qipan[j][i]==man) strcpy (P[j][i], "a");
  if (qipan[j][i]==com) strcpy (P[j][i], "a"); 
 printf ("0 1 2 3 4 5 6 7 8 9");
 printf ("┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐\n"); for (i=0,j=0;i<14;i++,j++) {printf ("%2d│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%d\n", j,p[i][0],p[i][1], P[i][2],p[i][3],p[i][4],p[i][5],p[i][6],p[i][7],p[i][8],p[i][9],p[i][10],p[i][11],p[i][12],p[i][13],p[i][14],j
 );
 printf ("├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤\n"); printf ("14│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│0\n", p[14][0],p[14][1],p[14][2],p[14][3],p[14][4],p[
 14][5],P[14][6],P[14][7],P[14][8],P[14][9],P[14][10],P[14][11],P[14][12],P[14][13],P[14][14]);
 printf ("└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘\n");
printf ("0 1 2 3 4 5 6 7 8 9"); static int Directionvalue (int * PiX, int * piy, int idirection) {switch (idirection) {case 0:--*pix;
 Break
  Case 4: ++*pix;
 Break
  Case 1:--*piy;
 Break
  Case 5: ++*piy;
 Break Case 2:--*pix;
  --*piy;
 Break Case 6: ++*pix;
  ++*piy;
 Break Case 3:--*pix;
  ++*piy;
 Break Case 7: ++*pix;
  --*piy;
 Break
 } if ((*pix >= 0 && *pix <= ORDEX-1) && (*piy >= 0 && *piy <= ORDEX-1)) return 1;
else return 0;
 static int Subvictory (SCMPIECE * pscmpiece, int idirection) {int IX = pscmpiece->ix;
 int IY = pscmpiece->iy;
 int ipiececount = 1;
 while (Directionvalue (&ix, &iy, idirection) && qipan[ix][iy] = = Pscmpiece->iflag) ++ipiececount;
 IX = pscmpiece->ix;
 IY = pscmpiece->iy; 
 while (Directionvalue (&ix, &iy, idirection+4) && qipan[ix][iy] = = Pscmpiece->iflag) ++ipiececount;
return ipiececount; The int victory (SCMPIECE * pscmpiece)/* judgment Victory P Q is the judge point coordinates, the Victory returns 1, otherwise returns 0 * * {int idirection = 0;
 while (Idirection < 4) if (Subvictory (pscmpiece, idirection++) >= 5) return 1;
return 0;  } void AI (int *p,int *q)/* Computer *p *q return coordinates * * {int i,j,k,max=0,i,j; /* I J for the next point coordinate * * (j=0;j<15;j++) for (i=0;i<15;i++) if (QIPAN[J][I]==SPA) {/* Calendar board, encountered a null point is calculated value, take the maximum value point.
    * * K=value (I,J); if (K>=max) {i=i; J=j; Max=k; }} *p=i;
*q=j; 
 The value of the int value (int p,int q)/* To compute the null point P Q is returned with k/{int n=1,k=0,k1,k2,k1,k2,x1,y1,z1,x2,y2,z2,temp; int a[2][4][4]={
   40,400,3000,10000,6,10,600,10000,20,120,200,0,6,10,500,0,30,300,2500,5000,2,8,300,8000,26,160,0,0,4,20,300,0};   /* Array A to store the value of their own and each other a total of 32 kinds of chess-type values of the 0 of each other 1 live 0 Punch 1 empty Live 2 empty flushes 3 sub 0-3 (0 for 1 children, 3 for 4) */while (n!=5) {k1=qixing (n,p,q); n+=4; /* K1,K2 is 2 reverse direction of the Chess type number * * * k2=qixing (N,P,Q);
  n-=3; if (K1&GT;K2) {temp=k1; K1=k2 k2=temp}/* Make the number small for K1, large for K2 */K1=K1;  K2=K2; /* K1 K2 Storage K1 K2 number/z1=k1%10; z2=k2%10; k1/=10; k2/=10; y1=k1%10; y2=k2%10; k1/=10; k2/=10; x1=k1%10;
   x2=k2%10; /* X Y Z, respectively, representing 0 of each other's 1 Live 0 flushes 1 empty Live 2 empty flushes 3 The number of children 0-3 (0 for 1, 3 for 4)/if (k1==-1) {if (k2<0) {k+=0; continue;} else k+=a[x2][y2][z2]+5; continue;};  /* Empty chess and other */if (k1==-2) {if (k2<0) {k+=0; continue;} else K+=A[X2][Y2][Z2]/2; continue;}; /* Boundary Punch type and other */if (k1==-3) {if (k2<0) {k+=0; continue;} else K+=A[X2][Y2][Z2]/3; continue;}; /* Boundary empty flush chess type and other */if ((k1>-1&&k1<4) && ((k2>-1&&k2<4) | | (k2>9&&k2<14)) | | ((k1>99&&k1<104) && ((k2>99&&k2<104) | | (k2>109&&k2<114)))
   {* * * * Live oneself live to live to live on the living to live to live against the game of the chess-type assignment/if (z1+z2>=2) {k+=a[x2][y2][3]; continue}
  else {k+=a[x2][y2][z1+z2+1]; continue;} if ((k1>9&&k1<14) && (k2>9&&k2<14)) | | ((k1>109&&k1<114) && (k2>109&&k2<114))
   {* * * * has been punching the hedge against the value of the chess-type assignment/if (z1+z2>=2) {k+=10000; continue}
  else {k+=0; continue;} if ((k1>-1&&k1<4) && ((k2>99&&k2<104) | | (k2>109&&k2<114)) | | ((k1>9&&k1<14) && ((k2>99&&k2<104) | | (k2>109&&k2<1 14))) {* * Live to live to live against the right to live in the hedge of the chess-type assigned value */if (z1==3| |
   z2==3) {k+=10000; continue;} 
  else {K+=A[X2][Y2][Z2]+A[X1][Y1][Z1]/4; continue;}
else {K+=A[X1][Y1][Z1]+A[X2][Y2][Z2]; continue}/* Other chess type's assignment/} return k; the int qixing (int n,int p,int q)/* return null point P Q in the n direction of the Chess model N is 1-8 direction from the right clockwise start number/{int k,m=0;/* Chess model NOTE: Live 000-003 have 010-013 to live 1
 00-103 Hedge 110-113 live 020-023 empty punch 030-033 to empty live 120-123 to air flush 130-133 empty-1 boundary punch-2 boundary air flush -3*/Yiwei (N,&AMP;P,&AMP;Q); if (p<0| | p>14| | q<0| |          Q&GT;14) k=-2;
     /* Boundary Punch Type */switch (Qipan[q][p]) {case com:{m++ Yiwei (n,&p,&q); if (p<0| | p>14| | q<0| |
     Q&GT;14) {k=m+9; return k;} while (qipan[q][p]==com) {m++ Yiwei (n,&p,&q); if (p<0| | p>14| | q<0| |
     Q&GT;14) {k=m+9; return k;}}       if (QIPAN[Q][P]==SPA) k=m-1;           /* Own Wooki type */Else k=m+9; * * own punch type/}Break
     Case man:{m++; Yiwei (N,&AMP;P,&AMP;Q); if (p<0| | p>14| | q<0| |
     Q&GT;14) {k=m+109; return k;} while (Qipan[q][p]==man) {m++ Yiwei (n,&p,&q); if (p<0| | p>14| | q<0| |
     Q&GT;14) {k=m+109; return k;}}       if (QIPAN[Q][P]==SPA) k=m+99;          * * each other Wooki type * * Else k=m+109;
  * * Each other Chong chess type/}break;
     Case spa:{Yiwei (n,&p,&q); if (p<0| | p>14| | q<0| |   Q&GT;14) {k=-3; return k;}
        /* Boundary Empty punch Chess type */switch (Qipan[q][p]) {case com:{m++; Yiwei (N,&AMP;P,&AMP;Q); if (p<0| | p>14| | q<0| |
        Q&GT;14) {k=m+29; return k;} while (qipan[q][p]==com) {m++ Yiwei (n,&p,&q); if (p<0| | p>14| | q<0| |
        Q&GT;14) {k=m+29; return k;}}      if (QIPAN[Q][P]==SPA) k=m+19;          /* Own air Wooki type */Else k=m+29;
     * * Your own empty punch chess type */}break;
        Case man:{m++; Yiwei (N,&AMP;P,&AMP;Q); if (p<0| | p>14| | q<0| |
        Q&GT;14) {k=m+129; return k;} while (Qipan[q][p]==man) {m++ Yiwei (n,&p,&q); if (p<0| | p>14| | q<0| |   
        Q&GT;14) {k=m+129; return k;}}      if (QIPAN[Q][P]==SPA) k=m+119;          * * Each other air Wooki type */Else k=m+129;
     * * Each other empty punch chess type * * *}break; Case Spa:k=-1;           Break
 * * Empty chess type */}}break;
} return k;
 } void Yiwei (int n,int *i,int *j)/* Shift coordinates I j on n direction from right clockwise starting number/{switch (n) {Case 1: *i+=1; break; Case 2: *i+=1; *j+=1;
 Break Case 3: *j+=1;
 Break Case 4: *i-=1; *j+=1;
 Break Case 5: *i-=1;
 Break Case 6: *i-=1; *j-=1;
 Break Case 7: *j-=1;
 Break Case 8: *i+=1; *j-=1;
 Break }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.