C语言五子棋玩家交替下棋示例

时间: 2024-01-04 15:04:27 浏览: 33
以下是一个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 isWin() { // 检查行 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE - 4; j++) { if (board[i][j] != ' ' && board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] == board[i][j+3] && board[i][j] == board[i][j+4]) { return 1; } } } // 检查列 for (int i = 0; i < SIZE - 4; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] != ' ' && board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] && board[i][j] == board[i+3][j] && board[i][j] == board[i+4][j]) { return 1; } } } // 检查主对角线 for (int i = 0; i < SIZE - 4; i++) { for (int j = 0; j < SIZE - 4; j++) { if (board[i][j] != ' ' && board[i][j] == board[i+1][j+1] && board[i][j] == board[i+2][j+2] && board[i][j] == board[i+3][j+3] && board[i][j] == board[i+4][j+4]) { return 1; } } } // 检查副对角线 for (int i = 0; i < SIZE - 4; i++) { for (int j = 4; j < SIZE; j++) { if (board[i][j] != ' ' && board[i][j] == board[i+1][j-1] && board[i][j] == board[i+2][j-2] && board[i][j] == board[i+3][j-3] && board[i][j] == board[i+4][j-4]) { return 1; } } } return 0; } int main() { initBoard(); int player = 1; // 玩家1先下棋 int row, col; while (1) { printBoard(); printf("Player %d, enter your move (row column): ", player); scanf("%d %d", &row, &col); if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || board[row][col] != ' ') { printf("Invalid move. Try again.\n"); continue; } if (player == 1) { board[row][col] = 'X'; } else { board[row][col] = 'O'; } if (isWin()) { printf("Player %d wins!\n", player); break; } player = (player == 1) ? 2 : 1; // 切换玩家 } printBoard(); return 0; } ```

相关推荐

最新推荐

recommend-type

C语言五子棋实验报告.docx

使用C语言基于EGE图形库实现的五子棋小游戏,实验报告,C语言程序设计的结课作业,实现了人机对弈与人人对战功能。有相应的音乐
recommend-type

基于C语言实现的迷宫算法示例

主要介绍了基于C语言实现的迷宫算法,结合具体实例形式分析了C语言解决迷宫问题算法的实现技巧与相关注意事项,需要的朋友可以参考下
recommend-type

codeblocks五子棋c语言代码.docx

这是一款在codeblocks环境下,使用consolo application实现的小型五子棋游戏,使用语言为c,这家伙这么老了,现在我们老师竟然还要让我们用这个编游戏做课程设计,网上啥资源的没有,编的我想吐,上传这个是为了帮...
recommend-type

C语言计算日期差的方法示例

主要介绍了C语言计算日期差的方法,结合具体实例形式分析了C语言针对日期转换、运算等相关操作技巧,需要的朋友可以参考下
recommend-type

c语言strftime时间格式化示例

C/C++程序中需要程序显示当前时间,可以使用标准函数strftime,本文提供一个示例供大家参考
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。