C language three sub chess game implementation code
- 2020-11-26 18:52:45
- OfStack
This article shares the specific code of C language 3 sub-chess game for your reference. The specific content is as follows
1. Introduction:
C language array and other related knowledge: to achieve a simple 3 sub chess game:
The general content of the 3 small chess games is
During the design of this game: three files are designed in total:
game. h: Declarations of types and functions
game.c: The concrete realization of the function function
test.c: Test file
2. Specific implementation:
(1). game. h module:
The code implementation is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
// Header file contains
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
// Declare functions
// Initializes the checkerboard
void InitBoard(char board[ROW][COL], int row, int col);
// Printcheckerboard
void DisplayBoard(char board[ROW][COL], int row, int col);
// The player to play chess
void PlayerMove(char board[ROW][COL], int row, int col);
// The computer to play chess
void ComputerMove(char board[ROW][COL], int row, int col);
// Check whether the game is winning or losing
char CheckWin(char board[ROW][COL], int row, int col);
(2). game. c module
Code implementation:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
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)
//{
// int i = 0;
// for (i = 0; i < row; i++)
// {
// // Print data
// printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
// // Print split line
// if (i<row-1)
// printf("---|---|---\n");
// }
//}
//
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
// Print data
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j<col-1)
printf("|");
}
printf("\n");
// Print split line
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j<col-1)
printf("|");
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" Players walk :>\n");
while (1)
{
printf(" Please enter coordinates :>");
scanf("%d%d", &x, &y);
// If the coordinate range is valid
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
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 illegal, please reenter \n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf(" Computer go :>\n");
while (1)
{
int x = rand() % row;//0-2
int y = rand() % col;//0-2
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
static int IsFull(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++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;// No Spaces
}
char CheckWin(char board[ROW][COL], int row, int col)
{
int i = 0;
// Line to see if there is 3 An equal
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
// Columns of the judgment
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
// The diagonal
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
// Decide if it's a tie
// Determine if the board is full. - Is there any space on the board?
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
// It's not a tie. Game on
return 'C';
}
(3). test. c module
The code implementation is as follows
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("******************************\n");
printf("******* 1. play *******\n");
printf("******* 0. exit *******\n");
printf("******************************\n");
}
void game()
{
char ret = 0;
// design 1 Under the 3 Son chess game
// Data-Storing
char board[ROW][COL] = {0};// Arrays should be initialized to Spaces
InitBoard(board, ROW, COL);// Initializing checkerboard - The blank space
// Printed board
DisplayBoard(board, ROW, COL);
// Analyze the following findings:
// In the course of playing the game
//1. The player to win - '*'
//2. Computer win - '#'
//3. Draw the - 'Q'
//4. Continue to - 'C'
while (1)
{
PlayerMove(board, ROW, COL);
// Judge the winning or losing
ret = CheckWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
// Judge the winning or losing
ret = CheckWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
}
if (ret == '*')
{
printf(" The player to win \n");
}
else if (ret == '#')
{
printf(" Computer win \n");
}
else if (ret == 'Q')
{
printf(" A draw \n");
}
DisplayBoard(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select a :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();//3 Son chess game
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" The wrong choice \n");
break;
}
//
} while (input);
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 set
JavaScript classic games don't stop playing
java classic game summary
javascript classic game summary