Subtitles section Play video
Dawn and the train are both breaking when Ethic and Hedge arrive in the woods.
[用程式設計的方式思考]
The adventurers have recovered the first artifact—
[地點(上):八達圍] [地點(下):198 森林]
the Node of Power—
[第五集:藝術家]
and have come to the 198forest in search of the second.
當艾希克和海吉搭乘火車 到達樹林時,天剛破曉。
Here they're welcomed by the director of the colony, Octavia.
這兩位冒險家已經取得了 第一件工藝品——
She established this treehouse sanctuary
力量球,
after the robots freed everyone from having to work.
現在來到 198 森林尋找第二個。
It was meant to be a haven where people could follow their passions,
在這裡迎接他們的是 殖民地領導人奧特薇雅。
take up crafts, and find fulfillment.
在機器人出現讓大家 可以免於工作之後,
Which they did… at first.
她建立了這個樹屋聖所。
Some years ago everyone forgot the point.
本意是要成為一個避風港, 讓大家能追求自己的熱情,
They abandoned arts and crafts
學習工藝,找到滿足感。
and instead just painted and exhibited pictures of themselves
他們也做到了……一開始時。
over, and over, and over.
幾年前,大家忘了重點。
The location of the second artifact is no secret;
他們拋棄了藝術和工藝,變成只是
it's in a tower, guarded by a garrison of bots,
繪製和展示他們自己的圖像,
a bottomless ravine, and who knows what other traps.
一而再,再而三。
As soon as the tower went up with the node inside,
第二件工藝品的所在並不是秘密;
human communication across the land went dark.
它在一個由機器人 警衛隊看守的塔中,
Octavia's been after it for years,
那是個不見底的深谷, 天知道還有什麼其他陷阱。
but try as she might, the defenses thwart her.
隨著塔帶著裡面的 力量球向上升起,
In order to even get to the tower, the team will need a distraction.
整片土地上的人類 交通網都陷入黑暗。
Octavia has an idea:
數年來,奧特薇雅都想取得它,
stir up the people through some well-intentioned vandalism.
但她怎麼努力也無法 通過塔的防禦。
The residents' paintings are all squares that come in different sizes,
要到達塔的所在,這個團隊 需要分散對方的注意力。
all an odd number of pixels across.
奧特薇雅有個點子:
Helper-bots pick up the finished portraits
用善意的破壞行為 來引起大家的騷動。
and hang them in public places for everyone to admire.
居民的畫作都是方形的,尺寸不一。
There's a slim margin of time when Hedge can access the paintings.
邊長的像素數目都是奇數。
If he were to deface each one with an X,
助手機器人會去取得完成的畫像,
the people would blame the helper-bots,
將它們公開掛出來給大家欣賞。
creating just the distraction the team needs.
有一個小小的空檔, 海吉可以接觸到那些畫作。
If only it were so easy.
如果他能把每張畫 都畫上一個大叉叉,
Hedge can't just paint an X—
大家就會怪罪助手機器人,
his painting processor requires very specific instructions.
就能幫團隊達到 分散注意力的效果。
Treating the paintings as square grids,
事實上沒那麼簡單。
he can fill in one pixel, or little square, at a time.
不能只叫海吉去畫個大叉叉,
He can move forwards and make 90 degree turns over the canvas,
他的繪圖處理器需要 非常明確的指令。
but can't move diagonally.
把畫作視為是方形的格子,
How does Ethic program Hedge to paint an X over each portrait?
他一次能填滿一個像素 或一個小方格。
Pause now to figure it out for yourself.
在畫布上,他能向前移動, 也可以做九十度的轉彎,
Here's a hint.
但不能走斜對角。
Try drawing a square grid like this,
艾希克要如何設定海吉的程式,
and simulating Hedge's path over it.
才能在每張畫像上 都畫一個大叉叉?
What patterns can you find to guide him?
[若你想要嘗試解題,請在此暫停]
Pause now to figure it out for yourself.
[題目規則一、二、三]
The challenge here is to craft a set of instructions
提示如下。
that will work for any square grid.
試試看畫一個像這樣的方格,
Fortunately, one of the strengths of programming
並模擬海吉在上面的路徑。
is the flexibility to solve not just one problem,
你能找出什麼模式來引導他?
but a whole class of them all at once.
[若你想要嘗試解題,請在此暫停]
It often helps to start with one case, and work towards the general.
這裡的挑戰,是要 設計出一組指令,
Let's say we had this square.
能夠用在任何方格上。
Hedge can measure the length of its sides and store that number as a variable.
幸運的是,寫程式的優點之一
Now, what we need is a plan for how Hedge will paint an X,
就是有彈性,可以 不只解一個問題,
pixel by pixel.
同類的問題都能解決。
There's more than one right answer for how to do this;
有個不錯的方式是 先處理一個,再普及到整體。
let's look at two.
比如,我們有這個方形。
First, what if Hedge went row by row, like a typewriter?
海吉可以測量它的邊長, 把這個數字當作變數儲存下來。
If it's a 9 pixel by 9 pixel painting,
我們現在需要一個計畫, 規劃海吉要如何畫出大叉叉。
in the first row he'd paint, skip 7, and then paint again.
一次畫一個像素。
In the second row he'd skip the first, paint, skip 5, and paint.
要做到這件事, 正確答案不只一個;
And so on.
咱們來看兩種做法。
The pattern here is that for each row the pixels skipped at the beginning
首先,如果海吉像打字機 一樣逐行進行呢?
go up by one,
如果一幅畫的長寬都是九像素,
and the pixels skipped in the middle go down by 2.
在第一行,他會先上色, 跳過七格,接著再上色。
Things get more complicated when Hedge reaches the center.
在第二行,他會跳過第一格, 上色,跳過五格,再上色。
Here there's a row with just one pixel painted.
以此類推。
Then the whole thing reverses—
這裡的模式是,每跳到下一行
the number of pixels skipped goes down by one each time on the left,
一開始要跳過的像素 數目就要多一個,
and up by two each time in the middle.
每行中間要跳過的像素 數目則會少兩個。
Instructing Hedge to do this with a series of loops will work
海吉越接近中心時, 情況就會越複雜。
and is a perfectly fine solution.
在這裡,這一行只要 將一個像素上色。
The main drawback is that this requires quite a bit of logic—
接著整個模式就會反轉——
knowing what to do in the middle, when to reverse the process,
每換一行,左側要跳過的 像素數目會少一個,
and exactly how to reverse it.
中間要跳過的像素 數目則會多兩個。
So how might we approach this
用一系列的迴圈可以 指示海吉做到這件事,
so that the logic remains consistent from start to finish?
且這個解決方案完全沒問題。
The key insight is to look at a grid as a series of concentric squares.
主要的缺點是,這個做法 需要相當的邏輯——
Each square follows the same pattern—
要知道在中間時該怎麼做、 何時要反轉過程,
painted pixels in the corners, and unaltered pixels in between.
以及要如何反轉過程。
So if we can figure out a way to paint one nested square,
我們要怎麼在邏輯 不變的情況下做到這件事?
transition to the next, and repeat, we can paint them all.
關鍵點在於要把一個方格 視為是一系列的同心方形。
Painting the outermost one is easy.
每個方形都遵循同樣的模式——
Start in a corner and paint that pixel.
將角落的像素上色, 中間的像素則不做改變。
If we call the length of the painting n,
如果我們能想辦法 將角落的方形上色,
fly forward n minus 1 spaces.
轉換到下一個,再重覆, 就可以全部上色完成。
Paint another pixel, and turn right.
將最外面的方形上色很容易。
Now do the whole thing again… and again.
從一個角落開始, 將那個像素上色。
Now move forward one less space, turn right, fly forward once,
如果我們把畫作的長度叫做 n,
and Hedge will be in the next concentric square
就是向前飛躍 n 減一格。
and ready to repeat the whole process.
將那個像素上色,接著向右轉。
Each square is n minus 2 pixels smaller than the last in length and width,
再把整個過程做一次, 然後再做一次。
and we can follow this spiral pattern all the way to the center
現在向前進的距離減少一格, 向右轉,向前飛躍一次,
with a loop and a variable that tracks how far Hedge should fly.
接著海吉就會進入 下一個同心方形,
Is one of these methods better than the other?
準備繼續重覆同樣的流程。
It really depends on what you value.
每一個方形的長寬都會比 上一個方形小 n 減二個像素,
The strength of the spiral is the simplicity of finding a pattern
我們可以循著這個 螺旋模式一路到中心點,
and reusing the same logic from start to finish.
只要用一個迴圈和一個變數 來追蹤海吉應該飛躍的距離即可。
The advantage of the typewriter approach
這些方法有高下嗎? 這就要看你重視什麼了。
is that it's a more generalized solution,
螺旋法的優點是很容易找到模式,
meaning it can be adapted much more simply to fill in any pattern.
同樣的邏輯可以 從頭到尾一直重覆使用。
For Ethic's sake, either will do just fine.
打字機法的優點在於 它是較一般化的解決方案,
So here's what happens.
意思就是,很容易就可以 把它改寫套用在任何模式上。
Hedge rapidly defaces all of the portraits.
就艾希克的情況來說, 兩種方法皆可行。
And within moments
所以,發生的狀況是這樣:
cries of anguish break out all over the forest.
海吉很快地破壞了所有的畫像。
The garrison guarding the tower abandon their posts
沒多久,整個森林各處 都可以聽見憤怒的吼叫。
to calm the agitated people,
看守著塔的警衛隊 離開了它們的崗位,
and Ethic, Hedge, and Octavia slip through—
去安撫激動的民眾,
and nearly slip into the depths of the gorge
而艾西克、海吉, 及奧特薇雅便溜過去——
standing between them and the tower.
差一點點滑落 他們和塔之間的深谷。