三字棋游戏

文章目录

  • 游戏具体运行截图
  • 一、游戏头文件
  • 二、游戏函数具体实现
  • 三、主函数调用

游戏具体运行截图

三字棋游戏_第1张图片

一、游戏头文件

#pragma once
#include 
#include 
#include 
#define ROW 3
#define COLUMN 3//通过改变ROW,COLUMN的数值可以把三字棋改变为五子棋等

void init_board(char board[ROW][COLUMN], int row, int column);//初始化棋盘,全部都初始化为空格
void print_board(char board[ROW][COLUMN], int row, int column);//打印棋盘
void player_move(char board[ROW][COLUMN], int row, int column);//玩家下棋
void computer_move(char board[ROW][COLUMN], int row, int column);//电脑下棋
char is_player_win(char board[ROW][COLUMN], int row, int column);//判断玩家是否胜利
char is_computer_win(char board[ROW][COLUMN], int row, int column);//判断电脑是否胜利

二、游戏函数具体实现

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char board[ROW][COLUMN], int row, int column)//初始化棋盘
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			board[i][j] = ' ';
		}
	}
}
void print_board(char board[ROW][COLUMN], int row, int column)//打印棋盘
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < column - 1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			for (int k = 0; k < column; k++)
			{
				printf("---");
				if (k < column - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}
void player_move(char board[ROW][COLUMN], int row, int column)//玩家下棋
{
	int x = 0, y = 0;
	printf("请输入您下棋的坐标:\n");
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row && y > 0 && y <= column && board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '*';
			break;
		}
		printf("输入错误,请重新输入:\n");
	}
}
void computer_move(char board[ROW][COLUMN], int row, int column)//电脑下棋
{
	printf("电脑输入:\n");
	
	while (1)
	{
		int x = rand() % 3;
		int y = rand() % 3;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}
char is_player_win(char board[ROW][COLUMN], int row, int column)//判断玩家是否胜利
{
	//判断*行满
	for (int i = 0; i < row; i++)
	{
		int cn = 0;
		for (int j = 0; j < column; j++)
		{
			if (board[i][j] == '*')
			{
				cn++;
			}
			else
				break;
		}
		if (cn == row)
			return '*';
	}
	//判断列满
	for (int i = 0; i < row; i++)
	{
		int cn = 0;
		for (int j = 0; j < column; j++)
		{
			if (board[j][i] == '*')
			{
				cn++;
			}
			else
				break;
		}
		if (cn == row)
			return '*';
	}
	//判断对角线满
	int cn = 0;
	for (int i = 0, j = 0; i < row; i++, j++)
	{
		if (board[i][j] == '*')
			cn++;
		else
			break;
	}
	if (cn == row)
		return '*';
	return 'C';
}
char is_computer_win(char board[ROW][COLUMN], int row, int column)//判断电脑是否胜利
{
	//判断*行满
	for (int i = 0; i < row; i++)
	{
		int cn = 0;
		for (int j = 0; j < column; j++)
		{
			if (board[i][j] == '#')
			{
				cn++;
			}
			else
				break;
		}
		if (cn == row)
			return '#';
	}
	//判断列满
	for (int i = 0; i < row; i++)
	{
		int cn = 0;
		for (int j = 0; j < column; j++)
		{
			if (board[j][i] == '#')
			{
				cn++;
			}
			else
				break;
		}
		if (cn == row)
			return '#';
	}
	//判断对角线满
	int cn = 0;
	for (int i = 0, j = 0; i < row; i++, j++)
	{
		if (board[i][j] == '#')
			cn++;
		else
			break;
	}
	if (cn == row)
		return '#';
	return 'C';
}


三、主函数调用

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()//打印菜单
{
	printf("***************************\n");
	printf("********1.开始游戏*********\n");
	printf("********0.退出游戏*********\n");
	printf("***************************\n");

}
//玩家下棋
//判断胜负赢了就break
//电脑下棋
//判断胜负赢了break

void game()
{
	char board[ROW][COLUMN];
	init_board(board, ROW, COLUMN);//初始化棋盘
	print_board(board, ROW, COLUMN);//打印棋盘
	while (1)
	{
		player_move(board, ROW, COLUMN);//玩家下棋
		print_board(board, ROW, COLUMN);//打印棋盘
		if (is_player_win(board, ROW, COLUMN) == '*')//判断玩家是否胜利
		{
			printf("玩家胜利\n");
			break;
		}
		computer_move(board, ROW, COLUMN);//电脑下棋
		print_board(board, ROW, COLUMN);//打印棋盘
		//is_computer_win(board, ROW, COLUMN);
		if (is_computer_win(board, ROW, COLUMN) == '#')//判断电脑是否胜利
		{
			printf("电脑胜利\n");
			break;
		}
			
	}
}
//玩家输入1开始游戏;输入0退出游戏
void test()
{   int n = 0;
	srand((unsigned int)time(NULL));//为后续电脑下棋时需要生成随机数做准备
	do{
		menu();//打印菜单
		scanf("%d", &n);
		switch (n)
		{
		case 1:
			printf("开始游戏\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("请重新输入\n");
			break;
		}
	} while (n);
}
int main()
{
	test();
	return 0;
}

你可能感兴趣的:(C语言,游戏,c语言)