C语言实现五子棋对战系统
作者:连长少尉
这篇文章主要为大家详细介绍了C语言实现五子棋对战系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C语言实现五子棋对战的具体代码,供大家参考,具体内容如下
一直以来,有不少热爱并希望学习五子棋的人,或者仅为了娱乐来下五子棋的人,他们一般通过下棋对战来增加自己的对战经验,而在现实生活由于五子棋布板麻烦,经常缺少能下棋的环境,并且下棋时效率较低,记录步数也较为麻烦。利用计算机来模拟下五子棋环境,只要有计算机,就可以很方便的随时随地进行下棋,并且对战过程中对步数和下子过程进行记录,方便了喜欢下五子棋的人,让他们的五子棋学习更加高效或者娱乐起来更加方便。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <conio.h> char draw[32][60] = {{" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, {" "}, {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"}, }; typedef struct//坐标结构体 { int x; int y; } node; typedef struct//栈结构体 { node data[120]; int top; } stack; int win[3], player, step1, step2; stack player1,player2; char ch; node pos; int convY(int y); int convX(int x); void refreshDraw(); void BLUE(); void RED(); void GREEN(); void WHITE(); void printMap(); void printMenu(); void refreshMap(); void playerOperation(char cheese); void jundge(int p, char cheese); void in(char cheese); void playerVsPlayer(); void playerVscomputer(); void gotoxy(int x, int y); void printStep(int player, int step); void askExit(); void printPos(int player, int x, int y); void printRoad(); int main() { char num[99]; printMenu(); while(1) { WHITE(); refreshDraw(); pos.x = 0; pos.y = 0; win[1] = 0; win[2] = 0; gets(num); if(strcmp(num, "1") == 0) { playerVsPlayer(); system("cls"); printMenu(); } else if(strcmp(num, "2") == 0) { askExit(); continue; } else { printf("请输入正确数字!\n"); } } return 0; } void BLUE()//字体变蓝 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE|FOREGROUND_INTENSITY); } void RED()//字体变红 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTENSITY); } void GREEN()//字体变绿 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_INTENSITY); } void WHITE()//字体变白 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } void printMap()//打印棋盘和棋子 { for(int i = 0; i <= 29; i++) { for(int j = 0; j <= 59; j++) { if(i==convX(pos.x) && (j==convY(pos.y)-1 || j==convY(pos.y)+1) ) { if(player == 1) { GREEN(); } else if(player == 2) { RED(); } } else if(draw[i][j] == '*') { RED(); } else if(draw[i][j] == 'O') { GREEN(); } printf("%c", draw[i][j]); WHITE(); } printf("\n"); } } void refreshDraw()//清空棋盘 { int i; for(i = 0; i <= 30; i=i+2) { strcpy(draw[i]," "); strcpy(draw[i+1],"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"); } strcpy(draw[31]," "); } void printMenu()//打印菜单 { WHITE(); printf("\n\n ****************************\n"); printf(" * *\n"); printf(" * 欢迎使用 *\n"); printf(" * 五子棋模拟对战系统 *\n"); printf(" * *\n"); printf(" ****************************\n\n\n"); printf("\n【设计制作:by 软件18-8邵蔚】\n\n"); printf("游戏规则为:\n二人参与游玩,双方分别使用两种棋子,\n"); printf("下在棋盘的 [ ] 上,\n"); printf("先形成5子连线(斜线、竖线或横线)的人获胜。\n\n"); printf("操作说明:\n玩家用键盘“W”“S”“A”“D”来选择下棋位置,选择好后,\n"); printf("按“J”键来下棋,然后本回合结束,下一名玩家操作。\n\n"); printf("输入 1 并按回车开始游戏\n"); printf("输入 2 并按回车退出游戏\n"); } int convY(int y)//将棋盘坐标转换为字符串数组下标 { return 4*(y+8)-3; } int convX(int x)//将棋盘坐标转换为字符串数组下标 { return 2*(8-x)-1; } void refreshMap()//刷新棋盘画面 { gotoxy(0,0); printMap(); gotoxy(0,0); } void playerOperation(char cheese)//扫描玩家输入的 "W""A""S""D""J""K" , 并且在棋盘显示或删除棋子; { while(1) { refreshMap(); ch = getch(); if(ch == 'a' && pos.y > -7) { pos.y--; } else if(ch == 'd' && pos.y < 7) { pos.y++; } else if(ch == 'w' && pos.x < 7) { pos.x++; } else if(ch == 's' && pos.x > -7) { pos.x--; } else if(ch == 'j' && draw[convX(pos.x)][convY(pos.y)] == ' ') { draw[convX(pos.x)][convY(pos.y)] = cheese; in(cheese); refreshMap(); break; } else if(ch == 'k' && player1.top > 0) { int tx, ty; if(cheese == '*') { tx = player1.data[player1.top].x; ty = player1.data[player1.top].y; player1.top--; draw[convX(tx)][convY(ty)] = ' '; refreshMap(); break; } else if(cheese == 'O') { tx = player2.data[player2.top].x; ty = player2.data[player2.top].y; player2.top--; } draw[convX(tx)][convY(ty)] = ' '; refreshMap(); break; } else if(ch == 'k' && player1.top <= 0) { system("cls"); printf("提示:没有棋子了!\n"); system("pause"); refreshMap(); printStep(1, step1); printStep(2, step2); gotoxy(65,16); printf(" 按 K 来悔棋。"); gotoxy(65, 23); GREEN(); printf("(玩家1 : O)"); gotoxy(65, 24); RED(); printf("(玩家1 : *)"); } } } void jundge(int p, char cheese)//判断是否有赢家 { int tx, ty, sum = 0; tx = pos.x; ty = pos.y; sum = 0; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; tx++; } tx = pos.x - 1; ty = pos.y; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; tx--; } if(sum >= 5) { win[p] = 1; return; } //-------------------------------------------------------------------- tx = pos.x; ty = pos.y; sum = 0; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; ty++; } tx = pos.x; ty = pos.y - 1; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; ty--; } if(sum >= 5) { win[p] = 1; return; } //---------------------------------------------------------------------- tx = pos.x; ty = pos.y; sum = 0; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; ty++; tx++; } tx = pos.x - 1; ty = pos.y - 1; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; ty--; tx--; } if(sum >= 5) { win[p] = 1; return; } //---------------------------------------------------------------------- tx = pos.x; ty = pos.y; sum = 0; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; tx--; ty++; } tx = pos.x + 1; ty = pos.y - 1; while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if(draw[convX(tx)][convY(ty)] == cheese) sum++; tx++; ty--; } if(sum >= 5) { win[p] = 1; return; } return; } void in(char cheese)//将棋子坐标入栈 { if(cheese == '*') { player2.top++; player2.data[player2.top].x = pos.x; player2.data[player2.top].y = pos.y; } else if(cheese == 'O') { player1.top++; player1.data[player1.top].x = pos.x; player1.data[player1.top].y = pos.y; } } void playerVsPlayer() { step1 = 0; step2 = 0; player1.top = 0; player2.top = 0; player1.data[0].x = -1; player1.data[0].y = -1; player2.data[0].x = -1; player2.data[0].y = -1; player = 2; printStep(1, step1); printStep(2, step2); gotoxy(65, 16); printf(" 按 K 来悔棋。"); gotoxy(65, 17); printf(" 按 J 来下子。"); gotoxy(65, 18); printf(" 按 W A S D 控制方向。"); gotoxy(65, 23); GREEN(); printf("(玩家1 : O)"); gotoxy(65, 24); RED(); printf("(玩家2 : *)"); WHITE(); while(win[1] == 0 && win[2] == 0) { int tx, ty; if(win[1] == 0 && win[2] == 0) { player = 1; playerOperation('O'); if(ch == 'k' || ch == 'K') step2--; else step1++; tx = player1.data[player1.top].x; ty = player1.data[player1.top].y; printStep(1, step1); printStep(2, step2); printPos(1, tx, ty); } jundge(1,'O'); if(win[1] == 0 && win[2] == 0) { player = 2; playerOperation('*'); if(ch == 'k' || ch == 'K') step1--; else step2++; tx = player2.data[player2.top].x; ty = player2.data[player2.top].y; printStep(1, step1); printStep(2, step2); printPos(2, tx, ty); } jundge(2,'*'); } if(win[1] == 1) { gotoxy(0,31); printf("【"); GREEN(); printf("玩家1"); WHITE(); printf("】 赢了 !\n"); } else if(win[2] == 1) { gotoxy(0,31); printf("【"); RED(); printf("玩家2"); WHITE(); printf("】 赢了 !\n"); } printf("按回车查看双方下棋路线!\n"); getchar(); system("cls"); if(win[1] == 1) { GREEN(); printf("【玩家1】 赢了 !\n"); } else if(win[2] == 1) { RED(); printf("【玩家2】 赢了 !\n"); } printRoad(); getchar(); return; } void askExit()//询问退出 { char dis[20]; system("cls"); printf("确定退出?\n Y/N?\n"); gets(dis); if(strcmp(dis, "y") == 0 || strcmp(dis, "Y") == 0) { system("cls"); printf("感谢你的使用!\n"); exit(0); } else { system("cls"); printMenu(); } return; } void gotoxy(int x, int y)//移动光标函数 { COORD pos = {x,y}; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); } void printStep(int player, int step)//打印已走步数 { if(player == 1) { gotoxy(65,4); GREEN(); printf("【玩家1】 :%02d", step); WHITE(); } else if(player == 2) { gotoxy(65,6); RED(); printf("【玩家2】 :%02d", step); WHITE(); } return; } void printPos(int player, int x, int y)//打印下子坐标 { if(player == 1) { gotoxy(65, 10); GREEN(); if(x == -1 && y == -1) { printf(" "); } else { printf("玩家1下棋位置:第%d行,第%d列",8 + x, 8 + y); } WHITE(); } else if(player == 2) { gotoxy(65, 10); RED(); if(x == -1 && y == -1) { printf(" "); } else { printf("玩家2下棋位置:第%d行,第%d列",8 + x, 8 + y); } WHITE(); } return; } void printRoad()//打印所有下子坐标 { int i; GREEN(); printf("玩家1的下棋路线为:\n"); for(i = 1; i <= player1.top; i++) { printf("[ 第%d步 : (%d, %d) ] ", i, 8 + player1.data[i].x, 8 + player1.data[i].y); if(i % 5 == 0) printf("\n"); } printf("\n"); RED(); printf("玩家2的下棋路线为:\n"); for(i = 1; i <= player2.top; i++) { printf("[ 第%d步 : (%d, %d) ] ", i, 8 + player2.data[i].x, 8 + player2.data[i].y); if(i % 5 == 0) printf("\n"); } printf("\n"); WHITE(); printf("输入 E 结束查看并返回菜单!"); while(1) { scanf("%c", &ch); if(ch == 'E' || ch == 'e') break; } return; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。