java五子棋軟代碼下載 新手java五子棋完整代碼

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

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

創(chuàng)新互聯(lián)公司是專業(yè)的鼓樓網(wǎng)站建設(shè)公司,鼓樓接單;提供網(wǎng)站制作、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行鼓樓網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

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

isRight = false;

}

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

isRight = false;

}

return isRight;

}

//判斷誰贏了

public void whoWin(Games wz) {

// 從數(shù)組挨個(gè)查找找到某個(gè)類型的棋子就從該棋子位置向右,向下,斜向右下 各查找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以空格隔開,輸入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五子棋源代碼

很sb的電腦五子棋:

import java.io.*;

import java.util.*;

public class Gobang {

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

private String[][] board;

// 定義棋盤的大小

private static int BOARD_SIZE = 15;

public void initBoard() {

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

board = new String[BOARD_SIZE][BOARD_SIZE];

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

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

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

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

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

}

}

}

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

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類的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)有棋子(通過判斷對(duì)應(yīng)數(shù)組元素只能是"╋"來確定)

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è)橫/豎/左斜/右斜行中的元素連接起來得到的一個(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類的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í)行后面的任何語句,包括最后那個(gè)return語句。

// (而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)值沒有越界的點(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可省略

}

}

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

return flag;

}

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

Gobang gb = new Gobang();

gb.initBoard();

gb.printBoard();

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

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

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

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

// 定義輸入字符串

String inputStr = null;

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

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

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

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

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

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

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

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

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

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

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

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

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

continue;

}

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

// String類的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點(diǎn)抗 pPlay();

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五子棋源代碼

我這有算法 不過沒做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)隔開如 1,1

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

*/

public class Chess {

// 定義棋盤大小

private static int SIZE = 15;

private String[][] board;

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

Chess chess = new Chess();

// 初始化棋盤

chess.initBoard();

// 畫出棋盤

chess.paintBoard();

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

int who = 0;

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

String str = null;

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

// 提取輸入的 以","分開的數(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++;

}

}

// 以 "┼" 初始化棋盤

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)前棋盤

public void paintBoard() {

// 以下代碼 這里為了使得棋盤坐標(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();

// 以上代碼 這里為了使得棋盤坐標(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;

}

}

新聞標(biāo)題:java五子棋軟代碼下載 新手java五子棋完整代碼
分享地址:http://muchs.cn/article44/ddigehe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、定制開發(fā)面包屑導(dǎo)航、網(wǎng)站營銷App開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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í)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)