C language implements the three child chess game

  • 2020-06-03 07:42:10
  • OfStack

The example of this paper shares the specific code of C language to implement 3 sub-chess games for your reference, the specific content is as follows


#include<stdio.h> 
#include<stdlib.h> 
#include<time.h> 
 
void chess_board(char arr[3][3]) // Printed board  
{ 
 int i = 0; 
 int j = 0; 
 for (i = 0; i < 3; i++) 
 { 
 printf( " %c | %c | %c \n", arr [i][0], arr[i][1], arr[i][2]); 
 if (i<2) 
 printf( "---|---|---\n"); 
 } 
} 
 
int success_or_failure(char arr[3][3]) // Determine the outcome  
{ 
 int i = 0; 
 int j = 0; 
 for (i = 0; i < 3; i++) 
 { 
 if ((arr [i][0] == arr[i][1]) && ( arr[i][j] == arr [i][2])) 
 { 
 if (arr [i][0] == '#') 
  return 1; // The user won  
 else if (arr[i][0] == '0') 
  return -1; // Computer won  
 } 
 } 
 for (j = 0; j < 3; j++) 
 { 
 if ((arr [0][j] == arr[1][j]) && ( arr[1][j] == arr [2][j])) 
 { 
 if (arr [0][j] == '#') 
 return 1;  // The user won  
 else if (arr[0][j] == '0') 
 return -1; // Computer won  
 } 
 } 
 if ((arr [0][0] == arr[1][1]) && ( arr[0][0] == arr [2][2])) 
 { 
 if (arr [1][1] == '#') 
  return 1; // The user won  
 else if (arr[1][1] == '0') 
  return -1; // Computer won  
 } 
 if ((arr [0][2] == arr[1][1]) && ( arr[0][2] == arr [2][0])) 
 { 
 if (arr [1][1] == '#') 
  return 1; // The user won  
 else if (arr[1][1] == '0') 
  return -1; // Computer won  
 } 
 for (i = 0; i < 3; i++) 
 { 
 for (j = 0; j < 3; j++) 
  { 
  if (arr [i][j] == ' ') 
  return 0; // Didn't win  
  } 
 } 
 return -2;  // A draw  
} 
 
int user_game(char arr[3][3])  // User input  
{ 
 int x = 0; 
 int y = 0; 
 while (1) 
 { 
 printf( "please user input:(x, y) "); 
 scanf( "%d%d", &x, &y); 
 printf( "\n"); 
 if ((x<1 && x>3) || (y<1 && y>3)) // Determine if user input is valid  
 { 
 printf( " The address is invalid. Please reenter it \n" );  
 break; 
 } 
 if (arr [x - 1][y - 1] == ' ') // Determine if the spot is occupied  
 {    
 arr[x - 1][y - 1] = '#' ; 
 break; 
 } 
 printf( " The address is invalid. Please reenter it \n" ); 
 } 
 chess_board( arr); 
 int ret=success_or_failure(arr ); 
 return ret; 
} 
 
int computer_game(char arr[3][3])  // Computer input  
{ 
 printf( "computer input:\n\n"); 
 int x = 0; 
 int y = 0; 
 while (1) 
 {  // produce 1 A position not occupied by a pawn  
 srand(( unsigned)time(NULL )); 
 x = rand() % 3; 
 srand(( unsigned)time(NULL )); 
 y = rand() % 3; 
 if (arr [x][y] == ' ')   
 break; 
 } 
 arr[x][y]= '0' ; 
 chess_board(arr); 
 int ret=success_or_failure(arr); 
 return ret; 
} 
 
 
void init(char arr[][3] )   // Initializing checkerboard  
{ 
 
 for (int i = 0; i < 3; i++)  
 { 
 for (int j = 0; j<3; j++) 
 { 
 arr[i][j] = ' '; 
 } 
 } 
} 
 
int main() 
{ 
 printf( "**********************\n"); // Print menu  
 printf( "******* *******\n"); 
 printf( "*******1.go 0.exit**\n"); 
 printf( "******* *******\n"); 
 printf( "**********************\n"); 
 printf( "**********************\n\n\n"); 
 char arr[3][3]; 
 while (1) 
 { 
 init(arr);  // Initializing checkerboard  
 int count = 0; 
 printf( "\nplease choice:1 or 0 : " ); 
 scanf( "%d", &count); // Choose function  
 if (count == 0) 
 { 
 exit( EXIT_FAILURE); 
 } 
 else if (count == 1) 
 { 
 printf( "\n**** The game start ****\n\n" ); 
 while (1) 
 { int n1 = user_game(arr); 
 { 
 if (n1 == 1) 
 { 
  printf( "**** Congratulations to the player for winning ****\n" ); 
  break; 
 } 
 }  
 int n2 = computer_game(arr);   
 { 
  if (n2 == -1) 
  { 
  printf( "**** Computer won ****\n" ); 
  break; 
  } 
 } 
 if ((n1 == -2) || (n2 == -2))  
 { 
 printf( "***** A draw ******\n" ); 
 break; 
 } 
 } 
  printf( "\n Come again 1 Bureau of \n\n" ); 
 } 
 return 0; 
} 

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game collection

JavaScript classic games don't stop playing

java classic game summary

javascript classic game summary


Related articles: