C语言小项目——井字棋游戏(升级版)

Posted 飞向星的客机

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言小项目——井字棋游戏(升级版)相关的知识,希望对你有一定的参考价值。

🌟 前言

Wassup guys😎

今天是我们的C语言小项目「井字棋升级版」

Let’s get it!


文章目录


游戏说明

井字棋,又名三子棋,英文名叫Tic-Tac-Toe;
 
是一种在3x3格子上进行的连珠游戏,和五子棋类似,格线排成井字故得名。
 
游戏需要的工具仅为纸和笔,然后由分别代表O和X的两个游戏者轮流在格子里留下标记(一般来说先手者为X),任意三个标记形成一条直线,则为获胜。
 
其游戏过程就像下面这样👇

游戏效果展示

游戏运行结果如下👇

游戏代码

我在函数这一篇总结中,讲过函数的声明和定义,所有这里我用三个文件来实现这个三子棋游戏
 
友友们可以将以下代码复制到自己的编译器当中运行:

game.h

游戏函数的声明

#pragma once
//游戏函数的声明
#include <stdio.h>
#include <stdlib.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);

//电脑检查自己是否会赢
int CheckComputer(char board[ROW][COL], int row, int col);

//电脑检查玩家是否会赢
int CheckPlayer(char board[ROW][COL], int row, int col, int k);

//判断输赢函数
char Is_Win(char board[ROW][COL], int row, int col);
/*
1.玩家赢 ---> 返回'*'
2.电脑赢 ---> 返回'#'
3. 平局  ---> 返回'P'
4. 继续  ---> 返回'C'
*/

game.c

游戏函数的实现

#define _CRT_SECURE_NO_WARNINGS 1
//游戏函数的实现

#include "game.h"

void InitBoard(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++)
		
			board[i][j] = ' ';
		
	


void DisplayBoard(char board[ROW][COL], int row, int col)

	int i = 0;
	int j = 0;
	//row = 3行
	//col = 3列
	for (i = 0; i < row; i++)
	
		if (i == 0)
		
			printf("┌----┬----┬----┐\\n");
		
		if ((i == 1) || (i == 2))
		
			printf("├----┼----┼----┤\\n");
		
		for (int j = 0; j < col; j++)
		
			printf("│  %c ", board[i][j]);
		
		printf("│\\n");
	
	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);
		printf("你下棋的位置↓\\n");
		if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
		
			if (board[x-1][y-1] == ' ')
			
				board[x-1][y-1] = 'x';
				break;
			
			else
			
				printf("该坐标被占用,请重新输入\\n");
			
		
		else
		
			printf("坐标非法,超出范围\\n");
		
	



void ComputerMove(char board[ROW][COL], int row, int col)

	int x = 0;
	int y = 0;

	//z是判断变量
	int z = 0;

	z = CheckComputer(board, row, col);

	printf("电脑下棋的位置↓\\n");
	while (0 == z)
	
		x = rand() % row;
		y = rand() % col;

		if (board[x][y] == ' ')
		
			board[x][y] = 'o';
			break;
		
	

//电脑检查自己能不能赢
int CheckComputer(char board[ROW][COL], int row, int col)

	int i = 0;
	int j = 0;

	//k用于返回值
	int k = 0;
	while (0 == k)
	
		//电脑判断自己在三行上是否会赢
		for (i = 0; i < row; i++)
		
			//如果第一个格子和第二个格子都是电脑的棋子,并且第三个格子是空的
			//那么电脑直接落子
			if (board[i][0] == board[i][1] && (board[i][0] == 'o' || board[i][1] == 'o') && board[i][2] == ' ')
			
				board[i][2] = 'o';
				k = 1;
				break;
			
			if (board[i][0] == board[i][2] && (board[i][0] == 'o' || board[i][2] == 'o') && board[i][1] == ' ')
			
				board[i][1] = 'o';
				k = 1;
				break;
			

			if (board[i][1] == board[i][2] && (board[i][1] == 'o' || board[i][2] == 'o') && board[i][0] == ' ')
			
				board[i][0] = 'o';
				k = 1;
				break;
			
		
		if (k != 0)
			break;

		//电脑判断自己在三列上是否会赢
		for (j = 0; j < col; j++)
		
			if (board[0][j] == board[1][j] && (board[0][j] == 'o' || board[1][j] == 'o') && board[2][j] == ' ')
			
				board[2][j] = 'o';
				k = 1;
				break;
			

			if (board[0][j] == board[2][j] && (board[0][j] == 'o' || board[2][j] == 'o') && board[1][j] == ' ')
			
				board[1][j] = 'o';
				k = 1;
				break;
			
			if (board[1][j] == board[2][j] && (board[1][j] == 'o' || board[2][j] == 'o') && board[0][j] == ' ')
			
				board[0][j] = 'o';
				k = 1;
				break;
			
		
		if (k != 0)
			break;

		//使用while循环判断对角线是否会赢
		while (0 == k)
		
			//左边对角线
			if (board[0][0] == board[1][1] && (board[0][0] == 'o' || board[1][1] == 'o') && board[2][2] == ' ')
			
				board[2][2] = 'o';
				k = 1;
				break;
			

			if (board[0][0] == board[2][2] && (board[0][0] == 'o' || board[2][2] == 'o') && board[1][1] == ' ')
			
				board[1][1] = 'o';
				k = 1;
				break;
			

			if (board[1][1] == board[2][2] && (board[1][1] == 'o' || board[2][2] == 'o') && board[0][0] == ' ')
			
				board[0][0] = 'o';
				k = 1;
				break;
			

			//右边对角线
			if (board[0][2] == board[1][1] && (board[0][2] == 'o' || board[1][1] == 'o') && board[2][0] == ' ')
			
				board[2][0] = 'o';
				k = 1;
				break;
			

			if (board[0][2] == board[2][0] && (board[0][2] == 'o' || board[2][0] == 'o') && board[1][1] == ' ')
			
				board[1][1] = 'o';
				k = 1;
				break;
			
			if (board[1][1] == board[2][0] && (board[1][1] == 'o' || board[2][0] == 'o') && board[0][2] == ' ')
			
				board[0][2] = 'o';
				k = 1;
				break;
			
			break;
		

		k = CheckPlayer(board, row, col, k);

		return k;
	


//检查玩家是否会赢
int CheckPlayer(char board[ROW][COL], int row, int col, int k)

	int i = 0;
	int j = 0;
	while (0 == k)
	
		//判断玩家在三行上是否会赢
		for (i = 0; i < row; i++)
		
			if (board[i][0] == board[i][1] && (board[i][0] == 'x' || board[i][1] == 'x') && board[i][2] == ' ')
			
				board[i][2] = 'o';
				k = 1;
				break;
			
			if (board[i][0] == board[i][2] && (board[i][0] == 'x' || board[i][2] == 'x') && board[i][1] == ' ')
			
				board[i][1] = 'o';
				k = 1;
				break;
			
			if (board[i][2] == board[i][1] && (board[i][2] == 'x' || board[i][1] == 'x') && board[i][0C语言实现简单的三子棋

C语言实现简单的三子棋

C语言初阶《三子棋》

C语言初阶《三子棋》

井字棋游戏升级版 - TopTicTacToe项目 简介

(教你简单地C语言黑框框三子棋(井字棋)