您好,登錄后才能下訂單哦!
利用Java,在控制臺操作下,編寫的五子棋,作為復習二維數組,面向對象等基礎知識。w表示白棋,b表示黑棋
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("請輸入坐標,范圍為(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("下的位置不合理,請重新開始"); 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') {// 遍歷數組,找到的第一個白棋 point[0] = i; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右數4個的棋盤 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)) {// 如果存在向右,向下數4個的棋盤 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)) {// 如果存在向下數4個的棋盤 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)) {// 如果存在向左,向下數4個的棋盤 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)) {// 如果存在向右數4個的棋盤 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)) {// 如果存在向右,向下數4個的棋盤 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)) {// 如果存在向下數4個的棋盤 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)) {// 如果存在向左,向下數4個的棋盤 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(); } } }
運行結果:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 請輸入坐標,范圍為(0,0)到(9,9) 4 6 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + w + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 請輸入坐標,范圍為(0,0)到(9,9) 6 5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + w + + + + + + + + + + + + + + + + + + b + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 請輸入坐標,范圍為(0,0)到(9,9)
誰輸誰贏我就不弄出來了,太長,占地。。
總結
以上就是本文關于Java編程實現五子棋人人對戰代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。