The Realization of the Three-Moku Mini Game

game.h

#ifndef __GAME_H__  
#define __GAME_H__  

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

#define ROW 3  
#define COL 3

void InitBoard(char board[ROW][COL], int row, int col);//Checkerboard initialization
void DisplayBoard(char board[ROW][COL], int row, int col);//Display chessboard
void PlayerMove(char board[ROW][COL], int row, int col);//Player moves
void ComputerMove(char board[ROW][COL], int row, int col);//The computer moves
char IsWin(char board[ROW][COL], int row, int col);//Judgment of winning or losing



#endif //__GAME_H__  

game.c

 
 
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void InitBoard(char board[ROW][COL], int row, int col)//Checkerboard initialization
{
	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)//Display chessboard
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf("%c |",board[i][j]);
		}
		printf("\n");
		for (j = 0; j < col; j++)
		{
			if (i < (row - 1))
			{
				printf("--|");
			}
		}
		printf("\n");
	}
}
void PlayerMove(char board[ROW][COL], int row, int col)//Player moves
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("Please enter the coordinates>\n");
		scanf("%d%d",&x,&y);
		if ((x >= 0 && x <= row) && (y >= 0 && y <= col))
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = 'x';
				break;
			}
			else
			{
				printf("Here has been passed, please play again\n");
			}
		}
		else
		{
			printf("Incorrect input, please re-enter\n");
		}
	}
}
void ComputerMove(char board[ROW][COL], int row, int col)//The computer moves
{
	while (1)
	{
		int x = rand() % 3;
		int y = rand() % 3;
		if (board[x][y] == ' ')
		{
			board[x][y] = '0';
			break;
		}
	}
}
static int  is_full(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] == ' ')//There are still empty positions on the chessboard
			{
				return 0;
			}
		}
	}
	return 1; //The board is full
}
char IsWin(char board[ROW][COL], int row, int col)//Judgment win or lose
{
	int i = 0;
	int j = 0;
	for (i = 0; i<row; i++)  
	{
		for (j = 0; j<col; j++)
		{
			//The horizontal three sons win  
			if ((board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j] != ' ') ||
				(board[i][j] == board[i][j - 1] && board[i][j - 1] == board[i][j + 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i][j - 1] && board[i][j - 1] == board[i][j - 2] && board[i][j] != ' '))
			{
				return board[i][j];
			}
			if ((board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i][j] != ' ') ||
				(board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i - 1][j] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j] && board[i - 1][j] == board[i - 2][j] && board[i][j] != ' '))
			{
				return board[i][j];
			}
			//The diagonal trio wins  
			if ((board[i][j] == board[i - 1][j + 1] && board[i - 1][j + 1] == board[i + 1][j - 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j + 1] && board[i - 1][j + 1] == board[i + 1][j - 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2] && board[i][j] != ' '))
			{
				return board[i][j];
			}
			if ((board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j - 1] && board[i - 1][j - 1] == board[i + 1][j + 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j - 1] && board[i - 1][j - 1] == board[i - 2][j - 2] && board[i][j] != ' '))
			{
				return board[i][j];
			}
		}
	}
	if (is_full(board, row, col))
	{
		return 'Q'; //Tie  
	}
	return ' '; // continue the game  
}


test.c
#define _CRT_SECURE_NO_WARNINGS 1
// three chess
/*
1. Initialize the board
2. Display the chessboard
3. The player walks
4. Computer walk
5 Judgment of winning or losing
*/

#include "game.h"


void menu() //print menu   
{
	printf("*****************************\n");
	printf("***Welcome to the Sanbang interface****\n");
	printf("*****************************\n");
	printf("****  1 play     0 exit  ****\n");
	printf("*****************************\n");
}


void game()
{
	char win = 0;
	int c;
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	printf("1 player goes first 2 computer goes first 0 to exit the game\n");
	scanf("%d", &c);
	switch (c)
	{
	case 1:
		printf("Player goes first:\n");
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
	case 2:
		printf("Computer go first:\n");
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
	}
	while (1)
	{
		printf("Player goes>\n");
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
		printf("Computer goes>\n");
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
	}
	if (win == 'x')
	{
		printf("Player won\n");
	}
	else if (win == '0')
	{
		printf("The computer won\n");
	}
	else
	{
		printf("Tie\n");
	}
}



void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do{
		menu();
		printf("Please choose: ");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Your input is incorrect, please try again!\n");
		}
	} while (input);
}


intmain()
{
	test();
	system("pause");
	return 0;
}

Guess you like

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