三子棋的实现

Posted 正义的伙伴啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三子棋的实现相关的知识,希望对你有一定的参考价值。

三子棋的游戏规则:

是黑白棋的一种。三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。

实现三子棋我们还是分为三个文件:test.c game.c game.h 来实现函数

我直接就放代码了,思路和扫雷差不多:
test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
#include<stdlib.h>
#include<time.h>
void game()
{
	char board[ROW][COL];
	initboard(board, COL, ROW);
	displayboard(board, COL, ROW);
	while (1)
	{
		char ret;
		playermove(board, COL, ROW);
		displayboard(board, COL, ROW);
		ret = check(board, COL, ROW);
		if (ret == '*')
		{
			printf("玩家获胜!!");
			break;
		}
		else if (ret == '#')
		{
			printf("电脑获胜!!!");
			break;
		}
		else if (ret == 'p')
		{
			printf("平局!!!");
			break;
		}

		computermove(board, COL, ROW);
		displayboard(board, COL, ROW);
		ret = check(board, COL, ROW);
		if (ret == '*')
		{
			printf("玩家获胜!!");
			break;
		}
		else if (ret == '#')
		{
			printf("电脑获胜!!!");
			break;
		}
		else if (ret == 'p')
		{
			printf("平局!!!");
			break;
		}
	}

}
void manu()
{
	printf("**********************************\\n");
	printf("***********1. play     ***********\\n");
	printf("***********2. exite    ***********\\n");
	printf("**********************************\\n");
	printf("**********************************\\n");
}
int main()
{
	srand((unsigned int)time(NULL));
	do
	{
		int n;
		manu();
		scanf("%d", &n);
		if (1 == n)
		{
			game();
		}
		else
			break;
	} while (1);
}

game.h

#pragma once
#define COL 3
#define ROW 3

void initboard(char board[][COL], int col, int row);                      //将棋盘初始化


void displayboard(char board[][COL], int col, int row);                   // 打印棋盘

void playermove(char board[][COL], int col, int row);                     //玩家移动

void computermove(char board[][COL], int col, int row);                    //电脑移动

char check(char board[][COL], int col, int row);

int isfull(char board[][COL], int col, int row);
//判断输赢: 1 玩家赢
//           2 电脑赢 
//           3 平局
//           4 以上都没有发生继续游戏,


game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void initboard(char board[ROW][COL], int col, int row)
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void displayboard(char board[][COL], int col, int row)
{
	for (int i = 0; i < ROW; i++)
	{
		int j = 0;
		for (j = 0; j < COL; j++)
		{
			if (COL - j == 1)
			{
				printf(" %c \\n", board[i][j]);
				break;
			}
			printf(" %c |", board[i][j]);
		}
		for (j = 0; j < COL; j++)
		{
			if (COL - j == 1)
			{
				printf(" - \\n");
				break;
			}
			printf(" - |");
		}
	}
}

void playermove(char board[][COL], int col, int row)
{
	while (1)
	{
		int x, y;
		printf("玩家输入:");
		scanf("%d %d", &x, &y);
		if (board[x-1][y-1] == ' ')
		{
			board[x-1][y-1] = '*';
			break;
		}
		else
			printf("该坐标已经被占用");
	}
}

void computermove(char board[][COL], int col, int row)
{
	printf("电脑走;\\n");
	while (1)
	{
		int x, y;
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

char check(char board[][COL], int col, int row)
{
	for (int i = 0; i < 3; i++)
	{
		if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0];
		}
	}
	for (int i = 0; i < 3; i++)
	{
		if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][i];
		}
	}
		
	if ((board[0][0] == board[1][1]&&board[1][1]==board[2][2] && board[0][0] != ' ')||(board[0][2] == board[1][1] && board[1][1] == board[2][1] && board[0][2] != ' '))
		{
			return board[0][0];
		}
	int ret=isfull(board, COL, ROW);
	if (ret != 0)
		return 'p';
	return 'c';



}
int isfull(char board[][COL], int col, int row)
{
	int ret;
	for (int i = 0; i < col; i++)
	{
		for (int j = 0; j < row; j++)
		{
			if (board[i][j] != ' ')
				ret = 1;
			else
				return 0;
		}
	}
	if (ret == 1)
		return 1;
}

以上是关于三子棋的实现的主要内容,如果未能解决你的问题,请参考以下文章

三子棋的实现,人工智能与人工智障

(C语言篇)三子棋的实现

三子棋的进阶玩法

C语言的三子棋

三子棋,人机博弈的鼻祖

C语言初阶《三子棋》