C language code to implement three sub chess game

  • 2020-11-20 06:11:04
  • OfStack

3 small game of chess, for your reference, the specific content is as follows

1. Basic framework

1. Initial interface: This interface allows users to choose to play or not to play.
2. Game interface: After entering the game, the board should be displayed.

2. Basic logic of the game

First, the user next X. And then decide, is the user winning, is the computer winning, is it a draw, or is it going to continue. Then, the computer next O. Then the judgment, the form of the judgment is the same as the form of the user's judgment after the game is played. Only after 1 side has won, or has drawn, does it jump out of the game.

3. Basic steps of the game

(1) Draw the checkerboard
(2) Users play chess
(3) Judgment
(4) Playing chess on the computer
(5) Judgment
(6) Draw the chessboard

The first five steps above are loops, where the judgment is to determine who has won, and then to exit after two draws.

4. The program

The header file game. h:


#ifndef _GAME_H_
#define _GAME_H_

#include<stdio.h>
#include<windows.h>
#include<time.h>

#define ROW 3
#define COL 3
#define P_COLOR 'X'
#define C_COLOR 'O'
#pragma warning (disable:4996)

void Menu();
void Game();
#endif

Game module game.c:


#include "game.h"

void Menu(){
 printf("####################\n");
 printf("###1.Play 2.Exit####\n");
 printf("####################\n");
 printf("please enter#");
}// Game initial interface 
void DrawBoard(char board[][COL], int row, int col){
 printf(" 1 | 2 | 3 |\n");
 printf("--------------\n");
 for (int i = 0; i < row; i++){
 printf("%d|", i+1);
 for (int j = 0; j < col; j++){
 printf(" %c |", board[i][j]);
 }
 printf("\n--------------\n");
 }
}// draw 1 A chessboard, and there are good pieces on the chessboard 
void PlayMove(char board[][COL], int row, int col){
 int x = 0;
 int y = 0;
 while (1){
 printf("Please Enter Your pos#");
 scanf("%d %d", &x, &y);
 if (x < 1 || x>3 || y < 1 || y>3){
 printf("Pos Error\n");
 continue;
 }// Determine if the position entered by the user is out of bounds 
 if (board[x - 1][y - 1]!=' '){
 printf("Chess Exist\n");
 continue;
 }// Determine if there are already pieces in the position entered by the user 
 board[x - 1][y - 1] = P_COLOR;
 return;// If there are no pieces, place the ' X'
 }
}
char Judge(char board[][COL], int row, int col){
 for (int 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];
 }
 }// Judge every 1 Okay, is it all already 1 If yes, output the corresponding character, if yes X Output, X, If it is O It outputs O . Note: Exclude all whitespaces. 
 for (int j = 0; j < col; j++){
 if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' '){
 return board[0][j];
 }
 }// Judge every 1 Column, whether it's already both 1 If yes, output the corresponding character, if yes X Output, X, If it is O It outputs O . Note: Exclude all whitespaces. 
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2]&&board[1][1]!=' '){
 return board[1][1];
 }
 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' '){
 return board[1][1];
 }// Determine whether both of these diagonal lines are already 1 If yes, output the corresponding character, if yes X Output, X, If it is O It outputs O . Note: Exclude all whitespaces. 
 for (int i = 0; i < row; i++){
 for (int j = 0; j < col; j++){
 if (board[i][j] != 0){
 return 'N';
 }
 }
 }// Determine if the average is if each 1 Every position is not a space, then it is a tie, or under 1 I'm going to go down. 
 return 'P';// Output average. 
}
void ComputerMove(char board[][COL], int row, int col){
 while (1){
 int x = rand() % row;
 int y = rand() % row;
 if (board[x][y] == ' '){
 board[x][y] = C_COLOR;
 break;// If this position is a space, the computer will go down here 1 a O . 
 }
 }
}


void Game(){
 srand((unsigned long)time(NULL));
 char win = '\0';
 char board[ROW][COL];
 memset(board, ' ', sizeof(board));
 do{
 DrawBoard(board,ROW,COL);
 PlayMove(board, ROW, COL);
 win=Judge(board, ROW, COL);// This determines the return of the program 1 A character. X You win on behalf of the user, O On behalf of the user, P It's a tie, N Rep goes on. 
 if (win != 'N'){
 break;
 }
 ComputerMove(board, ROW, COL);
 win=Judge(board, ROW, COL);
 if (win != 'N'){
 break;
 }
 } while (1);
 switch (win){
 case P_COLOR:
 printf("you win!\n");
 break;
 case C_COLOR:
 printf("you lose!\n");
 break;
 case 'P':
 printf(" A draw \n");
 break;
 default:
 break;
 }
 DrawBoard(board, ROW, COL);
}

The main program main. c:


#include "game.h"

int main(){
 int quit = 0;
 while (!quit){
 Menu();// Game initial interface 
 int select = 0;
 scanf("%d", &select);// Let the user choose whether to play or not 
 switch (select){
 case 1:
 Game();
 break;
 case 2:
 printf("quit!");
 quit = 1;
 break;
 default:
 printf("Enter Error!Try Again!\n");
 break;
 }
 }
 printf("Byebye!");
 system("pause");
 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: