C Language Implementing Three-chess Game

In order to realize the game of chess, we must first understand the rules of chess, so as to facilitate our programming and understanding.
The rules are as follows:
On the Nine-palace chessboard, as long as the three pieces go in one line (horizontal, vertical, diagonal), the other side will lose.
First of all, our game program should have a menu page, which is used to let users choose to play the game. We can use functions to implement it. First, we build a game.h as a header file, and then define a game.c file to realize the specific functions (creating chessboard, initializing chessboard, etc.). Finally, a main.c file is defined to implement the game.

1.game.h (header file)

game.h(Header file)
#ifndef  __GAME_H__
#define __GAME_H__
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[][3],int row,int col);
void ShowBoard(char board[][3],int row,int col);
void PlayerMove(char board[][3],int row,int col);
char IsWin(char board[][3],int row,int col);
void ComputerMove(char board[][3],int row,int col);
#endif//__GAME_H__

2. main. C (main function)

#include "game.h"

void menu()
{
	printf("*********************\n");
	printf("*******1.play********\n");
	printf("*******0.exit*********\n");
	printf("*********************\n");
}
void game()
{
	char ret = 0;
	char board[3][3] = {0};
	InitBoard(board,3,3);
	ShowBoard(board,3,3);
	while(1)
	{
		PlayerMove(board,3,3);
		ret = IsWin(board,3,3);
		if(ret != ' ')
		{
			break;//Player wins or draws.
		}
		ShowBoard(board,3,3);
		
		ComputerMove(board,3,3);
		ret = IsWin(board,3,3);
		if(ret != ' ')
		{
			break;//The computer wins or draws.
		}
		ShowBoard(board,3,3);
	}
	if(ret == 'Q')
	{
		printf("It ends in a draw\n");
	}
	else if(ret == 'X')
	{
		printf("Player wins\n");
	}
	else if(ret == 'O')
	{
		printf("Computer wins\n");
	}
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("Please enter your operation:");
		scanf("%d",&input);
		switch(input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Game exit\n");
			break;
		default:
			printf("Please enter the correct number\n");
			break;
		}
	}while(input);
	return 0;
}

3.game.c

#include "game.h"

void InitBoard(char board[][3],int row,int col)
{
	int i = 0;
	for(i = 0;i < row;i++)
	{
		int j = 0;
		for(j = 0;j < col;j++)
		{
			board[i][j] = ' ';
		}
	}
	//memset(board,' ',row*col*sizeof(char));
}

void ShowBoard(char board[][3],int row,int col)
{
	int i = 0;
	int j = 0;
	for(i = 0;i < row;i++)
	{
		for(j = 0;j < col;j++)
		{
			printf(" %c ",board[i][j]);
			if(j < col-1)
			{
				printf("|");
			}
		}
		printf("\n");

		if(i < row-1)
		{
			for(j = 0;j < col;j++)
			{
				printf("---");
				if(j < col-1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

void PlayerMove(char board[][3],int row,int col)
{
	int x = 0;
	int y = 0;
	while(1)
	{
		printf("Please input 1-3 Coordinates:");//X
		scanf("%d%d",&x,&y);//1,1
		if(x>=1 && x<=3 && y>=1 && y <= 3)
		{
			if(board[x-1][y-1] == ' ')
			{
				board[x-1][y-1] = 'X';
				break;
			}
			else
			{
				printf("This position has been occupied.\n");
			}
		}
		else
		{
			printf("Illegal coordinate position\n");
		}
	}
}

static int IsFull(char board[][3],int row,int col)
{
	int i = 0;
	for(i = 0;i < row;i++)
	{
		int j = 0;
		for(j = 0;j < col;j++)
		{
			if(board[i][j] == ' ')
			{
				return 0;//Not full
			}
		}
	}
	return 1;//full
}

char IsWin(char board[][3],int row,int col)
{
	int i = 0;
	for(i = 0;i < row;i++)
	{
		//That's ok
		if(board[i][0]==board[i][1] 
		&& board[i][1]==board[i][2] 
		&& board[i][0]!= ' ')
		{
			return board[i][0];
		}
		//column
		if(board[0][i]==board[1][i] && board[1][i]==board[2][i]
		&& board[0][i] != ' ')
		{
			return board[0][i];
		}
		if(board[0][0] == board[1][1] && board[1][1] ==board[2][2]
		&& board[0][0] != ' ')
		{
			return board[0][0];
		}

		if(board[0][2] == board[1][1] && board[1][1] ==board[2][0]
		&& board[0][0] != ' ')
		{
			return board[0][2];
		}

		//It ends in a draw
		if(IsFull(board,row,col) == 1)
		{
			return 'Q';
		}
	}
	//No win, no draw.
	return ' ';
}

void ComputerMove(char board[][3],int row,int col)
{

	int x = 0;
	int y = 0;
	printf("Computer mobile\n");
	while(1)
	{
		x = rand()%row+1;//3  [0-3)   [1-3]
		y = rand()%col+1;
		if(board[x-1][y-1] == ' ')
		{
			board[x-1][y-1] = 'O';
			break;
		}
	}
}

Keywords: Programming Mobile

Added by johncox on Sun, 06 Oct 2019 09:41:18 +0300