题目很简单,就是写一个圈叉棋的游戏,player one 下的棋子用圈表示,player two下的棋子用叉来表示。谁先横竖斜先连成三个子谁赢。
下面是在控制台中代码: 注本程序是在gcc编译器下编译。
#include <stdio.h>
#include <string.h>
#include <math.h>
int arr[4][4] = {0}; // map
char ch[4][4]; // picture of chess
void showMap(){ //显示地图
int i,j;
for (i = 1;i < 4;i++){
for (j = 1;j < 4;j++){
if(arr[i][j] == 1){
ch[i][j] = '*';
}
if(arr[i][j] == 2){
ch[i][j] = '#';
}
}
}
for (i = 1;i <= 3;i++){
printf("-------\n");
printf("|%c|%c|%c|\n",ch[i][1],ch[i][2],ch[i][3]);
}
printf("-------\n");
}
int judgeV(int p){ //判断胜利,即三子连成一线
int i,j;
for (i = 1;i <= 3;i++){
for (j = 1;j <= 3;j++){
if (i == 1){
if(arr[i][j] == p && arr[i][j] == arr[i+1][j] && arr[i+1][j] == arr[i+2][j])
return 1;
}
if (j == 1){
if(arr[i][j] == p && arr[i][j] == arr[i][j+1] && arr[i][j+1] == arr[i][j+2])
return 1;
}
if ((i == 1) && (j == 1)){
if(arr[i][j] == p && arr[i][j] == arr[i+1][j+1] && arr[i+1][j+1] == arr[i+2][j+2])
return 1;
}
}
}
return 0;
}
int main()
{
int x , y; // position
int num = 0; // number of change
int player = 1; // first_player = 1*, second_player = 2#;
memset(ch,' ',sizeof(ch));
showMap();
while(num < 9){
printf("Please input the position of your next turn: ");
scanf("%d %d",&x,&y);
while(x < 1 || x > 3 || y < 1 || y > 3 || arr[x][y]){ //outside the chessboard the pos
// -sition is occupied.
printf("Please input again: "); //若位置已被占用或输入位置出界,则要求重新输入
scanf("%d %d",&x,&y);
}
arr[x][y] = player;
showMap();
if(judgeV(player)){
if(player == 1)printf("The first player wins\n");
else printf("The second player wins\n");
break;
}
num++;
player = 3 - player;
}
if(num >= 9) {
printf("Dogfall\n");
}
}
运行演示:

本文详细介绍了如何使用C语言实现一个简单的圈叉棋游戏,包括游戏规则、地图显示、玩家交互、胜利条件判断等核心功能。通过注释丰富的代码示例,深入探讨了程序的实现细节。

被折叠的 条评论
为什么被折叠?



