栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[C语言]三子棋代码-电脑永远下不赢你的随机数模拟算法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[C语言]三子棋代码-电脑永远下不赢你的随机数模拟算法

提纲:

一、代码

二、容易出错的地方 

一、代码

//test.c 游戏实现逻辑
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void menu()
{
	printf("**********************n");
	printf("****** 0.exit ********n");
	printf("****** 1.play ********n");
	printf("**********************n");

}
void game()
{

	char ret = 0;
	char chessboard[ROW][COLUMN] = { 0 };
	//初始化棋盘  initialize
	initializechessboard(chessboard, ROW, COLUMN);
	printchessboard(chessboard, ROW, COLUMN);

	//下棋
	while (1)
	{
	
		//玩家下棋
		palyermove(chessboard, ROW, COLUMN);
		//判断输赢
		ret = iswin(chessboard, ROW, COLUMN);
		if (ret != 'C')
		{
			break;
		}
		printchessboard(chessboard, ROW, COLUMN);

		//电脑下棋
		computermove(chessboard, ROW, COLUMN);
		//判断输赢
		ret = iswin(chessboard, ROW, COLUMN);
		if (ret != 'C')
		{
			break;
		}
		printchessboard(chessboard, ROW, COLUMN);

	}
	if (ret == '*')
		printf("玩家赢了n");
	else if(ret == '#')
		printf("电脑赢了n");
	else
	{
		printf("平局n");
	}

	printchessboard(chessboard, ROW, COLUMN);

}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	menu();
	do
	{
		//menu();
		//printf("");
		printf("请输入 0/1 进行选择n");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("退出游戏n");
			break;
		case 1:
			//printf("三子棋n");
			game();
			break;
		default:
			printf("选择错误,请重新选择n");
			break;
		}
	} while (input);


	return 0;
}








 

game.h项目头文件
#pragma once
#include 
#include 
#include 
#define ROW 3
#define COLUMN 3

//棋盘
void initializechessboard(char chessboard[ROW][COLUMN], int row, int column);
void printchessboard(char chessboard[ROW][COLUMN], int row, int column);

//下棋
void palyermove(char chessboard[ROW][COLUMN], int row, int column);

void computermove(char chessboard[ROW][COLUMN], int row, int column);
char iswin(char chessboard[ROW][COLUMN], int row, int column);
int isfull(char chessboar[ROW][COLUMN], int row, int column);
game.c 游戏函数实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void initializechessboard(char chessboard[ROW][COLUMN], int row, int column)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			chessboard[i][j] = ' ';
		}

	}
}

void printchessboard(char chessboard[ROW][COLUMN], int row, int column)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			printf(" %c ", chessboard[i][j]);
			if(j < column - 1)
			printf("|");
		}
		printf("n");
		if (i < row - 1)
		{
			for (j = 0; j < column; j++)
			{
				printf("---");
				if (j < column - 1)
					printf("|");
			}
			printf("n");
		}

	}
}


void palyermove(char chessboard[ROW][COLUMN], int row, int column)
{

	
	int x = 0;
	int y = 0;

	printf("玩家下棋n");

	while (1)
	{
	printf("请输入坐标: x,y n");
	scanf("%d,%d", &x, &y);
	
		if (x >= 1 && x <= row && y >= 1 && y <= column)
		{
			if (chessboard[x - 1][y - 1] == ' ')
			{
				chessboard[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("坐标已被占用,请重新输入n");
			
		}
		else
			printf("坐标非法,请重新输入n");
		

	}

}

void computermove(char chessboard[ROW][COLUMN], int row, int column)
{
	int x = 0;
	int y = 0;
	while(1)
	{
		x = rand() % row;
		y = rand() % column;
		if (chessboard[x][y] == ' ')
		{
			chessboard[x][y] = '#';
			break;
		}
	}
}

int isfull(char chessboard[ROW][COLUMN], int row, int column)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			if (chessboard[i][j]== ' ')
				return 0;
		}
	}
	return 1;
}
char iswin(char chessboard[ROW][COLUMN], int row, int column)
{
	//行
	int i = 0;
	for(i = 0; i < row; i++)
	{ 
		if (chessboard[i][0] == chessboard[i][1] && chessboard[i][1] == chessboard[i][2] && chessboard[i][1] != ' ')
			return chessboard[i][1];
	}
	
	//列
	int j = 0;
	for (j = 0; j < column; j++)
	{
		if (chessboard[0][j] == chessboard[1][j] && chessboard[1][j] == chessboard[2][j] && chessboard[1][j] != ' ')
			return chessboard[1][j];
	}

	//对角线
	if (chessboard[0][0] == chessboard[1][1] && chessboard[1][1] == chessboard[2][2] && chessboard[1][1] != ' ')
		return chessboard[1][1];
	if (chessboard[0][2] == chessboard[1][1] && chessboard[1][1] == chessboard[2][0] && chessboard[1][1] != ' ')
		return chessboard[1][1];

	//平局
	if (isfull(chessboard, row, column))
	{
		return 'Q';

	}
		return 'C';

}

二、容易出错的地方:

1.封装函数的时候,传参的时候注意不只是行和列要传过来,棋盘也要传过来,所以需要建立数组来接收,所以不只是有数组名就行了,还得按创建数组的格式才行 数据类型+数组名+[](看是几维数组)

2.判断输赢的时候

不要漏了 return 'C';//因为除了输赢,平局,还有 继续 这个状态。

3. 用时间戳进行随机数模拟

rand()是一堆随机数,可以通过 % x 控制随机数范围,

要和 srand((unsigned int)time(NULL))一起用

4.判断输赢的方法是穷举法,只适合三子棋,不具有通用性

三、对于初学者来说比较有意思的地方

1.使用两个for循环打印棋盘

2.判断输赢的时候,通过retun 4个状态来判断输赢,切判断行和列的时候直接return chessboard[i][1]和 chessboard[0][1] 而不是分成'*'和'#'重复判断行和列的输赢情况

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/893181.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号