码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-06-26 15:21:46      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   tar   color   

  1 background_image_filename = blackground.png
  2 black_mouse_image_filename = black.png
  3 white_mouse_image_filename = white.png
  4 restart_icon_filename=restart.png
  5 pingju_icon_filename=pingju.jpg
  6 win_icon_filename=win.jpg
  7 lose_icon_filename=lose.jpg
  8 
  9 import pygame
 10 import time
 11 import sys
 12 
 13 from pygame.locals import *
 14 from sys import exit
 15 
 16 HIGHLIGHTCOLOR = (255,255,255)
 17 
 18 def AI(pp):
 19     #status is now white and black key in the board
 20     #taozi = white
 21     #computer = black
 22 
 23     num = 0
 24     x = 0
 25     y = 0
 26 
 27     if pp[1][1]==none:
 28         return 4
 29 
 30     
 31     for i in range(0,3):
 32         for j in range(0,3):
 33             if pp[i][j]==none:
 34                 pp[i][j]=white
 35                 cnt=0
 36                 
 37                 tx=i
 38                 ty=j
 39                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 40                     cnt=cnt+1
 41                     tx=tx+1
 42                 tx=i
 43                 ty=j
 44                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 45                     cnt=cnt+1
 46                     tx=tx-1
 47 
 48                 if cnt > num:
 49                     num = cnt
 50                     x=i
 51                     y=j
 52 
 53                 cnt=0
 54                 tx=i
 55                 ty=j
 56                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 57                     cnt=cnt+1
 58                     ty=ty+1
 59                 tx=i
 60                 ty=j
 61                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 62                     cnt=cnt+1
 63                     ty=ty-1
 64 
 65                 if cnt > num:
 66                     num = cnt
 67                     x=i
 68                     y=j
 69 
 70                 cnt=0
 71                 tx=i
 72                 ty=j
 73                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 74                     cnt=cnt+1
 75                     tx=tx+1
 76                     ty=ty+1
 77                 tx=i
 78                 ty=j
 79                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 80                     cnt=cnt+1
 81                     tx=tx-1
 82                     ty=ty-1
 83 
 84                 if cnt > num:
 85                     num = cnt
 86                     x=i
 87                     y=j
 88 
 89                 cnt=0
 90                 tx=i
 91                 ty=j
 92                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 93                     cnt=cnt+1
 94                     tx=tx+1
 95                     ty=ty-1
 96                 tx=i
 97                 ty=j
 98                 while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]==white:
 99                     cnt=cnt+1
100                     tx=tx-1
101                     ty=ty+1
102 
103                 if cnt > num:
104                     num = cnt
105                     x=i
106                     y=j
107 
108 
109                 pp[i][j]=none
110                     
111 
112     
113     return x*3+y
114 
115 def main():
116     
117     pygame.init()
118     screen = pygame.display.set_mode((800,500),0,32)
119     pygame.display.set_caption("you can never win!")
120     background = pygame.image.load(background_image_filename).convert()
121     mouse_cursor = pygame.image.load(white_mouse_image_filename).convert_alpha()
122 
123     
124     white_sign = pygame.image.load(white_mouse_image_filename).convert_alpha()
125     restart    = pygame.image.load(restart_icon_filename).convert_alpha()
126     pingju     = pygame.image.load(pingju_icon_filename).convert_alpha()
127     winicon    = pygame.image.load(win_icon_filename).convert_alpha()
128     lose       = pygame.image.load(lose_icon_filename).convert_alpha()
129 
130     
131     
132     black_sign = pygame.image.load(black_mouse_image_filename).convert_alpha()
133     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] ]
134     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] ]
135     positionColor=[ none,none,none,none,none,none,none,none,none]
136 
137     whoIsTurn = [computer,taozi]
138     turnKey   = 1   # 1 means taozi
139                     # 0 means computer
140     cnt = 0
141     pp=[ [none,none,none],
142          [none,none,none],[none,none,none]]
143     #pp = [ [
144     win=0
145     while 1:
146         newx =0
147         newy =0
148     
149         color = white
150         color1 = black
151         color2 = none
152         for event in pygame.event.get():
153             if event.type == QUIT:
154                 pygame.quit()
155                 sys.exit()
156             elif event.type == MOUSEBUTTONDOWN :
157                 newx,newy = pygame.mouse.get_pos()
158                 for i in position:
159                     if (newx>=i[0] and newx <= i[2]) and (newy >= i[1] and newy <= i[3]):
160                         j = i[4]
161                         if positionColor[j]!=none:
162                             continue
163                         positionColor[j]= color
164                         #positionColor[j+1]= color
165                         for ii in range(0,3):
166                             for jj in range(0,3):
167                                 pp[ii][jj]=positionColor[ii*3+jj]
168 
169                         # AI begin
170 
171                         res = AI( pp )
172                         positionColor[res]=color1
173 
174                         # AI finished
175                         
176                         cnt=cnt+1
177                 if newx>=674 and newx<=774 and newy>=396 and newy<= 496:
178                     for i in range(0,3):
179                         for j in range(0,3):
180                             positionColor[i*3+j]=none
181                             pp[i][j]=none
182                             win=0
183                    
184         screen.blit(background,(0,0))
185         screen.blit(restart,(674,396))
186         #x,y = pygame.mouse.get_pos()
187         #x -= mouse_cursor.get_width()/2
188         #y -= mouse_cursor.get_height()/2
189         #if win==0:
190         #    screen.blit(mouse_cursor,(x,y))
191 
192         #x,y = pygame.mouse.get_pos()
193         for i in position:
194             x,y = pygame.mouse.get_pos()
195             if (x>=i[0] and x <= i[2]) and (y >= i[1] and y <= i[3]):
196                 x-=26
197                 y-=26
198                 if positionColor[ int(i[4]) ]==none:
199                 #pygame.draw.rect(screen,HIGHLIGHTCOLOR,(x,y,52,52),0)
200                     screen.blit(mouse_cursor,(x,y))
201                     print("done it")
202                 else:
203                     print("color= ",positionColor[i[4]])
204 
205                         
206         for ii in [0,1,2,3,4,5,6,7,8]:
207             if positionColor[ii] != none:
208                 if positionColor[ii] == white:
209                     #print("write white_sign")
210                     screen.blit(white_sign,positionMid[ii] )
211                 else :
212                     #print("write black_sign")
213                     screen.blit(black_sign,positionMid[ii])
214         
215 
216         for i in range(0,3):
217             for j in range(0,3):
218                 pp[i][j]=positionColor[i*3+j]
219 
220 
221     
222         for i in range(0,3):
223             if pp[i][0]==pp[i][1] and pp[i][0]==pp[i][2] and pp[i][0]!=none:
224                 win=1
225                 winner=pp[i][0]
226             if pp[0][i]==pp[1][i] and pp[0][i]==pp[2][i] and pp[0][i]!=none:
227                 win=1
228                 winner=pp[0][i]
229         if pp[0][0]==pp[1][1] and pp[0][0]==pp[2][2] and pp[0][0]!=none:
230             win=1
231             winner=pp[0][0]
232         if pp[2][0]==pp[1][1] and pp[1][1] == pp[0][2] and pp[1][1]!=none:
233             win =1
234             winner=pp[1][1]
235         if win==0:
236             cntcolor=0
237             for i in range(0,3):
238                 for j in range(0,3):
239                     if pp[i][j]!=none:
240                         cntcolor=cntcolor+1
241             if cntcolor==9 :
242                 win =1
243                 winner=pingju
244 
245         if win==1 :
246             #print("someone win!")
247             if winner==white:
248                 screen.blit(winicon,(50,20))
249             elif winner==pingju:
250                 screen.blit(pingju,(100,100))
251             else:
252                 screen.blit(lose,(100,100))
253             #print("winner = ",winner)
254             #time.sleep(10000)
255                 
256 
257         
258         pygame.display.update()
259 
260 
261 
262 if __name__ == __main__:
263     main()

 

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

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

标签:style   class   blog   code   tar   color   

原文地址:http://www.cnblogs.com/symons1992/p/3809065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!