Java 编写程序 创建一个游戏【5、6两章的内容】【第5章】
每次使用單邊大腦的時間不要太久, 連續使用左邊腦30 分鐘如同使用左臂 30 分鐘一樣,
周期性性地交換讓大腦兩側輪流休息。
左腦活動包括了循序漸進的工作,解決邏輯問題與分析;
右腦的活動包括了陰雨、創造性思考、模式匹配與可視化。?
?
【自頂行下的設計】【首先進行高層設計,判斷需要用到的類和方法】
【了解實現的流程】【專注于程序中出現的事物,并設想出需要哪些對象】【流程圖】
? ? ? ? ? ? ? ? ? ? ? ? ?【面向對象的方式思考;專注于程序中出現的“事物”,而不是“過程”】
【開始編寫程序時】【遵循良好的面向對象的原則,并且沒有讓單一的類執行過多的任務】
? ? ? ? ? ? ? ? ? ? ? ? ? ? 你要決定哪個類先創建出來?假設某個類需要用到多個類,又該從哪里開始?
Key Points:
? ? ? ? ? ?創建新的類的時候需要寫出下列3種東西:【偽代碼——>測試碼——>真實碼】:
? ? ? ? ? ? ? ? ? ? ? ? 常用偽代碼語句:Declare ?Make ?Compute ?Invoke ?Repeat/While ?If ? ? ? ?Get ? ? ? ? Set ? Print
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?聲明 ? ? ?定義 ? ?計算 ? ? ? ??調用 ? ? ?循環 ? ? ? ? ? ? ??判斷 ? ?獲得輸入 ??設置 ?打印
?
創建Java類:
- 找出類應該做的事
- 列出實例變量和方法
- 編寫方法的偽代碼
- 編寫方法的測試用程序
- 實現類
- 測試方法
- 除錯或者重新設計
- 不靠譜之 邀請辣妹參加慶功派對,如家??七天酒店來一發??
給你聚一個栗子
- 游戲目標:以最少的猜測次數打掉計算機安排的 dot com目標。
- 游戲規則:在7*7的網格上,創建3個達康公司的目標。命中3次,則擊沉。
- 游戲進行:【由于欠缺圖形接口的程序設計,所以在這一次的命令欄上進行。】
- ? ? ? ? ? ? ? ? 計算機要求你輸入所猜測的位置(如,A3,B2),計算機反饋hit或者miss或者sunk等回應。清除所有的戰艦之后,游戲結束。
?
- ?【列出實例變量和方法】
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?實例變量的聲明? ??Declare an int array to hold the location cells.Call it locationCell.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Declare an int to hold the numble of hits.Call it numofHits and set it to 0.
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??方法的聲明 ? ??Declare?a checkYourelf() method that ?takes a String for the user's guess ("1","3" ,etc), checks ?it. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?and?returns a result ?representing a "hit" ?"kill" ?"miss".
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Declare a setLocationCells() setter method that takes an int array(which has the three cell location as ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ints (2.3.4.etc))
- 【編寫方法的偽代碼】
? ? ? ? ? ? ? ? ? ? ? ?Method : String checkYourself(String userGuess)
? ? ? ? ? ? ? ? ? ? ? ?Get ?the user's guess as a string parameter
? ? ? ? ? ? ? ? ? ? ? ?Convert the guess to an int
? ? ? ? ? ? ? ? ? ? ? ??Repeat with each of the location cells in the int array?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// Compare the user guess to the location cell
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if the user guess to matches
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?increment the numbers of hits ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//find out if it was the last location cell;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if number of ?hits is 3
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return "kill"?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else return "hit"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?end if
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else return "miss"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? end if
? ? ? ? ? ? ? ? ? ? ? ? ? ? end repeat
? ? ? ? ? ? ? ? ? ? ? end method
?
? ? ? ? ? ? ? ? ? ? ?Method :?void?setLocationCells?(int[] cellLocation)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Get the cell location as an array parameter
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Assign the cell locations parameter to the cell locations instance variable
? ? ? ? ? ? ? ? ? ??end method
概念:極限編程(XP)?建議閱讀專門的書籍,以免一知半解。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 在編寫真正可用的方法之前,首先編寫出測試方法用的代碼。
- 【編寫方法的測試用程序?】:創建對象并加以測試,其實只測試某一個方法;實現并正確執行;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
?
轉載于:https://www.cnblogs.com/Chuichui233/p/5153880.html
總結
以上是生活随笔為你收集整理的Java 编写程序 创建一个游戏【5、6两章的内容】【第5章】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu12中samba环境搭建与使
- 下一篇: Java 中的并发工具类