Presumably everyone has played Gobang , in fact, for me, Gobang is the most played chess game, of course, as a child also love to play chess and chess, but because Gobang relatively simple, so the first to achieve a simple human machine gobang. Recently, I was thinking that now learned a little bit of programming should be the previous childhood special love play games to realize , after all, it is a precious memory!
OK other not more said, into our topic, first of all, I am ready to achieve this Gobang two times the content, because after the production, the individual feel that the implementation of the AI Lazi algorithm is not well written, but also think for a long time, the feeling still did not achieve the desired effect, although recently holiday , but the school has things, so this time the interface is simply implemented, and some of the game logic functions, game judgment function first put out, and then carefully think about how to implement the AI algorithm, so that the AI more intelligent, can enhance the fun of the game, or the player will always win, Such a game is a failure!
First, a brief introduction, the main implementation of the following several functions:
Game function bool Isblackorwhite (); Randomly generated, judging who is a sunspot or white//game in the detection function void Resetgame (); Re-start Resource cleanup bool TimeOut (); Determine the timeout function bool Change_pos (); Converts the coordinates on the screen to the coordinates on the board bool Change_y (); Together with the above functions to form a conversion function bool Ishavechess (playerchess &pch); Determine if there are any other pieces (players) BOOL ishavechess (computerchess &cch); Determine if there are any other pieces (computer) void Regretpace () in the location of the landing point; Undo function (can only be undo three times) void Computer_play (); Computer Lazi bool Checkwin (); Judging whether the player wins or the computer wins
first here to explain, wherein the computer Lazi function, and determine the function of winning or losing this feeling is not good enough, the AI algorithm is very low-level, as long as a little will play players as long as not to lose, so the AI algorithm is still very failure, So prepare to think of a better AI algorithm, but even if they think again, after all, still the enemy of the human brain!
Here to further illustrate these functions, in which the function of the coordinate conversion function is to change the position of the mouse click to the specified position on the board, otherwise if the mouse to which point will be generated pieces, then the picture is not neat.
There is also a function to determine the timeout , generally speaking on the network of these board games will have time to detect, to determine whether it timed out. In this case, I'm 15 seconds. If there is no lazi, force the player to Lazi, and the position of Lazi is based on the position of the nearest coordinate point at the position where your mouse is located. Do not know this is not good, but the personal feeling is better than the effect of forced random lazi!
Next take a look at the internal implementation code, because the code is given a lot of space, so I only give a few important functions of the game code, generated windows such code is not posted, encapsulated in a small game engine! Of course, if you need source code, you can leave the email or private messages to me!
BOOL isblackorwhite ()//Determine whether it is a sunspot or a white child {int result = 0;result = rand ()% 2;if (0 = = Resul T) {return TRUE;} Else{return FALSE;}} void Resetgame ()//restart resource cleanup {g_isingame = False;g_iblacknum = 0;g_iwhitenum = 0; if (Isblackorwhite ()) {int i = 0;g_playercolor = black;for (i = 0; i < Chess_count; i++) {Pchess[i].color = black;pchess[ I].isexist = False;cchess[i].color = White;cchess[i].isexist = false;}} Else{int i = 0;g_playercolor = white;for (i = 0; i < Chess_count; i++) {Pchess[i].color = White;pchess[i].isexist = Fals E;cchess[i].color = Black;cchess[i].isexist = false;}}} BOOL timeout ()//Judgment timeout function {G_tnow = GetTickCount ();//Determine if the first lazi if (BLACK = = G_p Layercolor) {if (G_iblacknum = = 0) {return FALSE;}} Else{if (G_iwhitenum = = 0 && G_iblacknum = = 0) {return FALSE;}} if ((G_tnow-g_tpre > 20000) && (G_isingame = = True)) {return true;} Else{return FALSE;}} To determine the location of the landing has noHis pawn bool ishavechess (playerchess &pch) {BOOL flag = false;if (BLACK = = G_playercolor) First judge the player's pawn Color {for (int i = 0; i < G_iblacknum; i++) {if (pch.x = = pchess[i].x) && ; (Pch.y = = pchess[i].y)) {flag = TRUE;} } for (int i = 0; i < G_iwhitenum; i++) {if ((pch.x = = cchess[i].x) && (pch.y = cchess[i].y)) {flag = TRUE;} }}else//Player is the white child's judgment {for (int i = 0; i < G_iwhitenum; i++) {if (pch.x = = P chess[i].x) && (pch.y = = pchess[i].y)) {flag = TRUE;}} for (int i = 0; i < G_iblacknum; i++) {if ((pch.x = = cchess[i].x) && (pch.y = = cchess[i].y)) {flag = TRUE;}}} return flag;} BOOL Ishavechess (computerchess &cch) {bool flag = FALSE; if (BLACK = = G_playercolor)//To determine the player's piece color first {for (int i = 0; i < G_iblacknum; i++) {if ((cch.x = = pchess[i].x) && (cch.y = pchess[i].y)) {flag = TRUE;} } for (int i = 0; i < G_iwhiteNum; i++) {if ((cch.x = = cchess[i].x) && (cch.y = = cchess[i].y)) {flag = TRUE;}}} else//player is the white child's judgment {for (int i = 0; i < G_iwhitenum; i++) {if (cch.x = = Pche ss[i].x) && (cch.y = = pchess[i].y)) {flag = TRUE;}} for (int i = 0; i < G_iblacknum; i++) {if ((cch.x = = cchess[i].x) && (cch.y = cchess[i].y)) {flag = TRUE;}} }return Flag;} Undo function (only undo three times) void Regretpace () {if (BLACK = = G_playercolor) Determine the player's disc color {if (0 = = g_iblacknum)//player piece color number, so as to determine whether the player can undo {MessageBeep (mb_iconexclamation); The number of pieces is 0, cannot be undo, emits a warning, or else {pchess[g_iblacknum-1].isexist = FALSE; Clear the pawn g_iblacknum--; Black number minus one}}else {if (0 = = g_iwhitenum)//player piece color, so as to determine whether the player can undo {MessageBeep (mb_iconexclamation); The number of pieces is 0, cannot be undo, emits a warning, or else {pchess[g_iwhitenum-1].isexist = FALSE; Clear the pawn g_iwhitenum--; WhiteThe number of chess is reduced by one}}g_pacecount--;}
Of course there are several functions in the space, but it is a few simple operations, it is not given. What is more important is that there is no better solution for the algorithms inside the computer. In the following articles will certainly have a deep understanding of the AI algorithm, of course, here in order to test the need, I simply gave a random Lazi method.
//pc Lazi void Computer_play () {if (BLACK = = G_playercolor)//By judging the player's piece color, thus knowing the color of the computer piece {fo R (int i = 0; i < Chess_count; i++) {if (!cchess[i].isexist) {while (true) {G_ix = rand ()% 674;g_iy = rand ()% 660 + 40;i F (Change_pos ()) {cchess[i].x = G_ICHESSX;CCHESS[I].Y = G_ichessy;if (! Ishavechess (Cchess[i]))//To determine the position of the new piece to Lazi there are no other pieces {cchess[i].isexist = True;g_iwhitenum++;break;}}} Break;}}} else{for (int i = 0; i < Chess_count; i++) {if (!cchess[i].isexist) {while (true) {G_ix = rand ()% 674;g_iy = rand ()% 660 + 40;if (Change_pos ()) {cchess[i].x = G_ICHESSX;CCHESS[I].Y = G_ichessy;if (! Ishavechess (Cchess[i]))//To determine the position of the new piece to Lazi there are no other pieces {cchess[i].isexist = True;g_iblacknum++;break;}}} Break;}}}}
Well, because this time did not realize the core part of the game,AI algorithm implementation, so this time to say this, then will certainly be for the AI (artificial intelligence) algorithm to give the implementation, of course, if it is best to implement a network programming to achieve the battle of everyone , Of course this may be difficult to achieve, but the AI algorithm will definitely be implemented!
Finally look, of course, personal art do bad, please don't take offense Oh!
First is the initial interface, of course, here because of time reasons, not able to do multi-level pages, please forgive us!
Next take a look at the running process of the screen, but need to click the Start button, otherwise the game state switch does not pass!
Of course , the AI here is automatically random lazi, so it looks very irregular . You can also perform undo and concede, but the number of failures will increase by 1. Such as:
finally summed up, this is only a basic gobang, because temporarily did not think of a better AI algorithm, so the subsequent will continue to write Gobang, the inside of the AI algorithm in the next article will be given, and temporarily still in the testing phase .
Design of the---Gobang of Win32 game production (I.)