Hướng dẫn minesweeper-python code github - mineweeper-python code github

Permalink

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

Hậu thế random
ClassBoardsPot (đối tượng): boardSpot(object):
value=0 = 0
selected=False = False
mine=False = False
def__init __ (tự): __init__(self):
self.selected=False.selected = False
def__str __ (tự): __str__(self):
trở lại (boardspot.value) str(boardSpot.value)
Defismine (tự): isMine(self):
ifboardspot.value ==-1: boardSpot.value == -1:
trở lại True
returnFalse False
ClassboardClass (đối tượng): boardClass(object):
def__init __ (self, m_boardsize, m_nummines): __init__(self, m_boardSize, m_numMines):
self.board = [[boardspot () foriinrange (m_boardsize)] forjinrange (m_boardsize)]].board = [[boardSpot() for i in range(m_boardSize)] for j in range(m_boardSize)]
self.boardSize=m_boardSize.boardSize = m_boardSize
self.numMines=m_numMines.numMines = m_numMines
self.selectableSpots=m_boardSize*m_boardSize-m_numMines.selectableSpots = m_boardSize * m_boardSize - m_numMines
i=0 = 0
trong khi tôi i < m_numMines:
x = ngẫu nhiên.randint (0, self.boardsize-1) = random.randint(0, self.boardSize-1)
y = ngẫu nhiên.randint (0, self.boardsize-1) = random.randint(0, self.boardSize-1)
ifnotself.board [x] [y] .mine: not self.board[x][y].mine:
self.addmine (x, y).addMine(x, y)
i+=1 += 1
khác::
i-=1 -= 1
def__str __ (tự): __str__(self):
trở lại (boardspot.value) = " "
Defismine (tự): = "\n---"
ifboardspot.value ==-1: i in range(0, self.boardSize):
trở lại += " | " + str(i)
divider+="----" += "----"
returnFalse += "\n"
returnString+=divider += divider
ClassboardClass (đối tượng): y in range(0, self.boardSize):
def__init __ (self, m_boardsize, m_nummines): += str(y)
self.board = [[boardspot () foriinrange (m_boardsize)] forjinrange (m_boardsize)]] x in range(0, self.boardSize):
trong khi tôi self.board[x][y].mine and self.board[x][y].selected:
x = ngẫu nhiên.randint (0, self.boardsize-1) += " |" + str(self.board[x][y].value)
y = ngẫu nhiên.randint (0, self.boardsize-1) self.board[x][y].selected:
ifnotself.board [x] [y] .mine: += " | " + str(self.board[x][y].value)
khác::
ReturnString = "" += " | "
chia = "\ n ---" += " |"
returnString+=divider += divider
foriinrange (0, self.boardsize): returnString
ReturnString+= "|"+str (i) addMine(self, x, y):
self.board[x][y].value=-1.board[x][y].value = -1
self.board[x][y].mine=True.board[x][y].mine = True
chia+= "\ n" i in range(x-1, x+2):
ifi>=0andi i >= 0 and i < self.boardSize:
foryinrange (0, self.boardsize): y-1 >= 0 and not self.board[i][y-1].mine:
self.board[i][y-1].value+=1.board[i][y-1].value += 1
ify+1 y+1 < self.boardSize and not self.board[i][y+1].mine:
self.board[i][y+1].value+=1.board[i][y+1].value += 1
trở lại+= str (y) x-1 >= 0 and not self.board[x-1][y].mine:
self.board[x-1][y].value+=1.board[x-1][y].value += 1
ifx+1 x+1 < self.boardSize and not self.board[x+1][y].mine:
self.board[x+1][y].value+=1.board[x+1][y].value += 1
Foxinrange (0, self.boardsize): makeMove(self, x, y):
self.board[x][y].selected=True.board[x][y].selected = True
self.selectableSpots-=1.selectableSpots -= 1
ifelf.board [x] [y] .mineandself.board [x] [y] .Selected: self.board[x][y].value == -1:
returnFalse False
ClassboardClass (đối tượng): self.board[x][y].value == 0:
chia+= "\ n" i in range(x-1, x+2):
ifi>=0andi i >= 0 and i < self.boardSize:
foryinrange (0, self.boardsize): y-1 >= 0 and not self.board[i][y-1].selected:
trở lại+= str (y).makeMove(i, y-1)
ify+1 y+1 < self.boardSize and not self.board[i][y+1].selected:
Foxinrange (0, self.boardsize):.makeMove(i, y+1)
ifelf.board [x] [y] .mineandself.board [x] [y] .Selected: x-1 >= 0 and not self.board[x-1][y].selected:
ReturnString+= "|"+str (self.board [x] [y] .value).makeMove(x-1, y)
ifx+1 x+1 < self.boardSize and not self.board[x+1][y].selected:
elifelf.board [x] [y] .Selected:.makeMove(x+1, y)
trở lại True
khác::
trở lại True
returnFalse hitMine(self, x, y):
returnself.board[x][y].value==-1 self.board[x][y].value == -1
ClassboardClass (đối tượng): isWinner(self):
returnself.selectableSpots==0 self.selectableSpots == 0
def__init __ (self, m_boardsize, m_nummines):
self.board = [[boardspot () foriinrange (m_boardsize)] forjinrange (m_boardsize)]] playGame():
trong khi tôi = int(input("Choose the Width of the board: "))
x = ngẫu nhiên.randint (0, self.boardsize-1) = int(input("Choose the number of mines: "))
gameOver=False = False
winner=False = False
y = ngẫu nhiên.randint (0, self.boardsize-1) = boardClass(boardSize, numMines)
ifnotself.board [x] [y] .mine: not gameOver:
self.addmine (x, y)(Board)
khác:("Make your move:")
ReturnString = "" = int(input("x: "))
chia = "\ n ---" = int(input("y: "))
foriinrange (0, self.boardsize):.makeMove(x, y)
ReturnString+= "|"+str (i) = Board.hitMine(x, y)
chia+= "\ n" Board.isWinner() and gameOver == False:
gameOver=True = True
winner=True = True
self.addmine (x, y)(Board)
khác: winner:
ReturnString = ""("Congratulations, You Win!")
khác::
ReturnString = ""("You hit a mine, Game Over!")
chia = "\ n ---"()