java五子代碼 java五子棋簡(jiǎn)單代碼

java五子棋源代碼

我這有算法 不過(guò)沒(méi)做swing界面 DOS下可以直接運(yùn)行 要不你拿去改改

創(chuàng)新互聯(lián)建站專(zhuān)注于姜堰網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供姜堰營(yíng)銷(xiāo)型網(wǎng)站建設(shè),姜堰網(wǎng)站制作、姜堰網(wǎng)頁(yè)設(shè)計(jì)、姜堰網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造姜堰網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供姜堰網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

import java.io.BufferedReader;

import java.io.InputStreamReader;

/*

* 五子棋源碼

* 所用的符號(hào)標(biāo)識(shí) ○ ● ┼

* 在dos界面下運(yùn)行效果最佳

* 黑白雙方交叉輸入落子點(diǎn)坐標(biāo) 以逗號(hào)隔開(kāi)如 1,1

* 輸入空 或者一方勝出 程序停止

*/

public class Chess {

// 定義棋盤(pán)大小

private static int SIZE = 15;

private String[][] board;

public static void main(String[] args) throws Exception {

Chess chess = new Chess();

// 初始化棋盤(pán)

chess.initBoard();

// 畫(huà)出棋盤(pán)

chess.paintBoard();

// 根據(jù)who的奇偶性 判斷該誰(shuí)落子

int who = 0;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str = null;

while ((str = br.readLine()) != null) {

// 提取輸入的 以","分開(kāi)的數(shù) 分別對(duì)應(yīng)x y坐標(biāo)

String[] posStr = str.split(",");

int x = Integer.parseInt(posStr[0]);

int y = Integer.parseInt(posStr[1]);

// 判斷落子點(diǎn)是否合法

if (!"┼".equals(chess.board[x][y])) {

System.out.println("這里不允許落子,請(qǐng)重下..");

continue;

}

if (who % 2 == 0) {

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

chess.paintBoard();

// 判斷是否勝出

if (chess.isWin("○")) {

System.out.println("○獲勝");

return;

}

} else {

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

chess.paintBoard();

// 判斷是否勝出

if (chess.isWin("●")) {

System.out.println("●獲勝");

return;

}

}

who++;

}

}

// 以 "┼" 初始化棋盤(pán)

public void initBoard() {

board = new String[SIZE][SIZE];

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

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

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

}

}

}

// 描繪出當(dāng)前棋盤(pán)

public void paintBoard() {

// 以下代碼 這里為了使得棋盤(pán)坐標(biāo)看的清楚 加入了坐標(biāo)值

System.out.print(" ");

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

if (i 10) {

System.out.print(i + " ");

} else {

System.out.print((i - 10) + " ");

}

}

System.out.println();

// 以上代碼 這里為了使得棋盤(pán)坐標(biāo)看的清楚 加入了坐標(biāo)值

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

if (i 10) {

System.out.print(" " + i);

} else {

System.out.print(i);

}

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

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

}

System.out.println();

}

}

// 判斷是否獲勝

public boolean isWin(String sign) {

int count = 0;

// 橫向掃描各行

// 有一個(gè)sign的子 計(jì)數(shù)器+1

// 碰到不是sign的子 計(jì)數(shù)器置零

// 計(jì)數(shù)器到達(dá)5時(shí) 返回true 勝出

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

count = 0;

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

if (board[i][j].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

// 縱向掃描各列

// 方法同上

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

count = 0;

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

if (board[j][i].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

// 掃描斜右下

// 在橫向掃描基礎(chǔ)上 外層套一個(gè)循環(huán) 以k為標(biāo)識(shí)

// 坐標(biāo)x-y的范圍在-SIZE+1到SIZE-1之間

// 當(dāng)x-y的值相等時(shí) 在同一右下斜線上

for (int k = -SIZE + 1; k = SIZE - 1; k++) {

count = 0;

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

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

if (i - j == k) {

if (board[i][j].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

}

}

// 掃描斜左邊上

// 方法同上 坐標(biāo)x+y的值相等時(shí) 在同一左上斜線上

for (int k = -SIZE + 1; k = SIZE - 1; k++) {

count = 0;

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

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

if (i + j == k) {

if (board[i][j].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

}

}

return false;

}

}

求java五子棋代碼要注釋~現(xiàn)在1小時(shí)等待

package org.crazyit.gobang;

import java.io.BufferedReader;

import java.io.InputStreamReader;

/**

* 五子棋游戲類(lèi)

*

* @author yangenxiong yangenxiong2009@gmail.com

* @author Kelvin Mak kelvin.mak125@gmail.com

* @version 1.0

* br/網(wǎng)站: a href=""瘋狂Java聯(lián)盟/a

* brCopyright (C), 2009-2010, yangenxiong

* brThis program is protected by copyright laws.

*/

public class GobangGame {

// 定義達(dá)到贏條件的棋子數(shù)目

private final int WIN_COUNT = 5;

// 定義用戶(hù)輸入的X坐標(biāo)

private int posX = 0;

// 定義用戶(hù)輸入的X坐標(biāo)

private int posY = 0;

// 定義棋盤(pán)

private Chessboard chessboard;

/**

* 空構(gòu)造器

*/

public GobangGame() {

}

/**

* 構(gòu)造器,初始化棋盤(pán)和棋子屬性

*

* @param chessboard

* 棋盤(pán)類(lèi)

*/

public GobangGame(Chessboard chessboard) {

this.chessboard = chessboard;

}

/**

* 檢查輸入是否合法。

*

* @param inputStr

* 由控制臺(tái)輸入的字符串。

* @return 字符串合法返回true,反則返回false。

*/

public boolean isValid(String inputStr) {

// 將用戶(hù)輸入的字符串以逗號(hào)(,)作為分隔,分隔成兩個(gè)字符串

String[] posStrArr = inputStr.split(",");

try {

posX = Integer.parseInt(posStrArr[0]) - 1;

posY = Integer.parseInt(posStrArr[1]) - 1;

} catch (NumberFormatException e) {

chessboard.printBoard();

System.out.println("請(qǐng)以(數(shù)字,數(shù)字)的格式輸入:");

return false;

}

// 檢查輸入數(shù)值是否在范圍之內(nèi)

if (posX 0 || posX = Chessboard.BOARD_SIZE || posY 0

|| posY = Chessboard.BOARD_SIZE) {

chessboard.printBoard();

System.out.println("X與Y坐標(biāo)只能大于等于1,與小于等于" + Chessboard.BOARD_SIZE

+ ",請(qǐng)重新輸入:");

return false;

}

// 檢查輸入的位置是否已經(jīng)有棋子

String[][] board = chessboard.getBoard();

if (board[posX][posY] != "十") {

chessboard.printBoard();

System.out.println("此位置已經(jīng)有棋子,請(qǐng)重新輸入:");

return false;

}

return true;

}

/**

* 開(kāi)始下棋

*/

public void start() throws Exception {

// true為游戲結(jié)束

boolean isOver = false;

chessboard.initBoard();

chessboard.printBoard();

// 獲取鍵盤(pán)的輸入

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String inputStr = null;

// br.readLine:每當(dāng)鍵盤(pán)輸入一行內(nèi)容按回車(chē)鍵,則輸入的內(nèi)容被br讀取到

while ((inputStr = br.readLine()) != null) {

isOver = false;

if (!isValid(inputStr)) {

// 如果不合法,要求重新輸入,再繼續(xù)

continue;

}

// 把對(duì)應(yīng)的數(shù)組元素賦為"●"

String chessman = Chessman.BLACK.getChessman();

chessboard.setBoard(posX, posY, chessman);

// 判斷用戶(hù)是否贏了

if (isWon(posX, posY, chessman)) {

isOver = true;

} else {

// 計(jì)算機(jī)隨機(jī)選擇位置坐標(biāo)

int[] computerPosArr = computerDo();

chessman = Chessman.WHITE.getChessman();

chessboard.setBoard(computerPosArr[0], computerPosArr[1],

chessman);

// 判斷計(jì)算機(jī)是否贏了

if (isWon(computerPosArr[0], computerPosArr[1], chessman)) {

isOver = true;

}

}

// 如果產(chǎn)生勝者,詢(xún)問(wèn)用戶(hù)是否繼續(xù)游戲

if (isOver) {

// 如果繼續(xù),重新初始化棋盤(pán),繼續(xù)游戲

if (isReplay(chessman)) {

chessboard.initBoard();

chessboard.printBoard();

continue;

}

// 如果不繼續(xù),退出程序

break;

}

chessboard.printBoard();

System.out.println("請(qǐng)輸入您下棋的坐標(biāo),應(yīng)以x,y的格式輸入:");

}

}

/**

* 是否重新開(kāi)始下棋。

*

* @param chessman

* "●"為用戶(hù),"○"為計(jì)算機(jī)。

* @return 開(kāi)始返回true,反則返回false。

*/

public boolean isReplay(String chessman) throws Exception {

chessboard.printBoard();

String message = chessman.equals(Chessman.BLACK.getChessman()) ? "恭喜您,您贏了,"

: "很遺憾,您輸了,";

System.out.println(message + "再下一局?(y/n)");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

if (br.readLine().equals("y")) {

// 開(kāi)始新一局

return true;

}

return false;

}

/**

* 計(jì)算機(jī)隨機(jī)下棋

*/

public int[] computerDo() {

int posX = (int) (Math.random() * (Chessboard.BOARD_SIZE - 1));

int posY = (int) (Math.random() * (Chessboard.BOARD_SIZE - 1));

String[][] board = chessboard.getBoard();

while (board[posX][posY] != "十") {

posX = (int) (Math.random() * (Chessboard.BOARD_SIZE - 1));

posY = (int) (Math.random() * (Chessboard.BOARD_SIZE - 1));

}

int[] result = { posX, posY };

return result;

}

/**

* 判斷輸贏

*

* @param posX

* 棋子的X坐標(biāo)。

* @param posY

* 棋子的Y坐標(biāo)

* @param ico

* 棋子類(lèi)型

* @return 如果有五顆相鄰棋子連成一條直接,返回真,否則相反。

*/

public boolean isWon(int posX, int posY, String ico) {

// 直線起點(diǎn)的X坐標(biāo)

int startX = 0;

// 直線起點(diǎn)Y坐標(biāo)

int startY = 0;

// 直線結(jié)束X坐標(biāo)

int endX = Chessboard.BOARD_SIZE - 1;

// 直線結(jié)束Y坐標(biāo)

int endY = endX;

// 同條直線上相鄰棋子累積數(shù)

int sameCount = 0;

int temp = 0;

// 計(jì)算起點(diǎn)的最小X坐標(biāo)與Y坐標(biāo)

temp = posX - WIN_COUNT + 1;

startX = temp 0 ? 0 : temp;

temp = posY - WIN_COUNT + 1;

startY = temp 0 ? 0 : temp;

// 計(jì)算終點(diǎn)的最大X坐標(biāo)與Y坐標(biāo)

temp = posX + WIN_COUNT - 1;

endX = temp Chessboard.BOARD_SIZE - 1 ? Chessboard.BOARD_SIZE - 1

: temp;

temp = posY + WIN_COUNT - 1;

endY = temp Chessboard.BOARD_SIZE - 1 ? Chessboard.BOARD_SIZE - 1

: temp;

// 從左到右方向計(jì)算相同相鄰棋子的數(shù)目

String[][] board = chessboard.getBoard();

for (int i = startY; i endY; i++) {

if (board[posX][i] == ico board[posX][i + 1] == ico) {

sameCount++;

} else if (sameCount != WIN_COUNT - 1) {

sameCount = 0;

}

}

if (sameCount == 0) {

// 從上到下計(jì)算相同相鄰棋子的數(shù)目

for (int i = startX; i endX; i++) {

if (board[i][posY] == ico board[i + 1][posY] == ico) {

sameCount++;

} else if (sameCount != WIN_COUNT - 1) {

sameCount = 0;

}

}

}

if (sameCount == 0) {

// 從左上到右下計(jì)算相同相鄰棋子的數(shù)目

int j = startY;

for (int i = startX; i endX; i++) {

if (j endY) {

if (board[i][j] == ico board[i + 1][j + 1] == ico) {

sameCount++;

} else if (sameCount != WIN_COUNT - 1) {

sameCount = 0;

}

j++;

}

}

}

return sameCount == WIN_COUNT - 1 ? true : false;

}

public static void main(String[] args) throws Exception {

GobangGame gb = new GobangGame(new Chessboard());

gb.start();

}

}

跪求JAVA五子棋源代碼

很sb的電腦五子棋:

import java.io.*;

import java.util.*;

public class Gobang {

// 定義一個(gè)二維數(shù)組來(lái)充當(dāng)棋盤(pán)

private String[][] board;

// 定義棋盤(pán)的大小

private static int BOARD_SIZE = 15;

public void initBoard() {

// 初始化棋盤(pán)數(shù)組

board = new String[BOARD_SIZE][BOARD_SIZE];

// 把每個(gè)元素賦為"╋",用于在控制臺(tái)畫(huà)出棋盤(pán)

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

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

// windows是一行一行來(lái)打印的。坐標(biāo)值為(行值, 列值)

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

}

}

}

// 在控制臺(tái)輸出棋盤(pán)的方法

public void printBoard() {

// 打印每個(gè)數(shù)組元素

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

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

// 打印數(shù)組元素后不換行

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

}

// 每打印完一行數(shù)組元素后輸出一個(gè)換行符

System.out.print("\n");

}

}

// 該方法處理電腦下棋:隨機(jī)生成2個(gè)整數(shù),作為電腦下棋的坐標(biāo),賦給board數(shù)組。

private void compPlay() {

// 構(gòu)造一個(gè)隨機(jī)數(shù)生成器

Random rnd = new Random();

// Random類(lèi)的nextInt(int n))方法:隨機(jī)地生成并返回指定范圍中的一個(gè) int 值,

// 即:在此隨機(jī)數(shù)生成器序列中 0(包括)和 n(不包括)之間均勻分布的一個(gè)int值。

int compXPos = rnd.nextInt(15);

int compYPos = rnd.nextInt(15);

// 保證電腦下的棋的坐標(biāo)上不能已經(jīng)有棋子(通過(guò)判斷對(duì)應(yīng)數(shù)組元素只能是"╋"來(lái)確定)

while (board[compXPos][compYPos].equals("╋") == false) {

compXPos = rnd.nextInt(15);

compYPos = rnd.nextInt(15);

}

System.out.println(compXPos);

System.out.println(compYPos);

// 把對(duì)應(yīng)的數(shù)組元素賦為"○"。

board[compXPos][compYPos] = "○";

}

// 該方法用于判斷勝負(fù):進(jìn)行四次循環(huán)掃描,判斷橫、豎、左斜、右斜是否有5個(gè)棋連在一起

private boolean judgeWin() {

// flag表示是否可以斷定贏/輸

boolean flag = false;

// joinEle:將每一個(gè)橫/豎/左斜/右斜行中的元素連接起來(lái)得到的一個(gè)字符串

String joinEle;

// 進(jìn)行橫行掃描

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

// 每掃描一行前,將joinEle清空

joinEle = "";

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

joinEle += board[i][j];

}

// String類(lèi)的contains方法:當(dāng)且僅當(dāng)該字符串包含指定的字符序列時(shí),返回true。

if (joinEle.contains("●●●●●")) {

System.out.println("您贏啦!");

flag = true;

// 停止往下繼續(xù)執(zhí)行,提前返回flag。

// 如果執(zhí)行了這個(gè)return,就直接返回該方法的調(diào)用處;

// 不會(huì)再執(zhí)行后面的任何語(yǔ)句,包括最后那個(gè)return語(yǔ)句。

// (而break僅僅是完全跳出這個(gè)for循環(huán),還會(huì)繼續(xù)執(zhí)行下面的for循環(huán)。)

return flag;

} else if (joinEle.contains("○○○○○")) {

System.out.println("您輸啦!");

flag = true;

// 提前返回flag

return flag;

}

}

// 進(jìn)行豎行掃描

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;

}

}

// 進(jìn)行左斜行掃描

for (int i = -(BOARD_SIZE - 2); i BOARD_SIZE - 1; i++) {

joinEle = "";

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

int line = i + j;

// 只截取坐標(biāo)值沒(méi)有越界的點(diǎn)

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;

}

}

// 進(jìn)行右斜行掃描

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;

// 最后這個(gè)return可省略

}

}

// 確保該方法有返回值(如果上面條件都不滿(mǎn)足時(shí))

return flag;

}

public static void main(String[] args) throws Exception, IOException {

Gobang gb = new Gobang();

gb.initBoard();

gb.printBoard();

// BufferedReader類(lèi):帶緩存的讀取器————從字符輸入流中讀取文本,并緩存字符??捎糜诟咝ёx取字符、數(shù)組和行。

// 最好用它來(lái)包裝所有其 read() 操作可能開(kāi)銷(xiāo)很高的 Reader(如 FileReader 和 InputStreamReader)。

// 下面構(gòu)造一個(gè)讀取器對(duì)象。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// 定義輸入字符串

String inputStr = null;

// br.readLine():每當(dāng)在鍵盤(pán)上輸入一行內(nèi)容按回車(chē),剛輸入的內(nèi)容將被br(讀取器對(duì)象)讀取到。

// BufferedReader類(lèi)的readLine方法:讀取一個(gè)文本行。

// 初始狀態(tài)由于無(wú)任何輸入,br.readLine()會(huì)拋出異常。因而main方法要捕捉異常。

while ((inputStr = br.readLine()) != null) {

// 將用戶(hù)輸入的字符串以逗號(hào)(,)作為分隔符,分隔成2個(gè)字符串。

// String類(lèi)的split方法,將會(huì)返回一個(gè)拆分后的字符串?dāng)?shù)組。

String[] posStrArr = inputStr.split(",");

// 將2個(gè)字符串轉(zhuǎn)換成用戶(hù)下棋的坐標(biāo)

int xPos = Integer.parseInt(posStrArr[0]);

int yPos = Integer.parseInt(posStrArr[1]);

// 校驗(yàn)用戶(hù)下棋坐標(biāo)的有效性,只能是數(shù)字,不能超出棋盤(pán)范圍

if (xPos 15 || xPos 1 || yPos 15 || yPos 1) {

System.out.println("您下棋的坐標(biāo)值應(yīng)在1到15之間,請(qǐng)重新輸入!");

continue;

}

// 保證用戶(hù)下的棋的坐標(biāo)上不能已經(jīng)有棋子(通過(guò)判斷對(duì)應(yīng)數(shù)組元素只能是"╋"來(lái)確定)

// String類(lèi)的equals方法:比較字符串和指定對(duì)象是否相等。結(jié)果返回true或false。

if (gb.board[xPos - 1][yPos - 1].equals("╋")) {

// 把對(duì)應(yīng)的數(shù)組元素賦為"●"。

gb.board[xPos - 1][yPos - 1] = "●";

} else {

System.out.println("您下棋的點(diǎn)已有棋子,請(qǐng)重新輸入!");

continue;

}

// 電腦下棋

gb.compPlay();

gb.printBoard();

// 每次下棋后,看是否可以斷定贏/輸了

if (gb.judgeWin() == false) {

System.out.println("請(qǐng)輸入您下棋的坐標(biāo),應(yīng)以x,y的格式:");

} else {

// 完全跳出這個(gè)while循環(huán),結(jié)束下棋

break;

}

}

}

}

JAVA五子棋程序代碼分析(1)

樓主要是覺(jué)得看的不舒服可以拷到記事本里看~ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; //這一段import就不說(shuō)了,下面要用到的就import進(jìn)來(lái) public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //繼承Applet表明是個(gè)applet,后面的接口必須要實(shí)現(xiàn)每個(gè)接口的所有方法 { int color_Qizi=0;//旗子的顏色標(biāo)識(shí) 0:白子 1:黑子 int intGame_Start=0;//游戲開(kāi)始標(biāo)志 0未開(kāi)始 1游戲中 int intGame_Body[][]=new int[16][16]; //設(shè)置棋盤(pán)棋子狀態(tài) 0 無(wú)子 1 白子 2 黑子 Button b1=new Button("游戲開(kāi)始"); Button b2=new Button("重置游戲"); //兩個(gè)按鈕 Label lblWin=new Label(" "); //這個(gè)label用來(lái)顯示最后輸贏信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]; //用來(lái)表明選擇黑氣或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup(); //兩個(gè)checkbox必須放在同一個(gè)checkboxgroup里才能做到單選 public void init() //初始化,堆砌界面 { setLayout(null); //不設(shè)布局管理器 addMouseListener(this); //將本類(lèi)作為鼠標(biāo)事件的接口響應(yīng)鼠標(biāo)動(dòng)作 add(b1); //將事先定義好的第一個(gè)按鈕添加入界面 b1.setBounds(330,50,80,30); //設(shè)置第一個(gè)按鈕左上角的位置和大小 b1.addActionListener(this); //將本類(lèi)作為按鈕事件的接口響應(yīng)按鈕動(dòng)作 add(b2); //將事先定義好的第二個(gè)按鈕添加進(jìn)去 b2.setBounds(330,90,80,30); /設(shè)置第二個(gè)按鈕左上角的位置和大小 b2.addActionListener(this); //將本類(lèi)作為按鈕事件的接口響應(yīng)按鈕動(dòng)作 ckbHB[0]=new Checkbox("白子先",ckgHB,false); //new一個(gè)checkbox ckbHB[0].setBounds(320,20,60,30); //設(shè)置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false); //new第二個(gè)checkbox ckbHB[1].setBounds(380,20,60,30); //設(shè)置左上角位置和大小 add(ckbHB[0]); //將第一個(gè)checkbox加入界面 add(ckbHB[1]); //將第二個(gè)checkbox加入界面 ckbHB[0].addItemListener(this); //將本類(lèi)作為其事件接口來(lái)響應(yīng)選中動(dòng)作 ckbHB[1].addItemListener(this); //將本類(lèi)作為其事件接口來(lái)響應(yīng)選中動(dòng)作 add(lblWin); //將標(biāo)簽加入界面 lblWin.setBounds(330,130,80,30); //設(shè)置標(biāo)簽的左上角位置和大小 Game_start_csh(); //調(diào)用游戲初始化 } public void itemStateChanged(ItemEvent e) //ItemListener接口中的方法,必須要有 { if (ckbHB[0].getState()) //選擇黑子先還是白子先 { color_Qizi=0; //白棋先 } else { color_Qizi=1; //黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener接口中的方法,也是必須的 { Graphics g=getGraphics(); //這句話貌似可以去掉,g是用來(lái)畫(huà)圖或者畫(huà)界面的 if (e.getSource()==b1) //如果動(dòng)作的來(lái)源是第一個(gè)按鈕 { Game_start(); //游戲開(kāi)始 } else //否則 { Game_re(); //游戲重新開(kāi)始 } } public void mousePressed(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseClicked(MouseEvent e) //鼠標(biāo)單擊時(shí) { Graphics g=getGraphics(); //獲得畫(huà)筆 int x1,y1; x1=e.getX(); //單擊處的x坐標(biāo) y1=e.getY(); //單擊處的y坐標(biāo) if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300) //在棋盤(pán)范圍之外 { return; //則這是不能走棋的,直接返回 } //下面這兩個(gè)if和兩個(gè)賦值的作用是將x和y坐標(biāo)根據(jù)舍入原則修改成棋盤(pán)上格子的坐標(biāo) if (x1%2010) { x1+=20; } if(y1%2010) { y1+=20; } x1=x1/20*20; y1=y1/20*20; set_Qizi(x1,y1); //在棋盤(pán)上畫(huà)上一個(gè)棋子 } public void mouseEntered(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void paint(Graphics g) //重繪和applet程序裝載的時(shí)候會(huì)調(diào)用這個(gè)繪制的過(guò)程 { draw_qipan(g); //畫(huà)棋盤(pán) }

求一個(gè)簡(jiǎn)單的JAVA五子棋代碼!! 網(wǎng)上復(fù)制的別來(lái)了!

以下是現(xiàn)寫(xiě)的 實(shí)現(xiàn)了兩人對(duì)戰(zhàn) 自己復(fù)制后運(yùn)行把 沒(méi)什么難度 類(lèi)名 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] = "╋";

// }

}

}

}

//輸出棋盤(pán)

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("輸入錯(cuò)誤,請(qǐng)從新輸入");

isRight = false;

}

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

isRight = false;

}

return isRight;

}

//判斷誰(shuí)贏了

public void whoWin(Games wz) {

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

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

int ylabel;// 記錄第一次找到某個(gè)棋子的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("請(qǐng)"+roles+"輸入X,Y坐標(biāo),必須在0-15范圍內(nèi),xy以空格隔開(kāi),輸入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("輸入錯(cuò)誤,請(qǐng)從新輸入");

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編寫(xiě)的五子棋代碼,要有電腦AI的

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

下面的源代碼分為4個(gè)文件;

chessClient.java:客戶(hù)端主程序。

chessInterface.java:客戶(hù)端的界面。

chessPad.java:棋盤(pán)的繪制。

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

可同時(shí)容納50個(gè)人同時(shí)在線下棋,聊天。

沒(méi)有加上詳細(xì)注釋?zhuān)贿^(guò)絕對(duì)可以運(yùn)行,j2sdk1.4下通過(guò)。

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

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五子棋客戶(hù)端 "+"用戶(hù)名:"+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無(wú)法關(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("請(qǐng)黑棋下子");

}

else if(chessclient.isClient)

{

chessclient.chesspad.chessColor=-1;

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

}

}

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

{

chessclient.isOnChess=false;

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

chessclient.chesspad.statusText.setText("對(duì)方退出,請(qǐng)點(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("傳輸錯(cuò)誤:請(qǐng)退出程序,重新加入 \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; //下棋的客戶(hù)端連接?

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

boolean isClient=false; //如果是下棋的客戶(hù)端

Panel southPanel=new Panel();

Panel northPanel=new Panel();

Panel centerPanel=new Panel();

Panel westPanel=new Panel();

Panel eastPanel=new Panel();

chessClient()

{

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

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:無(wú)法連接,建議重新啟動(dòng)程序 \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("連接成功,請(qǐng)創(chuàng)建游戲或加入游戲");

}

}

catch(Exception ei)

{

chatpad.chatLineArea.setText("controlpad.connectButton:無(wú)法連接,建議重新啟動(dòng)程序 \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("必須先選定一個(gè)有效用戶(hù)");

}

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無(wú)法連接 \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無(wú)法連接 \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("請(qǐng)建立游戲或者加入游戲");

}

if(!isOnChess)

{

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

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

}

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無(wú)法連接,建議重新連接 \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無(wú)法連接,建議重新連接 \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+"."+"沒(méi)有用戶(hù)");

}

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+"."+"沒(méi)有用戶(hù)");

}

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("錯(cuò)誤:沒(méi)有這個(gè)用戶(hù),請(qǐng)退出程序,重新加入");

}

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("請(qǐng)先連接服務(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:無(wú)法連接 \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+",重新開(kāi)局,等待白棋下子...");

}

else if(chessColorWin==-1)

{

chessWhiteWin++;

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

}

}

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);

}

}

新聞名稱(chēng):java五子代碼 java五子棋簡(jiǎn)單代碼
鏈接分享:http://muchs.cn/article26/docchjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)面包屑導(dǎo)航、網(wǎng)站策劃、微信公眾號(hào)電子商務(wù)、網(wǎng)站制作

廣告

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

外貿(mào)網(wǎng)站制作