五子棋——C++

最近在学C++,这个是照葫芦画瓢的五子棋C++版- -

依赖SDL_PingGe_1.3,很多实用的函数,类我都封装成DLL了调用起来真是舒服啊。。

不过一方面要对DLL做测试,一方面要开发,一个人还真是累啊。

按钮我也取消了,毕竟只是花架子,比C版多了一个开场动画,鼠标也被修改成了一只小手,

还有分出胜负后五子将会被一条红线连接(这方面的数据交互没做好)

AI部分做了些修改,细化了计分表,总体水平强了不少,但是同学说缺少攻击性,哈哈没办法啦,

暂时不去优化了。

其实还有一个小BUG。。但是我很懒,也没那么多时间去仔细检查AI部分了,留给大家吧

BUG:当中间部位还差一子便可连成五子时,AI本应是最优先落中间位置,但是却落在别的地方- -

好了不多说了:

 

#ifndef _LINK_FIVE_H_
#define _LINK_FIVE_H_

#include "SDL_PingGe.h"
#include <iostream>
#include <fstream>

class Chess_Board
{
    public:
        //Initializes variables.
        Chess_Board(void);
        ~Chess_Board(void);

        //Player down the chess_piece.
        //Return true if down success, false if here can't down.
        bool Player_1_Down(Sint32 x, Sint32 y);
        bool Player_2_Down(Sint32 x, Sint32 y);

        //AI.
        //Change the X_Y struct.
        void AI(void);
        Sint32 Return_Score(Sint32 n);

        //If player down the chess_piece, judge is he win.
        //Return true if win, false is not win.
        bool Judge_Win(void);

    private:
        //The state of the chess board.
        Sint32 NO_CHESS;
        Sint32 WHITE_CHESS;
        Sint32 BLACK_CHESS;
        Sint32 WALL;

        //The size of the board.
        Sint32 SIZE;
        Sint32 PIXEL_PER_CELL;

    public:
        //The board array.
        Sint32 board[15][15];

        //Two kind coordinates.
        struct X_Y
        {
            Sint32 array_x, array_y;
            Sint32 image_x, image_y;
        }A, S, E;
        /*
        //For array.
        Sint32 array_x, array_y;

        //For image.
        Sint32 image_x, image_y;
        */
        Sint32 Play_Flag;

    public:
        //Change the array_xy to image_xy.
        inline void Change_X_Y_Image(struct X_Y& O);

        //Change the image_xy to array_xy.
        //inline void Change_X_Y_Array(void);

        //Judge the image_xy is lawful.
        //Return true if it is lawful.
        bool X_Y_Image_Lawful(struct X_Y& O);

        //Judge the array_xy is lawful.
        //Return true if it is lawful.
        bool X_Y_Array_Lawful(struct X_Y& O);
};
inline void Chess_Board::Change_X_Y_Image(struct X_Y& O)
{
    O.image_y = O.array_x * PIXEL_PER_CELL + 90;
    O.image_x = O.array_y * PIXEL_PER_CELL + 90;
}



class Render
{
    public:
        //Init everything.
        Render(void);
        //Off everything.
        ~Render(void);

        //Load image/font/audio.
        void Load(void);
        //Check everything load by Load().
        void Check(void);
        //Free everything load by Load().
        void Free(void);

        //Show Logo
        void Show_Logo(void) const;

        //Show Cursor
        void Show_Cursor(Sint32 x, Sint32 y) const;

        //Show Timer
        void Show_Timer(const Chess_Board & chess);

        //Show Board.
        void Show_Board(void) const;

        //Show Piece.
        void Show_Black(Sint32 x, Sint32 y) const;
        void Show_White(Sint32 x, Sint32 y) const;

        //Remind player.
        void Remind(void) const;

        //The screen.
        SDL_Surface *screen;

    private:
        //Use the to cut the Big.
        SDL_Rect clips[7];


        PingGe::Cursor * cursor;

        PingGe::Timer time_1, time_2;
        SDL_Color textColor;

        //The surface.
        SDL_Surface *Big,
                    *Remind_Player_1,
                    *Remind_Player_2,
                    *Chess_Piece_Black,
                    *Chess_Piece_White,
                    *Background,
                    *Logo,
                    *Cursor,
                    *Seconds;
        Mix_Chunk   *Low;
        TTF_Font    *Font;

    private:
        //Stream of log.
        std::ofstream Logger;
        inline void Log(std::string message_1, std::string message_2 = "")
        {
            //Write message to file.
            Logger << message_1 << message_2 << std::endl;

            //Flush the buffer.
            Logger.flush();
        }

    private:
        Sint32 SCREEN_WIDTH     = 800;
        Sint32 SCREEN_HEIGHT    = 600;
        Sint32 SCREEN_BPP       = 32;
};

#endif
View Code
#include "Link_Five.h"

const int FRAMES_PER_SECOND = 100;

//PvP
void Game_1(void);
//PvAI
void Game_2(void);

Render ren;

int main(int argc, char* args[])
{

    //SDL_ShowCursor(0);
    SDL_WarpMouse(400, 300);
    ren.Load();
    ren.Check();

    ren.Show_Logo();

    Game_2();

    ren.Free();
    return 0;
}

void Game_1(void)
{
    SDL_Event event;
    PingGe::Timer Fps;
    Chess_Board chess;
    ren.Show_Board();
    Sint32 done = 0;
    while(done == 0)
    {
        Fps.Start();
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                done = 1;
            }

            if(event.type == SDL_KEYDOWN)
            {
                if(event.key.keysym.sym == SDLK_ESCAPE)
                {
                    done = 1;
                }
            }
            if(event.type == SDL_MOUSEBUTTONDOWN)
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                {
                    if(chess.Play_Flag)
                    {
                        if(chess.Player_1_Down(event.motion.x, event.motion.y) == true)
                        {
                            ren.Show_Black(chess.A.image_x, chess.A.image_y);
                            {
                                int x, y;
                                SDL_GetMouseState(&x, &y);
                                ren.Show_Cursor(x, y);
                            }
                            ren.Remind();
                            if(chess.Judge_Win())
                            {
                                std::cout << "Black win!" << std::endl;
                            }
                        }
                    }
                    else
                    {
                        if(chess.Player_2_Down(event.motion.x, event.motion.y) == true)
                        {
                            ren.Show_White(chess.A.image_x, chess.A.image_y);
                            {
                                int x, y;
                                SDL_GetMouseState(&x, &y);
                                ren.Show_Cursor(x, y);
                            }
                            ren.Remind();
                            if(chess.Judge_Win())
                            {
                                std::cout << "White win!" << std::endl;
                            }
                        }
                    }
                }
            }
            int x, y;
            SDL_GetMouseState(&x, &y);
            ren.Show_Cursor(x, y);
        }
        ren.Show_Timer(chess);

        if((Fps.Get_Ticks() < 1000 / FRAMES_PER_SECOND ) )
        {
            //Sleep the remaining frame time
            SDL_Delay((1000 / FRAMES_PER_SECOND ) - Fps.Get_Ticks());
        }
    }
}
void Game_2(void)
{
    SDL_Event event;
    PingGe::Timer Fps;
    Chess_Board chess;
    ren.Show_Board();

    chess.Player_2_Down(300, 300);
    ren.Show_White(300, 300);
    chess.Play_Flag = ~chess.Play_Flag;

    Sint32 done = 0;
    while(done == 0)
    {
        Fps.Start();
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                done = 1;
            }

            if(event.type == SDL_KEYDOWN)
            {
                if(event.key.keysym.sym == SDLK_ESCAPE)
                {
                    done = 1;
                }
            }

            if(event.type == SDL_MOUSEBUTTONDOWN)
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                {

                    if(chess.Player_1_Down(event.motion.x, event.motion.y) == true)
                    {
                        ren.Show_Black(chess.A.image_x, chess.A.image_y);
                        if(chess.Judge_Win())
                        {
                            chess.Change_X_Y_Image(chess.S);
                            chess.Change_X_Y_Image(chess.E);
                            PingGe::Draw_Line(ren.screen, chess.S.image_x, chess.S.image_y, chess.E.image_x, chess.E.image_y, SDL_MapRGB(ren.screen->format, 0xFF, 0x00, 0x00));
                            SDL_Flip(ren.screen);
                            std::cout << "Black win!" << std::endl;
                        }
                        chess.AI();
                        chess.Player_2_Down(chess.A.image_x, chess.A.image_y);
                        ren.Show_White(chess.A.image_x, chess.A.image_y);
                        if(chess.Judge_Win())
                        {
                            chess.Change_X_Y_Image(chess.S);
                            chess.Change_X_Y_Image(chess.E);
                            PingGe::Draw_Line(ren.screen, chess.S.image_x, chess.S.image_y, chess.E.image_x, chess.E.image_y, SDL_MapRGB(ren.screen->format, 0xFF, 0x00, 0x00));
                            SDL_Flip(ren.screen);
                            std::cout << "White win!" << std::endl;
                        }
                    }
                }
            }
            int x, y;
            SDL_GetMouseState(&x, &y);
            ren.Show_Cursor(x, y);
        }

        if((Fps.Get_Ticks() < 1000 / FRAMES_PER_SECOND ) )
        {
            //Sleep the remaining frame time
            SDL_Delay((1000 / FRAMES_PER_SECOND ) - Fps.Get_Ticks());
        }
    }
}
View Code
#include "Link_Five.h"

Chess_Board::Chess_Board(void)
{
    NO_CHESS        = 0x0000;
    WHITE_CHESS     = 0x000F;
    BLACK_CHESS     = 0x00F0;
    WALL            = 0x0F00;

    SIZE            = 0x000F;
    Play_Flag       = ~0x0000;
    PIXEL_PER_CELL  = 0x001E;
    memset(board, NO_CHESS, sizeof(board));
}

Chess_Board::~Chess_Board(void)
{
    memset(board, NO_CHESS, sizeof(board));
}

bool Chess_Board::Player_1_Down(Sint32 x, Sint32 y)
{
    A.image_x = x;
    A.image_y = y;
    if(X_Y_Image_Lawful(A) == true)
    {
        //Change_X_Y_Array();
        if(X_Y_Array_Lawful(A) == true)
        {
            Change_X_Y_Image(A);
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }

    //IF everything is OK
    board[A.array_x][A.array_y] = BLACK_CHESS;

    Play_Flag = ~Play_Flag;
    return true;
}

bool Chess_Board::Player_2_Down(Sint32 x, Sint32 y)
{
    A.image_x = x;
    A.image_y = y;
    if(X_Y_Image_Lawful(A) == true)
    {
        //Change_X_Y_Array();
        if(X_Y_Array_Lawful(A) == true)
        {
            Change_X_Y_Image(A);
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }

    //IF everything is OK
    board[A.array_x][A.array_y] = WHITE_CHESS;

    Play_Flag = ~Play_Flag;
    return true;
}

bool Chess_Board::Judge_Win(void)
{
    Sint32 Chess_Color;
    if(~Play_Flag)
    {
        Chess_Color = BLACK_CHESS;
    }
    else
    {
        Chess_Color = WHITE_CHESS;
    }

    Sint32 n1, n2;

    n1 = n2 = 0;
    //left
    for(Sint32 i = A.array_x, j = A.array_y; j >= 0; j--)
    {
        if(board[i][j] == Chess_Color)      n1++;
        else
        {
            S.array_x = i;
            S.array_y = j + 1;
            break;
        }
    }
    //right
    for(Sint32 i = A.array_x, j = A.array_y; j < 15; j++)
    {
        if(board[i][j] == Chess_Color)      n2++;
        else
        {
            E.array_x = i;
            E.array_y = j - 1;
            break;
        }
    }
    if(n1 + n2 - 1 >= 5)
    {
        return true;
    }


    n1 = n2 = 0;
    //up
    for(Sint32 i = A.array_x, j = A.array_y; i >= 0; i--)
    {
        if(board[i][j] == Chess_Color)      n1++;
        else
        {
            S.array_x = i + 1;
            S.array_y = j;
            break ;
        }
    }
    //down
    for(Sint32 i = A.array_x, j = A.array_y; i < 15; i++)
    {
        if(board[i][j] == Chess_Color)      n2++;
        else
        {
            E.array_x = i - 1;
            E.array_y = j;
            break ;
        }
    }
    if(n1 + n2 - 1>=5)
    {
        return true;
    }


    n1 = n2 = 0;
    //left_up
    for(Sint32 i = A.array_x, j = A.array_y; ((i >= 0) && (j >= 0)); i--, j--)
    {
        if(board[i][j] == Chess_Color)      n1++;
        else
        {
            S.array_x = i + 1;
            S.array_y = j + 1;
            break;
        }
    }
    //right_down
    for(Sint32 i = A.array_x, j = A.array_y; ((i < 15) && (j < 15)); i++, j++)
    {
        if(board[i][j] == Chess_Color)      n2++;
        else
        {
            E.array_x = i - 1;
            E.array_y = j - 1;
            break;
        }
    }
    if(n1 + n2 - 1 >= 5)
    {
        return true;
    }


    n1 = n2 = 0;
    //right_up
    for(Sint32 i = A.array_x, j = A.array_y; ((i >= 0) && (j < 15)); i--, j++)
    {
        if(board[i][j] == Chess_Color)      n1++;
        else
        {
            S.array_x = i + 1;
            S.array_y = j - 1;
            break;
        }
    }
    //left_down
    for(Sint32 i = A.array_x, j = A.array_y; ((i < 15) && (j >= 0)); i++, j--)
    {
        if(board[i][j] == Chess_Color)      n2++;
        else
        {
            E.array_x = i - 1;
            E.array_y = j + 1;
            break;
        }
    }
    if(n1 + n2 - 1 >= 5)
    {
        return true;
    }
    return false;
}

bool Chess_Board::X_Y_Image_Lawful(struct X_Y& O)
{
    for(Sint32 i = 0; i < 15; i++)
    {
        if((O.image_x > 90 + i * PIXEL_PER_CELL - 8) && (O.image_x < 90 + i * PIXEL_PER_CELL + 8))
        {
            for(Sint32 j = 0; j < 15; j++)
            {
                if((O.image_y > 90 + j * PIXEL_PER_CELL - 8) && (O.image_y < 90 + j * PIXEL_PER_CELL + 8))
                {
                    O.array_y = i;
                    O.array_x = j;
                    return true;
                }
            }
        }
    }
    return false;
}

bool Chess_Board::X_Y_Array_Lawful(struct X_Y& O)
{
    if(board[O.array_x][O.array_y] == NO_CHESS)
    {
        return true;
    }
    return false;
}
View Code
#include "Link_Five.h"

Render::Render(void)
{
    //Open the log file
    Logger.open("LOG.txt");

    //Init SDL
    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
    {
        Log("Init SDL...", SDL_GetError());
    }
    else
    {
        Log("Init SDL...", "OK");

        if((screen  = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
        {
            Log("Set VideoMode...", SDL_GetError());
        }
        else
        {
            Log("Set VideoMode...", "OK");
            SDL_WM_SetCaption("Link Five","Link Five");
        }
    }


    //Init IMG
    if((IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF) & (IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF)) != (IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF))
    {
        Log("Init IMG...", IMG_GetError());
    }
    else
    {
        Log("Init IMG...", "OK");
    }

    //Init TTF
    if(TTF_Init() == -1)
    {
        Log("Init TTF...", TTF_GetError());
    }
    else
    {
        Log("Init TTF...", "OK");
    }

    //Init Mix
    if((Mix_Init(MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG) & (MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG)) != (MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG))
    {
        Log("Init IMG...", Mix_GetError());
    }
    else
    {
        Log("Init IMG...", "OK");

        if(Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
        {
            Log("Open Audio...", Mix_GetError());
        }
        else
        {
            Log("Open Audio...", "OK");
        }
    }
}

Render::~Render(void)
{
    //Close the audio
    Mix_CloseAudio();

    //Turn off the mix engine
    Mix_Quit();

    //Turn off the ttf engine
    TTF_Quit();

    //Turn off the img engine
    IMG_Quit();

    //Turn off the sdl engine
    SDL_Quit();

    //Close the log file
    Logger.close();
}

void Render::Load(void)
{
    Big                 = NULL;
    Remind_Player_1     = NULL;
    Remind_Player_2     = NULL;
    Chess_Piece_Black   = NULL;
    Chess_Piece_White   = NULL;
    Background          = NULL;
    Logo                = NULL;
    Cursor              = NULL;
    Seconds             = NULL;
    Low                 = NULL;
    Font                = NULL;


    Big     = PingGe::Load_Image("PIC/Big.png");

    Font    = TTF_OpenFont("arial.ttf", 25);

    Low     = Mix_LoadWAV("low.wav");

    //Background        //Remind_Player_1   //Remind_Player_2   //Chess_Piece_Black //Chess_Piece_White //Logo              //Cursor
    clips[0].x = 0;     clips[1].x = 800;   clips[2].x = 825;   clips[3].x = 850;   clips[4].x = 880;   clips[5].x = 800;   clips[6].x = 825;
    clips[0].y = 0;     clips[1].y = 0;     clips[2].y = 0;     clips[3].y = 0;     clips[4].y = 0;     clips[5].y = 40;    clips[6].y = 350;
    clips[0].w = 800;   clips[1].w = 25;    clips[2].w = 25;    clips[3].w = 30;    clips[4].w = 30;    clips[5].w = 457;   clips[6].w = 35;
    clips[0].h = 600;   clips[1].h = 40;    clips[2].h = 40;    clips[3].h = 30;    clips[4].h = 30;    clips[5].h = 266;   clips[6].h = 35;

    //Creat surface
    Background          = SDL_CreateRGBSurface(screen->flags, clips[0].w, clips[0].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
    Remind_Player_1     = SDL_CreateRGBSurface(screen->flags, clips[1].w, clips[1].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
    Remind_Player_2     = SDL_CreateRGBSurface(screen->flags, clips[2].w, clips[2].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
    Chess_Piece_Black   = SDL_CreateRGBSurface(screen->flags, clips[3].w, clips[3].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
    Chess_Piece_White   = SDL_CreateRGBSurface(screen->flags, clips[4].w, clips[4].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
    Logo                = SDL_CreateRGBSurface(screen->flags, clips[5].w, clips[5].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
    Cursor              = SDL_CreateRGBSurface(screen->flags, clips[6].w, clips[6].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);

    //Work with the colorkey
    SDL_FillRect(Logo,              NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
    SDL_FillRect(Cursor,            NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
    SDL_FillRect(Chess_Piece_Black, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
    SDL_FillRect(Chess_Piece_White, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0x00));

    //Set color key
    PingGe::Color_Key(Logo,              0xFF, 0xFF, 0xFF);
    PingGe::Color_Key(Cursor,            0x00, 0x00, 0x00);
    PingGe::Color_Key(Chess_Piece_Black, 0xFF, 0xFF, 0xFF);
    PingGe::Color_Key(Chess_Piece_White, 0xFF, 0xFF, 0x00);

    //Cut Big
    PingGe::Apply_Surface(Background,       0, 0, Big, &clips[0]);
    PingGe::Apply_Surface(Remind_Player_1,  0, 0, Big, &clips[1]);
    PingGe::Apply_Surface(Remind_Player_2,  0, 0, Big, &clips[2]);
    PingGe::Apply_Surface(Chess_Piece_Black,0, 0, Big, &clips[3]);
    PingGe::Apply_Surface(Chess_Piece_White,0, 0, Big, &clips[4]);
    PingGe::Apply_Surface(Logo,             0, 0, Big, &clips[5]);
    PingGe::Apply_Surface(Cursor,           0, 0, Big, &clips[6]);

    //Load the cursor
    cursor = new PingGe::Cursor(screen, Cursor, 12, 0);

    //Init the timer
    time_1.Start();time_1.Pause();
    time_2.Start();time_2.Pause();
    std::stringstream time;
    textColor = {0x00, 0x00, 0x00};
    time << (int)time_1.Get_Ticks() / 1000;
    Seconds = TTF_RenderText_Solid(Font, time.str().c_str(), textColor);
}

void Render::Check(void)
{
    if(Big == NULL)
    {
        Log("Error : ", "1");
    }
    if(Remind_Player_1 == NULL)
    {
        Log("Error : ", "2");
    }
    if(Remind_Player_2 == NULL)
    {
        Log("Error : ", "3");
    }
    if(Chess_Piece_Black == NULL)
    {
        Log("Error : ", "4");
    }
    if(Chess_Piece_White == NULL)
    {
        Log("Error : ", "5");
    }
    if(Background == NULL)
    {
        Log("Error : ", "6");
    }
    if(Logo == NULL)
    {
        Log("Error : ", "7");
    }
    if(Cursor == NULL)
    {
        Log("Error : ", "8");
    }
    if(Seconds == NULL)
    {
        Log("Error : ", "9");
    }
    if(Low == NULL)
    {
        Log("Error : ", "10");
    }
    if(Font == NULL)
    {
        Log("Error : ", "11");
    }
}

void Render::Free(void)
{
    //Free the surface
    SDL_FreeSurface(Big);
    SDL_FreeSurface(Remind_Player_1);
    SDL_FreeSurface(Remind_Player_2);
    SDL_FreeSurface(Chess_Piece_Black);
    SDL_FreeSurface(Chess_Piece_White);
    SDL_FreeSurface(Background);
    SDL_FreeSurface(Logo);
    SDL_FreeSurface(Cursor);
    SDL_FreeSurface(Seconds);

    //Free the font
    TTF_CloseFont(Font);

    //Free the chunk
    Mix_FreeChunk(Low);
}

void Render::Show_Logo(void) const
{
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
    SDL_Flip(screen);
    PingGe::Logo logo(Logo, screen, 400, 300);
    logo.Act3(350);
    //logo.Act2(440);
    SDL_Delay(500);
}

void Render::Show_Cursor(Sint32 x, Sint32 y) const
{
    cursor->Show_Cursor(x, y);
}

void Render::Show_Timer(const Chess_Board & chess)
{
    if(chess.Play_Flag)
    {
        time_1.Unpause();
        time_2.Pause();

        std::stringstream time;
        time << (int)time_1.Get_Ticks() / 1000;
        Seconds = TTF_RenderText_Solid(Font, time.str().c_str(), textColor);

        PingGe::Draw_FillRect(screen, 650, 52, 800, 87, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
        PingGe::Apply_Surface(screen, 650, 52, Seconds);
        SDL_UpdateRect(screen, 650, 52, 150, 35);
    }
    else
    {
        time_2.Unpause();
        time_1.Pause();

        std::stringstream time;
        time << (int)time_2.Get_Ticks() / 1000;
        Seconds = TTF_RenderText_Solid(Font, time.str().c_str(), textColor);

        PingGe::Draw_FillRect(screen, 650, 298, 800, 332, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
        PingGe::Apply_Surface(screen, 650, 298, Seconds);
        SDL_UpdateRect(screen, 650, 298, 150, 35);
    }
}

void Render::Show_Board(void) const
{
    PingGe::Apply_Surface(screen, 0, 0, Background);
    SDL_Flip(screen);
}

void Render::Show_Black(Sint32 x, Sint32 y) const
{
    x -= 15;
    y -= 15;
    PingGe::Apply_Surface(screen, x, y, Chess_Piece_Black);
    SDL_UpdateRect(screen, x, y, 30, 30);

    Mix_PlayChannel(-1,Low,0);
}

void Render::Show_White(Sint32 x, Sint32 y) const
{
    x -= 15;
    y -= 15;
    PingGe::Apply_Surface(screen, x, y, Chess_Piece_White);
    SDL_UpdateRect(screen, x, y, 30, 30);

    Mix_PlayChannel(-1,Low,0);
}

void Render::Remind(void) const
{
    static Sint32 flag = 0;
    flag++;

    if(flag % 2 == 1)
    {
        for(Sint32 i = -40; i <= 13; i++)
        {
            PingGe::Apply_Surface(screen, 285, i, Remind_Player_1);
            SDL_UpdateRect(screen, 285, 15, 25, 100);
            SDL_Delay(15);
        }
    }
    else
    {
        for(Sint32 i = -40; i <= 13; i++)
        {
            PingGe::Apply_Surface(screen, 285, i, Remind_Player_2);
            SDL_UpdateRect(screen, 285, 15, 25, 100);
            SDL_Delay(15);
        }
    }
}
View Code
#include "Link_Five.h"

Sint32 Chess_Board::Return_Score(Sint32 n)
{
    switch(n)
    {
        case 1: return 1;
        case 2: return 4;
        case 3: return 7;
        case 4: return 10;
        case 5: return 13;
        default:return 0;
    }
}

void Chess_Board::AI(void)
{
    Sint32 AI[15][15][5] = {0},     PE[15][15][5] = {0},
           AI_n,                    PE_n,
           Break_Point_Left_AI,     Break_Point_Right_AI,
           Break_Point_Left_PE,     Break_Point_Right_PE,
           Break_Point_Up_AI,       Break_Point_Down_AI,
           Break_Point_Up_PE,       Break_Point_Down_PE,
           AI_MAX = 0,              AI_x, AI_y,
           PE_MAX = 0,              PE_x, PE_y;
    bool   Flag_AI,                 Flag_PE;

    for(Sint32 i = 0; i < SIZE; i++)
    {
        for(Sint32 j = 0; j < SIZE; j++)
        {
            if(board[i][j] == NO_CHESS)
            {
                //1
                AI_n = PE_n = 1;
                Break_Point_Left_AI = Break_Point_Right_AI = Break_Point_Left_PE = Break_Point_Right_PE = WALL;
                //left
                Flag_AI = Flag_PE = true;
                for(Sint32 p = j - 1; p >= 0; p--)
                {
                    //AI
                    if((Flag_AI == true) && (board[i][p] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Left_AI = (board[i][p] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[i][p] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Left_PE = (board[i][p] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }
                //right
                Flag_AI = Flag_PE = true;
                for(Sint32 q = j + 1; q < SIZE; q++)
                {
                    //AI
                    if((Flag_AI == true) && (board[i][q] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Right_AI = (board[i][q] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[i][q] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Right_PE = (board[i][q] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }

                //AI
                if(Break_Point_Left_AI != NO_CHESS && Break_Point_Right_AI != NO_CHESS)         AI[i][j][1] = Return_Score(AI_n);
                else if(Break_Point_Left_AI != NO_CHESS || Break_Point_Right_AI != NO_CHESS)    AI[i][j][1] = Return_Score(AI_n) + 1;
                else                                                                            AI[i][j][1] = Return_Score(AI_n) + 2;
                //PE
                if(Break_Point_Left_PE != NO_CHESS && Break_Point_Right_PE != NO_CHESS)         PE[i][j][1] = Return_Score(PE_n);
                else if(Break_Point_Left_PE != NO_CHESS || Break_Point_Right_PE != NO_CHESS)    PE[i][j][1] = Return_Score(PE_n) + 1;
                else                                                                            PE[i][j][1] = Return_Score(PE_n) + 2;


                //2
                AI_n = PE_n = 1;
                Break_Point_Up_AI = Break_Point_Down_AI = Break_Point_Up_PE = Break_Point_Down_PE = WALL;
                //up
                Flag_AI = Flag_PE = true;
                for(Sint32 p = i - 1; p >= 0; p--)
                {
                    //AI
                    if((Flag_AI == true) && (board[p][j] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Up_AI = (board[p][j] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[p][j] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Up_PE = (board[p][j] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }
                //down
                Flag_AI = Flag_PE = true;
                for(Sint32 q = i + 1; q < SIZE; q++)
                {
                    //AI
                    if((Flag_AI == true) && (board[q][j] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Down_AI = (board[q][j] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[q][j] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Down_PE = (board[q][j] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }

                //AI
                if(Break_Point_Up_AI != NO_CHESS && Break_Point_Down_AI != NO_CHESS)            AI[i][j][2] = Return_Score(AI_n);
                else if(Break_Point_Up_AI != NO_CHESS || Break_Point_Down_AI != NO_CHESS)       AI[i][j][2] = Return_Score(AI_n) + 1;
                else                                                                            AI[i][j][2] = Return_Score(AI_n) + 2;
                //PE
                if(Break_Point_Up_PE != NO_CHESS && Break_Point_Down_PE != NO_CHESS)            PE[i][j][2] = Return_Score(PE_n);
                else if(Break_Point_Up_PE != NO_CHESS || Break_Point_Down_PE != NO_CHESS)       PE[i][j][2] = Return_Score(PE_n) + 1;
                else                                                                            PE[i][j][2] = Return_Score(PE_n) + 2;


                //3
                AI_n = PE_n = 1;
                Break_Point_Left_AI = Break_Point_Right_AI = Break_Point_Left_PE = Break_Point_Right_PE = WALL;
                //left
                Flag_AI = Flag_PE = true;
                for(Sint32 p = i + 1, q = j - 1; p < SIZE && q >= 0 ; p++, q--)
                {
                    //AI
                    if((Flag_AI == true) && (board[p][q] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Left_AI = (board[p][q] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[p][q] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Left_PE = (board[p][q] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }
                //right
                Flag_AI = Flag_PE = true;
                for(Sint32 p = i - 1, q = j + 1; p >= 0 && q < SIZE; p--, q++)
                {
                    //AI
                    if((Flag_AI == true) && (board[p][q] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Right_AI = (board[p][q] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[p][q] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Right_PE = (board[p][q] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }

                //AI
                if(Break_Point_Left_AI != NO_CHESS && Break_Point_Right_AI != NO_CHESS)         AI[i][j][3] = Return_Score(AI_n);
                else if(Break_Point_Left_AI != NO_CHESS || Break_Point_Right_AI != NO_CHESS)    AI[i][j][3] = Return_Score(AI_n) + 1;
                else                                                                            AI[i][j][3] = Return_Score(AI_n) + 2;
                //PE
                if(Break_Point_Left_PE != NO_CHESS && Break_Point_Right_PE != NO_CHESS)         PE[i][j][3] = Return_Score(PE_n);
                else if(Break_Point_Left_PE != NO_CHESS || Break_Point_Right_PE != NO_CHESS)    PE[i][j][3] = Return_Score(PE_n) + 1;
                else                                                                            PE[i][j][3] = Return_Score(PE_n) + 2;


                //4
                AI_n = PE_n = 1;
                Break_Point_Left_AI = Break_Point_Right_AI = Break_Point_Left_PE = Break_Point_Right_PE = WALL;
                //left
                Flag_AI = Flag_PE = true;
                for(Sint32 p = i - 1, q = j - 1; p >= 0 && q >= 0 ; p--, q--)
                {
                    //AI
                    if((Flag_AI == true) && (board[p][q] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Left_AI = (board[p][q] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[p][q] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Left_PE = (board[p][q] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }
                //right
                Flag_AI = Flag_PE = true;
                for(Sint32 p = i + 1, q = j + 1; p < SIZE && q < SIZE; p++, q++)
                {
                    //AI
                    if((Flag_AI == true) && (board[p][q] == BLACK_CHESS))   AI_n++;
                    else if(Flag_AI == true)
                    {
                        Flag_AI = false;
                        Break_Point_Right_AI = (board[p][q] == WHITE_CHESS ? WHITE_CHESS : NO_CHESS);
                    }
                    //PE
                    if((Flag_PE == true) && (board[p][q] == WHITE_CHESS))   PE_n++;
                    else if(Flag_PE == true)
                    {
                        Flag_PE = false;
                        Break_Point_Right_PE = (board[p][q] == BLACK_CHESS ? BLACK_CHESS : NO_CHESS);
                    }
                }

                //AI
                if(Break_Point_Left_AI != NO_CHESS && Break_Point_Right_AI != NO_CHESS)         AI[i][j][4] = Return_Score(AI_n);
                else if(Break_Point_Left_AI != NO_CHESS || Break_Point_Right_AI != NO_CHESS)    AI[i][j][4] = Return_Score(AI_n) + 1;
                else                                                                            AI[i][j][4] = Return_Score(AI_n) + 2;
                //PE
                if(Break_Point_Left_PE != NO_CHESS && Break_Point_Right_PE != NO_CHESS)         PE[i][j][4] = Return_Score(PE_n);
                else if(Break_Point_Left_PE != NO_CHESS || Break_Point_Right_PE != NO_CHESS)    PE[i][j][4] = Return_Score(PE_n) + 1;
                else                                                                            PE[i][j][4] = Return_Score(PE_n) + 2;
            }
        }
    }

    for(Sint32 i = 0; i < SIZE; i++)
    {
        for(Sint32 j = 0; j < SIZE; j++)
        {
            if(board[i][j] == NO_CHESS)
            {
                for(Sint32 p = 1; p < 4; p++)
                {
                    for(Sint32 q = p + 1; q < 5; q++)
                    {
                        if(AI[i][j][p] < AI[i][j][q])
                        {
                            AI[i][j][p] ^= AI[i][j][q] ^= AI[i][j][p] ^= AI[i][j][q];
                        }
                        if(PE[i][j][p] < PE[i][j][q])
                        {
                            PE[i][j][p] ^= PE[i][j][q] ^= PE[i][j][p] ^= PE[i][j][q];
                        }
                    }
                }

                //AI
                switch(AI[i][j][1])
                {
                    case 1 :                        AI[i][j][0] = 10;break;
                    case 2 :                        AI[i][j][0] = 20;break;
                    case 3 :                        AI[i][j][0] = 30;break;
                    case 4 :                        AI[i][j][0] = 40;break;
                    case 5 :                        AI[i][j][0] = 50;break;
                    case 7 :                        AI[i][j][0] = 60;break;
                    case 10:                        AI[i][j][0] = 70;break;

                    case 6 :
                    {
                        if(AI[i][j][2] == 6)        AI[i][j][0] = 100;
                        else                        AI[i][j][0] = 80;
                    }break;

                    case 8 :                        AI[i][j][0] = 90;break;

                    case 9 :
                    {
                        if(AI[i][j][2] == 9)        AI[i][j][0] = 140;
                        else if(AI[i][j][2] == 8)   AI[i][j][0] = 130;
                        else                        AI[i][j][0] = 110;
                    }break;

                    case 11:
                    {
                        if(AI[i][j][2] == 11)       AI[i][j][0] = 150;
                        else if(AI[i][j][2] == 9)    AI[i][j][0] = 150;
                        else                        AI[i][j][0] = 120;
                    }break;

                    case 12:                        AI[i][j][0] = 150;break;

                    case 13:
                    case 14:
                    case 15:                        AI[i][j][0] = 10086;break;

                    default:                        AI[i][j][0] = 0, Beep(100, 100);
                }
                //PE
                switch(PE[i][j][1])
                {
                    case 1 :                        PE[i][j][0] = 10;break;
                    case 2 :                        PE[i][j][0] = 20;break;
                    case 3 :                        PE[i][j][0] = 30;break;
                    case 4 :                        PE[i][j][0] = 40;break;
                    case 5 :                        PE[i][j][0] = 50;break;
                    case 7 :                        PE[i][j][0] = 60;break;
                    case 10:                        PE[i][j][0] = 70;break;

                    case 6 :
                    {
                        if(PE[i][j][2] == 6)        PE[i][j][0] = 100;
                        else                        PE[i][j][0] = 80;
                    }break;

                    case 8 :                        PE[i][j][0] = 90;break;

                    case 9 :
                    {
                        if(PE[i][j][2] == 9)        PE[i][j][0] = 140;
                        else if(PE[i][j][2] == 8)   PE[i][j][0] = 130;
                        else                        PE[i][j][0] = 110;
                    }break;

                    case 11:
                    {
                        if(PE[i][j][2] == 11)       PE[i][j][0] = 150;
                        else if(PE[i][j][2] == 9)    PE[i][j][0] = 150;
                        else                        PE[i][j][0] = 120;
                    }break;

                    case 12:                        PE[i][j][0] = 150;break;

                    case 13:
                    case 14:
                    case 15:                        PE[i][j][0] = 10086;break;

                    default:                        PE[i][j][0] = 0, Beep(100, 100);
                }

                //AI attack first
                AI[i][j][0] += 1;
            }
        }
    }

    for(Sint32 i = 0; i < SIZE; i++)
    {
        for(Sint32 j = 0; j < SIZE; j++)
        {
            if(AI[i][j][0] > AI_MAX)
            {
                AI_MAX = AI[i][j][0];
                AI_x = i;
                AI_y = j;
            }
            if(PE[i][j][0] > PE_MAX)
            {
                PE_MAX = PE[i][j][0];
                PE_x = i;
                PE_y = j;
            }
        }
    }

    AI_MAX > PE_MAX ? (A.array_x = AI_x, A.array_y = AI_y) : (A.array_x = PE_x, A.array_y = PE_y);
    Change_X_Y_Image(A);
}
View Code

五子棋——C++_第1张图片

你可能感兴趣的:(C++)