Realization of Simple Three-bang Game with C Language

This mini game of three-bang is implemented on a 3-by-3 board.

1. Header file First use the header file to declare

#ifndef _GAME_H_
#define _GAME_H_

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define ROW 3
#define COL 3

void Print(char board[ROW][COL], int row, int col); //Print the board 
void InitBoard(char board[ROW][COL], int row, int col); //Initialize the board 
void PlayerMove(char board[ROW][COL], int row, int col); //Player moves 
void ComputerMove(char board[ROW][COL], int row, int col); //Computer moves 
int JudgeWiner(char board[ROW] [COL], int row, int col); //Judgment win or lose 
int FullBoard(char board[ROW][COL], int row, int col); //Judgment draw or continue
#endif //_GAME_H_

2. Main function

If you feel that it is too complicated to display on multiple chessboards, you can add system("cls")

It is possible to always play on one board

#include "game.h"    //this is our game's menu
void menu()
{
	printf("*************************\n");
	printf("*******   1.play   ******\n");
	printf("*******   0.exit   ******\n");
	printf("*************************\n");
}
void game()     //Game function 
{ 
	char ret = '0';
	int row = ROW;
	int col = COL;
	char board[ROW][COL];
    InitBoard(board, row, col); //Initialize the board 
	Print(board, row, col); //Print the board
	do
	{
		PlayerMove(board, row ,col); //Player moves
		system("cls");
		Print(board, row, col);
		ret = JudgeWiner(board, row, col);
		if(ret !='x')
		{
			break;
		}
		ComputerMove(board, row, col); //computer go 
		system("cls"); //clear screen 
		Print(board, row, col); //print again
		ret = JudgeWiner(board, row, col);
		if(ret!='x')
		{
			break;
		}
	}while(1);
    ret = JudgeWiner(board, row, col);
	if (ret == '+')
	{
		printf("Congratulations on your victory\n");
	}
	else if (ret == '-')
	{
		printf("Unfortunately, the computer won\n");
	}
	else if (ret == 'p')
	{
		printf("Tie\n");
	}
}
intmain()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("Please select:\n");
		scanf("%d", &input);
		switch(input)
		{
		case 0:
			printf("Game over\n");
			break;
		case 1:
			game();
			break;
		default:
			printf("Incorrect selection, please re-select:\n");
			break;
		}

	}while(input);

	return 0;
}

3.game.c
#include "game.h"
First print out our three-by-three chessboard
void Print(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 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");
}}}
Then we need to initialize the placement of the moves in our chessboard to avoid random values	
 
 
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] = ' ';
		}
	}
}

This function implements the player's move, as long as the corresponding coordinates are entered void PlayerMove(char board[ROW][COL], int row, int col){int x = 0;int y = 0;printf("The player moves\ n");do{ printf("Please enter the coordinates:\n"); scanf("%d%d", &x, &y);if(x>=1 && x<=row && y>=1 && y <=col){if(board[x-1][y-1] == ' ') When there are chess pieces in the place where the pawn is placed, the player will be reminded to re-select the coordinates {board[x-1][y-1] = '+';break;}else{printf("There is a chess in the current position, please re-enter\n"); }}else{printf("The input is wrong, please re-enter:\n");}}while(1 );}
The computer will move randomly according to the empty space on the board
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("The computer goes\n");
	do
	{
	    x = rand() %3;
	    y = rand() %3;
		if(board[x][y] == ' ')
		{
			board[x][y] = '-';
			break;
		}
	}while(1);
}
 
 
This function judges whether the chessboard is full or not, because it is possible that the game is full and there will be a tie.
int FullBoard(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;
}
 
 
When there is a three-point line, one side will win, this function is used to judge the victory and print the object of the victory
int JudgeWiner(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	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];
		}
		else if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
		else if (board[0][0] == board[1][1]&&board[1][1] == board[2][2]&&board[1][1]!=' ')  
		{  
			return board[1][1];  
		}  
		else if (board[0][2] == board[1][1]&&board[1][1] == board[2][0] &&board[1][1]!= ' ')  
		{  
			return board[1][1];  
		}  
	}
			if(FullBoard(board, row , col) == 0)
			{
				return 'x';
			}
			else if(FullBoard(board, row , col) == 1)
			{
				return 'p';
			}	
	return 0;
}

This is a simple chess game, I hope to make progress together with everyone

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324885327&siteId=291194637