利用Java,在控制臺(tái)操作下,編寫的五子棋,作為復(fù)習(xí)二維數(shù)組,面向?qū)ο蟮然A(chǔ)知識(shí)。w表示白棋,b表示黑棋
中衛(wèi)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!import java.util.Scanner; public class MainMethod { public static char[][] c = new char[10][10]; public static void main(String[] args) { MainMethod mainMethod = new MainMethod(); mainMethod.init(); int i; boolean color; int[] point = new int[2]; Scanner scanner = new Scanner(System.in); for (i = 1; i < 100; i++) { mainMethod.draw();// 繪制棋盤 System.out.println("請(qǐng)輸入坐標(biāo),范圍為(0,0)到(9,9)"); int x = scanner.nextInt(); int y = scanner.nextInt(); if (i % 2 == 1) color = true; else color = false; point[0] = x; point[1] = y; if (mainMethod.paint(point, color) == false) {// System.out.println("下的位置不合理,請(qǐng)重新開始"); break; } if (mainMethod.win() == 1) { mainMethod.draw(); System.out.println("恭喜白棋贏??!"); break; } else if (mainMethod.win() == -1) { mainMethod.draw(); System.out.println("恭喜黑棋贏??!"); break; } } if (i == 100) System.out.println("平局"); } public char[][] init() {// 初始化棋盤 for (int i = 0; i < c.length; i++) { for (int j = 0; j < c.length; j++) { c[i][j] = '+'; } } return c; } public boolean paint(int[] point, boolean colr) {// 判斷下的位置是否正確,若正確改變棋盤 boolean b = true; int x = point[0]; int y = point[1]; if (inBoard(point) == false || c[x][y] == 'w' || c[x][y] == 'b' || x < 0) { b = false; return b; } else if (colr == true) {// 白棋下 if (c[x][y] == '+') { c[x][y] = 'w'; } } else if (colr == false) {// 黑棋下 if (c[x][y] == '+') { c[x][y] = 'b'; } } return b; } public boolean inBoard(int[] point) {// 判斷point是否在棋盤上 int x = point[0]; int y = point[1]; if (x < 0 || y < 0 || x >= c.length || y >= c.length) return false; else return true; } public int win() {// 判斷輸贏,1表示白的贏,-1黑的贏,0暫無輸贏 int whoWin = 0; int[] point = new int[2]; outer: while (true) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c.length; j++) { if (c[i][j] == 'w') {// 遍歷數(shù)組,找到的第一個(gè)白棋 point[0] = i; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右數(shù)4個(gè)的棋盤 if (c[i][j + 1] == 'w' && c[i][j + 2] == 'w' && c[i][j + 3] == 'w' && c[i][j + 4] == 'w') { whoWin = 1; break outer; } } point[0] = i + 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右,向下數(shù)4個(gè)的棋盤 if (c[i + 1][j + 1] == 'w' && c[i + 2][j + 2] == 'w' && c[i + 3][j + 3] == 'w' && c[i + 4][j + 4] == 'w') { whoWin = 1; break outer; } } point[0] = i + 4; point[1] = j; if (inBoard(point)) {// 如果存在向下數(shù)4個(gè)的棋盤 if (c[i + 1][j] == 'w' && c[i + 2][j] == 'w' && c[i + 3][j] == 'w' && c[i + 4][j] == 'w') { whoWin = 1; break outer; } } point[0] = i - 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向左,向下數(shù)4個(gè)的棋盤 if (c[i - 1][j + 1] == 'w' && c[i - 2][j + 2] == 'w' && c[i - 3][j + 3] == 'w' && c[i - 4][j + 4] == 'w') { whoWin = 1; break outer; } } } else if (c[i][j] == 'b') { point[0] = i; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右數(shù)4個(gè)的棋盤 if (c[i][j + 1] == 'b' && c[i][j + 2] == 'b' && c[i][j + 3] == 'b' && c[i][j + 4] == 'b') { whoWin = -1; break outer; } } point[0] = i + 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右,向下數(shù)4個(gè)的棋盤 if (c[i + 1][j + 1] == 'b' && c[i + 2][j + 2] == 'b' && c[i + 3][j + 3] == 'b' && c[i + 4][j + 4] == 'b') { whoWin = -1; break outer; } } point[0] = i + 4; point[1] = j; if (inBoard(point)) {// 如果存在向下數(shù)4個(gè)的棋盤 if (c[i + 1][j] == 'b' && c[i + 2][j] == 'b' && c[i + 3][j] == 'b' && c[i + 4][j] == 'b') { whoWin = -1; break outer; } } point[0] = i - 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向左,向下數(shù)4個(gè)的棋盤 if (c[i - 1][j + 1] == 'b' && c[i - 2][j + 2] == 'b' && c[i - 3][j + 3] == 'b' && c[i - 4][j + 4] == 'b') { whoWin = -1; break outer; } } } } } whoWin = 0; break outer; } return whoWin; } public void draw() {// 繪制棋盤 for (int i = 0; i < c.length; i++) { for (int j = 0; j < c.length; j++) { System.out.print(c[i][j] + " "); } System.out.println(); } } }
名稱欄目:Java編程實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn)代碼示例-創(chuàng)新互聯(lián)
文章分享:http://www.rwnh.cn/article46/csooeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站導(dǎo)航、網(wǎng)站收錄、標(biāo)簽優(yōu)化、做網(wǎng)站、響應(yīng)式網(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)
猜你還喜歡下面的內(nèi)容