前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【海贼王编程冒险 - C语言海上篇】C语言如何实现简单的三子棋游戏?

【海贼王编程冒险 - C语言海上篇】C语言如何实现简单的三子棋游戏?

作者头像
枫叶丹
发布2024-06-04 11:26:47
940
发布2024-06-04 11:26:47
举报
文章被收录于专栏:C++

1 -> 模块简介

test.c:测试游戏逻辑

game.c: 函数的实现

game.h:函数的声明

2 -> test.c

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h";

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

void game()
{
	//存放数据
	char board[ROW][COL] = { 0 };

	//初始化
	InitBoard(board, ROW, COL);

	//打印棋盘
	DisplayBoard(board, ROW, COL);

	char ret = 0;
	while (1)
	{
		//玩家下棋
		PlayerMove(board, ROW, COL);
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if ('C' != ret)
			break;

		//电脑下棋
		ComputerMove(board, ROW, COL);
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if ('C' != ret)
			break;
	}
	if ('*' == ret)
		printf("恭喜玩家获得胜利!!!\n");
	else if ('#' == ret)
		printf("恭喜电脑获得胜利!!!\n");
	else if ('Q' == ret)
		printf("双方平局\n");
}

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

	} while (input);

	return 0;
}

3 -> game.c

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h";

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
	memset(&board[0][0], ' ', row * col * sizeof(board[0][0]));
}

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

//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;

	while (1)
	{
		printf("请玩家下棋->\n");
		printf("请输入所在坐标:>");
		scanf("%d %d", &x, &y);

		if (x >= 1 && x <= row && y >= 1 && 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)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋->\n");

	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

//棋盘填满平局
int IsFull(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

//判断输赢
//玩家赢 -> *
//电脑赢 -> #
//平局 -> Q
//继续 -> C
char IsWin(char board[ROW][COL], int row, int col)
{
	//行判断
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
			return board[i][0];
	}

	//列判断
	for (int i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][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[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];

	//平局判断
	if (IsFull(board, row, col))
		return 'Q';
	return 'C';
}

4 -> game.h

代码语言:javascript
复制
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define ROW 3
#define COL 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);

//棋盘填满平局
int IsFull(char board[ROW][COL], int row, int col);

//判断输赢
char IsWin(char board[ROW][COL], int row, int col);

好久没写博客啦,以后会花更多时间写博客的,感谢各位大佬支持啦!!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-02-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 -> 模块简介
  • 2 -> test.c
  • 3 -> game.c
  • 4 -> game.h
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档