c language to realize three chess

Keywords: Windows

In the implementation of the game of three chess, the program is written in multi file format. First, the display menu is created in the main.c file, and the program implementation is placed in the chess.c file. In order to standardize, the function and header file that are called are declared in chess.h
chess.h file
major function
① package the header file declaration and function declaration together, and call this one directly for other files

#ifndef _CHESS_H_
#define _CHESS_H_

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

#define ROW 3
#define COL 3

#define PLAYER_COLOR 'X'
#define COMPUTER_COLOR 'O'

#pragma warning(disable:4996)

void Game();
void InitBoard(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);

#endif

main function file
Main functions:
① display menu, players can choose to continue the game, or exit, if continue to play, continue to call Game()

#include "chess.h"

void ShowMenu()
{
	printf("#################################\n");
	printf("## 1. Play             2. Exit ##\n");
	printf("#################################\n");
	printf("Please Select: ");
}
int main()
{
	int select = 0;
	int quit = 0;
	while (!quit){
		ShowMenu();
		scanf("%d", &select);
		switch (select){
		case 1:
			Game();
			break;
		case 2:
			printf("Bye bye!\n");
			quit = 1;
			break;
		default:
			printf("select error, try again!\n");
			break;
		}
	}
	system("pause");
	return 0;
}

chess.c file
Main functions:
① realize Game() function;
(2) initialize the board content as a space, and the player will go first. If the coordinates are legal, the player will fall (the player's chess is X), and then the computer will go randomly until a draw or a party wins.

#include "chess.h"

// char (*board)[COL]
void InitBoard(char board[][COL], int row, int col)
{
	int i = 0;
	for (; i < row; i++){
		int j = 0;
		for (; j < col; j++){
			board[i][j] = ' ';
		}
	}
}
void ShowBoard(char board[][COL], int row, int col)
{
	printf("   | 1 | 2 | 3\n");
	printf("----------------\n");
	int i = 1;
	for (; i <= 3; i++){
		printf(" %d | %c | %c | %c \n",\
			i, board[i-1][0], board[i-1][1],board[i-1][2]);
		if (i != 3){
			printf("----------------\n");
		}
	}
}
void PlayerMove(char board[][COL], int row, int col)
{
	while (1){
		int x = 0;
		int y = 0;
		printf("Please Enter Pos<x, y>: ");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3){
			if (board[x - 1][y - 1] == ' '){
				board[x - 1][y - 1] = PLAYER_COLOR;
				break;
			}
			else{
				printf("Your seat has been occupied, Please re-enter!\n");
			}
		}
		else{
			printf("The coordinates you entered are illegal, please re-enter!\n");
		}
	}
}
//return 'N'->next, 'X'->player 'O'->computer, 'E'->equal
char Judge(char board[][COL], int row, int col)
{
	int i = 0;
	for (; i < row; i++){
		if (board[i][0] != ' ' && board[i][0] == board[i][1] && \
			board[i][1] == board[i][2]){
			return board[i][0];
		}
	}
	for (i = 0; i < col; i++){
		if (board[0][i] != ' ' && board[0][i] == board[1][i] && \
			board[1][i] == board[2][i]){
			return board[0][i];
		}
	}
	if (board[0][0] != ' ' &&board[0][0] == board[1][1] && \
		board[1][1] == board[2][2]){
		return board[1][1];
	}
	if (board[0][2] != ' ' &&board[0][2] == board[1][1] && \
		board[1][1] == board[2][0]){
		return board[1][1];
	}
	for (i = 0; i < row; i++){
		int j = 0;
		for (; j < col; j++){
			if (board[i][j] == ' '){
				return 'N';
			}
		}
	}
	return 'E';
}

int GetRandom(int start, int end)//[1,3][7,10]
{
	//[start, end]
	return rand() % (end-start+1) + start;//[0-6] + 3=[3,9]
}
void ComputerMove(char board[][COL], int row, int col)
{
	while (1){
		//int x = rand()%row;[0-2]
		//int y = rand()%col;[0-2]
		int x = GetRandom(1, 3);//GetRandom(7, 10)
		int y = GetRandom(1, 3);// [1-3]
		if (board[x-1][y-1] == ' '){
			board[x-1][y-1] = COMPUTER_COLOR;
			break;
		}
	}
}

void Game()
{
	char result = '\0';
	char board[ROW][COL];
	InitBoard(board, ROW, COL);
	ShowBoard(board, ROW, COL);
	srand((unsigned int)time(NULL));
	while (1){
		PlayerMove(board, ROW, COL);
		ShowBoard(board, ROW, COL);
		result = Judge(board, ROW, COL);
		if (result != 'N'){
			break;
		}
		system("CLS");
		ComputerMove(board, ROW, COL);
		ShowBoard(board, ROW, COL);
		result = Judge(board, ROW, COL);
		if (result != 'N'){
			break;
		}
	}
	switch (result){
	case 'X': //you win
		printf("Congratulations, you won. Another one?\n");
		break;
	case 'O': //computer win
		printf("Almost won. Another face saving?\n");
		break;
	case 'E': //Equal
		printf("You're as smart as a computer,Are you sure you don't want to know who is smarter?\n");
		break;
	default:
		printf("bug?!\n");
		break;
	}
	//memset(board, ' ', sizeof(board));
}

Posted by endurox on Thu, 07 Nov 2019 13:24:00 -0800