python实现简单的五子棋游戏


Posted in Python onSeptember 01, 2020

本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下

# -*- 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):
    # 创建一个tk对象,窗口
    self.window = Tk()
    # 窗口名称
    self.window.title('五子棋游戏')
    # 窗口大小
    self.window.geometry('660x470')
    # 设置窗口不可缩放
    self.window.resizable(0,0)
    # 定义窗口的画布
    self.canvas = Canvas(self.window, bg="#EEE8AC", width=470, height=470)
    # 画出画布内容
    self.paint_board()
    # 定义画布所在的网格
    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):
    # 不点击“开始”与“清空”无法再次开始落子
    if self.flag_win == 1 or self.flag_empty == 0:
      return
    # 坐标转化为下标
    x, y = event.x - 25, event.y - 25
    x = round(x / 30)
    y = round(y / 30)
    # 点击位置没用落子,且没有在棋盘线外,可以落子
    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
 
  # 计算连子的数目,并返回最大连子数目
  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="结束游戏", 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()

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 实现一个贴吧图片爬虫的示例
Oct 12 Python
Python+tkinter使用80行代码实现一个计算器实例
Jan 16 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
Python进阶之全面解读高级特性之切片
Feb 19 Python
Python基础学习之函数方法实例详解
Jun 18 Python
python中必要的名词解释
Nov 20 Python
如何使用Python抓取网页tag操作
Feb 14 Python
python 实现任务管理清单案例
Apr 25 Python
Django实现后台上传并显示图片功能
May 29 Python
python实现图书馆抢座(自动预约)功能的示例代码
Sep 29 Python
五分钟学会怎么用Pygame做一个简单的贪吃蛇
Jan 06 Python
pandas中DataFrame检测重复值的实现
May 26 Python
Pycharm连接gitlab实现过程图解
Sep 01 #Python
pycharm不以pytest方式运行,想要切换回普通模式运行的操作
Sep 01 #Python
python selenium xpath定位操作
Sep 01 #Python
使用python库xlsxwriter库来输出各种xlsx文件的示例
Sep 01 #Python
Python3实现英文字母转换哥特式字体实例代码
Sep 01 #Python
python 解决pycharm运行py文件只有unittest选项的问题
Sep 01 #Python
Python2及Python3如何实现兼容切换
Sep 01 #Python
You might like
php一些公用函数的集合
2008/03/27 PHP
PHP面向对象之后期静态绑定功能介绍
2015/05/18 PHP
PHP url的pathinfo模式加载不同控制器的简单实现
2016/08/12 PHP
php通过pecl方式安装扩展的实例讲解
2018/02/02 PHP
php输出文字乱码的解决方法
2019/10/04 PHP
PHP PDO和消息队列的个人理解与应用实例分析
2019/11/25 PHP
$()JS小技巧
2007/07/21 Javascript
jQueryUI的Dialog的简单封装
2010/06/07 Javascript
二叉树的非递归后序遍历算法实例详解
2014/02/07 Javascript
javascript解析json实例详解
2014/11/05 Javascript
Jquery 实现弹出层插件
2015/01/28 Javascript
JavaScript中的acos()方法使用详解
2015/06/14 Javascript
实例代码详解jquery.slides.js
2015/11/16 Javascript
jquery实现倒计时效果
2015/12/14 Javascript
微信小程序 picker-view 组件详解及简单实例
2017/01/10 Javascript
Vue 项目部署到服务器的问题解决方法
2017/12/05 Javascript
vue项目常用组件和框架结构介绍
2017/12/24 Javascript
微信小程序实现文字从右向左无限滚动
2020/11/18 Javascript
layui数据表格重载实现往后台传参
2019/11/15 Javascript
vue将文件/图片批量打包下载zip的教程
2020/10/21 Javascript
jQuery使用hide()、toggle()函数实现相机品牌展示隐藏功能
2021/01/29 jQuery
[06:24]DOTA2 2015国际邀请赛中国区预选赛第二日TOP10
2015/05/27 DOTA
Python深入学习之闭包
2014/08/31 Python
python 文件查找及内容匹配方法
2018/10/25 Python
python 一个figure上显示多个图像的实例
2019/07/08 Python
Python3网络爬虫开发实战之极验滑动验证码的识别
2019/08/02 Python
Python实现socket非阻塞通讯功能示例
2019/11/06 Python
Python Flask框架实现简单加法工具过程解析
2020/06/03 Python
python爬虫爬取某网站视频的示例代码
2021/02/20 Python
100%植物性、有机、即食餐:Sakara Life
2018/10/25 全球购物
党员年终民主评议的自我评价
2013/11/05 职场文书
交通事故赔偿协议书
2014/04/15 职场文书
干部鉴定材料
2014/05/18 职场文书
python通配符之glob模块的使用详解
2021/04/24 Python
Python 内置函数速查表一览
2021/06/02 Python
java实现web实时消息推送的七种方案
2022/07/23 Java/Android