DAY 5
1

## Julia (5) -- Julia的標準輸出

• 加入新的數字
• 起始化2048的盤面
• 計算分數
• 決定遊戲的終點(2048達成或是game over)

## Julia的標準輸出

``````julia> @printf("%3.2f",3.141615926)
3.14

julia> @printf("%3.4E",3.141615926)
3.1416E+00
``````

NB: printf()和sprintf()的前面要加上@。@printf()和@sprintf在julia裡面並不是函式，而是巨集(macro)。julia提供了一個很方便的指令macroexpand()來檢視這個巨集的定義:

``````julia> macroexpand(:(@printf("%3.2f",3.141615926)))
quote
#130#out = Base.Printf.STDOUT
#131###x#7831 = 3.141615926
local #136#neg, #135#pt, #134#len, #129#exp, #132#do_out, #133#args
if Base.Printf.isfinite(#131###x#7831)
(#132#do_out,#133#args) = Base.Printf.fix_dec(#130#out,#131###x#7831,"",3,2,'f')
if #132#do_out
(#134#len,#135#pt,#136#neg) = #133#args
#136#neg && Base.Printf.write(#130#out,'-')
Base.Printf.print_fixed(#130#out,2,#135#pt,#134#len)
end
else
Base.Printf.write(#130#out,begin  # printf.jl, line 143:
if Base.Printf.isnan(#131###x#7831)
"NaN"
else
if (#131###x#7831 Base.Printf.< 0)
"-Inf"
else
"Inf"
end
end
end)
end
Base.Printf.nothing
end
``````

``````function printBoard(board)

boardStr=""
for rowIdx=1:size(board,1)
for colIdx=1:size(board,2)
tmpStr=@sprintf("%6d",board[rowIdx,colIdx])
boardStr=string(boardStr,tmpStr)
end
boardStr=string(boardStr,"\n")
end
return boardStr
end
``````

``````function testprintBoard()
println(printBoard([0 0 0 0;0 0 0 0;0 0 0 0;0 0 0 0]))
println(printBoard([1024 0 0 0;0 1024 0 0;0 0 1024 0; 0 0 0 1024]))
end
``````