通过本文主要向大家介绍了五子棋小游戏,五子棋小游戏双人,单人五子棋小游戏,五子棋小游戏大全,4399小游戏五子棋等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
首先我们先来看一个稍微简单些的实现方式:
#include <stdio.h> #include <stdlib.h> #define N 15 int chessboard[N + 1][N + 1] = { 0 }; int whoseTurn = 0; void initGame(void); void printChessboard(void); void playChess(void); int judge(int, int); int main(void) { initGame(); while (1) { whoseTurn++; playChess(); } return 0; } void initGame(void) { char c; printf("Please input \'y\' to enter the game:"); c = getchar(); if ('y' != c && 'Y' != c) exit(0); system("cls"); printChessboard(); } void printChessboard(void) { int i, j; for (i = 0; i <= N; i++) { for (j = 0; j <= N; j++) { if (0 == i) printf("%3d", j); else if (j == 0) printf("%3d", i); else if (1 == chessboard[i][j]) printf(" O"); else if (2 == chessboard[i][j]) printf(" X"); else printf(" *"); } printf("\n"); } } void playChess(void) { int i, j, winner; if (1 == whoseTurn % 2) { printf("Turn to player 1, please input the position:"); scanf("%d %d", &i, &j); while (chessboard[i][j] != 0) { printf("This position has been occupied, please input the position again:"); scanf("%d %d", &i, &j); } chessboard[i][j] = 1; } else { printf("Turn to player 1, please input the position:"); scanf("%d %d", &i, &j); while (chessboard[i][j] != 0) { printf("This position has been occupied, please input the position again:"); scanf("%d %d", &i, &j); } chessboard[i][j] = 2; } system("cls"); printChessboard(); if (judge(i, j)) { if (1 == whoseTurn % 2) { printf("Winner is player 1!\n"); exit(0); } else { printf("Winner is player 2!\n"); exit(0); } } } int judge(int x, int y) { int i, j; int t = 2 - whoseTurn % 2; for (i = x - 4, j = y; i <= x; i++) { if (i >= 1 && i <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j] && t == chessboard[i + 2][j] && t == chessboard[i + 3][j] && t == chessboard[i + 4][j]) return 1; } for (i = x, j = y - 4; j <= y; j++) { if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i][j + 1] && t == chessboard[i][j + 1] && t == chessboard[i][j + 3] && t == chessboard[i][j + 4]) return 1; } for (i = x - 4, j = y - 4; i <= x, j <= y; i++, j++) { if (i >= 1 && i <= N - 4 && j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j + 1] && t == chessboard[i + 2][j + 2] && t == chessboard[i + 3][j + 3] && t == chessboard[i + 4][j + 4]) return 1; } for (i = x + 4, j = y - 4; i >= 1, j <= y; i--, j++) { if (i >= 1 && i <= N - 4 && j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i - 1][j + 1] && t == chessboard[i - 2][j + 2] && t == chessboard[i - 3][j + 3] && t == chessboard[i - 4][j + 4]) return 1; } return 0; }</div>
演示截图
我们再来看一个更复杂些的
C语言实现五子棋小游戏 # include<stdio.h> # include<stdio.h> # include<stdio.h> # include<stdio.h>//插入输入输出头文件 # include<string.h>//字符数组的函数定义的头文件 # include<stdlib.h>//stdlib 头文件即standard library标准库头文件stdlib 头文件里包含了C、C++语言的最常用的系统函数该 ,文件包含了C语言标准库函数的定义. # define SPA 0//在C或C++语言源程序中允许用一个标识符来表示一个字符串,称为“宏”。 “define”为宏定义命令。 # define MAN 1 # define COM 2 /* 空位置设为0 ,玩家下的位置设为1 ,电脑下的位置设为2 */ #define ORDEX 15 int qipan[ORDEX][ORDEX]; //自己定义的预处理函数,以二维数组形式构建棋盘 /* 15*15的棋盘 */ typedef struct//typedef的作用是在C中定义一个结构体类型 { int iFlag; int iX, iY; }ScmPiece; int g_iPieceLen; ScmPiece g_ScmGame1, g_ScmGame2; void start(); /* 程序的主要控制函数 */ void draw(); /* 画棋盘 */ int Victory( ScmPiece * pScmPiece ); /* 判断胜利 p q为判断点坐标 */ void AI(int *p,int *q); /* 电脑下子 p q返回下子坐标 */ int value(int p,int q); /* 计算空点p q的价值 */ int qixing(int n,int p,int q); /* 返回空点p q在n方向上的棋型 n为1-8方向 从右顺时针开始数 */ void yiwei(int n,int *i,int *j); /* 在n方向上对坐标 i j 移位 n为1-8方向 从右顺时针开始数 */ void InitGame(){ int i; g_iPieceLen = ORDEX * ORDEX; g_ScmGame1.iX = 0; g_ScmGame1.iY = 0; g_ScmGame1.iFlag = COM; g_ScmGame2.iX = 7; g_ScmGame2.iY = 7; g_ScmGame2.iFlag = MAN; for( i=0; i < ORDEX*ORDEX; ++i ) *( (int*)qipan + i ) = SPA; } void main() { char k; do{ InitGame(); start(); printf("还要再来一把吗?输入y或n:"); getchar(); scanf("%c",&k); while(k!='y'&&k!='n'){ printf("输入错误,请重新输入\n"); scanf("%c",&k); } system("cls"); }while(k=='y'); printf("谢谢使用!\n"); } void MakePiece( ScmPiece * pScmPiece, int iGameFlag ){ if( iGameFlag ) { printf("请下子:"); while( scanf( "%d%d", &pScmPiece->iX, &pScmPiece->iY ) ) { if( ( pScmPiece->iX < 0 || pScmPiece->iX > ORDEX-1 ) || ( pScmPiece->iY < 0 || pScmPiece->iY > ORDEX-1 ) ) printf( "坐标错误!请重新输入:"); else if( qipan[pScmPiece->iX][pScmPiece->iY] ) printf( "该位置已有子!请重新输入:"); else break; } } qipan[pScmPiece->iX][pScmPiece->iY] = pScmPiece->iFlag; --g_iPieceLen; system("cls"); draw(); if( iGameFlag == 0 ) printf("电脑下在%d %d\n", pScmPiece->iX, pScmPiece->iY ); } void start() { int choice; //ScmPiece ScmGameTemp1, ScmGameTemp2;/* a1 b1储存玩家上手坐标 c1 d1储存电脑上手坐标 */ printf("\t╔═══════════════════════════════╗\n"); printf("\t║ ║\n"); printf("\t║ 欢迎使用五子棋对战程序 祝您玩的愉快挑战无极限 ║\n"); printf("\t║ ║\n"); printf("\t║ ._______________________. ║\n"); printf("\t║ | _____________________ | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I 五 子 棋 I | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I___________________I | ║\n"); printf("\t║ !_______________________! ║\n"); printf("\t║ ._[__________]_. ║\n"); printf("\t║ .___|_______________|___. ║\n"); printf("\t║ |::: ____ | ║\n"); printf("\t║ | ~~~~ [CD-ROM] | ║\n"); printf("\t║ !_____________________! ║\n"); printf("\t║ ║\n"); printf("\t║ ║\n"); printf("\t║ 寒 星 溪 月 疏 星 首,花 残 二 月 并 白 莲。 ║\n"); printf("\t║ 雨 月 金 星 追 黑 玉,松 丘 新 宵 瑞 山 腥。 ║\n"); printf("\t║ 星 月 长 峡 恒 水 流,白 莲 垂 俏 云 浦 岚。 ║\n"); printf("\t║ 黑 玉 银 月 倚 明 星,斜 月 明 月 堪 称 朋。 ║\n"); printf("\t║ 二 十 六 局 先 弃 二,直 指 游 星 斜 彗 星。 ║\n"); printf("\t║ ║\n"); printf("\t║ ║\n"); printf("\t║ 1.人机对战 2.人人对战 ║\n"); printf("\t║ ║\n"); printf("\t╚═══════════════════════════════╝\n"); printf("\t\t\t请输入1或2:"); while( scanf( "%d", &choice ), choice!=1 && choice!=2 ) printf( "输入错误,请重新输入:" ); system("cls"); if( choice == 1 ) { /* 人机模式 */ printf("\t\t\t欢迎使用五子棋人机对战!\n\t\t下子请输入坐标(如13 6)中间以空格分隔。\n\n\n"); draw(); printf("先下请按1,后下请按2:"); while( scanf( "%d", &choice ), choice!=1 && choice!=2 ) printf( "输入错 误,请重新输入:" ); if( choice == 2 )