安卓大作業(yè)(共28頁)_第1頁
安卓大作業(yè)(共28頁)_第2頁
安卓大作業(yè)(共28頁)_第3頁
安卓大作業(yè)(共28頁)_第4頁
安卓大作業(yè)(共28頁)_第5頁
已閱讀5頁,還剩24頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上Android應(yīng)用程序開發(fā)大作業(yè)題 目: 五子棋游戲的設(shè)計與實現(xiàn) 專 業(yè): 網(wǎng)絡(luò)工程 學(xué) 號: 學(xué)生姓名: 朱一萍 指導(dǎo)教師: 齊兵輝 完成日期: 2015-11-25 專心-專注-專業(yè)目錄1開發(fā)背景每個人手機上都有手機游戲,上下班以及無聊閑暇時,游戲是人們娛樂的一種方式。Android平臺下的手機游戲更是受到顧客的青睞,手機游戲不僅玩起來比較方便,還有助于開發(fā)我們的思維,使大家在娛樂的同時也增長了見識,拓展了思維。五子棋對思維及協(xié)調(diào)能力有益,能使我們注意力集中,耐心也有明顯的提升,培養(yǎng)我們的邏輯思維能力,對智力及記憶力都很有幫助。2.需求分析2.1功能需求1.適合不

2、同階段的玩家(新手和熟手)2.玩家和手機對弈2.2性能需求軟件環(huán)境:1.硬件環(huán)境:PC機2Windows XP、Android 2.1 以上3.概要設(shè)計五子棋分三個模塊:開始游戲、設(shè)置、退出。1. 開始游戲:點擊進(jìn)入游戲2. 游戲設(shè)置:音效設(shè)置:開啟/關(guān)閉背景音樂、開啟/關(guān)閉音效難度設(shè)置:簡單,中等,較難3. 退出游戲關(guān)閉游戲4.棋盤、棋子的繪制本設(shè)計中棋盤的設(shè)置采用drawLine(float startX,float startY,float stopX,float stopY,Paint paint)畫線,參數(shù)一起始點的X軸位置,參數(shù)二起始點的Y軸位置,參數(shù)三終點的水平位置,參數(shù)四垂直位

3、置,最后的一個參數(shù)為Paint畫刷對象。本設(shè)計中棋子是用drawCircle在坐標(biāo)上繪制一個經(jīng)典幾何圖形。先判斷是否有棋,是黑子還是白子,然后判斷是那個坐標(biāo),然后調(diào)用drawCircle繪制棋子canvas.drawCircle(float,float,float,Paint)方法用于畫圓,前兩個參數(shù)代表圓心坐標(biāo),第三個參數(shù)為圓半徑,第四個參數(shù)是畫筆。5.游戲核心設(shè)計5.1手機選擇落子位置評估當(dāng)前棋局中,某個位置的得分。五子棋要贏,必然要有五個棋子在一起成線,那么我們就可以計算棋盤中每一個五格相連的線,一下稱之為五元組。一般情況(包括專業(yè)五子棋)下棋盤是15*15的。那么應(yīng)該是572個五元組。

4、同時,針對五元組中黑子和白子的數(shù)量(可以不考慮相對位置)的不同,給該五元組評不同的分。然后每一個位置的得分就是包含這個位置的所有五元組的得分之和。說明:當(dāng)五元組為空,分?jǐn)?shù)為7,不為零的原因是,還有跟糟的情況:五元組中既有黑子又有白子,五元組也就是無效了,這時才給0分。根據(jù)得到的評分,手機選出最佳位置,進(jìn)行落子。5.2判斷游戲的輸贏在判斷輸贏時,用到的一個巧妙的方法。從所周知,無論誰下棋贏了以后總會成五子連線,而成五子連線時,第五顆棋總是會著落在棋盤上,所以我們在判斷是否有五子連線時,并不是全盤掃描,而是在每著落一子后就掃描該棋子左右、上下、以及斜著的四個方向。如果掃描出來了那個方向上的棋子首先

5、到達(dá)五子,就返回給上級調(diào)用,由上級調(diào)用者作出相應(yīng)判斷。如果是其它子,如果某個方向上成了六子或六子以上時,不能算作它贏得了棋,而必是五子才能贏得了棋。如果是五子以下的棋子時,將要根局相應(yīng)棋子成子情況,作出相應(yīng)反應(yīng),如加權(quán)、減權(quán)等操作。6.運行截圖首界面:開始游戲:設(shè)置:判斷輸贏:7.核心代碼7.1首界面public class StartActivity extends ActivityOverrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);this.setCont

6、entView(R.layout.activity_start);Button button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener()Overridepublic void onClick(View arg0) / TODO Auto-generated method stubIntent intent1 = new Intent();intent1.setClass(StartActivity.this, MainActivity.class);st

7、artActivity(intent1);StartActivity.this.finish(););Button button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) / TODO Auto-generated method stubIntent intent2 = new Intent();intent2.setClass(StartActivity.this, settin

8、gActivity.class);startActivity(intent2);StartActivity.this.finish(););Button button3 = (Button) findViewById(R.id.button3);button3.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View arg0) / TODO Auto-generated method stubStartActivity.this.finish(););7.2棋盤、棋子的初始化等private

9、ChessType chessMap = new ChessTypeROWSCOLS;private static float PADDING = (float) (screenWidth) / (COLS - 1) / 2;private static float PADDING_LEFT = (float) (screenWidth) / (COLS - 1) / 2;private static float PADDING_TOP = (float) (screenHeight) / (ROWS - 1) / 2;private static float ROW_MARGIN = (sc

10、reenHeight - PADDING * 2)/ (ROWS - 1);private static float COL_MARGIN = (screenWidth - PADDING * 2)/ (COLS - 1);private static float MARGIN;public GameView(Context context) super(context);this.context = context;this.setBackgroundResource(R.drawable.bg);PADDING_LEFT = (screenWidth) / (COLS - 1) / 2;P

11、ADDING_TOP = (screenHeight) / (ROWS - 1) / 2;PADDING = PADDING_LEFT < PADDING_TOP ? PADDING_LEFT : PADDING_TOP;ROW_MARGIN = (screenHeight - PADDING * 2) / (ROWS - 1);COL_MARGIN = (screenWidth - PADDING * 2) / (COLS - 1);MARGIN = ROW_MARGIN < COL_MARGIN ? ROW_MARGIN : COL_MARGIN;PADDING_LEFT =

12、(screenWidth - (COLS - 1) * MARGIN) / 2;PADDING_TOP = (screenHeight - (ROWS - 1) * MARGIN) / 2;/ 對棋子進(jìn)行初始化initChess();/ System.out.println(PADDING_LEFT + " " + PADDING_TOP);/對棋子進(jìn)行初始化public void initChess() for (int i = 0; i < ROWS; i+) for (int j = 0; j < COLS; j+) chessMapij = ChessT

13、ype.NONE;invalidate(); /畫棋盤Overrideprotected void onDraw(Canvas canvas) super.onDraw(canvas);Paint paint = new Paint();paint.setColor(Color.WHITE);/ 打印行for (int i = 0; i < ROWS; i+) canvas.drawLine(PADDING_LEFT, i * MARGIN + PADDING_TOP, (COLS - 1)* MARGIN + PADDING_LEFT, i * MARGIN + PADDING_TOP

14、, paint);/ 打印列for (int i = 0; i < COLS; i+) canvas.drawLine(PADDING_LEFT + i * MARGIN, PADDING_TOP,PADDING_LEFT + i * MARGIN, MARGIN * (ROWS - 1)+ PADDING_TOP, paint);for (int r = 0; r < ROWS; r+) for (int c = 0; c < COLS; c+) / System.out.print(chessMaprc + " ");if (chessMaprc =

15、ChessType.NONE)continue;if (chessMaprc = ChessType.BLACK) paint.setColor(Color.BLACK);canvas.drawCircle(r * MARGIN + PADDING_LEFT, c * MARGIN+ PADDING_TOP, MARGIN / 2, paint); else if (chessMaprc = ChessType.WHITE) paint.setColor(Color.WHITE);canvas.drawCircle(r * MARGIN + PADDING_LEFT, c * MARGIN+

16、PADDING_TOP, MARGIN / 2, paint);/ System.out.println();7.3 判斷輸贏public boolean hasWin(int r, int c) ChessType chessType = chessMaprc;System.out.println(chessType);int count = 1;/ 縱向搜索for (int i = r + 1; i < r + 5; i+) if (i >= GameView.ROWS)break;if (chessMapic = chessType) count+; elsebreak;fo

17、r (int i = r - 1; i > r - 5; i-) if (i < 0)break;if (chessMapic = chessType)count+;elsebreak;/ System.out.println(count +" "+"1");if (count >= 5)return true;/ 橫向搜索count = 1;for (int i = c + 1; i < c + 5; i+) if (i >= GameView.COLS)break;if (chessMapri = chessType)c

18、ount+;elsebreak;for (int i = c - 1; i > c - 5; i-) if (i < 0)break;if (chessMapri = chessType)count+;elsebreak;/ System.out.println(count +" " +"2");if (count >= 5)return true;/ 斜向""count = 1;for (int i = r + 1, j = c + 1; i < r + 5; i+, j+) if (i >= Game

19、View.ROWS | j >= GameView.COLS) break;if (chessMapij = chessType)count+;elsebreak;for (int i = r - 1, j = c - 1; i > r - 5; i-, j-) if (i < 0 | j < 0)break;if (chessMapij = chessType)count+;elsebreak;/ System.out.println(count +" " +"3");if (count >= 5)return true;

20、/ 斜向"/"count = 1;for (int i = r + 1, j = c - 1; i < r + 5; i+, j-) if (i >= GameView.ROWS | j < 0)break;if (chessMapij = chessType)count+;elsebreak;for (int i = r - 1, j = c + 1; i > r - 5; i-, j+) if (i < 0 | j >= GameView.COLS)break;if (chessMapij = chessType)count+;els

21、ebreak;/ System.out.println(count +" " +"4");if (count >= 5)return true;return false;7.4手機落子算法 /* 得到最優(yōu)點 * * return 得到最優(yōu)的點 */private Point getBestPoint() initValue();for (int r = 0; r < GameView.ROWS; r+) for (int c = 0; c < GameView.COLS; c+) if (chessMaprc = ChessType.N

22、ONE) puterMaprc = getValue(r, c, puterType);this.playerMaprc = getValue(r, c, this.playerType);int pcMax = 0, playerMax = 0;Random rd = new Random();Point pcPoint = new Point(-1, -1);Point playerPoint = new Point();/ 分別選出pc估分和玩家估分的最大值for (int r = 0; r < GameView.ROWS; r+) for (int c = 0; c < G

23、ameView.COLS; c+) / 選出電腦估分的最大值if (pcMax = computerMaprc) if (rd.nextInt(10) % 2 = 0) pcMax = computerMaprc;pcPoint.x = r;pcPoint.y = c; else if (pcMax < computerMaprc) pcMax = computerMaprc;pcPoint.x = r;pcPoint.y = c;/ 選出玩家估分的最大值if (playerMax = playerMaprc) if (rd.nextInt(10) % 2 = 0) playerMax

24、= playerMaprc;playerPoint.x = r;playerPoint.y = c; else if (playerMax < playerMaprc) playerMax = playerMaprc;playerPoint.x = r;playerPoint.y = c;/ 兩者都在90100分之間,優(yōu)先選擇PCif (pcMax >= 90 && pcMax < 100 && playerMax >= 90 && playerMax < 100) return pcPoint; else retu

25、rn playerMax > pcMax ? playerPoint : pcPoint; /* 得到該位置的分?jǐn)?shù) * * param r * 該棋子所在的行數(shù) * param c * 該棋子所在的列數(shù) * param chessType * 棋子的類型 * return 得到的分?jǐn)?shù) */private int getValue(int r, int c, ChessType chessType) int dir = new int4;dir0 = this.getHorCount(r, c, chessType);dir1 = this.getVerCount(r, c, chessT

26、ype);dir2 = this.getSloRCount(r, c, chessType);dir3 = this.getSloLCount(r, c, chessType);/ 成五for (int i = 0; i < dir.length; i+) if (diri >= 5)return ScoreTable.FIVE;int temp = 0;/ 雙活四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi != ChessStatus.DIED)temp+;if (temp

27、 = 2)return ScoreTable.DOUBLE_ALIVE_FOUR;int t1 = 0, t2 = 0;/ 活四死四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.ALIVE)t1 = 1;if (diri = 4 && chessStatusi != ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_FOUR_AND_DEAD_

28、FOUR;/ 活四活三t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi != ChessStatus.DIED)t1 = 1;if (diri = 3 && chessStatusi = ChessStatus.ALIVE)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_FOUR_AND_ALIVE_THREE;/ 活四死三t1 = 0;t2 = 0;for (int i = 0

29、; i < dir.length; i+) if (diri = 4 && chessStatusi != ChessStatus.DIED)t1 = 1;if (diri = 3 && chessStatusi != ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1) return ScoreTable.ALIVE_FOUR_AND_DEAD_THREE;/ 活四活二t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 &

30、amp;& chessStatusi != ChessStatus.DIED)t1 = 1;if (diri = 2 && chessStatusi != ChessStatus.ALIVE)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_FOUR_AND_ALIVE_TWO;/ 活四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.ALIVE) return Score

31、Table.ALIVE_FOUR;/ 雙死四temp = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)temp+;if (temp = 2)return ScoreTable.DOUBLE_DEAD_FOUR;/ 死四活3t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)t1 =

32、1;if (diri = 3 && chessStatusi != ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1) return ScoreTable.DEAD_FOUR_AND_ALIVE_THREE;/ 死四活2t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)t1 = 1;if (diri = 2 && chessStatusi !

33、= ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.DEAD_FOUR_AND_ALIVE_TWO;/ 雙活三temp = 0;for (int i = 0; i < dir.length; i+) if (diri = 3 && chessStatusi != ChessStatus.DIED)temp+;if (temp = 2)return ScoreTable.DOUBLE_ALIVE_THREE;/ 活死三t1 = 0;t2 = 0;for (int i = 0; i

34、< dir.length; i+) if (diri = 3 && chessStatusi = ChessStatus.ALIVE)t1 = 1;if (diri = 3 && chessStatusi = ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_THREE_AND_DEAD_THREE;/ 活三for (int i = 0; i < dir.length; i+) if (diri = 3 && chessStatus

35、i = ChessStatus.ALIVE)return ScoreTable.ALIVE_THREE;/ 死四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)return ScoreTable.DEAD_FOUR;/ 半活死3t1 = 0;t2 = 0;for (int i = 0; i < 4; i+) if (diri = 3 && chessStatusi = ChessStatus.DIED)t1 = 1;if (diri

36、 = 3 && chessStatusi = ChessStatus.HALFALIVE)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_THREE_AND_DEAD_THREE;/ 雙活2temp = 0;for (int i = 0; i < 4; i+) if (diri = 2 && chessStatusi = ChessStatus.ALIVE)temp+;if (temp = 2)return ScoreTable.DOUBLE_ALIVE_TWO;/ 死3for (in

37、t i = 0; i < 4; i+)if (diri = 3 && chessStatusi = ChessStatus.DIED)return ScoreTable.DEAD_THREE;/ 活2for (int i = 0; i < 4; i+)if (diri = 2 && chessStatusi = ChessStatus.ALIVE)return ScoreTable.ALIVE_TWO;/ 死2for (int i = 0; i < 4; i+)if (diri = 2 && chessStatusi = Che

38、ssStatus.DIED)return ScoreTable.DEAD_TWO;return 0; /* 橫向搜索 * * param r * 該棋子所在的行數(shù) * param c * 該棋子所在的列數(shù) * param chessType * 棋子的類型 * return 得到的個數(shù) */private int getHorCount(int r, int c, ChessType chessType) int count = 1;int t1 = c + 1;int t2 = c - 1;for (int j = c + 1; j < c + 5; j+) if (j >= G

39、ameView.COLS) chessStatus0 = ChessStatus.DIED;break;if (chessMaprj = chessType) count+;if (count >= 5)return count; else chessStatus0 = (chessMaprj = ChessType.NONE) ? ChessStatus.ALIVE: ChessStatus.DIED;t1 = j;break;for (int j = c - 1; j > c - 5; j-) if (j < 0) if (chessStatus0 = ChessStat

40、us.DIED && count < 5) return 0;chessStatus0 = ChessStatus.DIED;break;if (chessMaprj = chessType) count+;if (count >= 5)return count; else if (chessStatus0 = ChessStatus.DIED) if (count < 5 && chessMaprj != ChessType.NONE) return 0; else chessStatus0 = (chessMaprj) = ChessTyp

41、e.NONE ? ChessStatus.ALIVE: ChessStatus.DIED;t2 = j;/ 記錄遇到的空格/ 當(dāng)兩端都活的時候,看是否可以延伸if (chessStatus0 = ChessStatus.ALIVE) int tempCount1 = count, tempCount2 = count;boolean isAlive1 = false, isAlive2 = false;for (int i = t1 + 1; i < t1 + 5; i+) if (i >= GameView.ROWS)break;if (chessMapri = chessTyp

42、e) tempCount1+; else isAlive1 = (chessMapri = ChessType.NONE) ? true: false;break;for (int i = t2 - 1; i > t2 - 5; i-) if (i < 0)break;if (chessMapri = chessType) tempCount2+; else isAlive2 = (chessMapri = ChessType.NONE) ? true: false;break;/ 如果兩頭都是空,直接跳出if (tempCount1 = count && temp

43、Count2 = count)break;if (tempCount1 = tempCount2) count = tempCount1;chessStatus0 = (isAlive1 && isAlive2) ? ChessStatus.HALFALIVE: ChessStatus.DIED; else count = (tempCount1 > tempCount2) ? tempCount1: tempCount2;if (count >= 5)return 0;if (tempCount1 > tempCount2)chessStatus0 = (i

44、sAlive1) ? ChessStatus.HALFALIVE: ChessStatus.DIED;elsechessStatus0 = (isAlive2) ? ChessStatus.HALFALIVE: ChessStatus.DIED;break;return count; /* 縱向搜索 * * param chessType * 要搜索的棋子類型 * param r * 棋子所在的行 * param c * 棋子所在的列 * return */private int getVerCount(int r, int c, ChessType chessType) int t1 = r

45、 + 1;int t2 = r - 1;int count = 1;for (int i = r + 1; i < r + 5; i+) if (i >= GameView.ROWS) chessStatus1 = ChessStatus.DIED;break;if (chessMapic = chessType) count+;if (count >= 5) return count; else chessStatus1 = (chessMapic = ChessType.NONE) ? ChessStatus.ALIVE: ChessStatus.DIED;t1 = i;

46、break;for (int i = r - 1; i > r - 5; i-) if (i < 0) if (chessStatus1 = ChessStatus.DIED && count < 5) return 0;chessStatus1 = ChessStatus.DIED;break;if (chessMapic = chessType) count+;if (count >= 5) return count; else if (chessStatus1 = ChessStatus.DIED) if (chessMapic != ChessT

47、ype.NONE && count < 5) return 0; else chessStatus1 = chessMapic = ChessType.NONE ? ChessStatus.ALIVE: ChessStatus.DIED;t2 = i;/ 如果兩頭都活,看是否還可以延伸if (chessStatus1 = ChessStatus.ALIVE) int tempCount1 = count, tempCount2 = count;boolean isAlive1 = false, isAlive2 = false;for (int j = t1 + 1; j < t1 + 5; j+) if (j >= GameView.ROWS) / chessStatus1 = ChessStatus.D

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論