




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第使用python實(shí)現(xiàn)單人AI掃雷游戲def__eq__(self,other):
returnself.cells==other.cellsandself.count==other.count
def__str__(self):
returnf"{self.cells}={self.count}"
defknown_mines(self):
返回self.cells中已知為地雷的所有單元格的集合。
defknown_mines(self):
iflen(self.cells)==self.count:
returnself.cells
返回self.cells中已知安全的所有單元格的集合。
defknown_safes(self):
ifself.count==0:
returnself.cells
鑒于已知單元格是地雷,更新內(nèi)部知識(shí)表示。
defmark_mine(self,cell):
ifcellinself.cells:
self.cells.discard(cell)
self.count-=1
鑒于已知單元格是安全的,更新內(nèi)部知識(shí)表示。
defmark_safe(self,cell):
ifcellinself.cells:
self.cells.discard(cell)
掃雷游戲玩家
classMinesweeperAI():
def__init__(self,height=8,width=8):
#設(shè)置初始高度和寬度
self.height=height
self.width=width
#跟蹤點(diǎn)擊了哪些單元格
self.moves_made=set()
#跟蹤已知安全或地雷的細(xì)胞
self.mines=set()
self.safes=set()
#關(guān)于已知為真游戲的句子列表
self.knowledge=[]
將一個(gè)單元格標(biāo)記為地雷,并更新所有知識(shí)以將該單元格也標(biāo)記為地雷。
defmark_mine(self,cell):
self.mines.add(cell)
forsentenceinself.knowledge:
sentence.mark_mine(cell)
將一個(gè)單元格標(biāo)記為安全,并更新所有知識(shí)以將該單元格也標(biāo)記為安全。
defmark_safe(self,cell):
self.safes.add(cell)
forsentenceinself.knowledge:
sentence.mark_safe(cell)
用于獲取所有附近的單元格
defnearby_cells(self,cell):
cells=set()
foriinrange(cell[0]-1,cell[0]+2):
forjinrange(cell[1]-1,cell[1]+2):
if(i,j)==cell:
continue
if0=iself.heightand0=jself.width:
cells.add((i,j))
returncells
當(dāng)掃雷板告訴我們,對(duì)于給定的安全單元,有多少相鄰單元中有地雷時(shí)調(diào)用。
這個(gè)功能應(yīng)該:
1)將單元格標(biāo)記為已進(jìn)行的移動(dòng)
2)將單元格標(biāo)記為安全
3)根據(jù)cell和count的值在AI的知識(shí)庫(kù)中添加一個(gè)新句子
4)如果可以根據(jù)AI的知識(shí)庫(kù)得出結(jié)論,則將任何其他單元格標(biāo)記為安全或地雷
5)如果可以從現(xiàn)有知識(shí)中推斷出任何新句子,則將其添加到AI的知識(shí)庫(kù)中
defadd_knowledge(self,cell,count):
self.moves_made.add(cell)
#標(biāo)記單元格安全
ifcellnotinself.safes:
self.mark_safe(cell)
#獲取所有附近的單元格
nearby=self.nearby_cells(cell)
nearby-=self.safes|self.moves_made
new_sentence=Sentence(nearby,count)
self.knowledge.append(new_sentence)
new_safes=set()
new_mines=set()
forsentenceinself.knowledge:
iflen(sentence.cells)==0:
self.knowledge.remove(sentence)
else:
tmp_new_safes=sentence.known_safes()
tmp_new_mines=sentence.known_mines()
iftype(tmp_new_safes)isset:
new_safes|=tmp_new_safes
iftype(tmp_new_mines)isset:
new_mines|=tmp_new_mines
forsafeinnew_safes:
self.mark_safe(safe)
formineinnew_mines:
self.mark_mine(mine)
prev_sentence=new_sentence
new_inferences=[]
forsentenceinself.knowledge:
iflen(sentence.cells)==0:
self.knowledge.remove(sentence)
elifprev_sentence==sentence:
break
elifprev_sentence.cells=sentence.cells:
inf_cells=sentence.cells-prev_sentence.cells
inf_count=sentence.count-prev_sentence.count
new_inferences.append(Sentence(inf_cells,inf_count))
prev_sentence=sentence
self.knowledge+=new_inferences
defmake_safe_move(self):
返回一個(gè)安全的單元格以在掃雷板上選擇。必須知道該移動(dòng)是安全的,而不是已經(jīng)做出的移動(dòng)。
該函數(shù)可以使用self.mines、self.safes和self.moves_made中的知識(shí),但不應(yīng)修改任何這些值。
defmake_safe_move(self):
safe_moves=self.safes.copy()
safe_moves-=self.moves_made
iflen(safe_moves)==0:
returnNone
returnsafe_moves.pop()
defmake_random_move(self):
返回在掃雷板上進(jìn)行的移動(dòng)。應(yīng)該在以下單元格中隨機(jī)選擇:
1)尚未被選中
2)不知道是地雷
defmake_random_move(self):
iflen(self.moves_made)==56:
returnNone
random_move=random.randrange(self.height),random.randrange(self.height)
not_safe_moves=self.moves_made|self.mines
whilerandom_moveinnot_safe_moves:
random_move=random.randrange(self.height),random.randrange(self.height)
returnrandom_move
設(shè)置runner.py運(yùn)行程序
顏色
BLACK=(0,0,0)
GRAY=(180,180,180)
WHITE=(255,255,255)
創(chuàng)建游戲
pygame.init()
size=width,height=600,400
screen=pygame.display.set_mode(size)
字體
字體可以在自己電腦中C:\Windows\Fonts的位置選擇自己喜歡的復(fù)制到項(xiàng)目中assets/fonts目錄下即可,我用的是楷體
OPEN_SANS="assets/fonts/simkai.ttf"
smallFont=pygame.font.Font(OPEN_SANS,20)
mediumFont=pygame.font.Font(OPEN_SANS,28)
largeFont=pygame.font.Font(OPEN_SANS,40)
計(jì)算面板尺寸
BOARD_PADDING=20
board_width=((2/3)*width)-(BOARD_PADDING*2)
board_height=height-(BOARD_PADDING*2)
cell_size=int(min(board_width/WIDTH,board_height/HEIGHT))
board_origin=(BOARD_PADDING,BOARD_PADDING)
添加圖片
這里我們只用了兩張圖,一個(gè)是地雷,一個(gè)是用來(lái)標(biāo)記地雷的旗幟
flag=pygame.image.load("assets/images/flag.png")
flag=pygame.transform.scale(flag,(cell_size,cell_size))
mine=pygame.image.load("assets/images/mine.png")
mine=pygame.transform.scale(mine,(cell_size,cell_size))
創(chuàng)建游戲和AI代理
game=Minesweeper(height=HEIGHT,width=WIDTH,mines=MINES)
ai=MinesweeperAI(height=HEIGHT,width=WIDTH)
跟蹤顯示的單元格、標(biāo)記的單元格以及是否被地雷擊中
revealed=set()
flags=set()
lost=False
最初顯示游戲說(shuō)明
instructions=True
whileTrue:
#檢查游戲是否退出
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
sys.exit()
screen.fill(BLACK)
#顯示游戲說(shuō)明
ifinstructions:
#標(biāo)題
title=largeFont.render("海擁|掃雷",True,WHITE)
titleRect=title.get_rect()
titleRect.center=((width/2),50)
screen.blit(title,titleRect)
#Rules
rules=[
"單擊一個(gè)單元格以顯示它",
"右鍵單擊一個(gè)單元格以將其標(biāo)記為地雷",
"成功標(biāo)記所有地雷以獲勝!"
fori,ruleinenumerate(rules):
line=smallFont.render(rule,True,WHITE)
lineRect=line.get_rect()
lineRect.center=((width/2),150+30*i)
screen.blit(line,lineRect)
#開(kāi)始游戲按鈕
buttonRect=pygame.Rect((width/4),(3/4)*height,width/2,50)
buttonText=mediumFont.render("開(kāi)始游戲",True,BLACK)
buttonTextRect=buttonText.get_rect()
buttonTextRect.center=buttonRect.center
pygame.draw.rect(screen,WHITE,buttonRect)
screen.blit(buttonText,buttonTextRect)
#檢查是否點(diǎn)擊播放按鈕
click,_,_=pygame.mouse.get_pressed()
ifclick==1:
mouse=pygame.mouse.get_pos()
ifbuttonRect.collidepoint(mouse):
instructions=False
time.sleep(0.3)
pygame.display.flip()
continue
畫(huà)板
cells=[]
foriinrange(HEIGHT):
row=[]
forjinrange(WIDTH):
#為單元格繪制矩形
rect=pygame.Rect(
board_origin[0]+j*cell_size,
board_origin[1]+i*cell_size,
cell_size,cell_size
pygame.draw.rect(screen,GRAY,rect)
pygame.draw.rect(screen,WHITE,rect,3)
#如果需要,添加地雷、旗幟或數(shù)字
ifgame.is_mine((i,j))andlost:
screen.blit(mine,rect)
elif(i,j)inflags:
screen.blit(flag,rect)
elif(i,j)inrevealed:
neighbors=smallFont.render(
str(game.nearby_mines((i,j))),
True,BLACK
neighborsTextRect=neighbors.get_rect()
neighborsTextRect.center=rect.center
screen.blit(neighbors,neighborsTextRect)
row.append(rect)
cells.append(row)
AI移動(dòng)按鈕
aiButton=pygame.Rect(
(2/3)*width+BOARD_PADDING,(1/3)*height-50,
(width/3)-BOARD_PADDING*2,50
buttonText=mediumFont.render("AI移動(dòng)",True,BLACK)
buttonRect=buttonText.get_rect()
buttonRect.center=aiButton.center
pygame.draw.rect(screen,WHITE,aiButton)
screen.blit(buttonText,buttonRect)
重置按鈕
resetButton=pygame.Rect(
(2/3)*width+BOARD_PADDING,(1/3)*height+20,
(width/3)-BOARD_PADDING*2,50
buttonText=mediumFont.render("重置",True,BLACK)
buttonRect=buttonText.get_rect()
buttonRect.center=resetButton.center
pygame.draw.rect(screen,WHITE,resetButton)
screen.blit(buttonText,buttonRect)
顯示文字
text="失敗"iflostelse"獲勝"ifgame.mines==flagselse""
text=mediumFont.render(text,True,WHITE)
textRect=text.get_rect()
textRect.center=((5/6)*width,(2/3)*height)
screen.blit(text,textRect)
move=None
left,_,right=pygame.mouse.get_pressed()
檢查右鍵單擊以切換標(biāo)記
ifright==1andnotlost:
mouse=pygame.mouse.get_pos()
foriinrange(HEIGHT):
forjinrange(WIDTH):
ifcells[i][j].collidepoint(mouse)and(i,j)notinrevealed:
if(i,j)inflags:
flags.remove((i,j))
else:
flags.add((i,j))
time.sleep(0.2)
elifleft==1:
mouse=pygame.mouse.get_pos()
如果單擊AI按鈕,則進(jìn)行AI移動(dòng)
ifaiButton.colli
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖南省天壹名校聯(lián)盟2025屆高三5月適應(yīng)性考試(生物)
- 停車(chē)場(chǎng)合同范例
- 醫(yī)療供應(yīng)鏈中區(qū)塊鏈的追溯應(yīng)用研究
- 辦公室醫(yī)療融合發(fā)展兒童健康成長(zhǎng)與科學(xué)補(bǔ)鈣研究
- led燈工程合同范例
- 高中物理選修3-5重要知識(shí)點(diǎn)總結(jié)模版
- 醫(yī)療團(tuán)隊(duì)協(xié)作中的溝通與信任建設(shè)
- 醫(yī)療行業(yè)數(shù)據(jù)透明度與質(zhì)量控制的平衡點(diǎn)
- 區(qū)塊鏈技術(shù)助力構(gòu)建高效的物流供應(yīng)鏈體系
- 醫(yī)療數(shù)據(jù)智能化處理技術(shù)及其實(shí)踐
- 江蘇省蘇州市昆山、太倉(cāng)、常熟、張家港市2023-2024學(xué)年七年級(jí)下學(xué)期語(yǔ)文期末試卷
- 中西文化鑒賞 知到智慧樹(shù)網(wǎng)課答案
- 外研版(2019)選擇性必修第二冊(cè)Unit 3 Times change!Understanding ideas 課件
- 銻礦濕法冶金新技術(shù)
- 湖北省武漢市2024屆高中畢業(yè)生四月調(diào)研考試數(shù)學(xué)試卷
- 煙草原料知識(shí)培訓(xùn)課件
- 糖尿病低血糖診治
- TCALC 003-2023 手術(shù)室患者人文關(guān)懷管理規(guī)范
- 塵肺病介紹與預(yù)防課件
- 車(chē)載香氛凈化系統(tǒng)行業(yè)市場(chǎng)調(diào)研數(shù)據(jù)分析報(bào)告2024
- 腦機(jī)接口總體愿景與關(guān)鍵技術(shù)研究報(bào)告
評(píng)論
0/150
提交評(píng)論