導航:首頁 > 編程語言 > java五子棋保存對局

java五子棋保存對局

發布時間:2024-10-08 19:55:40

① 鎬庝箞java瀹炵幇榧犳爣鐐瑰嚮鐨勪簲瀛愭嬪晩 錛熷悇浣嶅ぇ渚 甯甯蹇

import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
class wuzi {
public static void main(String[] args) {
frame myFrame =new frame("浜斿瓙媯");
}
}
class frame extends Frame implements ActionListener{
MenuBar mbar=new MenuBar();
Menu mgame=new Menu("閫夐」");
Menu mhelp=new Menu("甯鍔");
MenuItem mstart=new MenuItem("寮濮 ");
MenuItem mclose=new MenuItem("緇撴潫");
MenuItem mabout=new MenuItem("鍏充簬");
myCanvas canvas;
frame(String t) {
super(t);
canvas = new myCanvas(this);
mgame.add(mstart);
mgame.add(mclose);
mhelp.add(mabout);
mgame.addActionListener(this);
mstart.addActionListener(this);
mclose.addActionListener(this);
mhelp.addActionListener(this);
mabout.addActionListener(this);
mbar.add(mgame);
mbar.add(mhelp);
this.setMenuBar(mbar);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
this.add(canvas);
this.pack();
this.show();
this.setLocation(380,100);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mstart)
{
repaint();
}
else if(e.getSource()==mclose){
this.dispose();
System.exit(0);
}
if(e.getSource()==mabout){
about frm=new about();
}
pack();
}
}

class chessman{
private boolean isExist;
private boolean isColor;
public chessman(boolean isExist){
this.isExist = isExist ;
}
boolean getChessExist() {
return isExist;
}
boolean getChessColor() {
return isColor;
}
void setChessExist(boolean m){
isExist = m;
}
void setChessColor(boolean m) { //璁劇疆媯嬬殑棰滆壊
isColor = m;
}
}

class myCanvas extends Canvas implements MouseListener {
frame myFrame;
//淇濆瓨媯嬪瓙
private chessman chessMan[][] = new chessman[19][19];

int mx,my;//鑾峰彇浣嶇疆
int x,y;
boolean state = true;
myCanvas(frame myFrame) {

this.myFrame = myFrame;
setBackground(Color.cyan);
addMouseListener(this);
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
chessMan[i][j] = new chessman(false);
}
public void start() {
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
chessMan[i][j].setChessExist(false);
}
public void paint(Graphics g) {//鐢葷嚎
Dimension size = this.getSize();
g.drawRect(0,0,size.width-1,size.height-1);
g.draw3DRect(1,1,size.width-3,size.height-3,true);
for(int i=0;i<19;i++)
g.drawLine(30,i*24+30,462,i*24+30);
for(int j=0;j<19;j++)
g.drawLine(j*24+30,30,j*24+30,462);
x=(int)((mx-20)/24);
y=(int)((my-20)/24); //璁$畻钀藉瓙浣嶇疆
if(!chessMan[y][x].getChessExist()&&(mx>=18||my>=18)){
chessMan[y][x].setChessExist(true);
chessMan[y][x].setChessColor(state);
state = !state;
}
for(int i =0;i<19;i++)
for(int j=0;j<19;j++){
if(chessMan[i][j].getChessExist())
{
if(chessMan[i][j].getChessColor()==true){
g.setColor(Color.black); //榛樿ら粦媯嬪厛涓

}
else{
g.setColor(Color.white);
}
g.fillOval(j*24+21,i*24+21,18,18);
// whowin(x, y) ;
}
}
}

/* public void whowin(int x, int y) {

int m1 = 0, m2 = 0, m3 = 0, m4 = 0;

for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戝乏-------

if (x - i > 0 && chessMan[x - i][y] == chessMan[x][y]) {
m1++;
}

}

for (int j = 1; j < 5; j++) {//鍒ゆ柇-------鍚戝彸-------

if (x + j < 20 && chessMan[x + j][y] == chessMan[x][y]) {
m1++;
}

}

//鍒ゆ柇杈撹耽------shu綰
for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戜笅-----------

if (y + i < 20 && chessMan[x][y + i] == chessMan[x][y]) {
m2++;
}
}
for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戜笂------------

if (y - i > 0 && chessMan[x][y - i] == chessMan[x][y]) {
m2++;
}

}
//鍒ゆ柇杈撹耽------\綰
for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戝乏涓-------------

if (x - i > 0 && y - i > 0 && chessMan[x - i][y - i] == chessMan[x][y]) {
m3++;

}

}
for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戝彸涓-------------

if (x + i < 20 && y + i < 20 && chessMan[x + i][y + i] == chessMan[x][y]) {
m3++;
}

}

//鍒ゆ柇杈撹耽------/綰
for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戝乏涓------------

if (x - i > 0 && y + i < 20 && chessMan[x - i][y + i] == chessMan[x][y]) {
m4++;
}
}
for (int i = 1; i < 5; i++) {//鍒ゆ柇-------鍚戝彸涓--------------

if (y - i > 0 && x + i < 20 && chessMan[x + i][y - i] == chessMan[x][y]) {
m4++;
}
}
//***************鏈鍚庡垽鏂杈撹耽********************
if (m1 > 3 || m2 > 3 || m3 > 3 || m4 > 3) {
if (chessMan[x][y] == ) {//----鍥犱負涓嬫嬪悗灝嗘嬪瓙棰滆壊鏀逛負瀵規柟鐨勯滆壊錛屾墍浠ヨ繖閲屼簰鐩歌皟涓涓---
JOptionPane.showMessageDialog(this, "hei-----win");
isgamealive = 0;
}
if (chessMan[x][y] == false) {
JOptionPane.showMessageDialog(this, "-----win");
isgamealive = 0;
}
}
}*/
public Dimension getPreferredSize() {
return new Dimension(492,492); //榪斿洖鍥懼艦澶у皬
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
mx = e.getX();
my = e.getY();
repaint();
}
}

class about extends Frame implements WindowListener //璁劇疆鍏充簬
{
about() {
this.setSize(300,170);
this.setTitle("鍏充簬浜斿瓙媯嬫父鎴");
this.addWindowListener(this);
this.setVisible(true);
this.setLocation(400,200);
this.setResizable(false);
this.setLayout(new FlowLayout());
Label text1 =new Label();
Label text2 =new Label();
Label text3=new Label();

text1.setText("娓告垙浠嬬粛錛");
text1.setFont(new Font("榛戜綋",Font.ITALIC,25));
text1.setAlignment(text1.CENTER);
text1.setVisible(true);

text2.setText("妯鎺掞紝絝栨帓錛屾枩鎺掞紝璋佸厛鏀懼埌5涓灝辮耽銆");
text2.setFont(new Font("榛戜綋",Font.PLAIN,15));
text2.setAlignment(text1.LEFT);
text2.setVisible(true);

text3.setText("鍒朵綔鑰咃細0957207鍚村悰");
text3.setFont(new Font("妤蜂綋",Font.PLAIN,15));
text3.setVisible(true);

this.add(text1,"North");
this.add(text2,"West");
this.add(text3,"South");
this.show(true);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){
}
public void windowClosing(WindowEvent e){
this.dispose();
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}

② 求一個簡單的JAVA五子棋代碼!! 網上復制的別來了!

以下是現寫的 實現了兩人對戰 自己復制後運行把 沒什麼難度 類名 Games

import java.util.Scanner;

public class Games {

private String board[][];
private static int SIZE = 17;
private static String roles = "A玩家";
//初始化數組
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!=0&&j==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) {
// 從數組挨個查找找到某個類型的棋子就從該棋子位置向右,向下,斜向右下 各查找5連續的位置看是否為5個相同的
int xlabel;// 記錄第一次找到某個棋子的x坐標
int ylabel;// 記錄第一次找到某個棋子的y坐標
// ●○╋
// 判斷人是否贏了
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j].equals("○")) {
xlabel = i;
ylabel = j;
// 橫向找 x坐標不變 y坐標以此加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坐標不變 y坐標以此加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坐標,必須在0-15范圍內,xy以空格隔開,輸入16 16結束程序");
int x = sc.nextInt();
int y = sc.nextInt();
if (x == SIZE && y == SIZE) {
System.out.println("程序結束");
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("此處已經有棋子,從新輸入");
continue;
}
if(roles.equals("A玩家")){
roles = "B玩家";
}else if(roles.equals("B玩家")){
roles = "A玩家";
}
}

}

}

系統框圖如下 java實現五子棋程序 可以實現人人對戰 人機對戰 簡單功能 悔棋 認輸

一、實驗題目

五子棋游戲。

二、問題分析

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

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

三、功能設計

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

1.等級設置

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

2.悔棋功能

模擬棧機制實現人悔棋,不限步長的悔棋。對應程序中的悔棋按鈕。

3.棋面繪制

根據不同機計算機的屏幕解析度,繪制逼真的棋盤。

4.圖片引入

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

5.背景設置

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

6.音樂播放

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

7.時間顯示

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

8.其他小功能

支持和棋、認輸、開啟新游戲、退出遊戲等操作。

四、數據結構與演算法設計

數據結構部分

1.當前棋局的存儲結構

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

1.本程序需要頻繁隨機訪問15*15的交叉點,對應查詢該點狀態以及改變該點狀態,隨機訪問是數組的特點。

2.15*15=225開二維數組的內存需求相對現在內存為2G及以上的計算機完全可以接受,且數組實現簡單、操作方便。

基於以上兩點,盡管創建動態的順序表—鏈表可能可以節省少量內存(可以只存當前有棋的點,原數組對應位置為0的點可以不存),但選擇數組的優勢完全在上述兩點體現了出來。

2.實現悔棋操作的數據結構

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

確定用ArrayList類實現棧機制後就必須考慮每個ArrayList單元具體存儲什麼。剛開始我存儲的是當前的棋局,即整個局面,而每個局面對應一個二維數組,這樣是很佔用內存的。試想一下,在最壞情況下,225個ArrayList單元,每個單元存放一個15*15的二維數組,盡管225*15*15在Java的內存管理機制下不會爆棧,但也是極不劃算的。之所以說不劃算,是因為有更好的解決方案。由於每次悔棋只是在回退倒數一步,多步悔棋只需循環回退,所以可以只存儲當前棋局最後一步的下法,對應一個二維點,完全可以自定義一個二維坐標類chessOneStep。

演算法設計部分

Java語言是面向對象的語言。我在進行五子棋游戲編程是總共傳創建了11個自定義的類。在編寫程序的過程中,我有一個明顯的體驗就是面向對象編程就是一項有關對象設計和對象介面技術,很多關鍵的技術就是如何設計自定義的對象。

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

1.mainFrame類:主框架類,我應用程序的入口;

2.chessPositon類:主控類,這個類是我程序的核心類,負責控制雙方的下棋,以及調用其他的類完成當前棋局的顯示繪制;

3.chessPanel類:面板類,調用其他底層類完成當前棋局的顯示繪制;

4.chessBoard類:棋盤繪制類,負責棋盤的繪制;

5.chessImage類:文件類,包含各種資源(背景圖片、背景音樂)以及靜態全局變數(publicstaticType);

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

7.chessMusic類:音樂類,負責調用Java庫類完成背景音樂、下棋音樂、取勝音樂等的播放;

8.chessPiece類:棋局類,定義棋局二維數組數據結構並完成相關操作;

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

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

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

詳細設計

1.mainFrame類

作為我的五子棋程序的主類,mainFrame類主要實例化相關的對象,如chessbutton,chessborad等,從而完成框架的創建。更重要的是實例化chessposition,這是本程序的核心類,控制游戲雙方行棋過程完成人機互動下棋,然後將MyChessPosition與滑鼠響應addMouseListener()關聯起來。

2.chessMusic類

一個好的游戲必須給人一種身臨其境的感覺,而聲音是營造這種氛圍的重要因素。參照網上各游戲運行商的音樂配置,我選擇相關逼真的聲音。包括背景音樂、下棋棋子落到棋盤發出的聲音以及一方勝出的配樂。所有這些功能的實現,依賴於自定義的chessMusic類,採用AudioInputStream配合Clip的方式完成音樂播放的軟硬體工作,然後定義兩個介面chessmusic(StringName)和Stop(),前者完成播放功能,後者完成關閉當前音樂功能。因為音頻文件相對較大,而我的程序提供在不同背景樂之間切換的功能,所以在打開另一個音頻文件之前必須關閉前一個正在播放的音頻文件,防止出現溢出。

3.chessImage類

適當的動畫或圖片能給游戲玩家帶來美的體驗。所以我的五子棋程序界面在不失和諧的前提下引入了盡可能多的圖片,包括對弈雙方、棋缽等。圖片引入的具體工作通過語句importjavax.imageio.ImageIO完成。同時,由於圖片要在用到它的類中被訪問,為了避免頻繁調用函數,我直接將圖片相關聯的對象定義為publicstatic,表明是公用的、靜態的。進一步引申開去,我將程序中用到的靜態全局變數都定義在chessImage類中。具體如下:

publicstaticDatebegin;//每局開始時間

publicstaticDatecur;//每局結束時間

;//結束端點1

;//結束端點2

publicstaticbooleanIsGameOver;//是否只有一方獲勝

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

publicstaticintColorOfWindows[][]={{60,179,113},{245,245,245},{122,122,122}};//背景顏色

publicstaticintWitchMatch;//背景搭配

;//背景音樂

publicstaticintCurrentStep;//記錄當前步數

publicstaticintRank;//設置難度等級

;//判斷是否認輸

publicstaticbooleanIsTie;//判斷是否認輸

publicstaticStringMessage;//輸出提示信息

publicstaticImageIconImage;//圖標

publicstaticImageblackBoard;//白棋盤

publicstaticImagewhiteBoard;//黑棋盤

publicstaticImageblackChess;//白棋棋子圖片

publicstaticImagewhiteChess;//白棋棋子圖片

publicstaticImageRightPlayer;//白棋棋罐圖片

publicstaticImageLeftPlayer;//白棋玩家頭像圖片

publicstaticStringpath="src/";//圖片的保存路徑

4.chessButton類

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

(1)、按鈕組件

本程序有5個按鈕,支持和棋、認輸、新游戲、退出、悔棋等。認輸和和棋按鈕終止當前的棋局,給出相應的提示信息;退出按鈕調用系統System.exit(0)的函數正常返回;悔棋按鈕調用後面要介紹的chessList類實現悔棋;新游戲按鈕則刷新當前棋局准備下一輪,要將記錄當前棋局的二維數組全部置0,刷新當前棋局開始時間等。

(2)、單選按鈕組件

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

(3)、文本框

在不同的單選按鈕前添加相應的文本框,提示用戶可以實現的功能。同時我用顏色模擬出顯示當前棋局耗用時間的文本框。

不論按鈕還是單選按鈕都要關聯相應的消息,把相應功能的實現放在消息響應處理函數理。這些主要是實現Java庫提供的消息響應介面里的方法。

5.chessPiece類

主要完成當前棋面的存儲,存儲棋面的數據結構為二維數組int[][]PositionFlag;然後定義獲取、設置某點以及整個棋面的狀態的方法。

(1)、SetPositionFlag(intx,inty,intflag)//設置(x,y)處的狀態為flag

(2)、GetPositionFlag(intx,inty)//獲取(x,y)處的狀態

(3)、SetAllFlag(int[][]NewFlag)//設置當前整個棋面的狀態為NewFlag

(4)、GetAllFlag()//獲取當前整個棋面的狀態

(5)、DrawChessPiece(Graphicsg)//繪制當前局面的棋子

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

6.chessBoard類

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

方法一:

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

方法二:

為了克服方法一繪制非水平或豎直線時造成的走樣,同時也為了更進一步學習Java語言,我猜想肯定會有類似OpenGL中設置線寬的畫刷,於是上網網路找到了相應的畫刷Stroke類。通過Java庫實現繪制不同線寬的直線,達到了反走樣效果。

7.chessOneStep類

這個類是為了配合chessList類實現悔棋以及在計算機下棋演算法實現返回有效狀態點而設計的。主要數據成員為

privateintx,y,weight;//其中x,y表示點坐標,weight表示將棋下到該點獲得的估價值。

主要方法如下:

(1)、GetX()//獲得當前對象的x坐標

(2)、GetY()//獲得當前對象的y坐標

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

8.chessList類

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

(1)、AddStep(chessOneStepOneStep)//添加一步棋到List中

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

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

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

由於每次刪除當前List中的最後一個元素,實現後進先出,所以可以模擬棧的功能實現悔棋。

9.myCompare類

由於在計算機下棋的極大極小博弈樹演算法中需要對自定義對象chessOneStep按weight進行排序,所以引入了myCompare類,通過實現Comparator介面中的compare方法完成自定義對象排序。

10.chessPanel類

程序的自定義面板類,主要負責完成當前框架內容的顯示。這是一個重要的與框架和圖形顯示密切相關的類。主要數據成員為

privatechessboardMyChessBoard;//當前顯示棋盤

privatechesspieceMyChessPiece;//當前顯示整個棋面的狀態

主要方法如下:

(1)、chesspanel(chessboardMyChessBoard1,chesspieceMyChessPiece1)//構造函數,分別用MyChessBoard1和MyChessPiece1初始化MyChessBoard和MyChessPiece

(2)display(chessboardMyChessBoard1,chesspieceMyChessPiece1)//自定義顯示回調函數,調用repaint()完成重新繪制游戲界面

(3)、paintComponent(Graphicsg)//核心方法,調用各種函數完成具體的繪制工作

11.chessPositon類

程序演算法核心類,總的功能是控制人和計算機輪流下棋,以及調用chessPanel類中的display(chessboard,chesspiece)方法完成界面的實時刷新。關於chessPositon類,我在此將重點介紹。chessPosition類的主要數據成員如下:

;//當前顯示棋盤

;//當前顯示整個棋面的狀態

;////當前顯示面板

=newchesslist();//當前下棋集合,用於悔棋

finalprivatestaticintINF=(1<<30);//表示正無窮大的常量,用於極大極小博弈數搜索演算法

publicstaticbooleanCanGo;//控制當前下棋一方

類的設計集中體現在成員方法的設計上。實現人機對戰,只有語言是遠遠不夠的,還要加入演算法,用演算法引導計算機下棋。下面介紹該類的方法成員:

(1)、chessposition(chesspanel,chessboard,chesspiece)//帶有參數的構造函數

(2)、chessposition()

不帶參數的構造函數

(3)、mouseClicked(MouseEventevent)

滑鼠響應函數,負責人的下棋,根據滑鼠點擊的位置轉換得到所在棋盤的相對位置。如果該位置不合法,即超出棋盤有效范圍,點擊無響應;如果該位置上已有棋,彈出消息框給出提示。這二者都要求重新給出下棋位置,即當前滑鼠響應無效…直到點擊到棋盤有效區域。

(4)、IsOver(int[][]Array,intx,inty)

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

(5)display()

通過調用自定義面板類的顯示回調函數用於重新顯示游戲界面,達到每下一步棋及時更新游戲界面的目的。

(6)、GetValue(intflag,intnum)

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

(7)、GetPredictValue(intflag,intnum)

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

(8)、Evaluate(int[][]Array,intx,inty)

根據棋面具體情況以及預先設定的估值函數,對某個點對應的局面進行評估。由於每次雙方只能下一顆棋,所以可以每次取當前局面的所有點中對應估值最大值點的估值作為整個局面的估值。

(9)、GetGreedNext()

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

Max取負無窮大

for(行i從0到15)

{

For(列j從0到15)

{

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

{

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

b.假設放上一顆由計算機控制的棋,求估價值;

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

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

}

}

}

最終Max對應的點就是當前整個局面中最大的估值點。至於上述為什麼要考慮雙方都在該點下棋的情況呢?主要原因為下五子棋是個攻防兼備的過程,不僅要考慮自己對自己最有利,還要考慮對對手最不利,通俗來講就是在自己贏的時候不能讓對手先贏。

(10)、GetSearchNext(intLookLength)

derectSearch(int[][]Array,booleanwho,intdeepth)

計算機下棋方法2:直接搜索法,對應難度等級為中等。

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

a.採用深度優先搜索需要遞歸,遞歸中狀態過多可能會爆棧,我們知道遞歸是用棧機制來實現的;採用寬度優先搜索又需要存儲為擴展的節點,這對內存容量要求很高。

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

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

直接搜索演算法偽代碼為

GetSearch(booleanflag,intdeep)

{

如果deep等於0,返回當前棋局估值;

for(行i從0到15)

{

For(列j從0到15)

{

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

{

如果輪到計算機下棋,置標志位為2

GetSearch(!flag,deep-1);

如果輪到人下棋,置標志位為1;

GetSearch(!flag,deep-1);

}

}

}

}

(11)、GetMinMaxsearchNext(intLookLength)

MinMaxsearch(int[][]Array,booleanwho,intdeepth)

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

④ java 五子棋 源代碼 在我的基礎上加個悔棋 判斷勝負後把勝利的一方顯示在屏幕上 或別的標明勝利一方的 謝

我的設計是:
allChess[19][19] ;//19 * 19 的棋盤 存放所有的棋子
0 --> 當前沒有棋子
1 --> 黑子
2 --> 白子
比如: allChess[2][3]=2 --> 第3行第4列為白子

如果想要悔棋的話,我的理解是這樣的:
拿白子舉例:
每下一個白子後,保存兩個數組,連續下兩次白子之後的數組,如果想悔棋,黑方確認之後,返回到上一次白子下後的數組。

樓主這樣試試

⑤ java課程設計,做了網路五子棋,老師要我加復盤的功能,怎麼實現思路

這個很簡單。五子棋棋盤是一個二維數組,然後2數組裡面有每個位置上只能有3個值,0,1,2,0表示沒有棋子,1表示白棋,2表示黑棋。然後你用一個類把二維數組封裝起來,然後保存每一步時候的狀態就OK了。

⑥ 跪求JAVA五子棋源代碼

很sb的電腦五子棋:
import java.io.*;
import java.util.*;

public class Gobang {
// 定義一個二維數組來充當棋盤
private String[][] board;
// 定義棋盤的大小
private static int BOARD_SIZE = 15;

public void initBoard() {
// 初始化棋盤數組
board = new String[BOARD_SIZE][BOARD_SIZE];
// 把每個元素賦為"╋",用於在控制台畫出棋盤
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
// windows是一行一行來列印的。坐標值為(行值, 列值)
board[i][j] = "╋";
}
}
}

// 在控制台輸出棋盤的方法
public void printBoard() {
// 列印每個數組元素
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
// 列印數組元素後不換行
System.out.print(board[i][j]);
}
// 每列印完一行數組元素後輸出一個換行符
System.out.print("\n");
}
}

// 該方法處理電腦下棋:隨機生成2個整數,作為電腦下棋的坐標,賦給board數組。
private void compPlay() {
// 構造一個隨機數生成器
Random rnd = new Random();
// Random類的nextInt(int n))方法:隨機地生成並返回指定范圍中的一個 int 值,
// 即:在此隨機數生成器序列中 0(包括)和 n(不包括)之間均勻分布的一個int值。
int compXPos = rnd.nextInt(15);
int compYPos = rnd.nextInt(15);
// 保證電腦下的棋的坐標上不能已經有棋子(通過判斷對應數組元素只能是"╋"來確定)
while (board[compXPos][compYPos].equals("╋") == false) {
compXPos = rnd.nextInt(15);
compYPos = rnd.nextInt(15);
}
System.out.println(compXPos);
System.out.println(compYPos);
// 把對應的數組元素賦為"○"。
board[compXPos][compYPos] = "○";
}

// 該方法用於判斷勝負:進行四次循環掃描,判斷橫、豎、左斜、右斜是否有5個棋連在一起
private boolean judgeWin() {
// flag表示是否可以斷定贏/輸
boolean flag = false;
// joinEle:將每一個橫/豎/左斜/右斜行中的元素連接起來得到的一個字元串
String joinEle;
// 進行橫行掃描
for (int i = 0; i < BOARD_SIZE; i++) {
// 每掃描一行前,將joinEle清空
joinEle = "";
for (int j = 0; j < BOARD_SIZE; j++) {
joinEle += board[i][j];
}
// String類的contains方法:當且僅當該字元串包含指定的字元序列時,返回true。
if (joinEle.contains("●●●●●")) {
System.out.println("您贏啦!");
flag = true;
// 停止往下繼續執行,提前返回flag。
// 如果執行了這個return,就直接返回該方法的調用處;
// 不會再執行後面的任何語句,包括最後那個return語句。
// (而break僅僅是完全跳出這個for循環,還會繼續執行下面的for循環。)
return flag;
} else if (joinEle.contains("○○○○○")) {
System.out.println("您輸啦!");
flag = true;
// 提前返回flag
return flag;
}
}
// 進行豎行掃描
for (int i = 0; i < BOARD_SIZE; i++) {
joinEle = "";
for (int j = 0; j < BOARD_SIZE; j++) {
// 豎行的元素是它們的列值相同
joinEle += board[j][i];
}
if (joinEle.contains("●●●●●")) {
System.out.println("您贏啦!");
flag = true;
return flag;
} else if (joinEle.contains("○○○○○")) {
System.out.println("您輸啦!");
flag = true;
return flag;
}
}
// 進行左斜行掃描
for (int i = -(BOARD_SIZE - 2); i < BOARD_SIZE - 1; i++) {
joinEle = "";
for (int j = 0; j < BOARD_SIZE; j++) {
int line = i + j;
// 只截取坐標值沒有越界的點
if (line >= 0 && line < 15) {
joinEle += board[j][line];
}
}
if (joinEle.contains("●●●●●")) {
System.out.println("您贏啦!");
flag = true;
return flag;
} else if (joinEle.contains("○○○○○")) {
System.out.println("您輸啦!");
flag = true;
return flag;
}
}
// 進行右斜行掃描
for (int i = 1; i < 2 * (BOARD_SIZE - 1); i++) {
joinEle = "";
for (int j = 0; j < BOARD_SIZE; j++) {
int line = i - j;
if (line >= 0 && line < 15) {
joinEle += board[j][line];
}
}
if (joinEle.contains("●●●●●")) {
System.out.println("您贏啦!");
flag = true;
return flag;
} else if (joinEle.contains("○○○○○")) {
System.out.println("您輸啦!");
flag = true;
// 最後這個return可省略
}
}
// 確保該方法有返回值(如果上面條件都不滿足時)
return flag;
}

public static void main(String[] args) throws Exception, IOException {
Gobang gb = new Gobang();
gb.initBoard();
gb.printBoard();
// BufferedReader類:帶緩存的讀取器————從字元輸入流中讀取文本,並緩存字元。可用於高效讀取字元、數組和行。
// 最好用它來包裝所有其 read() 操作可能開銷很高的 Reader(如 FileReader 和 InputStreamReader)。
// 下面構造一個讀取器對象。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 定義輸入字元串
String inputStr = null;
// br.readLine():每當在鍵盤上輸入一行內容按回車,剛輸入的內容將被br(讀取器對象)讀取到。
// BufferedReader類的readLine方法:讀取一個文本行。
// 初始狀態由於無任何輸入,br.readLine()會拋出異常。因而main方法要捕捉異常。
while ((inputStr = br.readLine()) != null) {
// 將用戶輸入的字元串以逗號(,)作為分隔符,分隔成2個字元串。
// String類的split方法,將會返回一個拆分後的字元串數組。
String[] posStrArr = inputStr.split(",");
// 將2個字元串轉換成用戶下棋的坐標
int xPos = Integer.parseInt(posStrArr[0]);
int yPos = Integer.parseInt(posStrArr[1]);
// 校驗用戶下棋坐標的有效性,只能是數字,不能超出棋盤范圍
if (xPos > 15 || xPos < 1 || yPos > 15 || yPos < 1) {
System.out.println("您下棋的坐標值應在1到15之間,請重新輸入!");
continue;
}
// 保證用戶下的棋的坐標上不能已經有棋子(通過判斷對應數組元素只能是"╋"來確定)
// String類的equals方法:比較字元串和指定對象是否相等。結果返回true或false。
if (gb.board[xPos - 1][yPos - 1].equals("╋")) {
// 把對應的數組元素賦為"●"。
gb.board[xPos - 1][yPos - 1] = "●";
} else {
System.out.println("您下棋的點已有棋子,請重新輸入!");
continue;
}
// 電腦下棋
gb.compPlay();
gb.printBoard();
// 每次下棋後,看是否可以斷定贏/輸了
if (gb.judgeWin() == false) {
System.out.println("請輸入您下棋的坐標,應以x,y的格式:");
} else {
// 完全跳出這個while循環,結束下棋
break;
}
}
}
}

⑦ 用JAVA設計游戲:五子棋游戲

下面的源代碼分為4個文件;
chessClient.java:客戶端主程序。
chessInterface.java:客戶端的界面。
chessPad.java:棋盤的繪制。
chessServer.java:伺服器端。
可同時容納50個人同時在線下棋,聊天。
沒有加上詳細注釋,不過絕對可以運行,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(userNumber>0 && !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無法關閉");
}
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("對方退出,請點放棄游戲退出連接");
chessclient.chesspad.isMouseEnabled=false;
}
else if(recMessage.equals("/OK"))
{
chessclient.chesspad.statusText.setText("創建游戲成功,等待別人加入...");
}
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; //如果是下棋的主機
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("連接成功,請創建游戲或加入游戲");
}

}
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;i<50;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("連接主機");
Button creatGameButton=new Button("建立游戲");
Button joinGameButton=new Button("加入游戲");
Button cancelGameButton=new Button("放棄游戲");
Button exitGameButton=new Button("關閉程序");

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;i<50;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("請先連接伺服器");

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]))
{

閱讀全文

與java五子棋保存對局相關的資料

熱點內容
閃傳的文件夾在哪裡 瀏覽:737
htc820u那些程序可以停用 瀏覽:716
macapp怎麼下載 瀏覽:83
excel顯示文件隱藏擴展名 瀏覽:889
華擎h110u盤安裝win10 瀏覽:85
電腦u盤文件自動備份 瀏覽:393
手機如何用網路限速軟體 瀏覽:433
wdlinux忘記密碼 瀏覽:949
廣饒蘋果手機 瀏覽:852
手機文件通用格式 瀏覽:453
微信紅包打開沒有拆字 瀏覽:316
華為90更新文件夾在哪裡 瀏覽:543
三星7009電信版本 瀏覽:695
成都編程培訓班哪個好 瀏覽:363
linuxfind文件大小 瀏覽:257
nvdia改密碼 瀏覽:825
華為微信解鎖怎麼設置不了 瀏覽:362
word中查找數字 瀏覽:262
java五子棋保存對局 瀏覽:156
word兩個文件同時打開文件 瀏覽:152

友情鏈接