PyGame每日一練——五子棋小遊戲
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- pygame開發小遊戲GAM遊戲
- C語言五子棋小遊戲C語言遊戲
- 基於 Blazor 開發五子棋⚫⚪小遊戲Blazor遊戲
- C語言實現小遊戲:五子棋C語言遊戲
- 每日一練(一)
- 五子棋遊戲(Java)遊戲Java
- 在 Pygame 遊戲中放置平臺GAM遊戲
- 每日一練(二)
- Java實現五子棋對戰小遊戲【完整版】Java遊戲
- JS之五子棋遊戲JS遊戲
- 使用 Python 和 Pygame 模組構建一個遊戲框架PythonGAM遊戲框架
- Python練習01-對戰小遊戲Python遊戲
- python中pygame遊戲打包為exe檔案PythonGAM遊戲
- Python遊戲開發!Mac完美安裝pygamePython遊戲開發MacGAM
- 安卓開發 五子棋遊戲安卓遊戲
- LeetCode每日一題:Nim遊戲(No.292)LeetCode每日一題遊戲
- 每日一練(47):找不同
- Python:遊戲:五子棋之人機對戰Python遊戲
- 【Demo見真章】基於HarmonyOS手機實現五子棋對戰小遊戲遊戲
- 【python小練習】簡單的猜數字遊戲Python遊戲
- openGauss每日一練(全文檢索)
- 每日一練(43):同構字串字串
- 每日一練(32):左旋轉字串字串
- 每日一練(37):實現 strStr()
- 安全員考試每日一練
- (003)我們一起學Python;鞏固練習,寫個小遊戲Python遊戲
- Pygame - Python 遊戲程式設計入門 class1GAMPython遊戲程式設計
- Pygame - Python 遊戲程式設計入門 class2GAMPython遊戲程式設計
- 用 Pygame 使你的遊戲角色移動起來GAM遊戲
- 【pygame遊戲開發】這幾個經典遊戲,勾起了少年的快樂GAM遊戲開發
- java簡單練習-五子棋Java
- pygame模組引數彙總(python遊戲程式設計)GAMPython遊戲程式設計
- 每日一練(41):Excel表列名稱Excel
- 每日一練(42):Excel表序號Excel
- 每日一練(36):有效的括號
- 每日一練(35):最長公共字首
- 硬核遊戲一定小眾?NO!遊戲
- python爆破一筆畫小遊戲Python遊戲