Pure C language to achieve gobang

  • 2020-04-02 02:59:44
  • OfStack

Consideration is being given to adding an MFC interface. It's not man versus machine.

Gobang. C


//Date July 7, 2014 09:53:24
//willows  
//gobang
 
#define _CRT_SECURE_NO_WARNINGS
 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
 
//Checkerboard initialization function
//Chessboard array, ln= Chessboard size, returns Chessboard on success, NULL on failure
void init_Chessboard(char Chessboard[][7], int ln)
{
  if ((Chessboard != NULL) && (ln>0)){
    int i = 0, j = 0;
    for (i = 0; i<ln; ++i){
      for (j = 0; j<ln; ++j){
        Chessboard[i][j] = 't';
      }
    }
  // return Chessboard;
  }
// return NULL;
}
 
 
//Display checkerboard function
void show_Chessboard(char Chessboard[][7], int ln)
{
  assert((Chessboard != NULL) && (ln > 0));
 
  int i = 0, j = 0;
  for (i = 0; i<ln; ++i){
    putchar('t');
    putchar(i + '0');
  }//end for1
  putchar('n');
  putchar('n');
 
  for (i = 0; i<ln; ++i){
    putchar(i + '0');
    for (j = 0; j<ln; ++j){
      if ('t' == Chessboard[i][j]){
        putchar(Chessboard[i][j]);
      }
      else
      {
        putchar('t');
        putchar(Chessboard[i][j]);
      }
 
    }
    putchar('n');
    putchar('n');
  }//end for2
}
 
//Son function
//Success returns 1, failure returns 0
int play(char Chessboard[][7], int ln, int x, int y, char ChessPieces)
{
  assert(Chessboard); //ChessPieces = NULL jump out
  if ((x<ln) && (y<ln) && (x >= 0) && (y >= 0)){
    if ('t' == Chessboard[x][y]){
 
      Chessboard[x][y] = ChessPieces;
      return 1;  //successful
    }
    else
    {
      return 0;
    }//end if2
  }//end if1
  return 0;
}
 
//Plateful judgment
//The board is full of judge // Return when full -1
int full_Chess(char Chessboard[][7], int ln)
{
  int i = 0, j = 0;
  for (i = 0; i<ln; ++i){
    for (j = 0; j<ln; ++j){
      if ('t' == Chessboard[i][j]){
        return 0;  //The board under
      }//end if
    }//end for j
  }//end for i
 
  return 1;//The board is full of
 
}
 
 
//Whether to join the five sub - line judgment function
//Chessboard= checkerboard array,ln= checkerboard width,(XS,YS) direction to determine the starting coordinates,(dx,dy) direction increment mark
//The line returns 1, not 0
int judga_line(char Chessboard[][7], int ln, int XS, int YS, int dx, int dy)
{
  assert((Chessboard != NULL) && (ln > 0));
  if((XS <ln) && (YS<ln)  //The starting point is inside the board
    && (XS >=0) && (YS >=0)
    && (dx != 0 || dy != 0))        //The increment in coordinates is not both 0
  {
 
    if (((XS + dx * 4) > ln) || ((XS + dx * 4)<0) || //Determine terminal coordinates
      ((YS + dy * 4)>ln) || ((YS + dy * 4) < 0) || //Outside the board
      ('t' == Chessboard[XS][YS]))
    {
        return 0;  //Not in the board, or the starting point is no son
    }
    else
    {
      int i = 0;
      for (i = 1; i < 5; ++i){
        if (Chessboard[XS][YS] != Chessboard[XS + (dx * i)][YS + (dy * i)])
        {
          return 0;  //If it's not the same 5 in a row
        }//end if3
      }//end for1
      return 1;  //All five are the same, and all are on the board
    }//end if 2
  }
  return 0;  //Other situations
}
 
//The referee function
//Chessboard array, ln= Chessboard width
//Wins return 1, otherwise returns 0
int judga(char Chessboard[][7], int ln)
{
  assert((NULL != Chessboard) && (ln>0));
  int i = 0, j = 0;
  //Vertically into five sub line judgment
  for (i = 0; i<(ln - 4); ++i){
    for (j = 0; j<ln; ++j){
      if (judga_line(Chessboard, ln, i, j, 1, 0)){
        return 1;
      }
    }//end for_j
  }//end for_i
 
    //Horizontal into five line judgment
  for (i = 0; i<ln; ++i){
    for (j = 0; j<(ln - 4); ++j){
      if (judga_line(Chessboard, ln, i, j, 0, 1)){
        return 1;
      }
    }//end for_j
  }//end for_i
 
    //From the top left to the bottom right into five line judgment
  for (i = 0; i<(ln - 4); ++i){
    for (j = 0; j<(ln - 4); ++j){
      if (judga_line(Chessboard, ln, i, j, 1, 1)){
        return 1;
      }
    }//end for_j
  }//end for_i
 
    //From the bottom left to the top right into five line judgment
  for (i = ln-1; i>(ln-4); --i){
    for (j = 0; j <(ln - 4); ++j){
      if (judga_line(Chessboard, ln, i, j, -1, 1)){
        return 1;
      }
    }//end for_j
  }//end for_i
 
  return 0;  //Can't win
}
 
 
//The main function
 
 
int main()
{
  char CB[7][7];
  char nameA[50] = " The player A";
  char nameB[50] = " The player B";
  int x = -1, y = -1;
  //Initialize the
  init_Chessboard(CB, 7);
 
  printf(" Please enter player A The name of the :");
  scanf("%s", nameA);
 
  printf(" Please enter player B The name of the :");
  scanf("%s", nameB);
   
  //Display board
  show_Chessboard(CB, 7);
 
  while (1){
    //Determine if the board is full
    if (full_Chess(CB, 7)){
      puts("n The board is full. Next time! ");
      break; //Jump out of the outermost while
    }//end if
 
 
    //Player A snap
    while (1){
      printf("n Please the player  %s  following  @n", nameA);
      printf(" Chessman row coordinates X=");
      scanf("%d", &x);
      printf(" Chessman column coordinates Y=");
      scanf("%d", &y);
 
      if (play(CB, 7, x, y, '@')){  //@ ascii=64
        break;   // following successful
      }
      else
      {
        puts(" Down - click failed, please re - select the position down - click ");
        continue;  // Following is not successful , and reset it 
      }//end if
    }//end while A
 
    //Display board
    show_Chessboard(CB, 7);
 
    //Determine whether player A has won
    if (judga(CB, 7)){
      printf("n Congratulations to the player  %s successful Win!! flowers n", nameA);
      getchar();
      getchar();
      break; //Jump out of the outermost while
    }//Don't use the
 
    //Player B
    while (1){
      printf("n Please the player  %s  following  On", nameB);
      printf(" Chessman row coordinates X=");
      scanf("%d", &x);
      printf(" Chessman column coordinates Y=");
      scanf("%d", &y);
 
      if ((play(CB, 7, x, y, 'O'))){ //O ascii=79
        break;   // following successful
      }
      else
      {
        puts(" Down - click failed, please re - select the position down - click ");
        continue;  // Following is not successful , and reset it 
      }//end if
 
    }//end while B
 
    //Display board
    show_Chessboard(CB, 7);
 
    //Determine whether player B has won
    if (judga(CB, 7)){
      printf("n Congratulations to the player  %s successful Win!!   flowers n", nameA);
      getchar();
      getchar();
      break; //Jump out of the outermost while
    }//Don't use the
 
  }
 
  return 0;
 
}

The above is all the content of this article, I hope you can enjoy it.


Related articles: