生活随笔
收集整理的這篇文章主要介紹了
C++五子棋(五)——实现AI落子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AI思考落子點
在之前我們已經實現計算權值了,現在要想讓AI落子,應根據之前的計算結果使棋子落在分值最大點上。當然可能會出現多個分值相同的最大點,這時在其中隨機取一個點落下即可。
typedef struct point{int row
;int col
;
} point_t
;
point_t
actionAI(ChessData
* data
);
#include <time.h>
#include <stdlib.h>
#include <vector>
point_t
actionAI(ChessData
* data
){calcScore(data
);int maxScore
= 0;std
::vector
<std
::pair
<int, int>> maxPoints
;int k
= 0;for(int row
= 0; row
< BOARD_GRAD_SIZE
; row
++){for(int col
= 0; col
< BOARD_GRAD_SIZE
; col
++){if(data
->chessMap
[row
][col
] == 0){if(data
->scoreMap
[row
][col
] > maxScore
){maxScore
.clear();k
= 0;maxScore
.push_back(std
::make_pair(row
, col
));k
++;}else if(data
->scoreMap
[row
][col
] == maxScore
){maxPoints
.push_back(std
::make_pair(row
, col
));k
++;}}}}srand((unsigned)time(0));int index
= rend() % k
;return maxPoints
[index
];}
實現AI落子
void AI_GO(){point_t point
= actionAI(&game
);clickPosRow
= point
.row
;clickPosCol
= point
.col
;Sleep(1000);chessDown(clickPosRow
, clickPosCol
, CHESS_WHITE
);updateGameMap(&game
, clickPosRow
, clickPosCol
);}
總結
以上是生活随笔為你收集整理的C++五子棋(五)——实现AI落子的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。