C语言三子棋(多子棋)人机对战小程序

三子棋小程序简单思路

主函数的构建 text.c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
	printf("**************\n");
	printf("****1.play****\n");
	printf("****0.exit****\n"); 
	printf("**************\n");
}
void game()
{
	char ret = 0;
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	while (1)
	{

		PlayerMove(board, ROW, COL);
		CheckWin(board, ROW, COL);
		ret = CheckWin(board, ROW, COL);
        /*DisplayBoard(board, ROW, COL);*/
		if (ret != ' ')
		{
			break;
		}
		srand((unsigned int)time(NULL));
        ComputerMove(board, ROW, COL);
		CheckWin(board, ROW, COL);
		int ret = CheckWin(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (ret != ' ')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	if (ret == '#')
	{
		printf("电脑赢\n");
	}
	if (ret == 'q')
	{
		printf("是平局\n");
	}
}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			printf("\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

首先主函数内让玩家选择游戏或者退出,用do while(input)
input为真时循环,直到玩家选择0退出
其中封装了一个menu()函数和一个game()函数
剩下的switch case语句就是正常的游戏步骤

game.c函数封装

  • 初始化函数
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= row - 1; i++)
	{
		for (j = 0; j <= col - 1; j++)
		{
			board[i][j] = ' ';
		}
	}
}

遍历函数,全部改为空格

  • 打印棋盘
void DisplayBoard(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++)
		{
			printf(" %c ", board[i][j]);
			if (j < col-1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
		}
		printf("\n");
	}
}

遍历函数,打印棋盘
注意:每行最后一个 | 不打印,所以之前加一个if语句
每列最后的 —|---|— 不打印

  • 玩家移动
void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走:>");
	while (1)
   {
	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] = '*';
				break;
			}
			else
			{
				printf("坐标被占用,请重新输入:>");
					continue;
			}
		}
		else
		{
			printf("坐标不合法,请重新输入:>\n");
		}
	}
}
  • 电脑移动
void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑走\n");
	int x = 0;
	int y = 0;
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

使用rand函数创造随机值

  • 判断胜利
char CheckWin(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 - 1] == board[i][j] && board[i][j] == board[i][j + 1] && board[i][j] != ' ')
			{
				return board[i][j];
			}
			if (board[i - 1][j] == board[i][j] && board[i][j] == board[i + 1][j] && board[i][j] != ' ')
			{
				return board[i][j];
			}
			if (board[i - 1][j-1] == board[i][j] && board[i][j] == board[i + 1][j+1] && board[i][j] != ' ')
			{
				return board[i][j];
			}
			if (board[i - 1][j + 1] == board[i][j] && board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')
			{
				return board[i][j];
			}
		}
		if (IsFull(board, ROW, COL))
		{
			return 'q';
		}
	}
	return ' ';
}

其中用到了IsFull()函数判断是否平局(判断条件是是否棋盘已满)

  • 完善后,判断输赢2.0,可以设置棋子数量(五子棋)
char CheckWin2(char board[ROW][COL], int row, int col)//五子棋
{
	int x = 0;
	int y = 0;
	int i = 0;
	for (x = 0; x < row; x++)
	{
		for (y = 0; y < col; y++)
		{
			//纵向
			for (i = 0; i < CHESS-1; i++) 
			{
				if (board[x][y] == board[x][y + i] && board[x][y] != ' ')
				{
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x][y + i]&&board[x][y]!=' ')
			{
				return board[x][y];
			}

			//纵向
			for (i = 0; i < CHESS - 1; i++) 
			{
				if (board[x][y] == board[x + i][y] && board[x][y] != ' ')
				{
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x + i][y] && board[x][y] != ' ')
			{
				return board[x][y];
			}

			//捺 斜向
			for (i = 0; i < CHESS - 1; i++)
			{
				if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ')
				{
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ')
			{
				return board[x][y];
			}

			//撇 斜向
			for (i = 0; i < CHESS - 1; i++)
			{
				if (board[x][y] == board[x - i][y + i] && board[x][y] != ' ')
				{
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x - i][y + i] && board[x][y] != ' ')
			{
				return board[x][y];
			}
		}
	}
	if (IsFull(board, ROW, COL))
	{
		return 'q';
	}
	return ' ';
}

核心思想:通过遍历棋盘,来看每个棋子连线加到第五个棋子是否是一样的,如果用此函数,可以实现五子棋,六子棋,只需在头文件内改CHESS的定义即可。

  • 判断平局
int IsFull(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;
}

game.h头文件

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
#define CHESSS 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 CheckWin(char board[ROW][COL], int row, int col); 
char CheckWin2(char board[ROW][COL], int row, int col); 
int IsFull(char board[ROW][COL], int row, int col);

代码实现

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

发布了23 篇原创文章 · 获赞 1 · 访问量 457
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览