三子棋游戏【C语言版】

一、游戏说明

三子棋相信大家都不陌生,小时候经常在纸上画出九宫格就可以直接和小伙伴一起玩,规则就是谁先在九宫格中下出同一行三个一样,或者同一列三个一样,又或者对角线的三个一样的棋子,谁就获胜;其实在编程中,代码也能实现人机版的三子棋小游戏,让我们一起来看看如何实现的吧!

二、游戏设计步骤

将三子棋游戏的实现逻辑分为以下步骤:

1.创建菜单(选择玩游戏或者退出游戏)
2.创建和初始化游戏棋盘(本质上为一个二维数组)
3.打印出棋盘
4.玩家下棋(用‘ * ’表示棋子)
5.判断输赢或者是否平局
6.电脑下棋(用‘ # ’表示棋子)
7.判断输赢(同第5步)
8.回到第3步直至确定输赢

三、代码实现

1.创建菜单

规定输入 1 为玩游戏,输入 0 为退出游戏

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

2.创建和初始化游戏棋盘

本质为创建一个二维数组并初始化

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

3.打印出棋盘

利用循环的方式将字符排版成棋盘的样式(n x n 的棋盘也同样可以打印)

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

4.玩家下棋

规定玩家的棋子为字符‘ * ’,同时需判断棋子坐标的合理性

void  PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走\n");
	printf("请输入坐标>:");
	
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3)//判断游戏合法性
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("坐标已被占用,请重新输入\n");
		}
		else
			printf("坐标非法,请重新输入\n");
	}
}

5.电脑下棋

规定电脑的棋子为字符‘ # ’,同样需判断棋子坐标的合理性

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

6.判断输赢或者是否平局

规定电脑赢返回字符‘ # ’,玩家赢返回字符‘ * ’,游戏继续返回字符‘C’,平局(即棋盘满但还未出现胜负)则返回字符‘Q’;而这里我们又需要注意,判断输赢的函数需返回一个字符,所以判断棋盘是否已满的函数需在判断输赢的函数中并且能返回一个字符,这样才能保证游戏输赢判断的完整性
(1)判断输赢函数:

char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (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 (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[0][0] != ' ')//判断对角线
	{
		return board[0][0];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')//判断对角线
	{
		return board[0][2];
	}
int ret = IsFull(board, row , col );
if (ret == 0)
{
	return 'Q';
}
else
    return 'C';
}

(2)判断棋盘是否已满的函数

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

以上是各各步骤函数的具体实现,而为了方便,我们将代码分为三个文件:
1.game.h------关于游戏相关的函数声明,符号声明及头文件的包含
2.game.c------游戏相关函数的实现
3.test.c------测试游戏的逻辑
具体如下:
game.h文件:

#include<stdio.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);
char IsWin(char board[ROW][COL], int row, int col);

game.c文件:

#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	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  PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走\n");
	printf("请输入坐标>:");
	
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
		{
			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() % 3;
		int y = rand() % 3;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

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

char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (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 (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[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][2] && board[2][0] != ' ')
	{
		return board[0][2];
	}
int ret = IsFull(board, row , col );
if (ret == 0)
{
	return 'Q';
}
else
    return 'C';
}

test.c文件:

#include"game.h"

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

void game()
{
	char board[ROW][COL];//定义棋盘
	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 (ret != 'C')
			break;
		ComputerMove(board, ROW, COL);//电脑下棋
		DisplayBoard(board, ROW, COL);
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
	{
		printf("玩家胜利!\n");
	}
	else if (ret == '#')
	{
		printf("电脑胜利!\n");
	}
	if(ret=='Q')
	{
		printf("平局\n");
	}
	DisplayBoard(board, ROW, COL);
}

int main()
{
	srand((unsigned int)time(NULL));
	
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:

			game();
			break;
		case 0:
			printf("游戏结束\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
		}
	} while (input);
	return 0;
}

四、运行效果

玩游戏的三种情况分别如下:
在这里插入图片描述

感兴趣的小伙伴可以去实现或者优化一下,期待各位博友能够实现地更完美!

  • 28
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
好的,设计三子棋游戏可以分为以下几个步骤: 1. 定义棋盘:通常使用一个三行三列的二维数组来表示棋盘,用数字0表示空位,用数字1表示玩家1下的棋子,用数字2表示玩家2下的棋子。 2. 实现落子功能:用户输入行列坐标,程序判断该位置是否为空位,如果是则将该位置设置为当前玩家下的棋子。 3. 判断胜负:每次玩家下完棋后,程序需要判断是否有一方取得胜利,可以通过遍历棋盘来实现。 4. 实现人机对战:可以使用随机数来模拟电脑的落子,也可以使用一些简单的算法来提高电脑的智能程度。 下面是一个简单的C语言三子棋游戏代码示例,供参考: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> int chessboard[3][3]; // 棋盘 int check_win(int player) // 判断胜负 { int i, j; for (i = 0; i < 3; i++) { if (chessboard[i][0] == player && chessboard[i][1] == player && chessboard[i][2] == player) { return 1; } if (chessboard[0][i] == player && chessboard[1][i] == player && chessboard[2][i] == player) { return 1; } } if (chessboard[0][0] == player && chessboard[1][1] == player && chessboard[2][2] == player) { return 1; } if (chessboard[0][2] == player && chessboard[1][1] == player && chessboard[2][0] == player) { return 1; } return 0; } void print_chessboard() // 打印棋盘 { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (chessboard[i][j] == 0) { printf(" "); } else if (chessboard[i][j] == 1) { printf("X"); } else if (chessboard[i][j] == 2) { printf("O"); } if (j < 2) { printf("|"); } } printf("\n"); if (i < 2) { printf("-+-+-\n"); } } } int main() { int player = 1; // 玩家先手 int row, col; srand((unsigned)time(NULL)); // 初始化随机数生成器 printf("Welcome to the Tic-Tac-Toe game!\n"); printf("Player 1: X\n"); printf("Player 2: O\n"); while (1) { printf("Player %d's turn.\n", player); if (player == 1) { printf("Please input the row and column numbers to place your chess(X).\n"); scanf("%d%d", &row, &col); if (chessboard[row][col] != 0) { printf("This place has been taken, please choose another place.\n"); continue; } chessboard[row][col] = 1; } else { printf("Thinking...\n"); while (1) { row = rand() % 3; col = rand() % 3; if (chessboard[row][col] == 0) { break; } } chessboard[row][col] = 2; } print_chessboard(); if (check_win(player)) { printf("Player %d wins!\n", player); break; } if (player == 1) { player = 2; } else { player = 1; } } return 0; } ``` 注意:以上代码仅作为参考,可能存在一些漏洞和不足之处,需要在实际使用中进行完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值