or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Python實作課程
第六堂課 : MineSweeper
2022 / 6 / 10
Tony
遊戲介紹
踩地雷,是一款單人或者多人的電腦遊戲。遊戲目標是找出所有沒有地雷的方格,完成遊戲;
要是按了有地雷的方格,遊戲失敗。
(From維基百科)
minesweeper.online
遊戲流程
如果沒有,一直向四面八方挖,直到每個邊界格子的周圍八格都至少有一顆地雷,並把數字標在格子上
開始寫Code
模板
一、Board class
我們先寫 Board class,完成__init__函式
make_new_board():
利用一個二微陣列創造一個空棋盤並且把炸彈隨機放在棋盤中
assign_values_to_board():
利用它知道每個格子周邊八格的地雷數
get_num_neighboring_bombs():
取得特定格子周圍的地雷數,透過已知的row跟column來得到周圍八格的座標
為了不要超出index的範圍,把row-1(+1)跟column-1(+1)與0和self.dim_size-1 兩兩做比較
dig():
挖格子的時候執行的函式,會回傳一個布林值表示是否挖到炸彈
透過遞迴滿足往外挖的功能
__str__():
這是一個特殊函式(跟__init__類似),當要把這個class的物件print出來時就會被呼叫,因此我們可以自訂要印出來的東西
模板已經幫你寫好了,可以理解一下上半部的Code
到這邊就完成Board class的所有東西了
現在回來寫play function
二、Play function
函式滿足四個步驟:
If沒有,連鎖挖直到周邊格子的周圍都有至少一顆地雷
2~4可以用迴圈完成
re.split(A, B):把B按照A的規則切開成不同的東西
A是一個正則表達式,偏複雜,有興趣可以研究一下
Code
最後完成main函式就結束了