三子棋 c语言

1 三子棋描述

两方对战游戏,在9宫格棋盘上双方依次落子,率先形成三子成线者胜,棋盘满但是未形成三子成线的情况为和局

2. 代码方案

实现三子游戏的关键点梳理:构造场景;实现交互;输赢算法;

构造场景:

void InitBoard(char board[Row][Col], int row, int col);
void DisplayBoard(char board[Row][Col], int row, int col);

实现交互:

void PlayMore(char board[Row][Col], int row, int col);
void ComputerMove(char board[Row][Col],int row,int col);

输赢算法:char Inwin(char board[Row][Col],int row, int col);

3. 代码实现

主函数框架:

#include"game.h"
void meau()
{
	printf("********************\n");
	printf("****   1.play   ****\n");
	printf("****   0.exit   ****\n");
	printf("********************\n");

}
void game()
{
	char board[Row][Col] = { 0 };
	
	InitBoard(board, Row, Col);
	int ret;
	while (1)
	{

		PlayMore(board, Row, Col);
		DisplayBoard(board, Row, Col);
		 ret = Inwin(board, Row, Col);
		if (ret != 'c')
		{
			break;
		}
		ComputerMove(board, Row, Col);
		DisplayBoard(board, Row, Col);

		ret = Inwin(board, Row, Col);
		if (ret != 'c')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢了\n");
	}
	if (ret == '#')
	{
		printf("电脑赢了\n");
	}
	if (ret == 'Q')
	{
		printf("平局\n");
	}
}
int main()
{
	int input;
	srand((unsigned int)time(NULL));
	do
	{
		meau();
		printf("请选择>:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出程序");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

头文件中函数的声明

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define Row 3
#define Col 3

#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>


//初始化函数
void InitBoard(char board[Row][Col], int row, int col);
//打印棋盘
void DisplayBoard(char board[Row][Col], int row, int col);
//玩家下棋
void PlayMore(char board[Row][Col], int row, int col);
//电脑下棋
void ComputerMove(char board[Row][Col],int row,int col);
//判断输赢
char Inwin(char board[Row][Col],int row, int col);

初始化 

void InitBoard(char board[Row][Col], int row, int col)
{ 
	memset(&board[0][0], ' ', row * col * sizeof(char));
}

打印棋盘

void DisplayBoard(char board[Row][Col], int row, int col)
{
	int i,j;
	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");
	}
}

玩家下棋

void PlayMore(char board[Row][Col], int row, int col)
{
	int x, y;
	printf("玩家下棋>\n");
	while (1)
	{
		printf("请输入要下棋的坐标:>");
		scanf("%d %d", &x, &y);
		if (0 < x && x <= row && 0 < y && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("坐标被占用,请重新输入\n");
			}
		}
		else
		{
			printf("输入错误,请重新输入\n");
		}

	}
}

电脑下棋

void ComputerMove(char board[Row][Col], int row, int col)
{
	printf("电脑下棋>\n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

判断输赢

char Inwin(char board[Row][Col], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		if (board[i][1] == board[i][0] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}
	for (int i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 'c';
			}
		}
	}
	return 'Q';
}

4. 运行实例演示

player A play this game

step1 : 启动 / start game

         

 

step2:  A 落子;

        

 

step3: 机器落子;

        

 

step4: 判断输赢;

         同一行字符相同:

 同一列字符相同:

 对角线字符相同:

 

5. 其他说明

        方案可以优化的地方:  当前机器是随机落子,可以改进成AI下棋

        注:该代码目前只在vs2022中测试过

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值