[C language] Sanzi chess

Mind map

tic-tac-toe

Create three files

1. Create the main operation file (test.c)
2. Create a game running file (game.c)
3. Create a global variable header file (game.h)

First, reference the header file

1. Introduce header file in (game.h)
#include <stdio.h>
2. Create the main function in (game.c)
int main()
{

    return 0;
}
3. Create a loop (test.c)
  • Print menu
  • Select menu: start game, end game, re-enter

Create loop

    • Print menu
    • Prompt, please select
menu()//Print menu functions
{
	printf("************************\n");
	printf("*****    1.play    *****\n");
	printf("*****    2.exit    *****\n");
	printf("************************\n");
}
int main()
{
	do
	{
		menu();//Print menu
		printf("Please select:>\n");
		}
	} while ();
	return 0;
}

Print menu

    • Create received variables
    • Select program
      • Input 1: game start
      • Enter 0: end of game
      • Enter other: re-enter
int main()
{
	int input = 0; //Create received variables
	do
	{
		menu();
		printf("Please select\n");
		scanf("%d", &input);//scanf is a variable that receives keyboard input
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("game over\n");
			break;
		default:
			printf("Input error, please re-enter!\n");
			break;
		}
	} while(input);
	return 0;
}
4. Enter the game function (game())
  • test.c
    • Define board as a two-dimensional array
    • Initialize the chessboard to print all spaces
void game()
{
	//Store data - 2D array
	char board[ROW][COL];
	//Initialize checkerboard - initialize spaces
	InitBoard(board, ROW, COL);
}
  • game.h
    • The header file is contained in game.h. if it is declared in the other two. c files, you can directly reference all files of the. h file
    • The board above needs two shaping. In order to facilitate later adjustment,
    • Define ROW and COL in the. h file for later modification
    • Initialize checkerboard - definition of function
//Inclusion of header file
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


//Definition of symbols
#define ROW 3
#define COL 3

//Declaration of function

//Initialize checkerboard
void InitBoard(char board[ROW][COL], int row, int col);
  • game.c (initialized to spaces)
    • Receive a two-dimensional array and receive parameters of two abscissa and ordinate coordinates
    • Print the two-dimensional chessboard in two-layer cycle and assign it as "space"
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] = ' ';
		}
	}
}
  • Test. C (print the chessboard)
    • Print the chessboard in order to display the game interface and update the print interface in real time
    • After playing chess, the player will print out the steps taken by the player
    • Computer steps are the same
Checkerboard style:
   |   |
---|---|---
   |   |
---|---|---
   |   |
void game()
{
	//Store data - 2D array
	char board[ROW][COL];
	//Initialize checkerboard - initialize spaces
	InitBoard(board, ROW, COL);

	//Print the chessboard - essentially print the contents of the array
	DisplayBoard(board, ROW, COL);

  • game.h
    • Definition of function
//Functions for printing checkerboard
void DisplayBoard(char board[ROW][COL], int row, int col);
  • game.c
    • Two layer cyclic printing
    • Print "space coordinate space" and "space" in each cycle
      • Print the third time because there is no "I" at the back of the chessboard
    • Print the third line - same as above
void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if(j<col-1) 
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
				if(j<col-1)
					printf("|");
			}
			printf("\n");
		}
	}
}
5. Accept game status
  • Test. C (players play chess and computers play chess)
    • Define a substitute variable
    • When the function changes, the substitution variable will change and return different values
    • At this time, judge the progress of the game according to the value returned by the substitute variable
	//Players play chess
	PlayerMove(board, ROW, COL);
	DisplayBoard(board, ROW, COL);//Print chessboard

	//Computer chess
	ComputerMove(board, ROW, COL);
	DisplayBoard(board, ROW, COL);//Print chessboard
void game()
{
	//Store data - 2D array
	char board[ROW][COL];
	//Initialize checkerboard - initialize spaces
	InitBoard(board, ROW, COL);

	//Print the chessboard - essentially print the contents of the array
	DisplayBoard(board, ROW, COL);
	char ret = 0;//Accept game status
	while (1)
	{
		//Players play chess
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);//Print chessboard

		//Computer chess
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);//Print chessboard
	}
	if (ret == '*')
	{
		printf("The player won\n");
	}
	else if(ret == '#')
	{
		printf("The computer won\n");
	}
	else
	{
		printf("it ends in a draw\n");
	}
	DisplayBoard(board, ROW, COL);
}
  • Game. H (player playing chess, computer playing chess)
    • Define function
//Players play chess
void PlayerMove(char board[ROW][COL], int row, int col);

//Computer chess
void ComputerMove(char board[ROW][COL], int row, int col);
  • Game. C (player walking, computer walking)

    • Player walk:
      1. Prompt the player to enter coordinates
      2. Receiving coordinates
      3. Judge whether the requirements are met
        • If the coordinates are not occupied, play chess
        • Occupied - prompt for re-entry
        • Out of coordinate range – prompt for re-entry
// 1. Players go
void PlayerMove(char board[][COL], int row, int col) 
{
	int x = 0;
	int y = 0;

	printf("Player go:>\n");

	while (1)
	{
		printf("Please enter the coordinates of playing chess:>");
		scanf("%d %d", &x, &y);
		//Judge the legitimacy of coordinates
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//play chess
			//Are coordinates occupied
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("Coordinates occupied, Please re-enter\n");
			}
		}
		else
		{
			printf("Illegal coordinates, Please re-enter\n");
		}
	}
}
  • Computer walk:
    1. Tell the player the computer to go
    2. Write random numbers with the - rand() function
    3. %The coordinate position limit is the maximum value (range) of a random number
    4. Determine location and print
void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("Computer walk:>\n");

	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;

		//Judge occupation
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}
5. Judge the progress of the game (end / continue)
		//Determine whether the player wins the game
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;

Overall code of game() function

void game()
{
	//Store data - 2D array
	char board[ROW][COL];
	//Initialize checkerboard - initialize spaces
	InitBoard(board, ROW, COL);

	//Print the chessboard - essentially print the contents of the array
	DisplayBoard(board, ROW, COL);
	char ret = 0;//Accept game status
	while (1)
	{
		//Players play chess
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//Determine whether the player wins the game
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//Computer chess
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//Judge whether the computer wins the game
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
	{
		printf("The player won\n");
	}
	else if(ret == '#')
	{
		printf("The computer won\n");
	}
	else
	{
		printf("it ends in a draw\n");
	}
	DisplayBoard(board, ROW, COL);
}
  • Game. H (define function)
    1. Player wins -*
    2. The computer won -#
    3. Draw - Q
    4. Game continue - C
//Determine whether the game has won or lost
char IsWin(char board[ROW][COL], int row, int col);
  • game.c (implementation of win / lose function)
    • Judge whether the three rows, three columns and diagonals are consistent
    • Whichever side wins, print the first symbol of this line directly
    • Judge draw the chessboard is full of draw / dissatisfaction continues
char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//Judge three lines
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return  board[i][1];//
		}
	}
	
	//Judge three columns
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}

	//Judge diagonal
	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];
	}

	//Judge the draw
	//If the chessboard is full, return 1, and if not, return 0
	int ret = IsFull(board, row, col);
	if (ret == 1)
	{
		return 'Q';
	}

	//continue
	return 'C';
}
1. The player won - *
2. The computer won - #
3. it ends in a draw     - Q
4. The game continues - C
  • A function is used above:
    • IsFull(board, row, col);
    • Used to judge whether the chessboard is full
    • If all chessboards are not full, return 0
    • The above will judge that = = 1 returns Q
    • Otherwise, return to C - the game continues
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;//The chessboard is not full
			}
		}
	}

Please look at the code of the original file

game.h

#pragma once

//Inclusion of header file
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


//Definition of symbols
#define ROW 3
#define COL 3

//Declaration of function

//Initialize checkerboard
void InitBoard(char board[ROW][COL], int row, int col);

//Functions for printing checkerboard
void DisplayBoard(char board[ROW][COL], int row, int col);

//Players play chess
void PlayerMove(char board[ROW][COL], int row, int col);

//Computer chess
void ComputerMove(char board[ROW][COL], int row, int col);

//
//1. Player wins -*
//2. The computer won -#
//3. Draw - Q
//4. Game continues - C

//Determine whether the game has won or lost
char IsWin(char board[ROW][COL], int row, int col);


test.c

#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()
{
	//Store data - 2D array
	char board[ROW][COL];
	//Initialize checkerboard - initialize spaces
	InitBoard(board, ROW, COL);

	//Print the chessboard - essentially print the contents of the array
	DisplayBoard(board, ROW, COL);
	char ret = 0;//Accept game status
	while (1)
	{
		//Players play chess
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//Determine whether the player wins the game
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//Computer chess
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//Judge whether the computer wins the game
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
	{
		printf("The player won\n");
	}
	else if(ret == '#')
	{
		printf("The computer won\n");
	}
	else
	{
		printf("it ends in a draw\n");
	}
	DisplayBoard(board, ROW, COL);
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Selection error, reselect\n");
			break;
		}
	} while (input);

	return 0;
}

game.c

#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++)
//	{
//		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
//		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++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if(j<col-1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
				if(j<col-1)
					printf("|");
			}
			printf("\n");
		}
	}
}


void PlayerMove(char board[][COL], int row, int col) 
{
	int x = 0;
	int y = 0;

	printf("Player go:>\n");

	while (1)
	{
		printf("Please enter the coordinates of playing chess:>");
		scanf("%d %d", &x, &y);
		//Judge the legitimacy of coordinates
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//play chess
			//Are coordinates occupied
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("Coordinates occupied, Please re-enter\n");
			}
		}
		else
		{
			printf("Illegal coordinates, Please re-enter\n");
		}
	}
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("Computer walk:>\n");

	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;

		//Judge occupation
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}


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;//The chessboard is not full
			}
		}
	}

	return 1;//The chessboard is full
}
char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//Judge three lines
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return  board[i][1];//
		}
	}
	
	//Judge three columns
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}

	//Judge diagonal
	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];
	}

	//Judge the draw
	//If the chessboard is full, return 1, and if not, return 0
	int ret = IsFull(board, row, col);
	if (ret == 1)
	{
		return 'Q';
	}

	//continue
	return 'C';
}

Tags: C

Posted on Sat, 04 Dec 2021 13:55:27 -0500 by symantec