python小项目之五子棋游戏

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

1.项目简介

在刚刚学习完python套接字的时候做的一个五子棋小游戏,可以在局域网内双人对战,也可以和电脑对战

2.实现思路

局域网对战

对于局域网功能来说,首先建立连接(tcp),然后每次下棋时将棋子的坐标发送给对方,当接收到坐标后实例化成棋子对象,这个接收时用了select函数,因为pygame需要循环渲染图片,所以要用非阻塞方式接收消息

select()的机制中提供一fd_set的数据结构,实际上是一long类型的数组, 每一个数组元素都能与一打开的文件句柄(不管是Socket句柄,还是其他文件或命名管道或设备句柄)建立联系,建立联系的工作由程序员完成, 当调用select()时,由内核根据IO状态修改fd_set的内容,由此来通知执行了select()的进程哪一Socket或文件可读或可写,主要用于Socket通信当中

主要代码如下:

# 接收cli的消息
if is_people:
 rs, ws, es = select.select(inputs, [], [], 0)
  for r in rs:
    if r is tcpclisock:
    try:
     data = r.recv(BUFSIZ)
      islink = True
      print(data.decode('utf8'))
      if data.decode('utf8') == 'again':
       is_recieve1 = True
      if data.decode('utf8') == 'yes':
       is_playagain = True
       is_play = True
      if data.decode('utf8') == 'no':
       is_recieve2 = True
       islink = False
      if not is_play and not result:
       me = storn.Storn_Black(eval(data))
       black_chesses.append(me)
        chesses.append(me)
       is_play = True
     except error:
      islink = False

电脑对战

电脑对战的思路也很简单,用了应该是最常见的也是最简单的方法,就是循环遍历棋盘的每一个点,判断该点的价值,选取价值最大的点落子,这个需要对五子棋的棋型有一定了解,这里介绍几种常见的棋型(约定1为己方棋子,2为对方棋子,0为空)

活四(011110):这时候四颗棋子相连,同时两端为空,已经阻止不了一方的胜利了,此时价值应该设置最高
死四(011112|10111|11011):四颗棋子,只有一个地方能形成连五,如果是自己的棋可以赢,是对方的也可以阻止对方赢棋,此时价值次高

就这样把每种棋型判断一下,获得该点的价值,主要代码如下:

# 判断每个点的价值
def point_value(pos, white_chesses, black_chesses, identify1, identify2):
 value = 0
 for i in range(1,9):
  # *1111_ 活四
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == 0:
   value += 40000
  # *11112 死四1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 30000
  # 1*111 死四2
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 30000
  # 11*11 死四3
  if get_point(pos, i, -2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1:
   value += 30000
  # *111_ 活三1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 20000
  # *1_11_ 活三2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == 0:
   value += 20000
  # *1112 死三1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify2:
   value += 15000
  # _1_112 死三2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # _11_12 死三3
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # 1__11 死三4
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 15000
  # 1_1_1 死三5
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 15000
  # 2_111_2 死三6
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify2 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # __11__ 活二1
  if get_point(pos, i, -1, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 1000
  # _1_1_ 活二2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 1000
  # *1__
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0:
   value += 30
  # *1_
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0:
   value += 20
  # *1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1:
   value += 10
 return value

电脑选择落子位置时,要判断是进攻还是防守,需要两次遍历棋盘,获取进攻时的最大价值和防守的最大价值,主要代码如下:

# 电脑选取落子的位置
def ai(white_chesses, black_chesses, chesses):
 value = max1 = max2 = 0
 pos1 = pos2 = ()
 # 进攻时
 for i in range(0,15):
   row = 28 + i * 40
   for j in range(0,15):
    col = 28 + j * 40
    pos = (row,col)
   if is_empty(pos, chesses):
     continue
    value = point_value(pos, white_chesses, black_chesses, 1, 2)
    if value > max1:
     max1 = value
     pos1 = (row,col)

  # 防守时
  for i in range(0,15):
   for j in range(0,15):
    row = 28 + i * 40
    col = 28 + j * 40
    if is_empty((row,col), chesses):
     continue
   value = point_value((row,col), white_chesses, black_chesses, 2, 1)
    if value > max2:
     max2 = value
     pos2 = (row,col)
  if max1 > max2:
   return pos1
  else:
   return pos2

3.游戏截图

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

(0)

相关推荐

  • python pygame实现五子棋小游戏

    今天学习了如何使用pygame来制作小游戏,下面是五子棋的代码,我的理解都写在注释里了 import pygame # 导入pygame模块 print(pygame.ver) # 检查pygame的版本,检查pygame有没有导入成功 EMPTY = 0 BLACK = 1 WHITE = 2 # 定义三个常量函数,用来表示白棋,黑棋,以及 空 black_color = [0, 0, 0] # 定义黑色(黑棋用,画棋盘) white_color = [255, 255, 255] # 定义白

  • python实现简单五子棋游戏

    本文实例为大家分享了python实现简单五子棋游戏的具体代码,供大家参考,具体内容如下 from graphics import * from math import * import numpy as np def ai(): """ AI计算落子位置 """ maxmin(True, DEPTH, -99999999, 99999999) return next_point[0], next_point[1] def maxmin(is_ai

  • python实现五子棋小游戏

    本文实例为大家分享了python实现五子棋小游戏的具体代码,供大家参考,具体内容如下 暑假学了十几天python,然后用pygame模块写了一个五子棋的小游戏,代码跟有缘人分享一下. import numpy as np import pygame import sys import traceback import copy from pygame.locals import * pygame.init() pygame.mixer.init() #颜色 background=(201,202

  • python实现五子棋小程序

    本文实例为大家分享了python实现五子棋小程序的具体代码,供大家参考,具体内容如下 一.结合书上例子,分三段编写: wuziqi.py #coding:utf-8 from win_notwin import * from show_qipan import * maxx=10 #10行10列 maxy=10 qipan=[[0,0,0,0,1,0,0,2,0,0],[0,1,2,1,1,0,2,0,0,0],[0,0,0,0,1,1,0,2,0,0],[0,0,0,0,2,0,0,1,0,0

  • python实现五子棋游戏

    本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下 话不多说,直接上代码: 全部工程文件,在GitHub:五子棋 效果预览: #!/usr/bin/env python3 #-*- coding:utf-8 -*- import pygame from pygame.locals import * from sys import exit import numpy background_image = 'qipan.png' white_image = 'whit

  • python版本五子棋的实现代码

    正文之前 前阵子做了个<人工智能> 的课程作业,然后写了个人工智障...大概就是个可以跟你下五子棋的傻儿子...下面是代码和效果 正文 1. 摘要 机器博弈是人工智能领域的重要分支,它的研究对象多以复杂的棋牌类智力游戏为主,已经得到解决的棋类游戏,几乎全部都应归功于机器博弈近半个世纪的发展.计算机解决问题的优势在于能把不易解析的问题,借助于现代计算机的运算速度优势枚举出所有的合理情形而得解;然而,博弈问题的复杂程度决定了它不能过度依赖机器的计算能力.许多待解决的或已经解决的棋类,其状态空间复杂

  • python实现五子棋人机对战游戏

    本文代码基于 python3.6 和 pygame1.9.4. 五子棋比起我之前写的几款游戏来说,难度提高了不少.如果是人与人对战,那么,电脑只需要判断是否赢了就可以.如果是人机对战,那你还得让电脑知道怎么下. 我们先从简单的问题来看. 开端 画棋盘 首先肯定是要画出棋盘来,用 pygame 画出一个 19 × 19 或 15 × 15 的棋盘并不是什么难事,这在之前的文章中已经多次用到,就不赘述了. 画棋子 需要说一下的是画棋子,因为没找到什么合适的棋子图片,所以只要自己来画棋子. 我们用 p

  • python五子棋游戏的设计与实现

    这个python的小案例是五子棋游戏的实现,在这个案例中,我们可以实现五子棋游戏的两个玩家在指定的位置落子,画出落子后的棋盘,并且根据函数判断出输赢的功能. 这个案例的思路如下所示: 首先,根据棋盘的样子画出棋盘 然后,对棋盘进行初始化,将可以落子的位置进行统一化处理 接下来,就是进入游戏的环节,双方轮流落子,落子后,并将棋盘画出 最后,根据落子的位置判断选手的的输赢情况,游戏结束 五子棋游戏的设计和实现 代码如下: def main(): print("五子棋游戏".center(5

  • python制作简单五子棋游戏

    本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下 #五子棋 ''' 矩阵做棋盘 16*16 "+" 打印棋盘 for for 游戏是否结束 开始下棋 while 游戏是否结束: 黑白交替 player=0 p%2==0 ==1 p+=1 下棋动作一样 但是棋子不一样 ''' 代码 #创建棋盘的程序 def initBoard(): global board #调用全局的board board=[None]*16 for i in range(len(boa

  • 使用python实现简单五子棋游戏

    用python实现五子棋简单人机模式的练习过程,供大家参考,具体内容如下 第一次写博客,我尽力把它写好. 最近在初学python,今天就用自己的一些粗浅理解,来记录一下这几天的python简单人机五子棋游戏的练习,下面是实现过程的理解(是在cmd中运行的): 主要流程: *重点内容* - 首先是模块及类的划分 - 棋子类和棋盘类的方法 - 对策略类里的功能进行细分,调用棋子类和棋盘类 - 写出判断输赢的方法 - 用main函数进行整个游戏进度的控制 模块及类的划分 类的划分涉及到了面向对象的内容

随机推荐