Linux中用C语言设计五子棋小游戏
概述
本文将详细解析一个简单的命令行五子棋游戏的C语言实现代码。该代码使用了基本的控制台输入输出和简单的逻辑判断来实现游戏功能。
代码结构
代码主要分为几个部分:
-
包含的头文件和全局变量定义
c复制代码#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <unistd.h> // 用于 usleep #include "getch.h" // 假设你已经正确实现了 getch.h 在前面发布过的迷宫游戏中有 char chessboard[15][15]; // 棋盘数组 int path_x = 0, path_y = 0; // 光标位置
chessboard[15][15]
:二维数组,用于表示15x15的棋盘。path_x
和path_y
:记录当前光标的位置,用于标记玩家下棋的位置。
-
打印棋盘函数
print_chessboard()
c复制代码void print_chessboard() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (i == path_x && j == path_y) { printf(" %c", current_player); // 显示光标位置 } else { printf(" %c", chessboard[i][j]); // 显示棋盘内容 } } printf("\n"); } }
- 该函数用于打印棋盘状态,将当前棋盘数组中的内容打印出来。
- 如果
(i, j)
位置与光标位置(path_x, path_y)
匹配,则显示当前玩家的棋子。
-
检测连续棋子的函数
has_continuous_chess(int x, int y)
c复制代码bool has_continuous_chess(int x, int y) { if (chessboard[x][y] != '#') { path_x = x; path_y = y; if (count_direction(1, 0) + count_direction(-1, 0) >= 4 || count_direction(0, 1) + count_direction(0, -1) >= 4 || count_direction(1, 1) + count_direction(-1, -1) >= 4 || count_direction(1, -1) + count_direction(-1, 1) >= 4) { return true; // 检测到连续的棋子 } } return false; // 未检测到连续的棋子 }
- 该函数检查指定位置
(x, y)
是否有至少4个相同棋子连成一线(包括横向、竖向、两个对角线方向)。 - 调用了
count_direction()
函数来计算特定方向上的连续棋子数量。
- 该函数检查指定位置
-
主函数
main()
c复制代码int main(int argc, const char* argv[]) { char current_player = '@'; // 当前玩家 bool game_over = false; // 游戏结束标志 while (!game_over) { system("clear"); // 清空控制台显示 print_chessboard(); // 打印当前棋盘 printf("当前玩家:%c", current_player); // 显示当前玩家 int ch = getch(); // 获取键盘输入 switch (ch) { case 183: // 上箭头键 if (path_x > 0) path_x--; break; case 184: // 下箭头键 if (path_x < 14) path_x++; break; case 185: // 右箭头键 if (path_y < 14) path_y++; break; case 186: // 左箭头键 if (path_y > 0) path_y--; break; case '\n': // 回车键,落子 if (chessboard[path_x][path_y] == '#') { chessboard[path_x][path_y] = current_player; // 在棋盘上落子 if (has_continuous_chess(path_x, path_y)) { system("clear"); // 清空控制台显示 print_chessboard(); // 打印最终棋盘状态 printf("玩家 %c 获胜!\n", current_player); // 显示获胜玩家 game_over = true; // 游戏结束 } current_player = (current_player == '@') ? '$' : '@'; // 切换玩家 } break; } } return 0; }
- 主函数控制游戏流程,通过不断获取键盘输入来控制光标移动和落子。
- 每次落子后,检查是否有玩家胜出,如果有则显示胜利信息并结束游戏。
总结
该代码实现了一个简单的命令行五子棋游戏,具备基本的游戏功能,包括棋盘显示、光标移动、落子判断和玩家胜负判定。通过该例子,展示了如何利用C语言的基本语法和控制台操作实现简单的游戏逻辑。
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h> // 用于 usleep
#include "getch.h" // 假设你已经正确实现了 getch.h
// 定义棋盘
char chessboard[15][15] = {
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
};
int path_x = 0, path_y = 0; // 记录光标位置
void print_chessboard() // 打印棋盘
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (i == path_x && j == path_y) {
printf(" %c",current_player); // 显示光标位置
} else {
printf(" %c", chessboard[i][j]);
}
}
printf("\n");
}
}
// 计算某个方向有连续的棋子数
int count_direction(int go_x, int go_y)
{
int count = 0;
for (int x = path_x + go_x, y = path_y + go_y; x >= 0 && x < 15 && y >= 0 && y < 15; x += go_x, y += go_y)
{
if (chessboard[x][y] == chessboard[path_x][path_y])
{
count++;
}
else
{
break;
}
}
return count;
}
// 判断是否有连续的棋子
bool has_continuous_chess(int x, int y)
{
if (chessboard[x][y] != '#')
{
path_x = x;
path_y = y;
if (count_direction(1, 0) + count_direction(-1, 0) >= 4)
{
return true;
}
if (count_direction(0, 1) + count_direction(0, -1) >= 4)
{
return true;
}
if (count_direction(1, 1) + count_direction(-1, -1) >= 4)
{
return true;
}
if (count_direction(1, -1) + count_direction(-1, 1) >= 4)
{
return true;
}
}
return false;
}
int main(int argc, const char* argv[]) {
char current_player = '@';
bool game_over = false;
while (!game_over) {
system("clear");
print_chessboard();
printf("当前玩家:%c",current_player);
int ch = getch(); // 获取键盘输入
switch (ch) {
case 183: // 上箭头键
if (path_x > 0) path_x--;
break;
case 184: // 下箭头键
if (path_x < 14) path_x++;
break;
case 185: // 右箭头键
if (path_y < 14) path_y++;
break;
case 186: // 左箭头键
if (path_y > 0) path_y--;
break;
case '\n': // 回车键,落子
if (chessboard[path_x][path_y] == '#') {
chessboard[path_x][path_y] = current_player;
if (has_continuous_chess(path_x, path_y)) {
system("clear");
print_chessboard();
printf("玩家 %c 获胜!\n", current_player);
game_over = true;
}
// 切换玩家
current_player = (current_player == '@') ? '$' : '@';
}
break;
}
}
return 0;
}