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語言遊戲
- 每日一練(43):同構字串字串
- 每日一練(32):左旋轉字串字串
- 每日一練(37):實現 strStr()
- 基於 Blazor 開發五子棋⚫⚪小遊戲Blazor遊戲
- C語言實現小遊戲:五子棋C語言遊戲
- 每日一練(41):Excel表列名稱Excel
- 每日一練(42):Excel表序號Excel
- openGauss每日一練(全文檢索)
- java簡單練習-五子棋Java
- 萌新練習寫程式碼的每日一練:括號生成
- 每日一練(39):二進位制求和
- 每日一練(28):平衡二叉樹二叉樹
- SCO、Linux下的shell五子棋小遊戲(轉)Linux遊戲
- 每日一練(46):兩個陣列的交集陣列
- 每日一練(27):二叉樹的深度二叉樹
- Java實現五子棋對戰小遊戲【完整版】Java遊戲
- 每日一練(45):長度最小的子陣列陣列
- C++每日一練26-四數相加 IIC++
- 每日一練(16):對稱的二叉樹二叉樹
- 每日一練(23):第一個只出現一次的字元字元
- 每日一練(22):連續子陣列的最大和陣列
- 每日一練(19):從上到下列印二叉樹二叉樹
- 每日一練(24):在排序陣列中查詢數字排序陣列
- 程式設計師大神,C語言程式設計製作不一樣的五子棋小遊戲程式設計師C語言遊戲
- 練手必備 | C 語言快速實現五子棋
- 初學練習,用Perl寫的命令列五子棋命令列
- 自學Python筆記-pygame模組《外星人入侵》練習篇Python筆記GAM
- 【Demo見真章】基於HarmonyOS手機實現五子棋對戰小遊戲遊戲
- Java每日基礎恢復訓練Java
- matlab每日練習 lenght函式Matlab函式
- Python練習01-對戰小遊戲Python遊戲
- SQL實戰-資料分析師-筆試面試-每日一練SQL筆試面試
- (003)我們一起學Python;鞏固練習,寫個小遊戲Python遊戲
- 【每日一練】Oracle OCP認證考試題庫解析052-1Oracle
- 【每日一練】Oracle OCP認證考試題庫解析052-2Oracle
- 【每日一練】Oracle OCP認證考試題庫解析052-3Oracle