C language to realize the game of sanbang

Use C language to implement a simple man-machine versus three-bang game

Go directly to the code:

game.h

#ifndef __GAME_H__
#define __GAME_H__

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

#define ROW 3//定义标识符常量,固定棋盘的大小,可以根据用户需要随时改变
#define COL 3

void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char IfWin(char board[ROW][COL], int row, int col);

#endif //GAME H

test.c contains the main function and test code

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//打印菜单
void menu()
{
	printf("***********************\n");
	printf("****欢迎来到三子棋游戏****\n");
	printf("******   1.play  ******\n");
    printf("******   0.exit  ******\n");
	printf("***********************\n");

}

//开始游戏
void game()
{
	char ret;
	int input = 0;
	char board[ROW][COL] = {0};
    InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	printf("在本游戏中,1 表示玩家,2 表示电脑\n");
	printf("请输入选择玩家先走或电脑先走\n");
	scanf("%d",&input);
	if(input == 1)//玩家输入1,玩家先走
	{
		printf("玩家先走\n");
		while(1)
		{
			PlayerMove(board, ROW, COL);
			ret = IfWin(board, ROW, COL);

			if(ret == '1')
			{
				break;
			}
			DisplayBoard(board, ROW, COL);
			ComputerMove(board, ROW, COL);
			
			ret = IfWin(board, ROW, COL);
			if(ret == '2')
			{
				break;
			}
			DisplayBoard(board, ROW, COL);
		}
		if(ret == '1')
		{
			printf("玩家赢\n");
		    DisplayBoard(board, ROW, COL);
		}
		if(ret == '2')
		{
			printf("电脑赢\n");
		    DisplayBoard(board, ROW, COL);
		}
	    if(ret == 'p')
		{
		    printf("平局\n");
		    DisplayBoard(board, ROW, COL);
		}
	}
	else if(input == 2)
	{
		printf("电脑先走\n");
		while(1)
		{
			ComputerMove(board, ROW, COL);
			ret = IfWin(board, ROW, COL);
			if(ret == '2')
			{
				break;
			}
			DisplayBoard(board, ROW, COL);

			PlayerMove(board, ROW, COL);
			ret = IfWin(board, ROW, COL);
			if(ret == '1')
			{
				break;
			}
			DisplayBoard(board, ROW, COL);
		}
		if(ret == '1')
		{
			printf("玩家赢\n");
		    DisplayBoard(board, ROW, COL);
		}
		if(ret == '2')
		{
			printf("电脑赢\n");
		    DisplayBoard(board, ROW, COL);
		}
	    if(ret == 'p')
		{
		    printf("平局\n");
		    DisplayBoard(board, ROW, COL);
		}
	}
}

//游戏测试
void test()
{
	int input = 0;
	do{
		menu();
	    printf("请选择:");
	    scanf("%d",&input);
	    switch(input)
	   {
		case 1:
			printf("开始游戏\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	}while(input);
}

int main()
{
	test();
	return 0;
}

game.c the actual game code

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//使用函数初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
	memset(board, ' ',row*col*sizeof(board[0][0]));//初始化棋盘为空格
}

//打印棋盘 包括落子之后的棋盘
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<row-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[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while(1)
	{
		printf("玩家走,请输入坐标");
	    scanf("%d %d",&x, &y);
		if(((x-1<=row-1) && (y-1<=col-1)))
		{
			if(board[x-1][y-1] == ' ')//判断玩家输入坐标是否已经有棋子
			{
				board[x-1][y-1] = '1';
			    break;
			}
			else 
			printf("该坐标已被占用,重新输入:\n");
		}
		else 
			printf("该坐标不在棋盘之内,重新输入:\n");
	}
}

//电脑走
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑走\n");
	srand((unsigned int) time(NULL));//随机生成电脑要落子的坐标
	while(1)
	{
		x = rand()%row;
		y = rand()%col;
	if(board[x][y] == ' ')
	{
		board[x][y] = '2';
		break;
	}
	}
}

static int IfFull(char board[ROW][COL], int row, int col)//判断棋盘是否已满
{
	int x = 0;
	int y = 0;
	for(x=0; x<row; x++)
	{
		for(y=0; y<col; y++)
		{
			if(board[x][y] == ' ')
				return 0;
		}
	}
	return 1;
}


//判断输赢 如果三个相同的子连接成一条线则胜利
char IfWin(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	for(x=0; x<row; x++)
	{
		for(y=0; y<col-2; y++)
		{
			if(board[x][y] != ' ' && board[x][y] == board[x][y+1] \
			&& board[x][y+1] == board[x][y+2])
			{
				return board[x][y];
			}
		}
	}
	for(x=0; x<row-2; x++)
	{
		for(y=0; y<col; y++)
		{
			if(board[x][y] != ' ' && board[x][y] == board[x+1][y] \
		         && board[x+1][y] == board[x+2][y])
		    {
			    return board[x][y];
		    }
		}
	}
	for(x=0; x<row-2; x++)
	{
		for(y=0; y<col-2; y++)
		{
			if(x == y && board[x][y] == board[x+1][y+1] && \
				board[x+1][y+1] ==  board[x+2][y+2] && board[x][y] != ' ')
			{
				return board[x][y];
			}
		}
	}
	for(x=0; x<row-2; x++)
	{
		for(y=0; y<col-2; y++)
		{
			if(board[x+2][y] == board[x+1][y+1] && \
				board[x+1][y+1] == board[x][y+2] && board[x+2][y] != ' ')
			{
				return board[x+2][y];
			}
		}
	}
	if(IfFull(board, row, col))
	{
		return 'p';
	}
	//else 
	//	return ' ';
}

Start playing now

1. First enter the game print menu, you can choose to start the game or exit the game, choose 1 to start the game 2. After starting the game, print the format of the board, and then choose insert image description here
the player to go first or the computer to go first
insert image description here
Go first, then the player enters the coordinates to move
insert image description here
insert image description here
insert image description here
4, the final player wins, and the game menu is printed in a loop. You can choose to start the game repeatedly, or exit the game
insert image description here

Guess you like

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