java五子棋人機(jī)代碼 新手java五子棋完整代碼

java 五子棋 人機(jī)

public void pcPK(){

站在用戶的角度思考問題,與客戶深入溝通,找到嵊泗網(wǎng)站設(shè)計與嵊泗網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋嵊泗地區(qū)。

if (type) {

if (jLabels[i][j].getIcon() == null) {

jLabels[i][j].setIcon(new ImageIcon("src/imagess/hei.gif"));

type = false;

initializationSum();

success(i, j);

} else {

JOptionPane.showMessageDialog(null, "已有棋子");

}

}

while (!type) {

int k = new Random().nextInt(8);

if (k == 1) {

if (jLabels[this.i + 1][this.j].getIcon() == null) {

jLabels[this.i + 1][this.j].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i + 1][this.j]);

success(this.i + 1, j);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 2) {

if (jLabels[this.i - 1][j].getIcon() == null) {

jLabels[this.i - 1][j].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i - 1][j]);

success(this.i - 1, j);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 3) {

if (jLabels[i][j + 1].getIcon() == null) {

jLabels[this.i][j + 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[i][j + 1]);

success(this.i, j + 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 4) {

if (jLabels[this.i][j - 1].getIcon() == null) {

jLabels[this.i][j - 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i][j - 1]);

success(this.i, j - 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 5) {

if (jLabels[this.i + 1][j + 1].getIcon() == null) {

jLabels[this.i + 1][j + 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[i + 1][j + 1]);

success(this.i + 1, j + 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 6) {

if (jLabels[this.i - 1][j - 1].getIcon() == null) {

jLabels[this.i - 1][j - 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i - 1][j - 1]);

success(this.i - 1, j - 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 7) {

if (jLabels[this.i + 1][j - 1].getIcon() == null) {

jLabels[this.i + 1][j - 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[i + 1][j - 1]);

success(this.i + 1, j - 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 0) {

if (jLabels[this.i - 1][j + 1].getIcon() == null) {

jLabels[this.i - 1][j + 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i - 1][j + 1]);

success(this.i - 1, j + 1);

type = true;

break;

} else {

type = false;

continue;

}

}

}

}

系統(tǒng)框圖如下 java實(shí)現(xiàn)五子棋程序 可以實(shí)現(xiàn)人人對戰(zhàn) 人機(jī)對戰(zhàn) 簡單功能 悔棋 認(rèn)輸

一、實(shí)驗(yàn)題目

五子棋游戲。

二、問題分析

五子棋是雙人博弈棋類益智游戲,由圍棋演變而來,屬純策略型。棋盤通常15*15,即15行,15列,共225個交叉點(diǎn),即棋子落點(diǎn);棋子由黑白兩色組成,黑棋123顆,白棋122顆。游戲規(guī)則為黑先白后,誰先五子連成一條直線誰贏,其中直線可以是橫的、縱的、45度、135度。

本次Java編程我的目的是現(xiàn)實(shí)人機(jī)對戰(zhàn),即游戲者一方是人,另一方計算機(jī)。這就要求程序不僅要具備五子棋的基本界面,還要編程指導(dǎo)計算機(jī)與人進(jìn)行對弈。為了使程序盡可能智能,我采用了貪心策略、傳統(tǒng)搜索算法、極大極小博弈樹算法,對應(yīng)游戲玩家的3個等級:簡單、中等、困難。

三、功能設(shè)計

我的程序基本功能是實(shí)現(xiàn)人機(jī)對弈五子棋。人和電腦交替下棋,誰先五子連成一條直線誰就贏。下面是我程序的功能模塊:

1.等級設(shè)置

核心功能是實(shí)現(xiàn)不同策略與算法的對比運(yùn)用,純貪心策略實(shí)現(xiàn)簡單等級對手,直接搜索算法實(shí)現(xiàn)中等等級對手,極大極小博弈樹算法實(shí)現(xiàn)困難等級對手。對應(yīng)程序中的3選1單選按鈕。

2.悔棋功能

模擬棧機(jī)制實(shí)現(xiàn)人悔棋,不限步長的悔棋。對應(yīng)程序中的悔棋按鈕。

3.棋面繪制

根據(jù)不同機(jī)計算機(jī)的屏幕分辨率,繪制逼真的棋盤。

4.圖片引入

兩張古典的人物圖片,生動模擬對弈雙方。人物圖片旁的黑白棋缽圖片顯示黑白棋歸屬。

5.背景設(shè)置

支持用戶選擇背景,包括棋盤、棋盤邊框、窗口邊框,彰顯個性。

6.音樂播放

下棋時有棋子落地的聲音,一方勝利時有五子連成一片的聲音。同時在設(shè)置背景時相應(yīng)的改變整個對弈過程中的背景音樂。

7.時間顯示

在棋盤正上方有一模擬文本框顯示當(dāng)前棋局用時。

8.其他小功能

支持和棋、認(rèn)輸、開啟新游戲、退出游戲等操作。

四、數(shù)據(jù)結(jié)構(gòu)與算法設(shè)計

數(shù)據(jù)結(jié)構(gòu)部分

1.當(dāng)前棋局的存儲結(jié)構(gòu)

我的五子棋程序選擇通常用到的15行*15列棋盤,可以開二維數(shù)組PositionFlag?=?new?int[15][15],PositionFlag[i][j]為0表示(i,j)點(diǎn)尚無棋,為1表示(i,j)點(diǎn)是人的棋子,為2表示(i,j)點(diǎn)是機(jī)器的棋子。之所以選擇二維數(shù)組,主要原因有兩點(diǎn):

1.本程序需要頻繁隨機(jī)訪問15*15的交叉點(diǎn),對應(yīng)查詢該點(diǎn)狀態(tài)以及改變該點(diǎn)狀態(tài),隨機(jī)訪問是數(shù)組的特點(diǎn)。

2.15*15=225開二維數(shù)組的內(nèi)存需求相對現(xiàn)在內(nèi)存為2G及以上的計算機(jī)完全可以接受,且數(shù)組實(shí)現(xiàn)簡單、操作方便。

基于以上兩點(diǎn),盡管創(chuàng)建動態(tài)的順序表—鏈表可能可以節(jié)省少量內(nèi)存(可以只存當(dāng)前有棋的點(diǎn),原數(shù)組對應(yīng)位置為0的點(diǎn)可以不存),但選擇數(shù)組的優(yōu)勢完全在上述兩點(diǎn)體現(xiàn)了出來。

2.實(shí)現(xiàn)悔棋操作的數(shù)據(jù)結(jié)構(gòu)

由于每次悔棋只需回退當(dāng)前幾步,后進(jìn)先出原則,這正是棧這種典型數(shù)據(jù)結(jié)構(gòu)的設(shè)計思想,于是我選擇棧。我自己先寫了用自定義數(shù)組模擬的棧,但由于是學(xué)Java語言且由于悔棋的存儲空間需要隨當(dāng)前步數(shù)增大而增大(由于每局最多下225步,即最多要悔225步,所以自己開個225的數(shù)組完全可以避免存儲空間自增長的問題且內(nèi)存完全可以接受,之所以不用自定義數(shù)組而用ArrayList類主要是為了嘗試Java中STL的用法),所有我最終改為用Java類庫中的ArrayList類。

確定用ArrayList類實(shí)現(xiàn)棧機(jī)制后就必須考慮每個ArrayList單元具體存儲什么。剛開始我存儲的是當(dāng)前的棋局,即整個局面,而每個局面對應(yīng)一個二維數(shù)組,這樣是很占用內(nèi)存的。試想一下,在最壞情況下,225個ArrayList單元,每個單元存放一個15*15的二維數(shù)組,盡管225*15*15在Java的內(nèi)存管理機(jī)制下不會爆棧,但也是極不劃算的。之所以說不劃算,是因?yàn)橛懈玫慕鉀Q方案。由于每次悔棋只是在回退倒數(shù)一步,多步悔棋只需循環(huán)回退,所以可以只存儲當(dāng)前棋局最后一步的下法,對應(yīng)一個二維點(diǎn),完全可以自定義一個二維坐標(biāo)類chessOneStep。

算法設(shè)計部分

Java語言是面向?qū)ο蟮恼Z言。我在進(jìn)行五子棋游戲編程是總共傳創(chuàng)建了11個自定義的類。在編寫程序的過程中,我有一個明顯的體驗(yàn)就是面向?qū)ο缶幊叹褪且豁?xiàng)有關(guān)對象設(shè)計和對象接口技術(shù),很多關(guān)鍵的技術(shù)就是如何設(shè)計自定義的對象。

下面我先概括給出我的所有類的作用:

1.mainFrame類:主框架類,我應(yīng)用程序的入口;

2.chessPositon類:主控類,這個類是我程序的核心類,負(fù)責(zé)控制雙方的下棋,以及調(diào)用其他的類完成當(dāng)前棋局的顯示繪制;

3.chessPanel類:面板類,調(diào)用其他底層類完成當(dāng)前棋局的顯示繪制;

4.chessBoard類:棋盤繪制類,負(fù)責(zé)棋盤的繪制;

5.chessImage類:文件類,包含各種資源(背景圖片、背景音樂)以及靜態(tài)全局變量(public?static?Type);

6.chessButton類:組件類,定義各種組件,包括按鈕、單選按鈕、文本框等;

7.chessMusic類:音樂類,負(fù)責(zé)調(diào)用Java庫類完成背景音樂、下棋音樂、取勝音樂等的播放;

8.chessPiece類:棋局類,定義棋局二維數(shù)組數(shù)據(jù)結(jié)構(gòu)并完成相關(guān)操作;

9.chessList類:棧類,完成悔棋等操作;

10.?chessOneStep類:棋子類,定義每步坐標(biāo)以及下在該處獲得的估價值;

11.myCompare類:排序類,完成chessOneStep類的自定義排序

詳細(xì)設(shè)計

1.mainFrame類

作為我的五子棋程序的主類,mainFrame類主要實(shí)例化相關(guān)的對象,如chessbutton,chessborad等,從而完成框架的創(chuàng)建。更重要的是實(shí)例化chessposition,這是本程序的核心類,控制游戲雙方行棋過程完成人機(jī)互動下棋,然后將MyChessPosition與鼠標(biāo)響應(yīng)addMouseListener()關(guān)聯(lián)起來。

2.chessMusic類

一個好的游戲必須給人一種身臨其境的感覺,而聲音是營造這種氛圍的重要因素。參照網(wǎng)上各游戲運(yùn)行商的音樂配置,我選擇相關(guān)逼真的聲音。包括背景音樂、下棋棋子落到棋盤發(fā)出的聲音以及一方勝出的配樂。所有這些功能的實(shí)現(xiàn),依賴于自定義的chessMusic類,采用AudioInputStream配合Clip的方式完成音樂播放的軟硬件工作,然后定義兩個接口chessmusic(String?Name)和Stop(),前者完成播放功能,后者完成關(guān)閉當(dāng)前音樂功能。因?yàn)橐纛l文件相對較大,而我的程序提供在不同背景樂之間切換的功能,所以在打開另一個音頻文件之前必須關(guān)閉前一個正在播放的音頻文件,防止出現(xiàn)溢出。

3.chessImage類

適當(dāng)?shù)膭赢嫽驁D片能給游戲玩家?guī)砻赖捏w驗(yàn)。所以我的五子棋程序界面在不失和諧的前提下引入了盡可能多的圖片,包括對弈雙方、棋缽等。圖片引入的具體工作通過語句import?javax.imageio.ImageIO完成。同時,由于圖片要在用到它的類中被訪問,為了避免頻繁調(diào)用函數(shù),我直接將圖片相關(guān)聯(lián)的對象定義為public?static,表明是公用的、靜態(tài)的。進(jìn)一步引申開去,我將程序中用到的靜態(tài)全局變量都定義在chessImage類中。具體如下:

public?static?Date?begin;//每局開始時間

public?static?Date?cur;//每局結(jié)束時間

public?static?chessOneStep?LineLeft;//結(jié)束端點(diǎn)1

public?static?chessOneStep?LineRight;//結(jié)束端點(diǎn)2

public?static?boolean?IsGameOver;//是否只有一方獲勝

public?static?int?ColorOfBackGround[][]=?{{255,?227,?132},{0,255,127},{218,165,32}};//背景顏色

public?static?int?ColorOfWindows[][]=?{{?60,179,113},{245,245,245},{122,122,122}};//背景顏色

public?static?int?WitchMatch;//背景搭配

public?static?String?MusicOfBackGround;//背景音樂

public?static?int?CurrentStep;//記錄當(dāng)前步數(shù)

public?static?int?Rank;//設(shè)置難度等級

public?static?boolean?IsSurrender;//判斷是否認(rèn)輸

public?static?boolean?IsTie;//判斷是否認(rèn)輸

public?static?String?Message;//輸出提示信息

public?static?Image?IconImage;//?圖標(biāo)

public?static?Image?blackBoard;//白棋盤

public?static?Image?whiteBoard;//黑棋盤

public?static?Image?blackChess;//?白棋棋子圖片

public?static?Image?whiteChess;//?白棋棋子圖片

public?static?Image?RightPlayer;//白棋棋罐圖片

public?static?Image?LeftPlayer;//白棋玩家頭像圖片

public?static?String?path?=?"src/";//?圖片的保存路徑

4.chessButton類

這個是程序的組件類。定義了各種功能鍵,完善程序功能,營造逼真的人機(jī)對戰(zhàn)游戲效果。分為3類:效果。。

(1)、按鈕組件

本程序有5個按鈕,支持和棋、認(rèn)輸、新游戲、退出、悔棋等。認(rèn)輸和和棋按鈕終止當(dāng)前的棋局,給出相應(yīng)的提示信息;退出按鈕調(diào)用系統(tǒng)System.exit(0)的函數(shù)正常返回;悔棋按鈕調(diào)用后面要介紹的chessList類實(shí)現(xiàn)悔棋;新游戲按鈕則刷新當(dāng)前棋局準(zhǔn)備下一輪,要將記錄當(dāng)前棋局的二維數(shù)組全部置0,刷新當(dāng)前棋局開始時間等。

(2)、單選按鈕組件

游戲界面支持設(shè)置個性化界面,包括背景顏色與背景音樂,跟重要的一點(diǎn)是設(shè)置難度(簡單、中等、困難)。單選按鈕只能多選一。背景顏色主要是存儲相關(guān)顏色搭配方案的RGB顏色,開2維數(shù)組,即對應(yīng)RGB3原色數(shù)組的一維數(shù)組,然后通過改變WitchMatch全局變量的值來有用戶自己選擇顏色搭配,不同的顏色搭配對應(yīng)不同的背景音樂表達(dá)一致的主題。難度設(shè)置主要是改變計算機(jī)的下棋算法,不同難度通過Rank判斷進(jìn)入不同的程序分支,實(shí)現(xiàn)不同智能等級的計算機(jī)下棋水平。

(3)、文本框

在不同的單選按鈕前添加相應(yīng)的文本框,提示用戶可以實(shí)現(xiàn)的功能。同時我用顏色模擬出顯示當(dāng)前棋局耗用時間的文本框。

不論按鈕還是單選按鈕都要關(guān)聯(lián)相應(yīng)的消息,把相應(yīng)功能的實(shí)現(xiàn)放在消息響應(yīng)處理函數(shù)理。這些主要是實(shí)現(xiàn)Java庫提供的消息響應(yīng)接口里的方法。

5.chessPiece類

主要完成當(dāng)前棋面的存儲,存儲棋面的數(shù)據(jù)結(jié)構(gòu)為二維數(shù)組int[][]?PositionFlag;然后定義獲取、設(shè)置某點(diǎn)以及整個棋面的狀態(tài)的方法。

(1)、SetPositionFlag(int?x,?int?y,?int?flag)//設(shè)置(x,y)處的狀態(tài)為flag

(2)、GetPositionFlag(int?x,?int?y)//獲?。▁,y)處的狀態(tài)

(3)、SetAllFlag(int?[][]NewFlag)//設(shè)置當(dāng)前整個棋面的狀態(tài)為NewFlag

(4)、GetAllFlag()//獲取當(dāng)前整個棋面的狀態(tài)

(5)、DrawChessPiece(Graphics?g)//繪制當(dāng)前局面的棋子

由于本類比較重要,所以附上了代碼,見源代碼1。

6.chessBoard類

功能為繪制棋盤線。由于圍棋的棋盤比較復(fù)雜,橫線、豎線較多,且為了使棋盤美觀,還要自定義窗口邊框、棋盤邊框、對弈雙方邊框等,對線寬、線型也有一定要求。有時要單像素線條,有時要多像素線條。對于多像素線條,我主要用了2種方法。

方法一:

在需要繪制多像素線條處首先繪制一條單像素線,然后根據(jù)線寬要求上下平移適當(dāng)像素達(dá)到繪制多像素的目的。這樣的方法適合繪制水平線或豎直線,繪制其他斜率的線條容易造成走樣。在沒有想到比較好的反走樣編程思想后我選擇了調(diào)用Java庫中已經(jīng)封裝好的函數(shù)。

方法二:

為了克服方法一繪制非水平或豎直線時造成的走樣,同時也為了更進(jìn)一步學(xué)習(xí)Java語言,我猜想肯定會有類似OpenGL中設(shè)置線寬的畫刷,于是上網(wǎng)百度找到了相應(yīng)的畫刷Stroke類。通過Java庫實(shí)現(xiàn)繪制不同線寬的直線,達(dá)到了反走樣效果。

7.chessOneStep類

這個類是為了配合chessList類實(shí)現(xiàn)悔棋以及在計算機(jī)下棋算法實(shí)現(xiàn)返回有效狀態(tài)點(diǎn)而設(shè)計的。主要數(shù)據(jù)成員為

private??int??x,y,weight;//其中x,y表示點(diǎn)坐標(biāo),weight表示將棋下到該點(diǎn)獲得的估價值。

主要方法如下:

(1)、GetX()//獲得當(dāng)前對象的x坐標(biāo)

(2)、GetY()//獲得當(dāng)前對象的y坐標(biāo)

(3)、GetWeight()//獲得當(dāng)前對象的(x,y)處的估價值

8.chessList類

程序支持悔棋功能,為了實(shí)現(xiàn)悔棋,自定義了chessList類。這個類主要通過引入java.util.ArrayList和java.util.List實(shí)現(xiàn)集合的數(shù)據(jù)類型。然后自定義一些方法,如下:

(1)、AddStep(chessOneStep?OneStep)//添加一步棋到List中

(2)、GetSize()//獲得當(dāng)前List的大小

(3)、ClearList()//清空List

(4)、RemoveLast()//刪去List中的最后元素

由于每次刪除當(dāng)前List中的最后一個元素,實(shí)現(xiàn)后進(jìn)先出,所以可以模擬棧的功能實(shí)現(xiàn)悔棋。

9.myCompare類

由于在計算機(jī)下棋的極大極小博弈樹算法中需要對自定義對象chessOneStep按weight進(jìn)行排序,所以引入了myCompare類,通過實(shí)現(xiàn)Comparator接口中的compare方法完成自定義對象排序。

10.chessPanel類

程序的自定義面板類,主要負(fù)責(zé)完成當(dāng)前框架內(nèi)容的顯示。這是一個重要的與框架和圖形顯示密切相關(guān)的類。主要數(shù)據(jù)成員為

private?chessboard?MyChessBoard;//當(dāng)前顯示棋盤

private?chesspiece?MyChessPiece;//當(dāng)前顯示整個棋面的狀態(tài)

主要方法如下:

(1)、chesspanel(chessboard?MyChessBoard1,?chesspiece?MyChessPiece1)//構(gòu)造函數(shù),分別用MyChessBoard1和MyChessPiece1初始化MyChessBoard和MyChessPiece

(2)display(chessboard?MyChessBoard1,?chesspiece?MyChessPiece1)//自定義顯示回調(diào)函數(shù),調(diào)用repaint()完成重新繪制游戲界面

(3)、paintComponent(Graphics?g)//核心方法,調(diào)用各種函數(shù)完成具體的繪制工作

11.chessPositon類

程序算法核心類,總的功能是控制人和計算機(jī)輪流下棋,以及調(diào)用chessPanel類中的display(chessboard?,?chesspiece?)方法完成界面的實(shí)時刷新。關(guān)于chessPositon類,我在此將重點(diǎn)介紹。chessPosition類的主要數(shù)據(jù)成員如下:

private?static?chessboard?MyChessBoard;//當(dāng)前顯示棋盤

public?static?chesspiece?MyChessPiece;//當(dāng)前顯示整個棋面的狀態(tài)

private?static?chesspanel?Mychesspanel;////當(dāng)前顯示面板

public?static?chesslist?MyChessList=new?chesslist();//當(dāng)前下棋集合,用于悔棋

final?private?static?int?INF?=?(1??30);?//?表示正無窮大的常量,用于極大極小博弈數(shù)搜索算法

public?static?boolean?CanGo;//控制當(dāng)前下棋一方

類的設(shè)計集中體現(xiàn)在成員方法的設(shè)計上。實(shí)現(xiàn)人機(jī)對戰(zhàn),只有語言是遠(yuǎn)遠(yuǎn)不夠的,還要加入算法,用算法引導(dǎo)計算機(jī)下棋。下面介紹該類的方法成員:

(1)、chessposition(chesspanel?,?chessboard?,chesspiece?)?//帶有參數(shù)的構(gòu)造函數(shù)

(2)、chessposition()

不帶參數(shù)的構(gòu)造函數(shù)

(3)、mouseClicked(MouseEvent?event)

鼠標(biāo)響應(yīng)函數(shù),負(fù)責(zé)人的下棋,根據(jù)鼠標(biāo)點(diǎn)擊的位置轉(zhuǎn)換得到所在棋盤的相對位置。如果該位置不合法,即超出棋盤有效范圍,點(diǎn)擊無響應(yīng);如果該位置上已有棋,彈出消息框給出提示。這二者都要求重新給出下棋位置,即當(dāng)前鼠標(biāo)響應(yīng)無效…直到點(diǎn)擊到棋盤有效區(qū)域。

(4)、IsOver(int[][]?Array,int?x,int?y)

判斷當(dāng)前int[][]Array對應(yīng)的棋局是否結(jié)束,即一方五子連成一條直線。此處有兩種思路,一種對當(dāng)前棋面上的所有棋子都進(jìn)行一次判斷,具體為水平方向、豎直方向、與水平線成45度方向、與水平線成135度方向,只要有一個方向五子連成一條直線就說明有一方獲勝,游戲結(jié)束;另一種思路為只在當(dāng)前下棋的4個方向進(jìn)行判斷,我的程序采用的是第二種,所以IsOver方法除了int[][]Array參數(shù)外,還有x,y參數(shù),(x,y)表示當(dāng)前下棋的坐標(biāo)點(diǎn)。

(5)display()

通過調(diào)用自定義面板類的顯示回調(diào)函數(shù)用于重新顯示游戲界面,達(dá)到每下一步棋及時更新游戲界面的目的。

(6)、GetValue(int?flag,?int?num)

估值函數(shù),根據(jù)經(jīng)驗(yàn)把棋局分成只有1顆棋相連,2顆棋相連且兩端被封死,2顆棋相連且一端封死另一端活的,2顆棋相連且兩端都是活的,同理3顆棋、4顆棋也各自可分3種情況。不同的情況對應(yīng)不同的估價值。估價值的設(shè)定是決定計算機(jī)一方是否智能的一個關(guān)鍵因素。

(7)、GetPredictValue(int?flag,?int?num)

對未連成一片但通過再下一顆子就能連成一片的局面進(jìn)行估值,這在雙方下棋的有限步驟內(nèi)是能產(chǎn)生重要影響的。如果每局棋僅考慮當(dāng)前一步,是不可取的。

(8)、Evaluate(int[][]?Array,?int?x,?int?y)

根據(jù)棋面具體情況以及預(yù)先設(shè)定的估值函數(shù),對某個點(diǎn)對應(yīng)的局面進(jìn)行評估。由于每次雙方只能下一顆棋,所以可以每次取當(dāng)前局面的所有點(diǎn)中對應(yīng)估值最大值點(diǎn)的估值作為整個局面的估值。

(9)、GetGreedNext()

計算機(jī)下棋方法1,對應(yīng)難度等級為簡單,采用貪心思想。每次下棋前在求得最有利點(diǎn)下棋,而是否最有利只是通過一步評估。算法偽碼描述為:

Max取負(fù)無窮大

for(行i從0到15)

{

For(列j從0到15)

{

If((i,j)對應(yīng)的位置無棋)

{

a.假設(shè)放上一顆由人控制的棋,求估價值;

b.假設(shè)放上一顆由計算機(jī)控制的棋,求估價值;

c.取二者中較大值作為(i,j)處的估價值tmp;

d.取tmp與Max較大值賦值給Max.

}

}

}

最終Max對應(yīng)的點(diǎn)就是當(dāng)前整個局面中最大的估值點(diǎn)。至于上述為什么要考慮雙方都在該點(diǎn)下棋的情況呢?主要原因?yàn)橄挛遄悠迨莻€攻防兼?zhèn)涞倪^程,不僅要考慮自己對自己最有利,還要考慮對對手最不利,通俗來講就是在自己贏的時候不能讓對手先贏。

(10)、GetSearchNext(int?LookLength)

derectSearch(int?[][]Array,boolean?who,int?deepth)

計算機(jī)下棋方法2:直接搜索法,對應(yīng)難度等級為中等。

每步棋最多有225個不同下法,若采用直接搜索法則對應(yīng)的孩子節(jié)點(diǎn)有225個(在下棋過程中會逐漸減少),即每層有最多225個節(jié)點(diǎn)待擴(kuò)展,這就決定了直接搜索進(jìn)行不超過2次—主要原因有兩點(diǎn):

a.采用深度優(yōu)先搜索需要遞歸,遞歸中狀態(tài)過多可能會爆棧,我們知道遞歸是用棧機(jī)制來實(shí)現(xiàn)的;采用寬度優(yōu)先搜索又需要存儲為擴(kuò)展的節(jié)點(diǎn),這對內(nèi)存容量要求很高。

b.不管深搜還是廣搜,在時間復(fù)雜度為O(N^m)的情況下都是不能接受的。其中N為當(dāng)前棋局的待擴(kuò)展節(jié)點(diǎn),最大225;m為搜索的深度。

綜上所述,在采用直接搜索法時搜索深度不能太深,嚴(yán)格來說是應(yīng)該控制在2層以內(nèi),在計算機(jī)運(yùn)算速度在10^7次每秒的情況下,理論和實(shí)驗(yàn)都表明超過2層就會變得很慢且這種趨勢成指數(shù)級增長。

直接搜索算法偽代碼為

GetSearch(boolean?flag,int?deep)

{

如果deep等于0,返回當(dāng)前棋局估值;

for(行i從0到15)

{

For(列j從0到15)

{

If((i,j)對應(yīng)的位置無棋)

{

如果輪到計算機(jī)下棋,置標(biāo)志位為2

GetSearch(!flag,deep-1);

如果輪到人下棋,置標(biāo)志位為1;

GetSearch(!flag,deep-1);

}

}

}

}

(11)、GetMinMaxsearchNext(int?LookLength)

MinMaxsearch(int?[][]Array,boolean?who,?int?deepth)

計算機(jī)下棋算法3:極大極小博弈樹法,對應(yīng)難度等級為困難。五子棋是個博弈游戲,當(dāng)前在尋找對自己最有利的下棋點(diǎn)時要盡可能保證對對手最不利,這種思想可以用極大極小博弈樹

求一個簡單的JAVA五子棋代碼!! 網(wǎng)上復(fù)制的別來了!

以下是現(xiàn)寫的 實(shí)現(xiàn)了兩人對戰(zhàn) 自己復(fù)制后運(yùn)行把 沒什么難度 類名 Games

import java.util.Scanner;

public class Games {

private String board[][];

private static int SIZE = 17;

private static String roles = "A玩家";

//初始化數(shù)組

public void initBoard() {

board = new String[SIZE][SIZE];

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

// if(i==0){

// String str = "";

// str += j+" ";

// board[i][j]= str;

// }else if(i!=0j==0){

// String str = "";

// str += i+" ";

// board[i][j]= str;

// }else{

board[i][j] = "╋";

// }

}

}

}

//輸出棋盤

public void printBoard() {

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

System.out.print(board[i][j]);

}

System.out.println();

}

}

//判斷所下棋子位置是否合理

public boolean isOk(int x, int y) {

boolean isRight = true;

if (x = 16 || x 1 || y = 16 | y 1) {

//System.out.println("輸入錯誤,請從新輸入");

isRight = false;

}

if (board[x][y].equals("●") || board[x][y].equals("○")) {

isRight = false;

}

return isRight;

}

//判斷誰贏了

public void whoWin(Games wz) {

// 從數(shù)組挨個查找找到某個類型的棋子就從該棋子位置向右,向下,斜向右下 各查找5連續(xù)的位置看是否為5個相同的

int xlabel;// 記錄第一次找到某個棋子的x坐標(biāo)

int ylabel;// 記錄第一次找到某個棋子的y坐標(biāo)

// ●○╋

// 判斷人是否贏了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("○")) {

xlabel = i;

ylabel = j;

// 橫向找 x坐標(biāo)不變 y坐標(biāo)以此加1連成字符串

String heng = "";

if (i + 5 SIZE j + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("○○○○○")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

// 向下判斷y不變 x逐增5 連成字符串

String xia = "";

for (int l = j; l i + 5; l++) {

xia += board[l][j];

// System.out.println(xia);

}

if (xia.equals("○○○○○")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

// 斜向右下判斷

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("○○○○○")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

}

}

}

}

// 判斷電腦是否贏了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("●")) {

xlabel = i;

ylabel = j;

// 橫向找 x坐標(biāo)不變 y坐標(biāo)以此加1連成字符串

String heng = "";

if (j + 5 SIZE i + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("●●●●●")) {

System.out.println(roles+"贏輸了!您輸了!");

System.exit(0);

}

// 向下判斷y不變 x逐增5 連成字符串

String xia = "";

for (int l = i; l i + 5; l++) {

xia += board[l][ylabel];

// System.out.println(xia);

}

if (xia.equals("●●●●●")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

// 斜向右下判斷

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("●●●●●")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

}

}

}

}

}

public static void main(String[] args) {

Games wz = new Games();

Scanner sc = new Scanner(System.in);

wz.initBoard();

wz.printBoard();

while (true) {

System.out.print("請"+roles+"輸入X,Y坐標(biāo),必須在0-15范圍內(nèi),xy以空格隔開,輸入16 16結(jié)束程序");

int x = sc.nextInt();

int y = sc.nextInt();

if (x == SIZE y == SIZE) {

System.out.println("程序結(jié)束");

System.exit(0);

}

if (x SIZE || x 0 || y SIZE | y 0) {

System.out.println("輸入錯誤,請從新輸入");

continue;

}

//如果roles是A玩家 就讓A玩家下棋,否則就讓B玩家下棋。

if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {

wz.board[x][y] = "○";

wz.printBoard();

//判斷輸贏

wz.whoWin(wz);

}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){

wz.board[x][y] = "●";

wz.printBoard();

//判斷輸贏

wz.whoWin(wz);

} else {

System.out.println("此處已經(jīng)有棋子,從新輸入");

continue;

}

if(roles.equals("A玩家")){

roles = "B玩家";

}else if(roles.equals("B玩家")){

roles = "A玩家";

}

}

}

}

求java編寫的五子棋代碼,要有電腦AI的

java網(wǎng)絡(luò)五子棋

下面的源代碼分為4個文件;

chessClient.java:客戶端主程序。

chessInterface.java:客戶端的界面。

chessPad.java:棋盤的繪制。

chessServer.java:服務(wù)器端。

可同時容納50個人同時在線下棋,聊天。

沒有加上詳細(xì)注釋,不過絕對可以運(yùn)行,j2sdk1.4下通過。

/*********************************************************************************************

1.chessClient.java

**********************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

class clientThread extends Thread

{

chessClient chessclient;

clientThread(chessClient chessclient)

{

this.chessclient=chessclient;

}

public void acceptMessage(String recMessage)

{

if(recMessage.startsWith("/userlist "))

{

StringTokenizer userToken=new StringTokenizer(recMessage," ");

int userNumber=0;

chessclient.userpad.userList.removeAll();

chessclient.inputpad.userChoice.removeAll();

chessclient.inputpad.userChoice.addItem("所有人");

while(userToken.hasMoreTokens())

{

String user=(String)userToken.nextToken(" ");

if(userNumber0 !user.startsWith("[inchess]"))

{

chessclient.userpad.userList.add(user);

chessclient.inputpad.userChoice.addItem(user);

}

userNumber++;

}

chessclient.inputpad.userChoice.select("所有人");

}

else if(recMessage.startsWith("/yourname "))

{

chessclient.chessClientName=recMessage.substring(10);

chessclient.setTitle("Java五子棋客戶端 "+"用戶名:"+chessclient.chessClientName);

}

else if(recMessage.equals("/reject"))

{

try

{

chessclient.chesspad.statusText.setText("不能加入游戲");

chessclient.controlpad.cancelGameButton.setEnabled(false);

chessclient.controlpad.joinGameButton.setEnabled(true);

chessclient.controlpad.creatGameButton.setEnabled(true);

}

catch(Exception ef)

{

chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close無法關(guān)閉");

}

chessclient.controlpad.joinGameButton.setEnabled(true);

}

else if(recMessage.startsWith("/peer "))

{

chessclient.chesspad.chessPeerName=recMessage.substring(6);

if(chessclient.isServer)

{

chessclient.chesspad.chessColor=1;

chessclient.chesspad.isMouseEnabled=true;

chessclient.chesspad.statusText.setText("請黑棋下子");

}

else if(chessclient.isClient)

{

chessclient.chesspad.chessColor=-1;

chessclient.chesspad.statusText.setText("已加入游戲,等待對方下子...");

}

}

else if(recMessage.equals("/youwin"))

{

chessclient.isOnChess=false;

chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);

chessclient.chesspad.statusText.setText("對方退出,請點(diǎn)放棄游戲退出連接");

chessclient.chesspad.isMouseEnabled=false;

}

else if(recMessage.equals("/OK"))

{

chessclient.chesspad.statusText.setText("創(chuàng)建游戲成功,等待別人加入...");

}

else if(recMessage.equals("/error"))

{

chessclient.chatpad.chatLineArea.append("傳輸錯誤:請退出程序,重新加入 \n");

}

else

{

chessclient.chatpad.chatLineArea.append(recMessage+"\n");

chessclient.chatpad.chatLineArea.setCaretPosition(

chessclient.chatpad.chatLineArea.getText().length());

}

}

public void run()

{

String message="";

try

{

while(true)

{

message=chessclient.in.readUTF();

acceptMessage(message);

}

}

catch(IOException es)

{

}

}

}

public class chessClient extends Frame implements ActionListener,KeyListener

{

userPad userpad=new userPad();

chatPad chatpad=new chatPad();

controlPad controlpad=new controlPad();

chessPad chesspad=new chessPad();

inputPad inputpad=new inputPad();

Socket chatSocket;

DataInputStream in;

DataOutputStream out;

String chessClientName=null;

String host=null;

int port=4331;

boolean isOnChat=false; //在聊天?

boolean isOnChess=false; //在下棋?

boolean isGameConnected=false; //下棋的客戶端連接?

boolean isServer=false; //如果是下棋的主機(jī)

boolean isClient=false; //如果是下棋的客戶端

Panel southPanel=new Panel();

Panel northPanel=new Panel();

Panel centerPanel=new Panel();

Panel westPanel=new Panel();

Panel eastPanel=new Panel();

chessClient()

{

super("Java五子棋客戶端");

setLayout(new BorderLayout());

host=controlpad.inputIP.getText();

westPanel.setLayout(new BorderLayout());

westPanel.add(userpad,BorderLayout.NORTH);

westPanel.add(chatpad,BorderLayout.CENTER);

westPanel.setBackground(Color.pink);

inputpad.inputWords.addKeyListener(this);

chesspad.host=controlpad.inputIP.getText();

centerPanel.add(chesspad,BorderLayout.CENTER);

centerPanel.add(inputpad,BorderLayout.SOUTH);

centerPanel.setBackground(Color.pink);

controlpad.connectButton.addActionListener(this);

controlpad.creatGameButton.addActionListener(this);

controlpad.joinGameButton.addActionListener(this);

controlpad.cancelGameButton.addActionListener(this);

controlpad.exitGameButton.addActionListener(this);

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(false);

southPanel.add(controlpad,BorderLayout.CENTER);

southPanel.setBackground(Color.pink);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

if(isOnChat)

{

try

{

chatSocket.close();

}

catch(Exception ed)

{

}

}

if(isOnChess || isGameConnected)

{

try

{

chesspad.chessSocket.close();

}

catch(Exception ee)

{

}

}

System.exit(0);

}

public void windowActivated(WindowEvent ea)

{

}

});

add(westPanel,BorderLayout.WEST);

add(centerPanel,BorderLayout.CENTER);

add(southPanel,BorderLayout.SOUTH);

pack();

setSize(670,548);

setVisible(true);

setResizable(false);

validate();

}

public boolean connectServer(String serverIP,int serverPort) throws Exception

{

try

{

chatSocket=new Socket(serverIP,serverPort);

in=new DataInputStream(chatSocket.getInputStream());

out=new DataOutputStream(chatSocket.getOutputStream());

clientThread clientthread=new clientThread(this);

clientthread.start();

isOnChat=true;

return true;

}

catch(IOException ex)

{

chatpad.chatLineArea.setText("chessClient:connectServer:無法連接,建議重新啟動程序 \n");

}

return false;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==controlpad.connectButton)

{

host=chesspad.host=controlpad.inputIP.getText();

try

{

if(connectServer(host,port))

{

chatpad.chatLineArea.setText("");

controlpad.connectButton.setEnabled(false);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

chesspad.statusText.setText("連接成功,請創(chuàng)建游戲或加入游戲");

}

}

catch(Exception ei)

{

chatpad.chatLineArea.setText("controlpad.connectButton:無法連接,建議重新啟動程序 \n");

}

}

if(e.getSource()==controlpad.exitGameButton)

{

if(isOnChat)

{

try

{

chatSocket.close();

}

catch(Exception ed)

{

}

}

if(isOnChess || isGameConnected)

{

try

{

chesspad.chessSocket.close();

}

catch(Exception ee)

{

}

}

System.exit(0);

}

if(e.getSource()==controlpad.joinGameButton)

{

String selectedUser=userpad.userList.getSelectedItem();

if(selectedUser==null || selectedUser.startsWith("[inchess]") ||

selectedUser.equals(chessClientName))

{

chesspad.statusText.setText("必須先選定一個有效用戶");

}

else

{

try

{

if(!isGameConnected)

{

if(chesspad.connectServer(chesspad.host,chesspad.port))

{

isGameConnected=true;

isOnChess=true;

isClient=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName);

}

}

else

{

isOnChess=true;

isClient=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName);

}

}

catch(Exception ee)

{

isGameConnected=false;

isOnChess=false;

isClient=false;

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chatpad.chatLineArea.setText("chesspad.connectServer無法連接 \n"+ee);

}

}

}

if(e.getSource()==controlpad.creatGameButton)

{

try

{

if(!isGameConnected)

{

if(chesspad.connectServer(chesspad.host,chesspad.port))

{

isGameConnected=true;

isOnChess=true;

isServer=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName);

}

}

else

{

isOnChess=true;

isServer=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName);

}

}

catch(Exception ec)

{

isGameConnected=false;

isOnChess=false;

isServer=false;

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

ec.printStackTrace();

chatpad.chatLineArea.setText("chesspad.connectServer無法連接 \n"+ec);

}

}

if(e.getSource()==controlpad.cancelGameButton)

{

if(isOnChess)

{

chesspad.chessthread.sendMessage("/giveup "+chessClientName);

chesspad.chessVictory(-1*chesspad.chessColor);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chesspad.statusText.setText("請建立游戲或者加入游戲");

}

if(!isOnChess)

{

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chesspad.statusText.setText("請建立游戲或者加入游戲");

}

isClient=isServer=false;

}

}

public void keyPressed(KeyEvent e)

{

TextField inputWords=(TextField)e.getSource();

if(e.getKeyCode()==KeyEvent.VK_ENTER)

{

if(inputpad.userChoice.getSelectedItem().equals("所有人"))

{

try

{

out.writeUTF(inputWords.getText());

inputWords.setText("");

}

catch(Exception ea)

{

chatpad.chatLineArea.setText("chessClient:KeyPressed無法連接,建議重新連接 \n");

userpad.userList.removeAll();

inputpad.userChoice.removeAll();

inputWords.setText("");

controlpad.connectButton.setEnabled(true);

}

}

else

{

try

{

out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+" "+inputWords.getText());

inputWords.setText("");

}

catch(Exception ea)

{

chatpad.chatLineArea.setText("chessClient:KeyPressed無法連接,建議重新連接 \n");

userpad.userList.removeAll();

inputpad.userChoice.removeAll();

inputWords.setText("");

controlpad.connectButton.setEnabled(true);

}

}

}

}

public void keyTyped(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public static void main(String args[])

{

chessClient chessClient=new chessClient();

}

}

/******************************************************************************************

下面是:chessInteface.java

******************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

class userPad extends Panel

{

List userList=new List(10);

userPad()

{

setLayout(new BorderLayout());

for(int i=0;i50;i++)

{

userList.add(i+"."+"沒有用戶");

}

add(userList,BorderLayout.CENTER);

}

}

class chatPad extends Panel

{

TextArea chatLineArea=new TextArea("",18,30,TextArea.SCROLLBARS_VERTICAL_ONLY);

chatPad()

{

setLayout(new BorderLayout());

add(chatLineArea,BorderLayout.CENTER);

}

}

class controlPad extends Panel

{

Label IPlabel=new Label("IP",Label.LEFT);

TextField inputIP=new TextField("localhost",10);

Button connectButton=new Button("連接主機(jī)");

Button creatGameButton=new Button("建立游戲");

Button joinGameButton=new Button("加入游戲");

Button cancelGameButton=new Button("放棄游戲");

Button exitGameButton=new Button("關(guān)閉程序");

controlPad()

{

setLayout(new FlowLayout(FlowLayout.LEFT));

setBackground(Color.pink);

add(IPlabel);

add(inputIP);

add(connectButton);

add(creatGameButton);

add(joinGameButton);

add(cancelGameButton);

add(exitGameButton);

}

}

class inputPad extends Panel

{

TextField inputWords=new TextField("",40);

Choice userChoice=new Choice();

inputPad()

{

setLayout(new FlowLayout(FlowLayout.LEFT));

for(int i=0;i50;i++)

{

userChoice.addItem(i+"."+"沒有用戶");

}

userChoice.setSize(60,24);

add(userChoice);

add(inputWords);

}

}

/**********************************************************************************************

下面是:chessPad.java

**********************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

class chessThread extends Thread

{

chessPad chesspad;

chessThread(chessPad chesspad)

{

this.chesspad=chesspad;

}

public void sendMessage(String sndMessage)

{

try

{

chesspad.outData.writeUTF(sndMessage);

}

catch(Exception ea)

{

System.out.println("chessThread.sendMessage:"+ea);

}

}

public void acceptMessage(String recMessage)

{

if(recMessage.startsWith("/chess "))

{

StringTokenizer userToken=new StringTokenizer(recMessage," ");

String chessToken;

String[] chessOpt={"-1","-1","0"};

int chessOptNum=0;

while(userToken.hasMoreTokens())

{

chessToken=(String)userToken.nextToken(" ");

if(chessOptNum=1 chessOptNum=3)

{

chessOpt[chessOptNum-1]=chessToken;

}

chessOptNum++;

}

chesspad.netChessPaint(Integer.parseInt(chessOpt[0]),Integer.parseInt(chessOpt[1]),Integer.parseInt(chessOpt[2]));

}

else if(recMessage.startsWith("/yourname "))

{

chesspad.chessSelfName=recMessage.substring(10);

}

else if(recMessage.equals("/error"))

{

chesspad.statusText.setText("錯誤:沒有這個用戶,請退出程序,重新加入");

}

else

{

//System.out.println(recMessage);

}

}

public void run()

{

String message="";

try

{

while(true)

{

message=chesspad.inData.readUTF();

acceptMessage(message);

}

}

catch(IOException es)

{

}

}

}

class chessPad extends Panel implements MouseListener,ActionListener

{

int chessPoint_x=-1,chessPoint_y=-1,chessColor=1;

int chessBlack_x[]=new int[200];

int chessBlack_y[]=new int[200];

int chessWhite_x[]=new int[200];

int chessWhite_y[]=new int[200];

int chessBlackCount=0,chessWhiteCount=0;

int chessBlackWin=0,chessWhiteWin=0;

boolean isMouseEnabled=false,isWin=false,isInGame=false;

TextField statusText=new TextField("請先連接服務(wù)器");

Socket chessSocket;

DataInputStream inData;

DataOutputStream outData;

String chessSelfName=null;

String chessPeerName=null;

String host=null;

int port=4331;

chessThread chessthread=new chessThread(this);

chessPad()

{

setSize(440,440);

setLayout(null);

setBackground(Color.pink);

addMouseListener(this);

add(statusText);

statusText.setBounds(40,5,360,24);

statusText.setEditable(false);

}

public boolean connectServer(String ServerIP,int ServerPort) throws Exception

{

try

{

chessSocket=new Socket(ServerIP,ServerPort);

inData=new DataInputStream(chessSocket.getInputStream());

outData=new DataOutputStream(chessSocket.getOutputStream());

chessthread.start();

return true;

}

catch(IOException ex)

{

statusText.setText("chessPad:connectServer:無法連接 \n");

}

return false;

}

public void chessVictory(int chessColorWin)

{

this.removeAll();

for(int i=0;i=chessBlackCount;i++)

{

chessBlack_x[i]=0;

chessBlack_y[i]=0;

}

for(int i=0;i=chessWhiteCount;i++)

{

chessWhite_x[i]=0;

chessWhite_y[i]=0;

}

chessBlackCount=0;

chessWhiteCount=0;

add(statusText);

statusText.setBounds(40,5,360,24);

if(chessColorWin==1)

{ chessBlackWin++;

statusText.setText("黑棋勝,黑:白為"+chessBlackWin+":"+chessWhiteWin+",重新開局,等待白棋下子...");

}

else if(chessColorWin==-1)

{

chessWhiteWin++;

statusText.setText("白棋勝,黑:白為"+chessBlackWin+":"+chessWhiteWin+",重新開局,等待黑棋下子...");

}

}

public void getLocation(int a,int b,int color)

{

if(color==1)

{

chessBlack_x[chessBlackCount]=a*20;

chessBlack_y[chessBlackCount]=b*20;

chessBlackCount++;

}

else if(color==-1)

{

chessWhite_x[chessWhiteCount]=a*20;

chessWhite_y[chessWhiteCount]=b*20;

chessWhiteCount++;

}

}

public boolean checkWin(int a,int b,int checkColor)

{

int step=1,chessLink=1,chessLinkTest=1,chessCompare=0;

if(checkColor==1)

{

chessLink=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a+step)*20==chessBlack_x[chessCompare]) ((b*20)==chessBlack_y[chessCompare]))

{

chessLink=chessLink+1;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a-step)*20==chessBlack_x[chessCompare]) (b*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

chessLink=1;

chessLinkTest=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if((a*20==chessBlack_x[chessCompare]) ((b+step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if((a*20==chessBlack_x[chessCompare]) ((b-step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

chessLink=1;

chessLinkTest=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a-step)*20==chessBlack_x[chessCompare]) ((b+step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a+step)*20==chessBlack_x[chessCompare]) ((b-step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

chessLink=1;

chessLinkTest=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a+step)*20==chessBlack_x[chessCompare]) ((b+step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a-step)*20==chessBlack_x[chessCompare]) ((b-step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

新聞名稱:java五子棋人機(jī)代碼 新手java五子棋完整代碼
標(biāo)題URL:http://muchs.cn/article32/doheesc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管