最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)簡單的五子棋游戲

 更新時間:2020年09月01日 14:04:12   作者:assasinSteven  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

# -*- coding:utf-8 -*-
# @Time: 2017/8/29 0029 10:14
# @Author: assasin
# @Email: assasin0308@sina.com
 
from tkinter import *
import math
 
class chessBoard():
  def __init__(self):
    # 創(chuàng)建一個tk對象,窗口
    self.window = Tk()
    # 窗口名稱
    self.window.title('五子棋游戲')
    # 窗口大小
    self.window.geometry('660x470')
    # 設(shè)置窗口不可縮放
    self.window.resizable(0,0)
    # 定義窗口的畫布
    self.canvas = Canvas(self.window, bg="#EEE8AC", width=470, height=470)
    # 畫出畫布內(nèi)容
    self.paint_board()
    # 定義畫布所在的網(wǎng)格
    self.canvas.grid(row=0, column=0)
 
  def paint_board(self):
    # 畫橫線
    for row in range(0, 15):
      if row == 0 or row == 14:
        self.canvas.create_line(25, 25 + row * 30, 25 + 14 * 30, 25 + row * 30, width=2)
    else:
      self.canvas.create_line(25, 25 + row * 30, 25 + 14 * 30, 25 + row * 30, width=1)
 
    # 畫豎線
    for column in range(0, 15):
      if column == 0 or column == 14:
        self.canvas.create_line(25 + column * 30, 25, 25 + column * 30, 25 + 14 * 30, width=2)
    else:
      self.canvas.create_line(25 + column * 30, 25, 25 + column * 30, 25 + 14 * 30, width=1)
 
    # 畫圓
    self.canvas.create_oval(112, 112, 118, 118, fill="black")
    self.canvas.create_oval(352, 112, 358, 118, fill="black")
    self.canvas.create_oval(112, 352, 118, 358, fill="black")
    self.canvas.create_oval(232, 232, 238, 238, fill="black")
    self.canvas.create_oval(352, 352, 358, 358, fill="black")
 
 
 
 
#定義五子棋游戲類
#0為黑子 , 1為白子 , 2為空位
class Gobang() :
  #初始化
  def __init__(self) :
    self.board = chessBoard()
    self.game_print = StringVar()
    self.game_print.set("")
    # 16*16的二維列表,保證不會out of index
    self.db = [([2] * 16) for i in range(16)]
    # 悔棋用的順序列表
    self.order = []
    # 棋子顏色
    self.color_count = 0
    self.color = 'black'
    # 清空與贏的初始化,已贏為1,已清空為1
    self.flag_win = 1
    self.flag_empty = 1
    self.options()
 
    # 黑白互換
  def change_color(self):
    self.color_count = (self.color_count + 1) % 2
    if self.color_count == 0:
      self.color = "black"
    elif self.color_count == 1:
      self.color = "white"
 
  # 落子
  def chess_moving(self,event):
    # 不點(diǎn)擊“開始”與“清空”無法再次開始落子
    if self.flag_win == 1 or self.flag_empty == 0:
      return
    # 坐標(biāo)轉(zhuǎn)化為下標(biāo)
    x, y = event.x - 25, event.y - 25
    x = round(x / 30)
    y = round(y / 30)
    # 點(diǎn)擊位置沒用落子,且沒有在棋盤線外,可以落子
    while self.db[y][x] == 2 and self.limit_boarder(y, x):
      self.db[y][x] = self.color_count
    self.order.append(x + 15 * y)
    self.board.canvas.create_oval(25 + 30 * x - 12, 25 + 30 * y - 12, 25 + 30 * x + 12, 25 + 30 * y + 12,fill=self.color, tags="chessman")
    if self.game_win(y, x, self.color_count):
      print(self.color, "獲勝")
      self.game_print.set(self.color + "獲勝")
    else:
      self.change_color()
      self.game_print.set("請" + self.color + "落子")
 
  # 保證棋子落在棋盤上
  def limit_boarder(self, y, x):
    if x < 0 or x > 14 or y < 0 or y > 14:
      return False
    else:
      return True
 
  # 計算連子的數(shù)目,并返回最大連子數(shù)目
  def chessman_count(self, y, x, color_count):
    count1, count2, count3, count4 = 1, 1, 1, 1
    # 橫計算
    for i in range(-1, -5, -1):
      if self.db[y][x + i] == color_count:
        count1 += 1
      else:
        break
 
    for i in range(1, 5, 1):
      if self.db[y][x + i] == color_count:
        count1 += 1
      else:
        break
    # 豎計算
    for i in range(-1, -5, -1):
      if self.db[y + i][x] == color_count:
        count2 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x] == color_count:
        count2 += 1
      else:
        break
    # /計算
    for i in range(-1, -5, -1):
      if self.db[y + i][x + i] == color_count:
        count3 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x + i] == color_count:
        count3 += 1
      else:
        break
 
    # \計算
    for i in range(-1, -5, -1):
      if self.db[y + i][x - i] == color_count:
        count4 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x - i] == color_count:
        count4 += 1
      else:
        break
 
    return max(count1, count2, count3, count4)
 
 
  # 判斷輸贏
  def game_win(self , y , x , color_count ):
    if self.chessman_count(y, x, color_count) >= 5:
      self.flag_win = 1
      self.flag_empty = 0
      return True
    else:
      return False
 
  #悔棋,清空棋盤,再畫剩下的n-1個棋子
  def withdraw(self):
    if len(self.order) == 0 or self.flag_win == 1:
      return
    self.board.canvas.delete("chessman")
    z = self.order.pop()
    x = z % 15
    y = z // 15
    self.db[y][x] = 2
    self.color_count = 1
    for i in self.order:
      ix = i % 15
    iy = i // 15
    self.change_color()
    self.board.canvas.create_oval(25 + 30 * ix - 12, 25 + 30 * iy - 12, 25 + 30 * ix + 12, 25 + 30 * iy + 12,
                   fill=self.color, tags="chessman")
    self.change_color()
    self.game_print.set("請" + self.color + "落子")
 
  # 清空
  def empty_all(self) :
    self.board.canvas.delete("chessman")
    # 還原初始化
    self.db = [([2] * 16) for i in range(16)]
    self.order = []
    self.color_count = 0
    self.color = 'black'
    self.flag_win = 1
    self.flag_empty = 1
    self.game_print.set("")
 
  #將self.flag_win置0才能在棋盤上落子
  def game_start(self):
    # 沒有清空棋子不能置0開始
    if self.flag_empty == 0:
      return
    self.flag_win = 0
    self.game_print.set("請" + self.color + "落子")
 
  def options(self):
    self.board.canvas.bind("<Button-1>", self.chess_moving)
    Label(self.board.window, textvariable=self.game_print, font=("Arial", 20)).place(relx=0, rely=0, x=495, y=200)
    Button(self.board.window, text="開始游戲", command=self.game_start, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495,y=15)
    Button(self.board.window, text="我要悔棋", command=self.withdraw, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495, y=60)
    Button(self.board.window, text="清空棋局", command=self.empty_all, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495,y=105)
    Button(self.board.window, text="結(jié)束游戲", command=self.board.window.destroy, width=13, font=("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
    self.board.window.mainloop()
 
 
if __name__ == '__main__':
  chess_game = Gobang()

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python如何提取script的部分內(nèi)容

    python如何提取script的部分內(nèi)容

    文章介紹了如何使用Python提取網(wǎng)頁中的腳本部分內(nèi)容,并提供了一個示例腳本,該腳本使用了`requests`庫來獲取網(wǎng)頁內(nèi)容,并使用`BeautifulSoup`庫來解析HTML,提取出`
    2025-02-02
  • Pycharm安裝scrapy及初始化爬蟲項(xiàng)目的完整步驟

    Pycharm安裝scrapy及初始化爬蟲項(xiàng)目的完整步驟

    因?yàn)槿腴Tpython以來一直使用pycharm,所以對著黑白的DOS不習(xí)慣,所以此次來實(shí)現(xiàn)使用pycharm進(jìn)行實(shí)現(xiàn)使用scrapy框架,下面這篇文章主要給大家介紹了關(guān)于Pycharm安裝scrapy及初始化爬蟲項(xiàng)目的完整步驟,需要的朋友可以參考下
    2022-08-08
  • 深入了解如何基于Python讀寫Kafka

    深入了解如何基于Python讀寫Kafka

    這篇文章主要介紹了深入了解如何基于Python讀寫Kafka,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • python爬蟲模擬登錄之圖片驗(yàn)證碼實(shí)現(xiàn)詳解

    python爬蟲模擬登錄之圖片驗(yàn)證碼實(shí)現(xiàn)詳解

    眾所周知python是一個很強(qiáng)大的語言,它擁有眾多的庫,今天我嘗試了使用python進(jìn)行驗(yàn)證碼的識別,下面這篇文章主要給大家介紹了關(guān)于python爬蟲模擬登錄之圖片驗(yàn)證碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Python中創(chuàng)建包和增添包的路徑(sys.path.append())

    Python中創(chuàng)建包和增添包的路徑(sys.path.append())

    本文主要介紹了Python中創(chuàng)建包和增添包的路徑(sys.path.append()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • pytorch 模型可視化的例子

    pytorch 模型可視化的例子

    今天小編就為大家分享一篇pytorch 模型可視化的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Pygame鼠標(biāo)進(jìn)行圖片的移動與縮放案例詳解

    Pygame鼠標(biāo)進(jìn)行圖片的移動與縮放案例詳解

    pygame是Python的第三方庫,里面提供了使用Python開發(fā)游戲的基礎(chǔ)包。本文將介紹如何通過Pygame實(shí)現(xiàn)鼠標(biāo)進(jìn)行圖片的移動與縮放,感興趣的可以關(guān)注一下
    2021-12-12
  • python圖形工具turtle繪制國際象棋棋盤

    python圖形工具turtle繪制國際象棋棋盤

    這篇文章主要為大家詳細(xì)介紹了python圖形工具turtle繪制國際象棋棋盤,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python Pandas知識點(diǎn)之缺失值處理詳解

    Python Pandas知識點(diǎn)之缺失值處理詳解

    這篇文章主要給大家介紹了關(guān)于Pandas知識點(diǎn)之缺失值處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例

    Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例

    這篇文章主要介紹了Python基于tkinter模塊實(shí)現(xiàn)的改名小工具,結(jié)合實(shí)例形式分析了tkinter模塊操作文件后綴名的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07

最新評論

沙坪坝区| 定边县| 崇左市| 巴南区| 枞阳县| 噶尔县| 鹿泉市| 霍州市| 和林格尔县| 金溪县| 科尔| 建湖县| 高州市| 黄龙县| 沾益县| 扎囊县| 资源县| 恩平市| 连云港市| 蒙自县| 阿拉尔市| 临夏县| 宜君县| 武定县| 花垣县| 盘锦市| 玉门市| 察隅县| 工布江达县| 湘潭市| 乌兰察布市| 兰州市| 浦江县| 留坝县| 五大连池市| 察雅县| 阿巴嘎旗| 故城县| 金川县| 奉化市| 博罗县|