PyGame每日一練——五子棋小遊戲

專注的阿熊發表於2022-01-17

class AI:

     def __init__(self, line_points, chessman):

         self._line_points = line_points

         self._my = chessman

         self._opponent = BLACK_CHESSMAN if chessman == WHITE_CHESSMAN else WHITE_CHESSMAN

         self._checkerboard = [[0] * line_points for _ in range(line_points)]

     def get_opponent_drop(self, point):

         self._checkerboard[point.Y][point.X] = self._opponent.Value

     def AI_drop(self):

         point = None

         score = 0

         for i in range(self._line_points):

             for j in range(self._line_points):

                 if self._checkerboard[j][i] == 0:

                     _score = self._get_point_score(Point(i, j))

                     if _score > score:

                         score = _score

                         point = Point(i, j)

                     elif _score == score and _score > 0:

                         r = random.randint(0, 100)

                         if r % 2 == 0:

                             point = Point(i, j)

         self._checkerboard[point.Y][point.X] = self._my.Value

         return point

     def _get_point_score(self, point):

         score = 0

         for os in offset:

             score += self._get_direction_score(point, os[0], os[1])

         return score

     def _get_direction_score(self, point, x_offset, y_offset):

         count = 0   # 落子處我方連續子數

         _count = 0  # 落子處對方連續子數

         space = None   # 我方連續子中有無空格

         _space = None  # 對方連續子中有無空格

         both = 0    # 我方連續子兩端有無阻擋

         _both = 0   # 對方連續子兩端有無阻擋

         # 如果是 1 表示是邊上是我方子, 2 表示敵方子

         flag =外匯跟單gendan5.com self._get_stone_color(point, x_offset, y_offset, True)

         if flag != 0:

             for step in range(1, 6):

                 x = point.X + step * x_offset

                 y = point.Y + step * y_offset

                 if 0 <= x < self._line_points and 0 <= y < self._line_points:

                     if flag == 1:

                         if self._checkerboard[y][x] == self._my.Value:

                             count += 1

                             if space is False:

                                 space = True

                         elif self._checkerboard[y][x] == self._opponent.Value:

                             _both += 1

                             break

                         else:

                             if space is None:

                                 space = False

                             else:

                                 break   # 遇到第二個空格退出

                     elif flag == 2:

                         if self._checkerboard[y][x] == self._my.Value:

                             _both += 1

                             break

                         elif self._checkerboard[y][x] == self._opponent.Value:

                             _count += 1

                             if _space is False:

                                 _space = True

                         else:

                             if _space is None:

                                 _space = False

                             else:

                                 break

                 else:

                     # 遇到邊也就是阻擋

                     if flag == 1:

                         both += 1

                     elif flag == 2:

                         _both += 1

         if space is False:

             space = None

         if _space is False:

             _space = None

         _flag = self._get_stone_color(point, -x_offset, -y_offset, True)

         if _flag != 0:

             for step in range(1, 6):

                 x = point.X - step * x_offset

                 y = point.Y - step * y_offset

                 if 0 <= x < self._line_points and 0 <= y < self._line_points:

                     if _flag == 1:

                         if self._checkerboard[y][x] == self._my.Value:

                             count += 1

                             if space is False:

                                 space = True

                         elif self._checkerboard[y][x] == self._opponent.Value:

                             _both += 1

                             break

                         else:

                             if space is None:

                                 space = False

                             else:

                                 break   # 遇到第二個空格退出

                     elif _flag == 2:

                         if self._checkerboard[y][x] == self._my.Value:

                             _both += 1

                             break

                         elif self._checkerboard[y][x] == self._opponent.Value:

                             _count += 1

                             if _space is False:

                                 _space = True

                         else:

                             if _space is None:

                                 _space = False

                             else:

                                 break

                 else:

                     # 遇到邊也就是阻擋

                     if _flag == 1:

                         both += 1

                     elif _flag == 2:

                         _both += 1

         score = 0

         if count == 4:

             score = 10000

         elif _count == 4:

             score = 9000

         elif count == 3:

             if both == 0:

                 score = 1000

             elif both == 1:

                 score = 100

             else:

                 score = 0

         elif _count == 3:

             if _both == 0:

                 score = 900

             elif _both == 1:

                 score = 90

             else:

                 score = 0

         elif count == 2:

             if both == 0:

                 score = 100

             elif both == 1:

                 score = 10

             else:

                 score = 0

         elif _count == 2:

             if _both == 0:

                 score = 90

             elif _both == 1:

                 score = 9

             else:

                 score = 0

         elif count == 1:

             score = 10

         elif _count == 1:

             score = 9

         else:

             score = 0

         if space or _space:

             score /= 2

         return score

     # 判斷指定位置處在指定方向上是我方子、對方子、空

     def _get_stone_color(self, point, x_offset, y_offset, next):

         x = point.X + x_offset

         y = point.Y + y_offset

         if 0 <= x < self._line_points and 0 <= y < self._line_points:

             if self._checkerboard[y][x] == self._my.Value:

                 return 1

             elif self._checkerboard[y][x] == self._opponent.Value:

                 return 2

             else:

                 if next:

                     return self._get_stone_color(Point(x, y), x_offset, y_offset, False)

                 else:

                     return 0

         else:

             return 0


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2852678/,如需轉載,請註明出處,否則將追究法律責任。

相關文章