C语言实现简单的五子棋代码

#define _CRT_SECURE_NO_WARNINGS		// VS2013添加这个宏,关闭C4996报错
#include <stdio.h>

#define N 16        // 定义棋盘大小



//显示五子棋棋盘
void wzq_show(char(*GAME)[N])
{

	int i = 0, j = 0;
	printf("五子棋棋盘如下:\n");
	for (i = 0; i<N; i++)
	{
		for (j = 0; j<N; j++)
		{
			printf("%c ", GAME[i][j]);
		}
		printf("\n");
	}
}



//初始化五子棋棋盘
void wzq_init(char(*GAME)[N])
{
	int i = 0, j = 0;
	for (i = 0; i<N; i++)

	{
		for (j = 0; j<N; j++)
		{
			GAME[i][j] = '+';
		}
	}
	//memset(GAME,'+',N*N);

}



//游戏是否胜利的运算,一旦有人胜利则返回1
int wzq_play(int a, int b, char(*GAME)[N])
{
	char qizi = GAME[a][b];
	int i = 0, j = 0, k = 0;
	//方向-上的

	for (j = b - 4; j<b + 4; j++)
	{

		if (j<0 || j>N - 1)
		{
			continue;
		}

		for (i = 0; i<5; i++)
		{
			//if ('@' == GAME[a][j + i
  • 11
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单C语言实现五子棋代码示例: ```c #include <stdio.h> #define SIZE 15 char board[SIZE][SIZE]; // 棋盘 void initBoard() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { board[i][j] = ' '; } } } void printBoard() { printf(" "); for (int i = 0; i < SIZE; i++) { printf("%2d", i); } printf("\n"); for (int i = 0; i < SIZE; i++) { printf("%2d", i); for (int j = 0; j < SIZE; j++) { printf(" %c", board[i][j]); } printf("\n"); } } int checkWin(int row, int col, char player) { int count = 0; int i, j; // 横向检查 for (i = col - 4; i <= col; i++) { if (i >= 0 && i + 4 < SIZE) { count = 0; for (j = i; j <= i + 4; j++) { if (board[row][j] == player) { count++; } } if (count == 5) { return 1; } } } // 纵向检查 for (i = row - 4; i <= row; i++) { if (i >= 0 && i + 4 < SIZE) { count = 0; for (j = i; j <= i + 4; j++) { if (board[j][col] == player) { count++; } } if (count == 5) { return 1; } } } // 左上到右下斜向检查 for (i = row - 4, j = col - 4; i <= row && j <= col; i++, j++) { if (i >= 0 && i + 4 < SIZE && j >= 0 && j + 4 < SIZE) { count = 0; for (int k = 0; k < 5; k++) { if (board[i + k][j + k] == player) { count++; } } if (count == 5) { return 1; } } } // 左下到右上斜向检查 for (i = row + 4, j = col - 4; i >= row && j <= col; i--, j++) { if (i >= 0 && i - 4 < SIZE && j >= 0 && j + 4 < SIZE) { count = 0; for (int k = 0; k < 5; k++) { if (board[i - k][j + k] == player) { count++; } } if (count == 5) { return 1; } } } return 0; } int main() { int row, col; char player = 'X'; initBoard(); while (1) { printBoard(); printf("Player %c's turn. Enter row and column: ", player); scanf("%d %d", &row, &col); if (row < 0 || row >= SIZE || col < 0 || col >= SIZE) { printf("Invalid input. Please try again.\n"); continue; } if (board[row][col] != ' ') { printf("This position is already occupied. Please try again.\n"); continue; } board[row][col] = player; if (checkWin(row, col, player)) { printf("Player %c wins!\n", player); break; } player = (player == 'X') ? 'O' : 'X'; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值