zoukankan      html  css  js  c++  java
  • 笔记:C语言图形基本函数及实例五子棋游戏


    初始化图形界面
    int gdriver;
    int gmode;
    gdriver = DETECT;
    initgraph(&gdriver,&gmode,”” );

    清空屏幕
    cleardevice ();

    退出图形化界面
    closegraph ();

    绘图之前未设定调用颜色。则背景色默觉得黑色,前景色默觉得白色
    setbkcolor (WHITE) //背景色
    setcolor (4);//前景色

    颜色代码    字符常量    颜色      用途
    0       BLACK       黑       前景,背景  
    1       BLUE        蓝       前景。背景  
    2       GREEN       绿       前景。背景  
    3       CYAN        青       前景,背景  
    4       RED     红       前景。背景  
    5       MAGENTA     洋红      前景,背景  
    6       BROWN       棕       前景。背景  
    7       LIGHTGRAY   淡灰      前景
    8       DARKGRAY    深灰      前景
    9       LIGHTBLUE   淡蓝      前景
    10      LIGHTGREEN  淡绿      前景
    11      LIGHTCYAN   淡青      前景
    12      LIGHTRED    淡红      前景
    13      LIGHTMAGENTA    淡洋红     前景
    14      YELLOW      黄       前景
    15      WHITE       白       前景
    128     BLINK       闪烁      前景

    ——-图形化界面函数
    //自己測试的时候以下四个得到的值都是0
    int far getmaxx (void)
    int far getmaxy (void)
    int far getx (void)
    int far gety (void)
    //自己測试的时候以下两个没看到效果
    void far moveto (int x, int y) 光标移动到 (x,y)点,在移动过程中画点
    void far moverel (int dx, int dy); 光标从 (x,y)移动到 (x+dx, y+dy)的位置,移动中不画店

    //以下的有看到效果
    多用
    void far line (int x1, int y1, int x2, int y2) 画线

    void far setlinestyle (int linestyle, unsigned upattern, int thickness);
    linestyle 设置线的形状,
    0 实线
    1 点线
    2 中心线
    3 点画线
    4 用户定义线
    
    thickness 设置线的宽度
    1 一个像素宽(默认值)
    3 三个像素宽
    
    当linestyle取值为 4时。还必须设置upattern參数。

    此时upattern的16位二进制数 每一位代表一个像素,该位为1则画像素点。为0则不画。

    /* 头文件  */
    #include "stdio.h"
    #include "conio.h"
    #include "graphics.h"
    
    void main ()
    {
        int gdriver;
        int gmode;
    
        int arw[16] = {200, 102,
                       300, 102,
                       300, 107,
                       330, 100,
                       300, 93,
                       300, 98,
                       200, 98,
                       200, 102};
        gdriver = DETECT;
        initgraph(&gdriver,&gmode,"" );
        setbkcolor (WHITE);
        cleardevice ();
        setcolor (BLUE);
        circle (320, 240, 98);
        setlinestyle (0, 0, 3);
        setcolor (GREEN);
        rectangle (220, 140, 420, 340);
        setcolor (GREEN);
        setlinestyle (4,0xaaaa, 3);
        line (220, 240, 420, 240);
        line (320, 140, 320, 340);
        getch();
        closegraph ();
        return 0;
    
    }
    

    void far circle (int x, int y, int radius); 画圆圈
    void far rectangle (int x1, int y1, int x2, int y2); 画矩形

    少用
    void far putpixel (int x, int y, int color)画点
    void far linerel (int dx, int dy); 从当前光标 (x,y)画线到点 (x +dx , y + dy)
    void far arc (int x, int y, int stangle, int endangle, int radius); 画弧线
    void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius) 画椭型
    void far drawpoly (int numpoints , int far * polypoints) 画多边形
    顶点数为 numpoints 各顶点坐标由 polypoints 给出

    ———–填充函数
    void far setfillstyle (int pattern, int color )
    设置填充模式以及颜色。

    符号常量        数值      含义  
    略           0       以背景色颜色填充
                1       以实线填充
                2       以直线填充
                3       以斜线填充(阴影线)
                4       以粗斜线填充(粗阴影线)
                5       以粗反斜线填充(~)
                6       以反斜线填充(~)
                7       以直方网格填充
                8       以斜网格填充
                9       以间隔点填充
                10      以大间隔点填充
                11      以小间隔点填充
                12      以用户自己定义样图填充
    
    做五子棋的时候。 我这样设置红方的棋子。
    setfillstyle(1,RED);  
    floodfill(XiaoYuanX1+10,XiaoYuanY1+10,7);
    

    void far floodfill (int x, int y, int border);

    ————————-图形方式下文本输出
    void far outtext (char far *textstring);
    当前位置输出字符串
    void far outtext (int x, int y, far *textstring )
    在 (x。y) 位置输出字符串
    outtextxy(230,30,”player use Red chess!”);

    int sprintf (char * str, char *format, 变量列表)
    char s[30];
    int i = 400;
    sprintf (s, “your score is %d”, i );

    void far settextjustify (int horiz, int vert);
    定位输出字符串时的对齐方式,

    參数名     符号常量    值   含义
    horiz       LEFT_TEXT   0   左对齐
            CENTER_TEXT 1   中心对齐
            RIGHT_TEXT  2   右对齐
    
    vert        BOTTON  

    void far settextstyle (int font, int direction, int charsize);
    font 字型
    direction 输出方向
    charsize 字符大小

    font
    符号      数值      含义
    DEFAULT_FONT    0       8*8点阵字(默认)
    TRIPLEX_FONT    1       三重笔画字体
    SMALL_FONT  2       小号画笔字体
    SANSSERIF_FONT  3       无衬线画笔字体
    GOTHIC_FONT 4       黑体笔画笔
    
    direction
    HORIZ_DIR   0       从左到右
    VERT_DIR    1       从底到顶
    
    
    charsize
            1       8*8
            2       16*16
            3       24*24
    '''都是+8
            10      80*80
        USER_CHAR_SIZE=0    用户定义字符大小

    当初课程设计做的一个五子棋游戏。


    这里写图片描写叙述
    这里写图片描写叙述

    /*  结构化课程设计 : 五子棋游戏
     *  时间:  大二上学期
     *  说明:该程序要在 win-xp系统。win-tc下编译才干出现界面
     *  或者下载一个Dos-Box,在win-tc上编译,然后将编译完的.exe文件拖入到dos-box中
     *  由于codeblocks 头文件库中没有 "graphics.h" 该头文件。无法编译。

    */ /* 头文件 */ #include "stdio.h" #include "conio.h" #include "graphics.h" #include <bios.h> /* 系统键 玩家1 和 玩家2 的操作键 */ #define Esc 0x11b #define Player1_up 18432 #define Player1_down 20480 #define Player1_left 19200 #define Player1_right 19712 #define Player1_do 7181 /*enter*/ #define Player2_up 4471 #define Player2_down 8051 #define Player2_left 7777 #define Player2_right 8292 #define Player2_do 14624 /*space*/ int i; int j; int key;/*键盘输入*/ int flag;/*标志*/ int PlayMode;/*游戏模式*/ int RedXianhou;/*红方先后*/ int AllChessNumber = 0;/*棋盘中全部的棋子*/ int Chess[15][15] = {0};/*存棋子的棋盘*/ int CountNumber; /*连成五子时画出的五子连接线*/ int JudgeX; int JudgeY; int LineX1; int LineX2; int LineY1; int LineY2; int XiaoQiPanX1 = 230;/*小棋盘数据*/ int XiaoQiPanX2 = 380; int XiaoQiPanY1 = 230; int XiaoQiPanY2 = 380; int XiaoYuanX1 = 100;/*代表红方白方的圆的数据*/ int XiaoYuanY1 = 335; int XiaoYuanX2 = 520; int XiaoYuanY2 = 335; int YuanR = 20; int DaQiPanX1 = 170;/*大棋盘的数据*/ int DaQiPanX2 = 450; int DaQiPanY1 = 100; int DaQiPanY2 = 380; int ChessRedLocalX = 170;/*初始红方棋子的落点 */ int ChessRedLocalY = 100; int ChessWhiteLocalX = 170;/* 初始白方棋子的落点 */ int ChessWhiteLocalY = 100; int ChessLocalBoxX;/*棋子在棋盘二维数组中的位置*/ int ChessLocalBoxY; void Welcome(void);/*欢迎界面*/ void ModeChoose(void);/*模式选择*/ void DrawChessBox(void);/*棋盘*/ void ChooseFirst_Or_Second(void);/*先手后手的选择*/ void DrawChessR_W(int x,int y,int color);/*画棋盘上的红子和白子*/ void DrawStandForRed();/*代表红方的圆*/ void DrawStandForWhite();/*代表白方的圆*/ void Player1_vs_Player2(void);/*人人对战*/ void Player_vs_Computer(void);/*人机对战*/ void ChessNowLocal(int x,int y);/* 光标显示当前棋子位置 */ void ChessActionRed(int key);/*player1*/ void ChessActionWhite(int key); /*player2*/ void RedWin(void);/*红方胜利显示的胜利提示*/ void WhiteWin(void); /*白方。

    。*/ void PrintfChess(void);/*在清空棋盘后将全部的棋子又一次打印出来*/ void JudgeHeng(int x,int y,int R_or_W);/*各个方向上的推断*/ void JudgeShu(int x,int y,int R_or_W); void JudgeZuoXie(int x,int y,int R_or_W); void JudgeYouXie(int x,int y,int R_or_W); int ComputeYouXie(int x,int y,int R_or_W);/*人机模式时各个方向的分数推断*/ int ComputeShu(int x,int y,int R_or_W); int ComputeZuoXie(int x,int y,int R_or_W); int ComputeHeng(int x,int y,int R_or_W); int ScoreReturn(int CountLeft,int CountRight,int LeftLive,int RightLive,int R_or_W);/*分数计算*/ void PCChessActionWhite();/*机器*/ void Exit();/*退出游戏*/ main()/*主函数*/ { int gdriver; int gmode; gdriver = DETECT; initgraph(&gdriver,&gmode,"" ); Welcome();/*欢迎界面*/ outtextxy(230,120,"player1 vs player2"); outtextxy(230,160,"player vs computer"); DrawChessR_W(200,120,BLUE); ModeChoose();/*模式选择,人机或者人人*/ DrawChessBox();/*棋盘*/ ChooseFirst_Or_Second();/*先后*/ DrawChessBox();/*棋盘*/ if(PlayMode == 1) { Player1_vs_Player2();/*人人对战*/ } else { Player_vs_Computer();/*人机对战*/ } } void Player1_vs_Player2(void)/*人人对战*/ { if (RedXianhou == 1)/*红方先手*/ { setcolor(GREEN); outtextxy(220,40,"Red,please put your chess! "); outtextxy(400,400,"press Esc to exit!"); DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED);/*画红方第一颗棋子*/ ChessNowLocal(ChessRedLocalX,ChessRedLocalY);/*棋子光标*/ DrawStandForRed();/*代表红方的圆*/ while(bioskey(1) == 0); key = bioskey(0); if(key != Esc) { ChessActionRed(key); } else { Exit(); } } else/*白方先手*/ { setcolor(GREEN); outtextxy(220,40,"White,please put your chess! "); outtextxy(400,400,"press Esc to exit!"); DrawChessR_W(ChessWhiteLocalX,ChessWhiteLocalY,WHITE);/*画白方第一颗棋子*/ ChessNowLocal(ChessWhiteLocalX,ChessWhiteLocalY);/*棋子光标*/ DrawStandForWhite(); while(bioskey(1) == 0); key = bioskey(0); if(key != Esc) { ChessActionWhite(key); } else { Exit(); } } } void Player_vs_Computer(void)/*人机对战*/ { if (RedXianhou == 1)/*红方先手*/ { setcolor(GREEN); outtextxy(220,40,"White,please put your chess! "); outtextxy(400,400,"press Esc to exit!"); DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED);/*画红方第一颗棋子*/ ChessNowLocal(ChessRedLocalX,ChessRedLocalY);/*棋子光标*/ DrawStandForRed(); while(bioskey(1) == 0); key = bioskey(0); if(key != Esc) { ChessActionRed(key); } else { Exit(); } } else/*白方先手*/ { Chess[9][9] = 2; PrintfChess(); setcolor(GREEN); outtextxy(220,40,"Red,please put your chess! "); outtextxy(400,400,"press Esc to exit!"); DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED);/*画红方第一颗棋子*/ ChessNowLocal(ChessRedLocalX,ChessRedLocalY);/*棋子光标*/ DrawStandForRed(); while(bioskey(1) == 0); key = bioskey(0); if(key != Esc) { ChessActionRed(key); } else { Exit(); } } } void ChessActionRed(int key) { setcolor(GREEN); outtextxy(220,40,"Red,please put your chess。"); do { DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED); ChessNowLocal(ChessRedLocalX,ChessRedLocalY); ChessLocalBoxX = (ChessRedLocalX - 170) / 20; ChessLocalBoxY = (ChessRedLocalY - 100) / 20; flag = 0; switch(key) { case Player1_up : ChessRedLocalY = ChessRedLocalY - 20;break; case Player1_down : ChessRedLocalY = ChessRedLocalY + 20;break; case Player1_left : ChessRedLocalX = ChessRedLocalX - 20;break; case Player1_right: ChessRedLocalX = ChessRedLocalX + 20;break; case Player1_do : { if (Chess[ChessLocalBoxX][ChessLocalBoxY] != 0)/*若此处不为零则说明有棋子存在,系统提示*/ { flag = 1; setcolor(GREEN); outtextxy(180,60,"Red,you can not put your chess here! "); break; } Chess[ChessLocalBoxX][ChessLocalBoxY] = 1;/*没有棋子的情况下,则运行这里的操作*/ JudgeHeng(ChessLocalBoxX,ChessLocalBoxY,1); cleardevice(); DrawChessBox(); PrintfChess(); DrawStandForWhite(); setcolor(GREEN); outtextxy(220,40,"White,please put your chess! "); if (PlayMode == 1) /*若是人人对战则进入这里*/ { DrawChessR_W(ChessWhiteLocalX,ChessWhiteLocalY,WHITE); ChessNowLocal(ChessWhiteLocalX,ChessWhiteLocalY); setcolor(BLACK); line(ChessRedLocalX-5,ChessRedLocalY,ChessRedLocalX+5,ChessRedLocalY); line(ChessRedLocalX,ChessRedLocalY-5,ChessRedLocalX,ChessRedLocalY+5); while(bioskey(1) == 0); key = bioskey(0); if (key != Esc)/*得到当前白方输入的 key 进行调用,若是输入ESC则退出游戏*/ { ChessActionWhite(key); } else { Exit(); } } else /*人机对战*/ { PCChessActionWhite(); } }break; default : break; } if (ChessRedLocalX >= 170 && ChessRedLocalX <= 450 && ChessRedLocalY >= 100 && ChessRedLocalY <= 380 && key != Player1_do)/*推断是否越界了*/ {/*没有越界则运行移动,*/ cleardevice(); DrawChessBox(); PrintfChess(); DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED); ChessNowLocal(ChessRedLocalX,ChessRedLocalY); DrawStandForRed(); setcolor(BLACK); line(ChessWhiteLocalX-5,ChessWhiteLocalY,ChessWhiteLocalX+5,ChessWhiteLocalY); line(ChessWhiteLocalX,ChessWhiteLocalY-5,ChessWhiteLocalX,ChessWhiteLocalY+5); setcolor(GREEN); outtextxy(220,40,"Red,please put your chess! "); outtextxy(400,400,"press Esc to exit!"); } else { /*越界了,则将加上的减掉则还是在原来的位置*/ switch (key) { case Player1_up : ChessRedLocalY = ChessRedLocalY + 20;break; case Player1_down : ChessRedLocalY = ChessRedLocalY - 20;break; case Player1_left : ChessRedLocalX = ChessRedLocalX + 20;break; case Player1_right: ChessRedLocalX = ChessRedLocalX - 20;break; } } while(bioskey(1) == 0); key = bioskey(0); if (key == Esc) { Exit(); } }while(1); } void PCChessActionWhite() { int i; int j; int max; int remberx; int rembery; int PCChessScore[15][15] = {0};/*存分数的二维数组*/ int PlayerChessScore[15][15] = {0};/*存分数的二维数组*/ for(i = 0;i < 15;i++) { for (j = 0;j < 15;j++) { if (Chess[i][j] == 0) { PlayerChessScore[i][j] = ComputeHeng (i,j,1) + ComputeShu(i,j,1) + ComputeZuoXie(i,j,1) + ComputeYouXie(i,j,1); PCChessScore[i][j] = ComputeHeng (i,j,2) + ComputeShu(i,j,2) + ComputeZuoXie(i,j,2) + ComputeYouXie(i,j,2); } } } max = PlayerChessScore[0][0]; remberx = 0; rembery = 0; for (i = 0; i < 15;i++) { for (j = 0;j < 15;j++) { if (max < PlayerChessScore[i][j]) { max = PlayerChessScore[i][j]; remberx = i; rembery = j; } if (max < PCChessScore[i][j]) { max = PCChessScore[i][j]; remberx = i; rembery = j; } } } Chess[remberx][rembery] = 2; cleardevice(); DrawChessBox(); PrintfChess(); JudgeHeng(remberx,rembery,2); ChessWhiteLocalX = remberx * 20 + 170; ChessWhiteLocalY = rembery * 20 + 100; setcolor(BLACK); line(ChessWhiteLocalX-5,ChessWhiteLocalY,ChessWhiteLocalX+5,ChessWhiteLocalY); line(ChessWhiteLocalX,ChessWhiteLocalY-5,ChessWhiteLocalX,ChessWhiteLocalY+5); DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED); ChessNowLocal(ChessRedLocalX,ChessRedLocalY); DrawStandForRed(); while(bioskey(1) == 0); key = bioskey(0); if (key != Esc) { ChessActionRed(key); } else { Exit(); } } int ScoreReturn(int CountLeft,int CountRight,int LeftLive,int RightLive,int R_or_W) { int Score = CountLeft + CountRight; if(R_or_W == 1) { switch(Score) { case 0 : { return 0; }break; case 1 : { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活2*/ { return 50; } else { if(LeftLive == 0 && RightLive == 0)/*下在这个空格能成死2*/ { return 10; } else /*下在这个空格能成半活2*/ { return 35; } } }break; case 2: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活3*/ { return 700; } else { if (LeftLive == 0 && RightLive == 0)/*下在这个空格能成死3*/ { return 100; } else/*下在这个空格能成半活3*/ { return 650; } } }break; case 3: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活4*/ { return 3500; } else { if (LeftLive == 0 && RightLive == 0) /*下在这个空格能成死4*/ { return 800; } else/*下在这个空格能成半活4*/ { return 3000; } } }break; case 4: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活5*/ { return 15000; } else { if (LeftLive == 0 && RightLive == 0)/*下在这个空格能成死5*/ { return 15000; } else/*下在这个空格能成半活5*/ { return 15000; } } }break; } } else { switch(Score) { case 0: { return 0; }break; case 1: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活2*/ { return 60; } else { if (LeftLive == 0 && RightLive == 0)/*下在这个空格能成死2*/ { return 10; } else/*下在这个空格能成半活2*/ { return 40; } } }break; case 2: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活3*/ { return 950; } else { if (LeftLive == 0 && RightLive == 0)/*下在这个空格能成死3*/ { return 100;/*下在这个空格能成死3*/ } else/*下在这个空格能成半活3*/ { return 900; } } }break; case 3: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活4*/ { return 6000; } else { if (LeftLive == 0 && RightLive == 0)/*下在这个空格能成死4*/ { return 3600; } else/*下在这个空格能成半活4*/ { return 5000; } } }break; case 4: { if (LeftLive == 1 && RightLive == 1)/*下在这个空格能成活5*/ { return 20000; } else { if (LeftLive == 0 && RightLive == 0) /*下在这个空格能成死5*/ { return 20000; } else { return 20000; } } }break; } } } int xx; int yy; int LeftLive; int RightLive; int CountLeft; int CountRight; int ComputeHeng(int x,int y,int R_or_W) { LeftLive = 1; RightLive = 1; CountLeft = 0; CountRight = 0; xx = x-1; yy = y; while(Chess[xx][yy] == R_or_W && xx >= 0) { CountLeft++; xx = xx-1; } if (Chess[xx][yy] == 0) { LeftLive = 1; } else { LeftLive = 0; } xx = x+1; yy = y; while(Chess[xx][yy] == R_or_W && xx <=14) { CountRight++; xx = xx+1; } if (Chess[xx][yy] == 0) { RightLive = 1; } else { RightLive = 0; } return ScoreReturn(CountLeft,CountRight,LeftLive,RightLive,R_or_W); } int ComputeShu(int x,int y,int R_or_W) { LeftLive = 1; RightLive = 1; CountLeft = 0; CountRight = 0; xx = x; yy = y - 1; while(Chess[xx][yy] == R_or_W && yy >= 0)/*计算左右的己方棋子个数以及推断是 活/死*/ { CountLeft++; yy--; } if (Chess[xx][yy] == 0) { LeftLive = 1; } else { LeftLive = 0; } yy = y + 1; while(Chess[xx][yy] == R_or_W && yy <= 14) { CountRight++; yy++; } if (Chess[xx][yy] == 0) { RightLive = 1; } else { RightLive = 0; } return ScoreReturn(CountLeft,CountRight,LeftLive,RightLive,R_or_W); } int ComputeZuoXie(int x,int y,int R_or_W) { LeftLive = 1; RightLive = 1; CountLeft = 0; CountRight = 0; xx = x-1; yy = y-1; while(Chess[xx][yy] == R_or_W && xx >= 0 && yy >= 0) { CountLeft++; xx = xx-1; yy = yy-1; } if (Chess[xx][yy] == 0) { LeftLive = 1; } else { LeftLive = 0; } xx = x+1; yy = y+ 1; while(Chess[xx][yy] == R_or_W && xx <= 14 && yy <= 14) { CountRight++; xx = xx+1; yy = yy+ 1; } if (Chess[xx][yy] == 0) { RightLive = 1; } else { RightLive = 0; } return ScoreReturn(CountLeft,CountRight,LeftLive,RightLive,R_or_W); } int ComputeYouXie(int x,int y,int R_or_W) { LeftLive = 1; RightLive = 1; CountLeft = 0; CountRight = 0; xx = x+1; yy = y-1; while(Chess[xx][yy] == R_or_W && xx <= 14 && yy >= 0) { CountLeft++; xx = xx+1; yy = yy-1; } if (Chess[xx][yy] == 0) { LeftLive = 1; } else { LeftLive = 0; } xx = x-1; yy = y+ 1; while(Chess[xx][yy] == R_or_W && xx >= 0 && yy <= 14) { CountRight++; xx = xx-1; yy = yy+ 1; } if (Chess[xx][yy] == 0) { RightLive = 1; } else { RightLive = 0; } return ScoreReturn(CountLeft,CountRight,LeftLive,RightLive,R_or_W); } void ChessActionWhite(int key) { setcolor(BLACK); line(ChessRedLocalX-5,ChessRedLocalY,ChessRedLocalX+5,ChessRedLocalY); line(ChessRedLocalX,ChessRedLocalY-5,ChessRedLocalX,ChessRedLocalY+5); setcolor(GREEN); outtextxy(220,40,"White,please put your chess! "); do { DrawChessR_W(ChessWhiteLocalX,ChessWhiteLocalY,WHITE); ChessNowLocal(ChessWhiteLocalX,ChessWhiteLocalY); ChessLocalBoxX = (ChessWhiteLocalX - 170) / 20; ChessLocalBoxY = (ChessWhiteLocalY - 100) / 20; flag = 0; switch(key) { case Player2_up : ChessWhiteLocalY = ChessWhiteLocalY - 20;break; case Player2_down : ChessWhiteLocalY = ChessWhiteLocalY + 20;break; case Player2_left : ChessWhiteLocalX = ChessWhiteLocalX - 20;break; case Player2_right : ChessWhiteLocalX = ChessWhiteLocalX + 20;break; case Player2_do : { if (Chess[ChessLocalBoxX][ChessLocalBoxY] != 0) { flag = 1; setcolor(GREEN); outtextxy(180,60,"White,you can not put your chess here! "); break; } Chess[ChessLocalBoxX][ChessLocalBoxY] = 2; JudgeHeng(ChessLocalBoxX,ChessLocalBoxY,2); cleardevice(); DrawChessBox(); PrintfChess(); DrawStandForRed(); setcolor(GREEN); outtextxy(220,40,"Red,please put your chess!"); DrawChessR_W(ChessRedLocalX,ChessRedLocalY,RED); ChessNowLocal(ChessRedLocalX,ChessRedLocalY); setcolor(BLACK); line(ChessWhiteLocalX-5,ChessWhiteLocalY,ChessWhiteLocalX+5,ChessWhiteLocalY); line(ChessWhiteLocalX,ChessWhiteLocalY-5,ChessWhiteLocalX,ChessWhiteLocalY+5); while(bioskey(1) == 0); key = bioskey(0); if (key != Esc)/*得到当前白方输入的 key 进行调用,若是输入ESC则退出游戏*/ { ChessActionRed(key); } else { Exit(); } }break; default : break; } if (ChessWhiteLocalX >= 170 && ChessWhiteLocalX <= 450 && ChessWhiteLocalY >= 100 && ChessWhiteLocalY <= 380 && key != Player2_do) { cleardevice(); DrawChessBox(); PrintfChess(); DrawChessR_W(ChessWhiteLocalX,ChessWhiteLocalY,WHITE); ChessNowLocal(ChessWhiteLocalX,ChessWhiteLocalY); DrawStandForWhite(); setcolor(BLACK); line(ChessRedLocalX-5,ChessRedLocalY,ChessRedLocalX+5,ChessRedLocalY); line(ChessRedLocalX,ChessRedLocalY-5,ChessRedLocalX,ChessRedLocalY+5); setcolor(GREEN); outtextxy(220,40,"White,please put your chess! "); outtextxy(400,400,"press Esc to exit!"); } else { switch(key) { case Player2_up : ChessWhiteLocalY = ChessWhiteLocalY + 20;break; case Player2_down : ChessWhiteLocalY = ChessWhiteLocalY - 20;break; case Player2_left : ChessWhiteLocalX = ChessWhiteLocalX + 20;break; case Player2_right : ChessWhiteLocalX = ChessWhiteLocalX - 20;break; } } while(bioskey(1) == 0); key = bioskey(0); if (key == Esc) { Exit(); } }while(1); } void PrintfChess(void) { for (i = 0;i < 15;i++) { for (j = 0;j < 15;j++) { if (Chess[i][j] == 1) { DrawChessR_W(170 + 20 * i,100 + 20 * j,RED); } if (Chess[i][j] == 2) { DrawChessR_W(170 + 20 * i,100 + 20 * j,WHITE); } } } } void JudgeHeng(int x,int y,int R_or_W) { CountNumber = 1; JudgeX = x - 1; JudgeY = y; LineX1 = x; LineX2 = x; LineY1 = y; LineY2 = y; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeX >= 0) { LineX1 = JudgeX; LineY1 = JudgeY; CountNumber++; JudgeX--; } JudgeX = x + 1; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeX <= 14) { LineX2 = JudgeX; LineY2 = JudgeY; CountNumber++; JudgeX++; } if (CountNumber >= 5 && R_or_W == 1) { setcolor(WHITE); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); RedWin(); } if (CountNumber >= 5 && R_or_W == 2) { setcolor(RED); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); WhiteWin(); } JudgeShu(x,y,R_or_W); } void JudgeShu(int x,int y,int R_or_W) { CountNumber = 1; JudgeX = x; JudgeY = y - 1; LineX1 = x; LineX2 = x; LineY1 = y; LineY2 = y; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeY >= 0) { LineX1 = JudgeX; LineY1 = JudgeY; CountNumber++; JudgeY--; } JudgeY = y + 1; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeY <= 14) { LineX2 = JudgeX; LineY2 = JudgeY; CountNumber++; JudgeY++; } if (CountNumber >= 5 && R_or_W == 1) { setcolor(WHITE); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); RedWin(); } if (CountNumber >= 5 && R_or_W == 2) { setcolor(RED); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); WhiteWin(); } JudgeZuoXie(x,y,R_or_W); } void JudgeZuoXie(int x,int y,int R_or_W) { CountNumber = 1; JudgeX = x - 1; JudgeY = y - 1; LineX1 = x; LineX2 = x; LineY1 = y; LineY2 = y; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeY >= 0 && JudgeX >= 0) { LineX1 = JudgeX; LineY1 = JudgeY; CountNumber++; JudgeX--; JudgeY--; } JudgeX = x + 1; JudgeY = y + 1; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeY <= 14 && JudgeX <= 14) { LineX2 = JudgeX; LineY2 = JudgeY; CountNumber++; JudgeX++; JudgeY++; } if (CountNumber >= 5 && R_or_W == 1) { setcolor(WHITE); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); RedWin(); } if (CountNumber >= 5 && R_or_W == 2) { setcolor(RED); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); WhiteWin(); } JudgeYouXie(x,y,R_or_W); } void JudgeYouXie(int x,int y,int R_or_W) { CountNumber = 1; JudgeX = x + 1; JudgeY = y - 1; LineX1 = x; LineX2 = x; LineY1 = y; LineY2 = y; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeY >= 0 && JudgeX >= 0) { LineX1 = JudgeX; LineY1 = JudgeY; CountNumber++; JudgeX++; JudgeY--; } JudgeX = x - 1; JudgeY = y + 1; while (Chess[JudgeX][JudgeY] == R_or_W && JudgeY <= 14 && JudgeX <= 14) { LineX2 = JudgeX; LineY2 = JudgeY; CountNumber++; JudgeX--; JudgeY++; } if (CountNumber >= 5 && R_or_W == 1) { setcolor(WHITE); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); RedWin(); } if (CountNumber >= 5 && R_or_W == 2) { setcolor(RED); line (LineX1 * 20 + 170,LineY1 * 20 + 100,LineX2 * 20 + 170,LineY2 * 20 + 100); WhiteWin(); } } void RedWin(void) { setcolor(RED); outtextxy(470,160,"******************"); outtextxy(470,170,"* *"); outtextxy(470,180,"* *"); outtextxy(470,190," Player:RED "); outtextxy(470,200," Win "); outtextxy(470,210,"* *"); outtextxy(470,220,"* *"); outtextxy(470,230,"******************"); getch(); Exit(); } void WhiteWin(void) { setcolor(WHITE); outtextxy(10,160,"******************"); outtextxy(10,170,"* *"); outtextxy(10,180,"* *"); outtextxy(10,190," Player:White "); outtextxy(10,200," Win "); outtextxy(10,210,"* *"); outtextxy(10,220,"* *"); outtextxy(10,230,"******************"); getch(); Exit(); } void ChessNowLocal(int x,int y)/* 光标显示当前棋子位置 */ { setcolor(BLACK); outtextxy (x-20,y-3,">"); outtextxy (x+15,y-3,"<"); outtextxy (x-3,y-20,"v"); outtextxy (x-3,y+15,"^"); } void Welcome(void)/*欢迎界面*/ { setbkcolor(YELLOW); setcolor(1); outtextxy(230,30,"welcome to Wuziqi Game!"); setcolor(2); outtextxy(250,100,"Mode Choose:"); setcolor(3); rectangle(XiaoQiPanX1,XiaoQiPanY1,XiaoQiPanX2,XiaoQiPanY2);/*Welcome界面中的小棋盘*/ for (i = 1;i < 15;i++) { line(XiaoQiPanX1 + i * 10,XiaoQiPanY1,XiaoQiPanX1 + i * 10,XiaoQiPanY2); line(XiaoQiPanX1,XiaoQiPanY1 + i * 10,XiaoQiPanX2,XiaoQiPanY1 + i * 10); } DrawStandForRed();/*代表红方的圆*/ DrawStandForWhite();/*代表白方的圆*/ setcolor(GREEN); /*红方与白方的操作按键*/ rectangle(50,245,150,375); outtextxy(60,235,"Player:Red "); outtextxy(55,255,"UP----up"); outtextxy(55,265,"DOWN--down"); outtextxy(55,275,"LEFT--left"); outtextxy(55,285,"RIGHT-right"); outtextxy(55,295,"DO----enter"); rectangle(460,245,550,375); outtextxy(470,235,"Plyaer:White"); outtextxy(465,255,"UP----w"); outtextxy(465,265,"DOWN--s"); outtextxy(465,275,"LEFT--a"); outtextxy(465,285,"RIGHT-d"); outtextxy(465,295,"DO----space"); } void ModeChoose(void)/*模式选择*/ { PlayMode = 1; do { while(bioskey(1) == 0); key = bioskey(0); if (key == Player1_up) { cleardevice(); Welcome(); outtextxy(230,120,"player1 vs player2"); outtextxy(230,160,"player vs computer"); DrawChessR_W(200,120,BLUE); PlayMode = 1; /* player1 VS player2*/ } if (key == Player1_down) { cleardevice(); Welcome(); outtextxy(230,120,"player1 vs player2"); outtextxy(230,160,"player vs computer"); DrawChessR_W(200,160,BLUE); PlayMode = 2;/* player VS computer*/ } }while (key != Player1_do); } void DrawChessBox(void)/*棋盘*/ { cleardevice(); setbkcolor(BLUE); setcolor(DARKGRAY); rectangle(DaQiPanX1 - 20,DaQiPanY1 - 20,DaQiPanX2 + 20,DaQiPanY2 + 20); /*棋盘外框*/ setfillstyle(1,CYAN); floodfill(DaQiPanX1 - 10,DaQiPanY1 - 10,DARKGRAY); setcolor(CYAN); rectangle(DaQiPanX1,DaQiPanY1,DaQiPanX2,DaQiPanY2); /*棋盘内框*/ setfillstyle(1,CYAN); floodfill(DaQiPanX1 + 10,DaQiPanY1 + 10,DARKGRAY); setcolor(BLACK); rectangle(DaQiPanX1,DaQiPanY1,DaQiPanX2,DaQiPanY2); /*棋盘中间的线*/ for (i = 1;i < 15;i++) { line(DaQiPanX1 + i * 20,DaQiPanY1,DaQiPanX1 + i * 20,DaQiPanY2); line(DaQiPanX1,DaQiPanY1 + i * 20,DaQiPanX2,DaQiPanY1 + i * 20); } circle(230,160,3);/*棋盘中那5个小圆*/ circle(390,160,3); circle(230,320,3); circle(390,320,3); circle(310,240,3); outtextxy(DaQiPanX1 + 0 ,DaQiPanY1 - 10,"A"); /*棋盘边的字母和数字*/ outtextxy(DaQiPanX1 + 20 ,DaQiPanY1 - 10,"B"); outtextxy(DaQiPanX1 + 40 ,DaQiPanY1 - 10,"C"); outtextxy(DaQiPanX1 + 60 ,DaQiPanY1 - 10,"D"); outtextxy(DaQiPanX1 + 80 ,DaQiPanY1 - 10,"E"); outtextxy(DaQiPanX1 + 100,DaQiPanY1 - 10,"F"); outtextxy(DaQiPanX1 + 120,DaQiPanY1 - 10,"G"); outtextxy(DaQiPanX1 + 140,DaQiPanY1 - 10,"H"); outtextxy(DaQiPanX1 + 160,DaQiPanY1 - 10,"I"); outtextxy(DaQiPanX1 + 180,DaQiPanY1 - 10,"J"); outtextxy(DaQiPanX1 + 200,DaQiPanY1 - 10,"K"); outtextxy(DaQiPanX1 + 220,DaQiPanY1 - 10,"L"); outtextxy(DaQiPanX1 + 240,DaQiPanY1 - 10,"M"); outtextxy(DaQiPanX1 + 260,DaQiPanY1 - 10,"N"); outtextxy(DaQiPanX1 + 280,DaQiPanY1 - 10,"O"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 - 0 ,"1"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 20,"2"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 40,"3"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 60,"4"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 80,"5"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 100,"6"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 120,"7"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 140,"8"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 160,"9"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 180,"10"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 200,"11"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 220,"12"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 240,"13"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 260,"14"); outtextxy(DaQiPanX1 - 15 ,DaQiPanY1 + 280,"15"); setcolor(GREEN); rectangle(80,20,540,70); outtextxy(260,10,"MessageBox"); } void ChooseFirst_Or_Second(void)/*先手后手的选择*/ { RedXianhou = 1; setcolor(GREEN); if (PlayMode == 1) { outtextxy(230,30,"player1 use Red chess!"); outtextxy(230,40,"player2 use White chess!"); } else { outtextxy(230,30,"player use Red chess!"); outtextxy(230,40,"computer use White chess!"); } outtextxy(100,50,"Red first? (letf/right) to change,(enter)to make sure"); DrawStandForRed(); do { while(bioskey(1) == 0); key = bioskey(0); if (key == Player1_left) { cleardevice(); DrawChessBox(); outtextxy(100,50,"Red first?

    (letf/right) to change,(enter)to make sure"); DrawStandForRed(); RedXianhou = 1; } if (key == Player1_right) { cleardevice(); DrawChessBox(); outtextxy(100,50,"White first?

    (letf/right) to change,(enter)to make sure"); DrawStandForWhite(); RedXianhou = 2; } setcolor(GREEN); if (PlayMode == 1) { outtextxy(230,30,"player1 use Red chess!"); outtextxy(230,40,"player2 use White chess!"); } else { outtextxy(230,30,"player use Red chess!"); outtextxy(230,40,"computer use White chess!"); } }while(Player1_do != key); cleardevice(); } void DrawChessR_W(int x,int y,int color)/*画棋盘上的红子和白子*/ { setcolor(color); circle(x,y,10); setfillstyle(1,color); floodfill(x+5,y+5,color); } void DrawStandForRed()/*代表红方的圆*/ { setcolor(7); circle(XiaoYuanX1,XiaoYuanY1,YuanR); setfillstyle(1,RED); floodfill(XiaoYuanX1+10,XiaoYuanY1+10,7); } void DrawStandForWhite()/*代表白方的圆*/ { setcolor(15); circle(XiaoYuanX2,XiaoYuanY2,YuanR); setfillstyle(1,WHITE); floodfill(XiaoYuanX2+10,XiaoYuanY2+10,15); } void Exit()/*退出游戏*/ { cleardevice(); setbkcolor(BLACK); setcolor(GREEN); outtextxy(280,350,"goodbye!"); getch(); closegraph(); }

  • 相关阅读:
    阿里巴巴的26款超神Java开源项目
    10个爬虫工程师必备的工具
    微服务的发现与注册--Eureka
    国内最火5款Java微服务开源项目
    LeetCode 700. 二叉搜索树中的搜索
    LeetCode 104. 二叉树的最大深度
    LeetCode 908. 最小差值 I
    LeetCode 728. 自除数
    LeetCode 704. 二分查找
    LeetCode 852. 山脉数组的峰顶索引 (二分)
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7297548.html
Copyright © 2011-2022 走看看