五子棋人机对弈

来源:互联网 发布:网络安全设备巡检报告 编辑:程序博客网 时间:2024/05/02 23:52
#include "windows.h"#include <stdio.h>  #include <stdlib.h>    #include <conio.h>#ifndef __cplusplus#include <stdbool.h>   /*包含bool、false */#endif //__cplusplus/*****************************************/#define BOARD 16COORD g_chessPos={38,18};   /*存储光标位置*/short g_chessInfo[BOARD][BOARD]; /*g_chessInfo存储棋局信息,白子置为1,黑子为2,空则为0*/ /*****************************************/ void map(void);          //绘制棋盘 void gotoxy(short,short);void down(short,short);  //落子 void play(int);               void run(void);void showCursor(void);   //显示光标void clsCursor(void);    //清除光标void mark(int) ;        //参数为1标记白子,参数为0标记黑子 void printInfo(void);   //输出信息bool isEmpty(short,short);     //有棋子则为false bool isWin(int);    /*判断是否获胜,true为获胜,int参数为1白子 ,2为黑子*/ void generateWay(void);bool isWin(int);    /*判断是否获胜,true为获胜,int参数为1白子 ,2为黑*/ int counter(short,short,short);int mmax(int ,int);/*****************************************/int main(int argc, char *argv[]) {  system("mode con lines=36 cols=100");   //设置控制台大小 system("color f0");         CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };//隐藏光标SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);printInfo();map();showCursor();run();down(g_chessPos.X,g_chessPos.Y);system("pause");return 0;}  void gotoxy(short x,short y){HANDLE s_hConsole=GetStdHandle(STD_OUTPUT_HANDLE);COORD coord={x,y};SetConsoleCursorPosition(s_hConsole,coord);}void map(void){short x,y;for( x=10;x<=70;x+=4){for( y=2;y<=32;y+=2){gotoxy(x,y);    printf("╋━");gotoxy(x,y+1);  printf("┃");}}/*清除棋盘多余部分*/for(y=2;y<=32;y+=2){gotoxy(70+2,y);printf(" ");}for(x=10;x<=70;x+=4){gotoxy(x,32+1);printf(" ");}}void printInfo(void){gotoxy(76, 9);printf("控制:"); gotoxy(76,10);printf("↓ ↑ ← →控制移动"); gotoxy(76,11);printf("空格键落子");gotoxy(76,12);printf("按Esc退出"); }void down(short x,short y){    //黑子下棋 gotoxy(x,y);printf("●");mark(0);      //标记为黑子 if(isWin(2))  //判断黑棋是否获胜 {gotoxy(38,18);printf("黑子获胜");system("pause");}    //白子下棋 gotoxy(76,16);printf("执白子");generateWay();mark(1);   //标记白子 if(isWin(1)){ gotoxy(38,18);printf("bai子获胜"); system("pause");}gotoxy(76,16);printf("执黑子");}int mmax(int x,int y){return x>=y?x:y;}void run(void){gotoxy(76,16);printf("执黑子");while(1)if(_kbhit())play(_getch());}bool isEmpty(short x,short y){if(g_chessInfo[(x-10)/4] [(y-2)/2])    return false;return true;}void mark(int i) {/*i为1,白子置为1,i为0置黑子为2*/ g_chessInfo[(g_chessPos.X-10)/4] [(g_chessPos.Y-2)/2]=(i?1:2);}void play(int c){clsCursor();switch(c){case 72:    //上if(g_chessPos.Y-2>=2)  /*确保在棋盘内*/g_chessPos.Y-=2;break;     case 80:    //下if(g_chessPos.Y+2<=32)g_chessPos.Y+=2;   break;     case 75:    //左if(g_chessPos.X-4>=10)g_chessPos.X-=4;   break;      case 77:    //右if(g_chessPos.X+4<=70)g_chessPos.X+=4;   break;     case 32:   //空格落子    if(isEmpty(g_chessPos.X,g_chessPos.Y))    down(g_chessPos.X,g_chessPos.Y);break;case 27:   //Esc键exit(1);break;}showCursor();}void showCursor(void){gotoxy(g_chessPos.X-2,g_chessPos.Y-1);printf("┏━┓");gotoxy(g_chessPos.X-2,g_chessPos.Y+1);printf("┗━┛");}void clsCursor(void){gotoxy(g_chessPos.X-2,g_chessPos.Y-1);printf("     ");gotoxy(g_chessPos.X-2,g_chessPos.Y+1);printf("     ");if(g_chessPos.Y!=2){gotoxy(g_chessPos.X,g_chessPos.Y-1);printf("┃");}if(g_chessPos.Y!=32){gotoxy(g_chessPos.X,g_chessPos.Y+1);printf("┃");}}void generateWay(){short i,j;int num1=0,num2=0;COORD cd1={42,22},cd2={42,22};for(i=10;i<=70;i+=4){for(j=2;j<=32;j+=2){if(isEmpty(i,j)){if(num1<counter(i,j,1)){cd1.X=i; cd1.Y=j;num1=counter(i,j,1);}if(num2<counter(i,j,2)){cd2.X=i;cd2.Y=j;num2=counter(i,j,2);}}}}if(num1>=4) g_chessPos=cd1;else if(num2>=4)g_chessPos=cd2;else if(num1>=3)g_chessPos=cd1;else if(num2>=3)g_chessPos=cd2;else if(num1>=2)g_chessPos=cd1;else if(num2>=2)g_chessPos=cd2;elseg_chessPos=cd2;gotoxy(g_chessPos.X,g_chessPos.Y);printf("○");}int counter(short cx,short cy,short t)/*不会做,抄某位前辈的,呵呵......*/{int i,j,num,tnum;    int x=(cx-10)/4;int y=(cy-2)/2;    /////////////////////////////////////////////i=x+1;j=y;num=0,tnum=0;//横向向右while(i>=0&&i<BOARD&&t==g_chessInfo[i][j]){++num;++i;}i=x-1;    //横向向左while(i>=0&&i<BOARD&&t==g_chessInfo[i][j]){++num;--i;}     tnum=mmax(tnum,num);/////////////////////////////////////////////i=x;j=y+1;num=0;//纵向向下while(j>=0&&j<BOARD&&t==g_chessInfo[i][j]){++num;++j;}j=y-1;//纵向向上while(j>=0&&j<BOARD&&t==g_chessInfo[i][j]){++num;--j;}     tnum=mmax(tnum,num);/////////////////////////////////////////////i=x+1;j=y+1;num=0;//左斜向下while(j>=0&&j<BOARD&&t==g_chessInfo[i][j]){++num;++i;++j;}i=x-1;j=y-1;//左斜向上while(j>=0&&j<BOARD&&t==g_chessInfo[i][j]){++num;--i;--j;}     tnum=mmax(tnum,num);i=x-1;j=y+1;num=0;//右斜向下while(j>=0&&j<BOARD&&t==g_chessInfo[i][j]){++num;--i;++j;}i=x+1;j=y-1;//右斜向向上while(j>=0&&j<BOARD&&t==g_chessInfo[i][j]){++num;++i;--j;}     return mmax(tnum,num);}bool isWin(int tmp){if(counter(g_chessPos.X,g_chessPos.Y,tmp)>=4)return true;return false;}

0 0
原创粉丝点击