C language to implement three sub chess game in detail

  • 2020-11-26 18:54:54
  • OfStack

Before using C to implement a game of 3 pieces, we should understand the rules of the game of 3 pieces: on a nine-grid board, which side and 3 pieces join together to form a line (1 row or 1 column or diagonal) determines which side wins.

The following is the source code:

Print out the menu first.


void menu()
{
 printf("**********************************\n");
 printf("**********************************\n");
 printf("***** 0.play *************\n");
 printf("***** 1.exit *************\n");
 printf("**********************************\n");
} 
menu();
 do
 {
 printf(" Please enter the option you want to select : ");
 scanf("%d",&input);
 switch (input)
 {
 case 0:
 game();
 break; 
 case 1:
 printf(" Quit the game ");
 break;
 default:
 printf(" Please enter the option you want to select :\n");
 break;
 }
 } while (input);

2. Create a 2-dimensional array, board, to store the elements of the 3 children and initialize it.


void Initboard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 board[i][j] = ' ';
 }
 }
}

3. Print a 9-grid checkerboard.


void Displayboard(char board[ROW][COL], int row, int col) // Printed board 
{
 int i;
 int j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col ; j++)
 {
 printf(" %c ", board[i][j]);
 if (j == col - 1)
 continue;
 printf("|");
 }
 printf("\n");
 if (i < row - 1)
 printf("-----------\n");
 }
}

4. Use the Playermove and Computermove functions to make changes to the board array to simulate player and computer drop.


void Playermove(char board[ROW][COL]) // Player input coordinates 
{
 while (1)
 {
 int x = 0;
 int y = 0;
 printf(" Ask the player to enter coordinates ");
 scanf_s("%d %d", &x, &y);
 if (x <= ROW && x >= 1 && y <= COL && y >= 1) // Determine if the input coordinates are out of bounds 

 {
 if (board[x - 1][y - 1] == ' ') // The input coordinates must be null in order to place a child 
 {
 board[x - 1][y - 1] = '*';
 break;
 }
 else
 printf(" The coordinates are occupied. Please re-enter \n");
 }
 else
 printf(" The coordinates are wrong, please re-enter \n");
 }
 }
 void Computermove(char board[ROW][COL]) // Computer input coordinates 
{
 int x = 0;
 int y = 0;
 while (1)
 {
 x = rand() % 3; // use rand The function generates random Numbers and takes the remainder to get the computer coordinates 
 y = rand() % 3;
 if (board[x][y] == ' ')
 {
 printf(" Computer input coordinates %d %d\n", x+1, y+1);
 board[x][y] = '#';
 break;  // Jump out of the loop after printing 
 }
 }
}

5. Every time the player and the computer drop a child, they have to judge whether the game is won or not once, and judge the result of the game according to the return value.


int Iswin(char board[ROW][COL], int row, int col) // Judge who wins 
{
 int i;
 int j;
 //1 Win for the player  0 Win for computers  2 To draw 
 for (i = 0; i < row; i++)
 {
 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') // Judgment with 1 Whether the rows are equal 
 return 1; 
 else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
 return 0;
 }
 for (j = 0; j < col; j++)
 {
 if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') // Judgment with 1 Are the columns equal 
 return 1;
 else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#')
 return 0;
 }
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') // Determine if the left diagonal is equal 
 return 1;
 else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#')
 return 0;
 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') // Determine if the right diagonal is equal 
 return 1;
 else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
 return 0;
 if (Full(board, ROW, COL)) // Judge if it's a tie 
 return 2;
 return 3;  // return 3 To continue the game 
}

6. Function declaration, macro definition, function definition implementation, testing.


#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include"game.h"
# define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3

void Initboard(char board[ROW][COL], int row, int col);
void Displayboard(char board[ROW][COL], int row, int col);
void Playermove(char board[ROW][COL]);
void Computer(char board[ROW][COL]);
int Iswin(char board[ROW][COL], int row, int col);
int Full(char board[ROW][COL], int row, int col);
void menu()
{
 printf("**********************************\n");
 printf("**********************************\n");
 printf("***** 0.play *************\n");
 printf("***** 1.exit *************\n");
 printf("**********************************\n");
}
void game()
{
 char board[ROW][COL] = { 0 };
 int ret;
 Initboard(board, ROW, COL); // with Initboard The function initializes the array with the  ' Initializing array 
 printf(" The game start \n");
 while (1)
 {
 Playermove(board); // Player input coordinates 
 Displayboard(board, ROW, COL);
 ret = Iswin(board, ROW, COL); // Judge whether to win or not 
 if (ret == 1)
 {
 printf(" The player won ");
 break;
 }
 else if (ret == 0)
 {
 printf(" Computer win ");
 break;
 }
 else if (ret == 2)
 {
 printf(" A draw ");
 break;
 }
 printf("\n\n");
 Computer(board); // Computer input coordinates 
 Displayboard(board, ROW, COL);
 ret = Iswin(board, ROW, COL); // Judge whether to win or not 
 if (ret == 1)
 {
 printf(" The player won ");
 break;
 }
 else if (ret == 0)
 {
 printf(" Computer win ");
 break;
 }
 else if (ret == 2)
 {
 printf(" A draw ");
 break;
 }
 printf("\n\n");
 }
}
int main()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 menu();
 do
 {
 printf(" Please enter the option you want to select : ");
 scanf("%d",&input);
 switch (input)
 {
 case 0:
 game();
 break; 
 case 1:
 printf(" Quit the game ");
 break;
 default:
 printf(" Please enter the option you want to select :\n");
 break;
 }
 } while (input);
}
void Initboard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 board[i][j] = ' ';
 }
 }
}

void Displayboard(char board[ROW][COL], int row, int col) // Printed board 
{
 int i;
 int j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col ; j++)
 {
 printf(" %c ", board[i][j]);
 if (j == col - 1)
 continue;
 printf("|");
 }
 printf("\n");
 if (i < row - 1)
 printf("-----------\n");
 }
}

void Playermove(char board[ROW][COL]) // Player input coordinates 
{
 while (1)
 {
 int x = 0;
 int y = 0;
 printf(" Ask the player to enter coordinates ");
 scanf_s("%d %d", &x, &y);
 if (x <= ROW && x >= 1 && y <= COL && y >= 1)
 {
 if (board[x - 1][y - 1] == ' ')
 {
 board[x - 1][y - 1] = '*';
 break;
 }
 else
 printf(" The coordinates are occupied. Please re-enter \n");
 }
 else
 printf(" The coordinates are wrong, please re-enter \n");
 }
}

void Computer(char board[ROW][COL]) // Computer input coordinates 
{
 int x = 0;
 int y = 0;
 while (1)
 {
 x = rand() % 3;
 y = rand() % 3;
 if (board[x][y] == ' ')
 {
 printf(" Computer input coordinates %d %d\n", x+1, y+1);
 board[x][y] = '#';
 break;  // Jump out of the loop after printing 
 }
 }
}

int Iswin(char board[ROW][COL], int row, int col) // Judge who wins 
{
 int i;
 int j;
 //1 Win for the player  0 Win for computers  2 To draw 
 for (i = 0; i < row; i++)
 {
 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') // Judgment with 1 Whether the rows are equal 
 return 1; 
 else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
 return 0;
 }
 for (j = 0; j < col; j++)
 {
 if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') // Judgment with 1 Are the columns equal 
 return 1;
 else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#')
 return 0;
 }
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') // Determine if the left diagonal is equal 
 return 1;
 else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#')
 return 0;
 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') // Determine if the right diagonal is equal 
 return 1;
 else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
 return 0;
 if (Full(board, ROW, COL)) // Judge if it's a tie 
 return 2;
 return 3;  // return 3 To continue the game 
}

int Full(char board[ROW][COL], int row, int col) // Determine if the board is full 
{
 int i;
 int j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 if (board[i][j] == ' ')
 return 0; // So the board is full 
 }
 }
 return 1; // The chessboard is full 
}

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: