五子棋(机器人随机下棋简单版本)

Posted youzhi青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了五子棋(机器人随机下棋简单版本)相关的知识,希望对你有一定的参考价值。

相信大家小时候都玩过五子棋游戏,五子棋是起源于中国古代的传统黑白棋之一,在一块类似围棋的棋盘上,你和对手轮流放下黑白棋子,无论是横竖还是斜。只要有五颗相同颜色的棋子连成一线即可 获得一句胜利。它不仅能使人增强思维能力,提高智力,而且富含丰富的哲理,有助于修身养性。现在五子棋有“连珠”、“连五子”、“五子连珠”、“串珠”、 “五目”、“五目碰”、“五格”等多种称谓。游戏步骤如下:

  1. 准备棋盘。

  2. 先手后手轮流走棋

  3. 每颗子所投的地方没有限制。

  4. 只要任意一方在棋盘上且同一个回合上连为五子为胜。

  5. 若棋盘下满也没有一方在棋盘上且同一个回合上连为五子则该回合为平局。

那么如何用c语言复刻一个简单版的五子棋人机战呢(简单是因为我们的电脑无法根据玩家的出棋而出棋可以说是一个智障电脑人)

那么首先进入游戏我们都有一个菜单选择页面,用c语言做一个简单的界面并不难

void Menu()

	printf("**********************\\n");
	printf("******1.开始游戏******\\n");
	printf("******2.退出游戏******\\n");
	printf("**********************\\n");
	int a = 0;
	printf("请输入你的选择:\\n");
	scanf("%d",&a);
	if (a == 1)
	
		system("cls");
		Game( );
	
	else
	
		if (a == 2)
		
			exit(0);
		
		else
		
			system("cls");
			printf("你的输入有误!\\n");
			Menu();
		
	
	

 当用户选择1并且进入游戏后首先应该给用户展示棋盘,在初始化棋盘的时候我们选择用二维数组来保存对局信息再加上简单的格式控制我们的row行list列棋盘就初始化成功了(我们对其先全部赋值为’ ‘)

void Show(int ShowBoard[row][list], int Row, int List)

	printf(" ");
	for (int i = 1;i<=List;i++)
	
		printf(" %2d",i);
	
	printf("\\n");
	for (int i = 1;i <= Row;i++)
	
		printf("%2d",i);
		for (int j = 0;j<=List-1;j++)
		
			printf(" %c|",ShowBoard[i-1][j]);
			
		printf("\\n");
		printf("  ");
		for (int j = 0;j <= List - 1;j++)
		
			printf("---");
		
		printf("\\n");
	

棋盘打印完成以后就该轮到先手走棋(这里我们讲用户设置为先手并且用宏player表示用户的棋子用computer表式电脑的棋子)考虑到用户输入坐标的合法性(用户选择的坐标是否在棋盘上和该坐标处是否已经有棋子)我们需要判断坐标的合法性,这里我将这一步骤放在用户输入坐标之后,在用户走完棋后就轮到我们的电脑走棋这里我们用随机数来设定电脑走棋的位置,同样产生的随机坐标上不应该有棋子(player或computer)该处坐标的值应该是初始化时的值,否则应该重新生成随机坐标走值;

void SetChess(int ShowBoard[row][list], int Row, int List)

		printf("请输入你要放置棋子的坐标(X,Y):\\n");
		int X = 0;
		int Y = 0;
			while (1)
			
				scanf("%d,%d", &X, &Y);
				if ((X > Row || Y > List || X < 1 || Y < 1))
				
					printf("你输入的坐标有误,请重新输入!\\n");
					SetChess(ShowBoard, row, list);
					while (getchar() != '\\n');
					continue;//问题所在
				
				else
				
					if (ShowBoard[X - 1][Y - 1] == ' ')
					
						ShowBoard[X - 1][Y - 1] = player;
						Select(ShowBoard, row, list);
						JudgementS(ShowBoard, row, list, X, Y);
						break;
					
					else
					
						printf("此处已经有棋子,请重新选择位置\\n");
						X = 0;Y = 0;
						SetChess(ShowBoard, row, list);
						continue;
					
				
			

void ComputerSet(int ShowBoard[row][list], int Row, int List)

		int a = 0 + rand() % (Row - 1 - 0 + 1);
		int b = 0 + rand() % (List - 1 - 0 + 1);
		if (ShowBoard[a][b] == ' ')
		
			ShowBoard[a][b] = computer;
			Select(ShowBoard, row, list);
		    JudgementC(ShowBoard, row, list, a, b);
		
		else
		
			ComputerSet(ShowBoard, row, list);
		

一直重复走棋直到有一方有win个棋子练成一条线(该方获胜)或者将棋盘走满棋子,这就是游戏胜负的判断标准,我们应该在走一步棋之后判断一次,先判定是否平局再判定双方谁获胜select函数判定平局judgementc函数判定电脑是否获胜judgements函数判定玩家是否获胜其实电脑和玩家获胜的机制是相同的

void Select(int ShowBoard[row][list], int Row, int List)

	int num = 0;
	for (int i = 0;i < Row;i++)
	
		for (int j = 0;j < List;j++)
		
			if (ShowBoard[i][j] == ' ')
			
				num += 1;
			
		
	
	printf("%d\\n",num);
	if (num == 0)
	
		system("cls");
		printf("平局\\n");
		Show(ShowBoard, row, list);
		Menu();
	

void JudgementS(int ShowBoard[row][list], int Row, int List, int a, int b)

	system("cls");
	Show(ShowBoard, row, list);
	int num = 0;
	//竖行够win个胜利
	for (int i = 0;i <= a - 1;i++)
	
		if (ShowBoard[a - 1 - i][b - 1] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= Row - a;i++)
	
		if (ShowBoard[a + i - 1][b - 1] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num = 0;
	横行够win个胜利
	for (int i = 0;i <= b - 1;i++)
	
		if (ShowBoard[a - 1][b - 1 - i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= List - b;i++)
	
		if (ShowBoard[a - 1][b - 1 + i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num = 0;
	判断右斜满win个
	for (int i = 0;i <= (((Row - a) < (b - 1)) ? (Row - a) : (b - 1));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 - i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (a - 1)) ? (List - b) : (a - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 + i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num = 0;
	判断左斜满win个
	for (int i = 0;i <= (((a - 1) < (b - 1)) ? (a - 1) : (b - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 - i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (Row - a)) ? (List - b) : (Row - a));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 + i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	

void JudgementC(int ShowBoard[row][list], int Row, int List,int a,int b)

	a += 1;
	b += 1;
	system("cls");
	Show(ShowBoard, row, list);
	int num = 0;
	//竖行够win个胜利
	for (int i = 0;i <= a - 1;i++)
	
		if (ShowBoard[a - 1 - i][b - 1] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= Row - a;i++)
	
		if (ShowBoard[a + i - 1][b - 1] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num = 0;
	横行够win个胜利
	for (int i = 0;i <= b - 1;i++)
	
		if (ShowBoard[a - 1][b - 1 - i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= List - b;i++)
	
		if (ShowBoard[a - 1][b - 1 + i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num = 0;
	判断右斜满win个
	for (int i = 0;i <= (((Row - a) < (b - 1)) ? (Row - a) : (b - 1));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 - i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (a - 1)) ? (List - b) : (a - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 + i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num = 0;
	判断左斜满win个
	for (int i = 0;i <= (((a - 1) < (b - 1)) ? (a - 1) : (b - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 - i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (Row - a)) ? (List - b) : (Row - a));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 + i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	

至此五子棋游戏c语言简单版本就做出来了

以下是头文件

#pragma once
#pragma warning(disable:4996)
#define row 10
#define list 10
#define computer 'X'
#define player 'O'
#define win 5
#include<stdio.h>
#include<time.h>
#include<windows.h>
extern void init(int ShowBoard[row][list], int Row, int List);
extern void Show(int ShowBoard[row][list], int Row, int List);
extern void SetChess(int ShowBoard[row][list], int Row, int List);
extern void ComputerSet(int ShowBoard[row][list], int Row, int List);
extern void JudgementS(int ShowBoard[row][list], int Row, int List,int a,int b);
extern void JudgementC(int ShowBoard[row][list], int Row, int List, int a, int b);
extern void Menu();
extern void Select(int ShowBoard[row][list], int Row, int List);

以下是主函数和菜单

#include"Gomoku.h"
void Menu()

	printf("**********************\\n");
	printf("******1.开始游戏******\\n");
	printf("******2.退出游戏******\\n");
	printf("**********************\\n");
	int a = 0;
	printf("请输入你的选择:\\n");
	scanf("%d",&a);
	if (a == 1)
	
		system("cls");
		Game( );
	
	else
	
		if (a == 2)
		
			exit(0);
		
		else
		
			system("cls");
			printf("你的输入有误!\\n");
			Menu();
		
	
	


int main()
  
	srand((unsigned)time(NULL));
	Menu( );
	return 0;

以下是Game函数

#include"Gomoku.h"
void init(int ShowBoard[row][list], int Row, int List)

	for (int i = 0;i < Row;i++)
	
		for (int j = 0;j < List;j++)
		
			ShowBoard[i][j] = ' ';
		
	

void Show(int ShowBoard[row][list], int Row, int List)

	printf(" ");
	for (int i = 1;i<=List;i++)
	
		printf(" %2d",i);
	
	printf("\\n");
	for (int i = 1;i <= Row;i++)
	
		printf("%2d",i);
		for (int j = 0;j<=List-1;j++)
		
			printf(" %c|",ShowBoard[i-1][j]);
			
		printf("\\n");
		printf("  ");
		for (int j = 0;j <= List - 1;j++)
		
			printf("---");
		
		printf("\\n");
	

void SetChess(int ShowBoard[row][list], int Row, int List)

		printf("请输入你要放置棋子的坐标(X,Y):\\n");
		int X = 0;
		int Y = 0;
			while (1)
			
				scanf("%d,%d", &X, &Y);
				if ((X > Row || Y > List || X < 1 || Y < 1))
				
					printf("你输入的坐标有误,请重新输入!\\n");
					SetChess(ShowBoard, row, list);
					while (getchar() != '\\n');
					continue;//问题所在
				
				else
				
					if (ShowBoard[X - 1][Y - 1] == ' ')
					
						ShowBoard[X - 1][Y - 1] = player;
						Select(ShowBoard, row, list);
						JudgementS(ShowBoard, row, list, X, Y);
						break;
					
					else
					
						printf("此处已经有棋子,请重新选择位置\\n");
						X = 0;Y = 0;
						SetChess(ShowBoard, row, list);
						continue;
					
				
			

void Select(int ShowBoard[row][list], int Row, int List)

	int num = 0;
	for (int i = 0;i < Row;i++)
	
		for (int j = 0;j < List;j++)
		
			if (ShowBoard[i][j] == ' ')
			
				num += 1;
			
		
	
	printf("%d\\n",num);
	if (num == 0)
	
		system("cls");
		printf("平局\\n");
		Show(ShowBoard, row, list);
		Menu();
	

void ComputerSet(int ShowBoard[row][list], int Row, int List)

		int a = 0 + rand() % (Row - 1 - 0 + 1);
		int b = 0 + rand() % (List - 1 - 0 + 1);
		if (ShowBoard[a][b] == ' ')
		
			ShowBoard[a][b] = computer;
			Select(ShowBoard, row, list);
		    JudgementC(ShowBoard, row, list, a, b);
		
		else
		
			ComputerSet(ShowBoard, row, list);
		

void JudgementS(int ShowBoard[row][list], int Row, int List, int a, int b)

	system("cls");
	Show(ShowBoard, row, list);
	int num = 0;
	//竖行够win个胜利
	for (int i = 0;i <= a - 1;i++)
	
		if (ShowBoard[a - 1 - i][b - 1] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= Row - a;i++)
	
		if (ShowBoard[a + i - 1][b - 1] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num = 0;
	横行够win个胜利
	for (int i = 0;i <= b - 1;i++)
	
		if (ShowBoard[a - 1][b - 1 - i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= List - b;i++)
	
		if (ShowBoard[a - 1][b - 1 + i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num = 0;
	判断右斜满win个
	for (int i = 0;i <= (((Row - a) < (b - 1)) ? (Row - a) : (b - 1));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 - i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (a - 1)) ? (List - b) : (a - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 + i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num = 0;
	判断左斜满win个
	for (int i = 0;i <= (((a - 1) < (b - 1)) ? (a - 1) : (b - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 - i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (Row - a)) ? (List - b) : (Row - a));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 + i] == player)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("你赢了!!\\n");
		Menu();
	
	

void JudgementC(int ShowBoard[row][list], int Row, int List,int a,int b)

	a += 1;
	b += 1;
	system("cls");
	Show(ShowBoard, row, list);
	int num = 0;
	//竖行够win个胜利
	for (int i = 0;i <= a - 1;i++)
	
		if (ShowBoard[a - 1 - i][b - 1] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= Row - a;i++)
	
		if (ShowBoard[a + i - 1][b - 1] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num = 0;
	横行够win个胜利
	for (int i = 0;i <= b - 1;i++)
	
		if (ShowBoard[a - 1][b - 1 - i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= List - b;i++)
	
		if (ShowBoard[a - 1][b - 1 + i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num = 0;
	判断右斜满win个
	for (int i = 0;i <= (((Row - a) < (b - 1)) ? (Row - a) : (b - 1));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 - i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (a - 1)) ? (List - b) : (a - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 + i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num = 0;
	判断左斜满win个
	for (int i = 0;i <= (((a - 1) < (b - 1)) ? (a - 1) : (b - 1));i++)
	
		if (ShowBoard[a - 1 - i][b - 1 - i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	
	num -= 1;
	for (int i = 0;i <= (((List - b) < (Row - a)) ? (List - b) : (Row - a));i++)
	
		if (ShowBoard[a - 1 + i][b - 1 + i] == computer)
		
			num += 1;
		
		else
		
			break;
		
	
	if (num >= win)
	
		printf("电脑赢了!!\\n");
		Menu();
	


void Game()

	int ShowBoard[row][list];
	init(ShowBoard, row, list);
	Show(ShowBoard, row, list);

	while (1)
	
		SetChess(ShowBoard, row, list);
		ComputerSet(ShowBoard, row, list);
		system("cls");
		Show(ShowBoard, row, list);
	
		

以上是关于五子棋(机器人随机下棋简单版本)的主要内容,如果未能解决你的问题,请参考以下文章

java项目开发----五子棋

C语言实现简单的三子棋

C语言实现简单的三子棋

女朋友下棋比我厉害,还特别嚣张,Python敲个辅助,看她如何嚣张

五子棋人机博弈游戏(cocos creator)

结对-结对编项目作业名称-需求分析