五子棋——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[][]; //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 + ;
O.image_x = O.array_y * PIXEL_PER_CELL + ;
} 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[]; 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 = ;
Sint32 SCREEN_HEIGHT = ;
Sint32 SCREEN_BPP = ;
}; #endif
#include "Link_Five.h" const int FRAMES_PER_SECOND = ; //PvP
void Game_1(void);
//PvAI
void Game_2(void); Render ren; int main(int argc, char* args[])
{ //SDL_ShowCursor(0);
SDL_WarpMouse(, );
ren.Load();
ren.Check(); ren.Show_Logo(); Game_2(); ren.Free();
return ;
} void Game_1(void)
{
SDL_Event event;
PingGe::Timer Fps;
Chess_Board chess;
ren.Show_Board();
Sint32 done = ;
while(done == )
{
Fps.Start();
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
done = ;
} if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
done = ;
}
}
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() < / FRAMES_PER_SECOND ) )
{
//Sleep the remaining frame time
SDL_Delay(( / 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(, );
ren.Show_White(, );
chess.Play_Flag = ~chess.Play_Flag; Sint32 done = ;
while(done == )
{
Fps.Start();
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
done = ;
} if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
done = ;
}
} 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() < / FRAMES_PER_SECOND ) )
{
//Sleep the remaining frame time
SDL_Delay(( / FRAMES_PER_SECOND ) - Fps.Get_Ticks());
}
}
}
#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 = ;
//left
for(Sint32 i = A.array_x, j = A.array_y; j >= ; j--)
{
if(board[i][j] == Chess_Color) n1++;
else
{
S.array_x = i;
S.array_y = j + ;
break;
}
}
//right
for(Sint32 i = A.array_x, j = A.array_y; j < ; j++)
{
if(board[i][j] == Chess_Color) n2++;
else
{
E.array_x = i;
E.array_y = j - ;
break;
}
}
if(n1 + n2 - >= )
{
return true;
} n1 = n2 = ;
//up
for(Sint32 i = A.array_x, j = A.array_y; i >= ; i--)
{
if(board[i][j] == Chess_Color) n1++;
else
{
S.array_x = i + ;
S.array_y = j;
break ;
}
}
//down
for(Sint32 i = A.array_x, j = A.array_y; i < ; i++)
{
if(board[i][j] == Chess_Color) n2++;
else
{
E.array_x = i - ;
E.array_y = j;
break ;
}
}
if(n1 + n2 - >=)
{
return true;
} n1 = n2 = ;
//left_up
for(Sint32 i = A.array_x, j = A.array_y; ((i >= ) && (j >= )); i--, j--)
{
if(board[i][j] == Chess_Color) n1++;
else
{
S.array_x = i + ;
S.array_y = j + ;
break;
}
}
//right_down
for(Sint32 i = A.array_x, j = A.array_y; ((i < ) && (j < )); i++, j++)
{
if(board[i][j] == Chess_Color) n2++;
else
{
E.array_x = i - ;
E.array_y = j - ;
break;
}
}
if(n1 + n2 - >= )
{
return true;
} n1 = n2 = ;
//right_up
for(Sint32 i = A.array_x, j = A.array_y; ((i >= ) && (j < )); i--, j++)
{
if(board[i][j] == Chess_Color) n1++;
else
{
S.array_x = i + ;
S.array_y = j - ;
break;
}
}
//left_down
for(Sint32 i = A.array_x, j = A.array_y; ((i < ) && (j >= )); i++, j--)
{
if(board[i][j] == Chess_Color) n2++;
else
{
E.array_x = i - ;
E.array_y = j + ;
break;
}
}
if(n1 + n2 - >= )
{
return true;
}
return false;
} bool Chess_Board::X_Y_Image_Lawful(struct X_Y& O)
{
for(Sint32 i = ; i < ; i++)
{
if((O.image_x > + i * PIXEL_PER_CELL - ) && (O.image_x < + i * PIXEL_PER_CELL + ))
{
for(Sint32 j = ; j < ; j++)
{
if((O.image_y > + j * PIXEL_PER_CELL - ) && (O.image_y < + j * PIXEL_PER_CELL + ))
{
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;
}
#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) == -)
{
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() == -)
{
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, , ) == -)
{
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", ); Low = Mix_LoadWAV("low.wav"); //Background //Remind_Player_1 //Remind_Player_2 //Chess_Piece_Black //Chess_Piece_White //Logo //Cursor
clips[].x = ; clips[].x = ; clips[].x = ; clips[].x = ; clips[].x = ; clips[].x = ; clips[].x = ;
clips[].y = ; clips[].y = ; clips[].y = ; clips[].y = ; clips[].y = ; clips[].y = ; clips[].y = ;
clips[].w = ; clips[].w = ; clips[].w = ; clips[].w = ; clips[].w = ; clips[].w = ; clips[].w = ;
clips[].h = ; clips[].h = ; clips[].h = ; clips[].h = ; clips[].h = ; clips[].h = ; clips[].h = ; //Creat surface
Background = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
Remind_Player_1 = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
Remind_Player_2 = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
Chess_Piece_Black = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
Chess_Piece_White = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
Logo = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
Cursor = SDL_CreateRGBSurface(screen->flags, clips[].w, clips[].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, , , Big, &clips[]);
PingGe::Apply_Surface(Remind_Player_1, , , Big, &clips[]);
PingGe::Apply_Surface(Remind_Player_2, , , Big, &clips[]);
PingGe::Apply_Surface(Chess_Piece_Black,, , Big, &clips[]);
PingGe::Apply_Surface(Chess_Piece_White,, , Big, &clips[]);
PingGe::Apply_Surface(Logo, , , Big, &clips[]);
PingGe::Apply_Surface(Cursor, , , Big, &clips[]); //Load the cursor
cursor = new PingGe::Cursor(screen, Cursor, , ); //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() / ;
Seconds = TTF_RenderText_Solid(Font, time.str().c_str(), textColor);
} void Render::Check(void)
{
if(Big == NULL)
{
Log("Error : ", "");
}
if(Remind_Player_1 == NULL)
{
Log("Error : ", "");
}
if(Remind_Player_2 == NULL)
{
Log("Error : ", "");
}
if(Chess_Piece_Black == NULL)
{
Log("Error : ", "");
}
if(Chess_Piece_White == NULL)
{
Log("Error : ", "");
}
if(Background == NULL)
{
Log("Error : ", "");
}
if(Logo == NULL)
{
Log("Error : ", "");
}
if(Cursor == NULL)
{
Log("Error : ", "");
}
if(Seconds == NULL)
{
Log("Error : ", "");
}
if(Low == NULL)
{
Log("Error : ", "");
}
if(Font == NULL)
{
Log("Error : ", "");
}
} 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, , );
logo.Act3();
//logo.Act2(440);
SDL_Delay();
} 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() / ;
Seconds = TTF_RenderText_Solid(Font, time.str().c_str(), textColor); PingGe::Draw_FillRect(screen, , , , , SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
PingGe::Apply_Surface(screen, , , Seconds);
SDL_UpdateRect(screen, , , , );
}
else
{
time_2.Unpause();
time_1.Pause(); std::stringstream time;
time << (int)time_2.Get_Ticks() / ;
Seconds = TTF_RenderText_Solid(Font, time.str().c_str(), textColor); PingGe::Draw_FillRect(screen, , , , , SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
PingGe::Apply_Surface(screen, , , Seconds);
SDL_UpdateRect(screen, , , , );
}
} void Render::Show_Board(void) const
{
PingGe::Apply_Surface(screen, , , Background);
SDL_Flip(screen);
} void Render::Show_Black(Sint32 x, Sint32 y) const
{
x -= ;
y -= ;
PingGe::Apply_Surface(screen, x, y, Chess_Piece_Black);
SDL_UpdateRect(screen, x, y, , ); Mix_PlayChannel(-,Low,);
} void Render::Show_White(Sint32 x, Sint32 y) const
{
x -= ;
y -= ;
PingGe::Apply_Surface(screen, x, y, Chess_Piece_White);
SDL_UpdateRect(screen, x, y, , ); Mix_PlayChannel(-,Low,);
} void Render::Remind(void) const
{
static Sint32 flag = ;
flag++; if(flag % == )
{
for(Sint32 i = -; i <= ; i++)
{
PingGe::Apply_Surface(screen, , i, Remind_Player_1);
SDL_UpdateRect(screen, , , , );
SDL_Delay();
}
}
else
{
for(Sint32 i = -; i <= ; i++)
{
PingGe::Apply_Surface(screen, , i, Remind_Player_2);
SDL_UpdateRect(screen, , , , );
SDL_Delay();
}
}
}
#include "Link_Five.h" Sint32 Chess_Board::Return_Score(Sint32 n)
{
switch(n)
{
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
default:return ;
}
} void Chess_Board::AI(void)
{
Sint32 AI[][][] = {}, PE[][][] = {},
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 = , AI_x, AI_y,
PE_MAX = , PE_x, PE_y;
bool Flag_AI, Flag_PE; for(Sint32 i = ; i < SIZE; i++)
{
for(Sint32 j = ; j < SIZE; j++)
{
if(board[i][j] == NO_CHESS)
{
//
AI_n = PE_n = ;
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 - ; p >= ; 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 + ; 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][] = Return_Score(AI_n);
else if(Break_Point_Left_AI != NO_CHESS || Break_Point_Right_AI != NO_CHESS) AI[i][j][] = Return_Score(AI_n) + ;
else AI[i][j][] = Return_Score(AI_n) + ;
//PE
if(Break_Point_Left_PE != NO_CHESS && Break_Point_Right_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n);
else if(Break_Point_Left_PE != NO_CHESS || Break_Point_Right_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n) + ;
else PE[i][j][] = Return_Score(PE_n) + ; //
AI_n = PE_n = ;
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 - ; p >= ; 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 + ; 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][] = Return_Score(AI_n);
else if(Break_Point_Up_AI != NO_CHESS || Break_Point_Down_AI != NO_CHESS) AI[i][j][] = Return_Score(AI_n) + ;
else AI[i][j][] = Return_Score(AI_n) + ;
//PE
if(Break_Point_Up_PE != NO_CHESS && Break_Point_Down_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n);
else if(Break_Point_Up_PE != NO_CHESS || Break_Point_Down_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n) + ;
else PE[i][j][] = Return_Score(PE_n) + ; //
AI_n = PE_n = ;
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 + , q = j - ; p < SIZE && q >= ; 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 - , q = j + ; p >= && 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][] = Return_Score(AI_n);
else if(Break_Point_Left_AI != NO_CHESS || Break_Point_Right_AI != NO_CHESS) AI[i][j][] = Return_Score(AI_n) + ;
else AI[i][j][] = Return_Score(AI_n) + ;
//PE
if(Break_Point_Left_PE != NO_CHESS && Break_Point_Right_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n);
else if(Break_Point_Left_PE != NO_CHESS || Break_Point_Right_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n) + ;
else PE[i][j][] = Return_Score(PE_n) + ; //
AI_n = PE_n = ;
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 - , q = j - ; p >= && q >= ; 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 + , q = j + ; 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][] = Return_Score(AI_n);
else if(Break_Point_Left_AI != NO_CHESS || Break_Point_Right_AI != NO_CHESS) AI[i][j][] = Return_Score(AI_n) + ;
else AI[i][j][] = Return_Score(AI_n) + ;
//PE
if(Break_Point_Left_PE != NO_CHESS && Break_Point_Right_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n);
else if(Break_Point_Left_PE != NO_CHESS || Break_Point_Right_PE != NO_CHESS) PE[i][j][] = Return_Score(PE_n) + ;
else PE[i][j][] = Return_Score(PE_n) + ;
}
}
} for(Sint32 i = ; i < SIZE; i++)
{
for(Sint32 j = ; j < SIZE; j++)
{
if(board[i][j] == NO_CHESS)
{
for(Sint32 p = ; p < ; p++)
{
for(Sint32 q = p + ; q < ; 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][])
{
case : AI[i][j][] = ;break;
case : AI[i][j][] = ;break;
case : AI[i][j][] = ;break;
case : AI[i][j][] = ;break;
case : AI[i][j][] = ;break;
case : AI[i][j][] = ;break;
case : AI[i][j][] = ;break; case :
{
if(AI[i][j][] == ) AI[i][j][] = ;
else AI[i][j][] = ;
}break; case : AI[i][j][] = ;break; case :
{
if(AI[i][j][] == ) AI[i][j][] = ;
else if(AI[i][j][] == ) AI[i][j][] = ;
else AI[i][j][] = ;
}break; case :
{
if(AI[i][j][] == ) AI[i][j][] = ;
else if(AI[i][j][] == ) AI[i][j][] = ;
else AI[i][j][] = ;
}break; case : AI[i][j][] = ;break; case :
case :
case : AI[i][j][] = ;break; default: AI[i][j][] = , Beep(, );
}
//PE
switch(PE[i][j][])
{
case : PE[i][j][] = ;break;
case : PE[i][j][] = ;break;
case : PE[i][j][] = ;break;
case : PE[i][j][] = ;break;
case : PE[i][j][] = ;break;
case : PE[i][j][] = ;break;
case : PE[i][j][] = ;break; case :
{
if(PE[i][j][] == ) PE[i][j][] = ;
else PE[i][j][] = ;
}break; case : PE[i][j][] = ;break; case :
{
if(PE[i][j][] == ) PE[i][j][] = ;
else if(PE[i][j][] == ) PE[i][j][] = ;
else PE[i][j][] = ;
}break; case :
{
if(PE[i][j][] == ) PE[i][j][] = ;
else if(PE[i][j][] == ) PE[i][j][] = ;
else PE[i][j][] = ;
}break; case : PE[i][j][] = ;break; case :
case :
case : PE[i][j][] = ;break; default: PE[i][j][] = , Beep(, );
} //AI attack first
AI[i][j][] += ;
}
}
} for(Sint32 i = ; i < SIZE; i++)
{
for(Sint32 j = ; j < SIZE; j++)
{
if(AI[i][j][] > AI_MAX)
{
AI_MAX = AI[i][j][];
AI_x = i;
AI_y = j;
}
if(PE[i][j][] > PE_MAX)
{
PE_MAX = PE[i][j][];
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);
}
五子棋——C++的更多相关文章
- 自己写的HTML5 Canvas + Javascript五子棋
看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本.样式,帮助大众,成为受欢迎的人,感觉满羡慕的.我也想学会前端技术,变得受欢迎呀.于是心血来潮,开始学习前端知识,并写下了这 ...
- java swing 双人五子棋源代码
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...
- HTML5 五子棋 - JS/Canvas 游戏
背景介绍 因为之前用c#的winform中的gdi+,java图形包做过五子棋,所以做这个逻辑思路也就驾轻就熟,然而最近想温故html5的canvas绘图功能(公司一般不用这些),所以做了个五子棋,当 ...
- [收藏]C++简单五子棋
#include<iostream> #include<iomanip> using namespace std; ; //棋盘行数 ; //棋盘列数 char p[X][Y] ...
- jQuery网页版五子棋小游戏源码下载
体验效果:http://hovertree.com/texiao/game/4/ 网页五子棋源代码: <!DOCTYPE html> <html> <head> & ...
- js+html5双人五子棋(源码下载)
代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- jquery在线五子棋
在线五子棋试玩地址:http://keleyi.com/game/12/ 以下是完整代码,保存到html文件打开也可以玩: <!DOCTYPE html> <html> < ...
- 五子棋AI清月连珠开源
经过差不多两年的业余时间学习和编写,最近把清月连珠的无禁手部分完善得差不多了.这中间进行了很多思考,也有很多错误认识,到现在有一些东西还没有全面掌握,所以想通过开源于大家共同交流. 最近一直发表一些五 ...
- PyQt写的五子棋
技术路线 GUI的实现 使用PyQt技术作为基础.PyQt是一个支持多平台的客户端开发SDK,使用它实现的客户端可以运行在目前几乎所有主流平台之上. 使用PyQt,Qt设计器实现UI,通过pyuic4 ...
- 浅析基本AI五子棋算法
五子棋是所有棋类博弈中比较简单的了,这里介绍的也只是一种非常基本的AI策略.其实,包括之前的AI贪吃蛇,感觉这两个AI其实体现的都是一种建模思想,把一个现实中的问题模型化,抽象化,得到其一般特征,再设 ...
随机推荐
- 从零开始学习jQuery-------jQuery元素选择器(三)
下面我们来介绍一下jQuery元素选择器,在Web开发中我们最常用的操作是获取元素,然后对获取的元素进行一系列的操作,jQuery根据获取页面元素的不同,可以将jQuery选择器分为四大类:基本选择器 ...
- 使用ssh对服务器进行登录
一.什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会 ...
- 【开源java游戏框架libgdx专题】-12-开发工具-图片合成
TexturePackerGui工具: 1.工具使用: 首先看到texturepacker的界面 界面介绍: New pack:创建项目按钮,单击后输入文件名称,创建文件. Input directo ...
- JavaScript设计模式之观察者模式(学习笔记)
设计模式(Design Pattern)对于软件开发来说其重要性不言而喻,代码可复用.可维护.可扩展一直都是软件工程中的追求!对于我一个学javascript的人来说,理解设计模式似乎有些困难,对仅切 ...
- 黑马程序员-hashtable
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列 ...
- C#部分方法定义
C#部分方法定义 部分类也可以定义部分方法.部分方法在部分类中定义,但没有方法体,在另一个部分类中执行.在这两个部分类中,都要使用partial关键字. public partial class My ...
- 99%Bug 修复方法
以下仅支持4.0.3或之后的设备 (写在前面,这个教程需要安装两个app,且卸载后无法达到效果,所以有app drawer洁癖者慎重(你可以把它们隐藏起来么). 当然等官方rom更新或者安装最新三方r ...
- java学习——集合框架(泛型,Map)
泛型: ... Map:一次添加一对元素.Collection 一次添加一个元素. Map也称为双列集合,Collection集合称为单列集合. 其实map集合中存储的就是键值对. map集合中必须保 ...
- javascript之attribute 和 property
首先看看这两个单词的英文释义(来自有道词典).先是property: property ['prɔpəti] n. 性质,性能:财产:所有权 英英释义: any area set aside for ...
- Qt中如何在QCursor移动的时候不触发moveEvent
有时候有这样的需求,比如想对全局光标进行一次setPos(),但这个时候又不想触发消息队列触发mouseMoveEvent,这个时候就可以这么做. myWidget->clearFocus(); ...