python井字棋ai_[Python100行系列]-井字棋游戏

这篇博客介绍了如何利用Python的turtle库制作一个井字棋游戏。文章详细讲解了绘制棋盘、监听用户点击、判断游戏状态和获胜条件等步骤,并提供了相应的代码实现。
摘要由CSDN通过智能技术生成

博客:Hzy的博客 | Hzy Blog​hzeyuan.cn91e7f53489cf6e31cab66a8ee3c7e182.png一些学习python的小项目,小游戏。python小项目​github.com

话不多说,今天尝试用turtle库来写一个井字棋游戏。1.首先需要画一个井字的棋盘

2.需要圈圈和叉叉两名玩家,通过点击棋盘来O和×

3.判断条件,当满足获胜条件后,游戏结束。

1.我们先来定义棋盘的大小,600*600这样分成9宫格,每一个格子就是200*200

我选取每个格子的中心点为坐标

# 所有格子的中心位置

position = {(-200, 200): 0, (0, 200): 0, (200, 200): 0

, (-200, 0): 0, (0, 0): 0, (200, 0): 0

, (-200, -200): 0, (0, -200): 0, (200, -200): 0}这是一个键为坐标,值为O或者x的字典,当为0时,则认为这个格子还没有人使用。定义屏幕大小

turtle.setup(width, height)

2.画井字棋盘

# 画线

def draw_line(x, y, direction=None):

if direction:

turtle.seth(direction)

turtle.penup()

turtle.goto(x, y)

turtle.pendown()

turtle.forward(width)

# 井棋盘

def tic_tac_toe():

draw_line(-300, 100)

draw_line(-300, -100)

draw_line(-100, 300, 270)

draw_line(100, 300, 270)

turtle.update()

3.监听点击事件,当点击的坐标在格子里,则在格子中画一个O或X通过判断中心点,点击点之间的距离是否小于格子的半径,来判断点击的是哪一个格子

同时,还要判断格子里是否已经被使用了,使用过的不能使用。

游戏开始时,画的是O,然后是×,交替,直到游戏结束。

# 在点击的位置画圆,画着画x

def draw_flag(x, y):

global last_person

in_pos = None

can_write = False

for p in position.keys():

if (p[0] - x) ** 2 + (p[1] - y) ** 2 < 10000:

in_pos = p

if position.get(in_pos, None) == 0:

can_write = True

break

# 该位置可以画圈或x

if can_write:

# 画x还是画O,当True时,我们画圈,当False时,我们画x

if len(circle_list) > len(x_list):

circle_x = False

position[in_pos] = 'x'

last_person = 'x'

x_list.append(in_pos)

else:

circle_x = True

circle_list.append(in_pos)

position[in_pos] = 'o'

last_person = 'o'

# 画圈圈

if circle_x:

turtle.color("red")

turtle.seth(270)

turtle.penup()

turtle.goto(in_pos[0] - 100, in_pos[1])

turtle.pendown()

turtle.circle(100)

# 画x

else:

turtle.color("black")

for i in range(4):

turtle.penup()

turtle.goto(in_pos[0], in_pos[1])

turtle.pendown()

turtle.seth(45 + i * 90)

turtle.forward(sqrt(20000))

turtle.update()

who_win()

4. 判断获胜的条件,在每一次点击过后。

# 判断赢家

def who_win():

v = list(position.values())

# 胜利的情况,一条横线,一条竖线,斜线

win_1 = v[0] == v[1] == v[2] != 0 or v[3] == v[4] == v[5] != 0 or v[6] == v[7] == v[8] != 0

win_2 = v[0] == v[3] == v[6] != 0 or v[1] == v[4] == v[7] != 0 or v[2] == v[5] == v[8] != 0

win_3 = v[0] == v[4] == v[8] != 0 or v[2] == v[4] == v[6] != 0

if win_1 or win_2 or win_3:

turtle.goto(-120, 0)

turtle.write("game over,{} is winner".format(last_person), font=("Arial", 25, "normal"))

turtle.onscreenclick(None)

最后一个简单的井字棋游戏就写好了!!如果你也在学python,也想做一些有意思的事情,不妨关注下我,嘻嘻。

好的,下面是实现AI井字棋的alpha-beta剪枝算法的Python代码: ```python import math MAX = math.inf MIN = -math.inf def minimax(board, depth, alpha, beta, is_maximizing): result = check_winner(board) if result != None: return result if is_maximizing: best_score = MIN for i in range(3): for j in range(3): if board[i][j] == ' ': board[i][j] = 'X' score = minimax(board, depth+1, alpha, beta, False) board[i][j] = ' ' best_score = max(score, best_score) alpha = max(alpha, best_score) if beta <= alpha: break return best_score else: best_score = MAX for i in range(3): for j in range(3): if board[i][j] == ' ': board[i][j] = 'O' score = minimax(board, depth+1, alpha, beta, True) board[i][j] = ' ' best_score = min(score, best_score) beta = min(beta, best_score) if beta <= alpha: break return best_score def find_best_move(board): best_score = MIN best_move = None for i in range(3): for j in range(3): if board[i][j] == ' ': board[i][j] = 'X' score = minimax(board, 0, MIN, MAX, False) board[i][j] = ' ' if score > best_score: best_score = score best_move = (i, j) return best_move def check_winner(board): for i in range(3): if board[i][0] == board[i][1] == board[i][2] != ' ': return 1 if board[i][0] == 'X' else -1 if board[0][i] == board[1][i] == board[2][i] != ' ': return 1 if board[0][i] == 'X' else -1 if board[0][0] == board[1][1] == board[2][2] != ' ': return 1 if board[0][0] == 'X' else -1 if board[0][2] == board[1][1] == board[2][0] != ' ': return 1 if board[0][2] == 'X' else -1 for i in range(3): for j in range(3): if board[i][j] == ' ': return None return 0 board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] while True: print(board[0]) print(board[1]) print(board[2]) winner = check_winner(board) if winner != None: if winner == 0: print("Tie!") else: print("You win!" if winner == 1 else "AI wins!") break row = int(input("Enter row (0, 1, or 2): ")) col = int(input("Enter column (0, 1, or 2): ")) if board[row][col] != ' ': print("Invalid move!") continue board[row][col] = 'O' winner = check_winner(board) if winner != None: if winner == 0: print("Tie!") else: print("You win!" if winner == 1 else "AI wins!") break row, col = find_best_move(board) board[row][col] = 'X' ``` 这个代码实现了一个简单的人机对战井字棋游戏,其中AI使用了alpha-beta剪枝算法来下棋。当玩家输入坐标时,程序会将玩家的棋子放在对应的位置,并检查是否有获胜者。如果没有获胜者,AI会使用minimax算法和alpha-beta剪枝来找到最好的下棋位置,并将棋子放在那里。程序会不断循环,直到游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值