C语言学习 / 三子棋(井字棋)

1、打印欢迎界面

void welcome()
{
	printf("******************************\n");
	printf("**********  三子棋  **********\n");
	printf("***  1.play        0.exit  ***\n");
	printf("******************************\n");
}

void playgame()
{
	int input = 0;
	do
	{
		welcome();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("开始游戏\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	} while (input);
}

int main()
{
	playgame();
	return 0;
}

2、打印棋盘

void game()
{
	char board[ROW][COL] = { 0 };//创建数组
	initial(board, ROW, COL);//数组初始化
	show(board, ROW, COL);//打印棋盘
}

void initial(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 show(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");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,设计三子棋游戏可以分为以下几个步骤: 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; } ``` 注意:以上代码仅作为参考,可能存在一些漏洞和不足之处,需要在实际使用中进行完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值