五子棋游戲的java代碼 基于java的五子棋游戲的設(shè)計(jì)代碼
java五子棋源代碼
我這有算法 不過(guò)沒(méi)做swing界面 DOS下可以直接運(yùn)行 要不你拿去改改
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代碼旁邊就注釋 讓我看的清楚明白一些,要詳細(xì)的解釋和思路
import java.applet.*;
import t.*;
import t.event.*;
import java.applet.Applet;
import t.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 黑子
求java五子棋代碼要注釋~現(xiàn)在1小時(shí)等待
package org.crazyit.gobang;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 五子棋游戲類(lèi)
*
* @author yangenxiong [email protected]
* @author Kelvin Mak [email protected]
* @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;
// 定義用戶輸入的X坐標(biāo)
private int posX = 0;
// 定義用戶輸入的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à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);
// 判斷用戶是否贏了
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)用戶是否繼續(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
* "●"為用戶,"○"為計(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代碼 java編寫(xiě)矩陣乘法
欄 目:Java編程
下一篇:沒(méi)有了
本文標(biāo)題:五子棋游戲的java代碼 基于java的五子棋游戲的設(shè)計(jì)代碼
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/17355.html
您可能感興趣的文章
- 04-07貪吃蛇原代碼java 貪吃蛇游戲代碼java
- 04-07java連連看腳本源代碼 java 連連看
- 04-05java井字棋代碼論文 用java寫(xiě)井字游戲
- 01-10跟我學(xué)Java Swing之游戲設(shè)計(jì)(2)
- 01-10一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)
- 01-10跟我學(xué)Java Swing之游戲設(shè)計(jì)(1)
- 01-10Java編程經(jīng)典小游戲設(shè)計(jì)-打磚塊小游戲源碼
- 01-10Java編程實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn)代碼示例
- 01-10Java編程實(shí)現(xiàn)打地鼠文字游戲?qū)嵗a


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 7什么是 WSH(腳本宿主)的詳細(xì)解釋
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-10五子棋游戲的java代碼 基于java的五子
- 04-10矩陣乘法java代碼 java編寫(xiě)矩陣乘法
- 04-10找質(zhì)數(shù)java實(shí)現(xiàn)代碼 找質(zhì)數(shù)java實(shí)現(xiàn)代
- 04-10前端寫(xiě)java代碼 java寫(xiě)前端還是后端
- 04-10java改變字體代碼 java怎么改變字體
- 04-10java學(xué)習(xí)代碼庫(kù) java代碼教學(xué)
- 04-10b2bjava開(kāi)源代碼的簡(jiǎn)單介紹
- 04-09java視頻下載代碼解釋 java下載網(wǎng)頁(yè)視
- 04-09簡(jiǎn)易教務(wù)系統(tǒng)java代碼 簡(jiǎn)易教務(wù)系統(tǒng)
- 04-09java遮罩層代碼 java面板
隨機(jī)閱讀
- 01-10Delphi實(shí)現(xiàn)圖像文本旋轉(zhuǎn)特效完整實(shí)例
- 08-05dede導(dǎo)航部分去掉最后一個(gè)循環(huán)多出來(lái)
- 01-10C#中Timer使用及解決重入問(wèn)題
- 01-10解析C++編程中如何使用設(shè)計(jì)模式中的
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10C#實(shí)現(xiàn)QQ截圖功能及相關(guān)問(wèn)題
- 01-11Swift如何調(diào)用Objective-C的可變參數(shù)函數(shù)
- 08-05織夢(mèng)教程:dedecms留言板統(tǒng)計(jì)留言數(shù)量
- 01-10C++ 中私有繼承的作用
- 01-10stringstream操縱string的方法總結(jié)