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. linux free命令(转载)

    来源:http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存 ...

  2. Java队列集合的性能测试

    同时开10个线程存入和取出100万的数据,结论如下: DoubleBufferedQueue < ConcurrentLinkedQueue < ArrayBlockingQueue &l ...

  3. linux下shell脚本学习

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

  4. WPF NotifyIcon and Taskbar 任务栏示例

    Demo Source 转自:http://www.codeproject.com/Articles/36788/WPF-XAML-NotifyIcon-and-Taskbar-System-Tray ...

  5. Java中的代码块

    代码块 普通代码块 构造代码块 静态块 同步代码块 普通代码块 为了在方法里面编写过多的变量,防止变量重复,可以用代码块进行隔离. package org.lyk.main; public class ...

  6. 光流算法:Brox算法

    参考论文:1. High Accuracy Optical Flow Estimation Based on a Theory for Warping, Thomas Box, ECCV20042. ...

  7. esriSRProjCSType Constants

    ArcGIS Developer Help  (Geometry)     esriSRProjCSType Constants See Also esriSRProjCS2Type Constant ...

  8. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  9. Linux Hugepage ,AMM及 USE_LARGE_PAGES for oracle 11G(转载)

    1.  Hugepage基本概念     系统进程是通过虚拟地址访问内存,但是CPU必须把它转换成物理内存地址才能真正访问内存.为了提高这个转换效率,CPU会缓存最近的“虚拟内存地址和物理内存地址”的 ...

  10. 清理SQL Server服务器名称列表

    SQL2008: C:\Users\TQ\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin SQ ...