C语言代码实现简单三子棋游戏
简介
三子棋是一种经典的策略游戏,在游戏中两位玩家轮流放置棋子,直到有一方在棋盘上形成连续的三枚棋子为止。由于其简单而有趣,常被用于讲解人工智能、算法等知识点。本文将介绍如何使用C语言编写一个简单的三子棋游戏。
准备工作
在开始编码前,需要先准备好C语言的开发环境。推荐使用Visual Studio Code和MinGW来进行开发。另外需要掌握C语言的基本语法和数组的使用。
程序设计
设计思路
本游戏使用5x5的棋盘进行游戏,定义了三种状态:O表示玩家1放置的棋子,X表示玩家2放置的棋子,-表示空位置。在游戏开始时,由玩家1先手,两位玩家轮流放置棋子,直到有一方在棋盘上形成连续的三枚棋子。
实现步骤
1. 初始化棋盘
定义一个5x5的二维字符数组来表示棋盘,初始值全部设置为‘-’,即空。
char board[5][5] = {
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'}
};
2. 游戏开始
在控制台输出欢迎语,并给出玩家1的指示。
printf("欢迎来玩三子棋游戏!\n");
while (1) {
int row, col;
printf("玩家1,请输入行列坐标(格式:x y):");
scanf("%d %d", &row, &col);
// ...放置玩家1的棋子...
// ...判断胜负...
printf("玩家2,请输入行列坐标(格式:x y):");
scanf("%d %d", &row, &col);
// ...放置玩家2的棋子...
// ...判断胜负...
}
3. 给棋盘放置棋子
根据玩家的输入,确定要放置棋子的位置,并在棋盘上进行标记。
while (1) {
int row, col;
printf("玩家1,请输入行列坐标(格式:x y):");
scanf("%d %d", &row, &col);
while (board[row][col] != '-') {
printf("该位置已经有棋子了,请重新输入:");
scanf("%d %d", &row, &col);
}
board[row][col] = 'O';
// 绘制棋盘
// 判断胜负
printf("玩家2,请输入行列坐标(格式:x y):");
scanf("%d %d", &row, &col);
// ...放置玩家2的棋子...
// ...判断胜负...
}
4. 绘制棋盘
使用for循环逐行打印棋盘即可。
void draw_board() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
5. 判断胜负
判断胜负的方法比较直观。从横、竖、斜三个方向逐一扫描棋盘,对于每个位置,检查其所在的行、列、对角线是否连续三个相同的棋子。如果有,则对应的玩家胜利。
int check_win(char player) {
for (int i = 0; i < 5; i++) {
// 检查横向
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return 1;
}
if (board[i][1] == player && board[i][2] == player && board[i][3] == player) {
return 1;
}
if (board[i][2] == player && board[i][3] == player && board[i][4] == player) {
return 1;
}
// 检查竖向
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return 1;
}
if (board[1][i] == player && board[2][i] == player && board[3][i] == player) {
return 1;
}
if (board[2][i] == player && board[3][i] == player && board[4][i] == player) {
return 1;
}
}
// 检查对角线
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return 1;
}
if (board[1][1] == player && board[2][2] == player && board[3][3] == player) {
return 1;
}
if (board[2][2] == player && board[3][3] == player && board[4][4] == player) {
return 1;
}
if (board[0][4] == player && board[1][3] == player && board[2][2] == player) {
return 1;
}
if (board[1][3] == player && board[2][2] == player && board[3][1] == player) {
return 1;
}
if (board[2][2] == player && board[3][1] == player && board[4][0] == player) {
return 1;
}
// 没有胜出
return 0;
}
6. 示例
可以使用以下代码来进行一次游戏示例:
#include <stdio.h>
char board[5][5] = {
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-'}
};
int check_win(char player) {
for (int i = 0; i < 5; i++) {
// 检查横向
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return 1;
}
if (board[i][1] == player && board[i][2] == player && board[i][3] == player) {
return 1;
}
if (board[i][2] == player && board[i][3] == player && board[i][4] == player) {
return 1;
}
// 检查竖向
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return 1;
}
if (board[1][i] == player && board[2][i] == player && board[3][i] == player) {
return 1;
}
if (board[2][i] == player && board[3][i] == player && board[4][i] == player) {
return 1;
}
}
// 检查对角线
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return 1;
}
if (board[1][1] == player && board[2][2] == player && board[3][3] == player) {
return 1;
}
if (board[2][2] == player && board[3][3] == player && board[4][4] == player) {
return 1;
}
if (board[0][4] == player && board[1][3] == player && board[2][2] == player) {
return 1;
}
if (board[1][3] == player && board[2][2] == player && board[3][1] == player) {
return 1;
}
if (board[2][2] == player && board[3][1] == player && board[4][0] == player) {
return 1;
}
// 没有胜出
return 0;
}
void draw_board() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int main() {
printf("欢迎来玩三子棋游戏!\n");
while (1) {
int row, col;
printf("玩家1,请输入行列坐标(格式:x y):");
scanf("%d %d", &row, &col);
while (board[row][col] != '-') {
printf("该位置已经有棋子了,请重新输入:");
scanf("%d %d", &row, &col);
}
board[row][col] = 'O';
draw_board();
if (check_win('O')) {
printf("玩家1胜利!\n");
break;
}
printf("玩家2,请输入行列坐标(格式:x y):");
scanf("%d %d", &row, &col);
while (board[row][col] != '-') {
printf("该位置已经有棋子了,请重新输入:");
scanf("%d %d", &row, &col);
}
board[row][col] = 'X';
draw_board();
if (check_win('X')) {
printf("玩家2胜利!\n");
break;
}
}
}
总结
通过本文介绍,读者能够了解如何使用C语言编写一个简单的三子棋游戏,并能够基于此进行更加复杂的扩展。此外,读者还能够掌握C语言数组的基本用法,以及对于简单问题的解决思路。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言代码实现简单三子棋游戏 - Python技术站