【python教程】使用python做一个井字棋

大家好,我是帅哥(学习python使我快乐)

今天用python做一个简易の井字棋

需要使用的第三方库:

from tkinter import *
import tkinter.messagebox as msg

引入好了之后,就开始我们的正式代码8!

一、基础信息

root = Tk()
root.title('井字棋')
# labels
Label(root, text="player1 : X", font="times 15").grid(row=0, column=1)
Label(root, text="player2 : O", font="times 15").grid(row=0, column=2)

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# for player1 sign = X and for player2 sign= Y
mark = ''

# counting the no. of click
count = 0

panels = ["panel"] * 10

二、胜利方

def win(panels, sign):
    return ((panels[1] == panels[2] == panels[3] == sign)
            or (panels[1] == panels[4] == panels[7] == sign)
            or (panels[1] == panels[5] == panels[9] == sign)
            or (panels[2] == panels[5] == panels[8] == sign)
            or (panels[3] == panels[6] == panels[9] == sign)
            or (panels[3] == panels[5] == panels[7] == sign)
            or (panels[4] == panels[5] == panels[6] == sign)
            or (panels[7] == panels[8] == panels[9] == sign))


def checker(digit):
    global count, mark, digits

    # Check which button clicked

    if digit == 1 and digit in digits:
        digits.remove(digit)
        ##player1 will play if the value of count is even and for odd player2 will play
        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button1.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 2 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button2.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 3 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button3.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 4 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button4.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 5 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button5.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 6 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button6.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 7 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button7.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 8 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button8.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    if digit == 9 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button9.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "Player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "Player2 wins")
            root.destroy()

    ###if count is greater then 8 then the match has been tied
    if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):
        msg.showinfo("Result", "Match Tied")
        root.destroy()

四、button定义

####define buttons
button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))
button1.grid(row=1, column=1)
button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))
button2.grid(row=1, column=2)
button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))
button3.grid(row=1, column=3)
button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))
button4.grid(row=2, column=1)
button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5))
button5.grid(row=2, column=2)
button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6))
button6.grid(row=2, column=3)
button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7))
button7.grid(row=3, column=1)
button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8))
button8.grid(row=3, column=2)
button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9))
button9.grid(row=3, column=3)

root.mainloop()

知识点:

1.def   2.if   3.elif

1.def:

        定义一个函数:如print这种的

2.if:

        如果的意思

3.elif:

        如果否则

预告:下一次是商品管理(炒鸡简单)

以上就是所有内容,记得关注帅哥

ღ( ´・ᴗ・` )比心

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 Python 代码实现井字棋游戏。 ```python # 初始化棋盘 board = [" " for i in range(9)] # 打印棋盘 def print_board(): row1 = "|{}|{}|{}|".format(board[0], board[1], board[2]) row2 = "|{}|{}|{}|".format(board[3], board[4], board[5]) row3 = "|{}|{}|{}|".format(board[6], board[7], board[8]) print() print(row1) print(row2) print(row3) print() # 判断是否有胜者 def check_win(player): # 横向 if board[0] == board[1] == board[2] == player: return True elif board[3] == board[4] == board[5] == player: return True elif board[6] == board[7] == board[8] == player: return True # 竖向 elif board[0] == board[3] == board[6] == player: return True elif board[1] == board[4] == board[7] == player: return True elif board[2] == board[5] == board[8] == player: return True # 对角线 elif board[0] == board[4] == board[8] == player: return True elif board[2] == board[4] == board[6] == player: return True else: return False # 游戏主循环 def main(): print("Welcome to Tic Tac Toe!") print_board() while True: # 玩家 X 的回合 x_choice = int(input("Player X, choose your move (1-9): ")) if board[x_choice-1] == " ": board[x_choice-1] = "X" else: print("That space is already taken!") continue print_board() if check_win("X"): print("Congratulations! Player X wins!") break # 判断是否平局 if " " not in board: print("It's a tie!") break # 玩家 O 的回合 o_choice = int(input("Player O, choose your move (1-9): ")) if board[o_choice-1] == " ": board[o_choice-1] = "O" else: print("That space is already taken!") continue print_board() if check_win("O"): print("Congratulations! Player O wins!") break # 判断是否平局 if " " not in board: print("It's a tie!") break if __name__ == "__main__": main() ``` 运行代码后,你可以在终端输入数字 1-9 来选择下棋的位置,例如输入 1 就会将 X 放在棋盘左上角的位置。 Enjoy the game!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值