2014-04-03-c-五子棋实例

我想念泉州的夏天了

一个c语言五子棋实例。

图

图

/* 头文件  */
#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();
}