background_image_filename = 'blackground.png'
black_mouse_image_filename = 'black.png'
white_mouse_image_filename = 'white.png'
restart_icon_filename='restart.png'
pingju_icon_filename='pingju.jpg'
win_icon_filename='win.jpg'
lose_icon_filename='lose.jpg' import pygame
import time
import sys from pygame.locals import *
from sys import exit HIGHLIGHTCOLOR = (255,255,255) def AI(pp):
#status is now white and black key in the board
#taozi = white
#computer = black num = 0
x = 0
y = 0 if pp[1][1]=='none':
return 4 for i in range(0,3):
for j in range(0,3):
if pp[i][j]=='none':
pp[i][j]='white'
cnt=0 tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx+1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx-1 if cnt > num:
num = cnt
x=i
y=j cnt=0
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
ty=ty+1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
ty=ty-1 if cnt > num:
num = cnt
x=i
y=j cnt=0
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx+1
ty=ty+1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx-1
ty=ty-1 if cnt > num:
num = cnt
x=i
y=j cnt=0
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx+1
ty=ty-1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx-1
ty=ty+1 if cnt > num:
num = cnt
x=i
y=j pp[i][j]='none' return x*3+y def main(): pygame.init()
screen = pygame.display.set_mode((800,500),0,32)
pygame.display.set_caption("you can never win!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(white_mouse_image_filename).convert_alpha() white_sign = pygame.image.load(white_mouse_image_filename).convert_alpha()
restart = pygame.image.load(restart_icon_filename).convert_alpha()
pingju = pygame.image.load(pingju_icon_filename).convert_alpha()
winicon = pygame.image.load(win_icon_filename).convert_alpha()
lose = pygame.image.load(lose_icon_filename).convert_alpha() black_sign = pygame.image.load(black_mouse_image_filename).convert_alpha()
position=[ [27,11,93,68,0],[240,6,304,60,1],[476,4,525,42,2],[24,206,80,265,3],[246,212,292,257,4],[477,212,528,258,5],[26,440,73,479,6],[244,435,292,478,7],[475,435,527,480,8] ]
positionMid=[ [48-26,24-24],[269-26,24-24],[500-26,24-24],[48-26,233-24],[269-26,234-24],[500-26,234-24],[48-26,459-24],[269-26,460-24],[500-26,460-24] ]
positionColor=[ 'none','none','none','none','none','none','none','none','none'] whoIsTurn = ['computer','taozi']
turnKey = 1 # 1 means taozi
# 0 means computer
cnt = 0
pp=[ ['none','none','none'],
['none','none','none'],['none','none','none']]
#pp = [ [
win=0
while 1:
newx =0
newy =0 color = 'white'
color1 = 'black'
color2 = 'none'
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN :
newx,newy = pygame.mouse.get_pos()
for i in position:
if (newx>=i[0] and newx <= i[2]) and (newy >= i[1] and newy <= i[3]):
j = i[4]
if positionColor[j]!='none':
continue
positionColor[j]= color
#positionColor[j+1]= color
for ii in range(0,3):
for jj in range(0,3):
pp[ii][jj]=positionColor[ii*3+jj] # AI begin res = AI( pp )
positionColor[res]=color1 # AI finished cnt=cnt+1
if newx>=674 and newx<=774 and newy>=396 and newy<= 496:
for i in range(0,3):
for j in range(0,3):
positionColor[i*3+j]='none'
pp[i][j]='none'
win=0 screen.blit(background,(0,0))
screen.blit(restart,(674,396))
#x,y = pygame.mouse.get_pos()
#x -= mouse_cursor.get_width()/2
#y -= mouse_cursor.get_height()/2
#if win==0:
# screen.blit(mouse_cursor,(x,y)) #x,y = pygame.mouse.get_pos()
for i in position:
x,y = pygame.mouse.get_pos()
if (x>=i[0] and x <= i[2]) and (y >= i[1] and y <= i[3]):
x-=26
y-=26
if positionColor[ int(i[4]) ]=='none':
#pygame.draw.rect(screen,HIGHLIGHTCOLOR,(x,y,52,52),0)
screen.blit(mouse_cursor,(x,y))
print("done it")
else:
print("color= ",positionColor[i[4]]) for ii in [0,1,2,3,4,5,6,7,8]:
if positionColor[ii] != 'none':
if positionColor[ii] == 'white':
#print("write white_sign")
screen.blit(white_sign,positionMid[ii] )
else :
#print("write black_sign")
screen.blit(black_sign,positionMid[ii]) for i in range(0,3):
for j in range(0,3):
pp[i][j]=positionColor[i*3+j] for i in range(0,3):
if pp[i][0]==pp[i][1] and pp[i][0]==pp[i][2] and pp[i][0]!='none':
win=1
winner=pp[i][0]
if pp[0][i]==pp[1][i] and pp[0][i]==pp[2][i] and pp[0][i]!='none':
win=1
winner=pp[0][i]
if pp[0][0]==pp[1][1] and pp[0][0]==pp[2][2] and pp[0][0]!='none':
win=1
winner=pp[0][0]
if pp[2][0]==pp[1][1] and pp[1][1] == pp[0][2] and pp[1][1]!='none':
win =1
winner=pp[1][1]
if win==0:
cntcolor=0
for i in range(0,3):
for j in range(0,3):
if pp[i][j]!='none':
cntcolor=cntcolor+1
if cntcolor==9 :
win =1
winner='pingju' if win==1 :
#print("someone win!")
if winner=='white':
screen.blit(winicon,(50,20))
elif winner=='pingju':
screen.blit(pingju,(100,100))
else:
screen.blit(lose,(100,100))
#print("winner = ",winner)
#time.sleep(10000) pygame.display.update() if __name__ == '__main__':
main()

根据之前的博文,我把给同学做的三子棋小游戏的代码发出来,只是界面很丑很丑,AI算法很笨很笨,过几天我传到网盘上,提供大家下载娱乐的更多相关文章

  1. 使用pcs api往免费的百度网盘上传下载文件

    百度个人云盘空间大,完全免费,而且提供了pcs api供调用操作文件,在平时的项目里往里面保存一些文件是很实用的. 环境准备: 开通读写网盘的权限及获取access_token:http://blog ...

  2. 【推荐网站】下载国外网盘+强大的离线下载站—offcloud.com

    博主在网上浏览时看到一篇帖子,推荐了一个离线下载网站–offcloud.com,支持上传种子文件.磁力链和几十家网盘的直连下载,厉害了我的哥,这是个啥网站这么666.即使之前咱们写了几篇文章来自建下载 ...

  3. 一个国外网盘pCloud——支持离线下载

    给大家分享一个国外网盘<支持离线下载> https://my.pcloud.com/#page=register&invite=HiegZ8aBrt7

  4. For macOS.百度网盘 破解SVIP、下载速度限制~

    For macOS.百度网盘 破解SVIP.下载速度限制~ 是插件的 https://github.com/CodeTips/BaiduNetdiskPlugin-macOS 2019-01-03 让 ...

  5. MultCloud – 支持数据互传的网盘管理

    MultCloud https://www.multcloud.com/ 是一款在线服务,可以在一个地方管理众多网盘,支持国产百度盘, 最具有特色的地方是你可以直接在 MultCloud 里操作将 D ...

  6. 百度网盘免VIP全速下载!

    不知道大家在用百度网盘下载文件时会不会遇到这样一个问题: 过分! 太过分了! 100M的宽带你就给我限速到20KB/s... 当然 解决办法有很多 1.充钱(这辈子都不可能的) ······ 百度上有 ...

  7. Visual Studio 2017 发布 15.5 版本,百度网盘离线安装包下载。

    Visual Studio 2017 15.5 版本已正式发布,同时发布的还有 Visual Studio for Mac 7.3 .此次更新包含主要性能改进,新特性以及 bug 修复.发行说明中文版 ...

  8. 百度网盘无vip高速下载的方法

    我拿的是谷歌浏览器做实验 首先下载一个可以改user-agent的插件 我chorm里面下载的是User-Agent Switcher for Chrome插件 将百度网盘的url地址中的baidu. ...

  9. 百度网盘上下载文件,调用api接口的请求方式和参数

    REST api 功能:下载单个文件. Download接口支持HTTP协议标准range定义,通过指定range的取值可以实现断点下载功能. 例如: 如果在request消息中指定“Range: b ...

随机推荐

  1. adb 工具学习

    adb (android debug bridge)简单介绍: 1.adb 是 Android SDK中所带工具.使用adb,可以在PC上操作Android设备或者模拟器 2.主要功能有: 将本地ap ...

  2. Javascript函数声明与函数表达式

    在定义函数时,我们一般使用下面这两种方法: 使用函数声明定义: function sum (a, b) { return a + b; } 使用函数表达式定义: var sum = function ...

  3. 转--android Toast大全(五种情形)建立属于你自己的Toast

    Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...

  4. JAVA 处理程序异常,(try、catch、finally),(thorws)

    一.try语句: try{//这里写可能出现异常的程序} catch(Exception e){//这里写如果出现异常怎么处理的程序} 二.throws语句 语法:函数方法()  throws Exc ...

  5. qt上用opencv显示摄像头视频

    参考:http://blog.csdn.net/augusdi/article/details/8865541 代码如下: 注意,要在ui界面上放置一个“Vertical Layout”控件,调整到合 ...

  6. LInux SSH远程文件/目录传输命令scp(转载)

    From:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS是经常会在不同VPS间相互备份数据或转移数据.一般情况下VPS都已安装了Ngnix或者 ...

  7. linux下shell脚本学习

    在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ...

  8. APPDelegate----launchOptions启动类型

    IOS 中的 AppDelegate.m/h 文件是很重要的呢,因为它是对 Application 的整个生命周期进行管理的. 先明白,每个iPhone应用程序都有一个UIApplication,UI ...

  9. 第6章 System V消息队列

    6.1 概述 System V消息队列在内核中是list存放的,头结点中有2个指针msg_first 和msg_last.其中每个节点包含:下个节点地址的指针.类型.长度.数据等. 6.2 函数 6. ...

  10. ASPxGridView改变列颜色

    protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs ...