當前位置:編程學習大全網 - 編程語言 - 解釋五子棋的java代碼

解釋五子棋的java代碼

import java.util.Scanner;

public class FiveChess {

static char[][] chess = new char[16][16];

static boolean isBlack = true;

public static void main(String[] args) {

System.out.println(" =======五子棋遊戲======= ");

System.out.println();

for (int i = 0; i < chess.length; i++) {

for (int j = 0; j < chess[i].length; j++) {

chess[i][j] = '*';

}

}// for end!

Scanner sca = new Scanner(System.in);

printChess();

while (true) {

System.out.println();

System.out.println("請" + (isBlack ? "黑方" : "白方") + "落子:");

String str = sca.next();

int r = fromCharToInt(str.charAt(0));

int c = fromCharToInt(str.charAt(1));

if ((r < 0 || r > 15) || (c < 0 || c > 15)) {

System.out.println("輸入位置錯誤,請重新輸入!");

continue;

} else if (chess[r][c] != '*') {

System.out.println("該位置有子,請重新輸入!");

continue;

} else {

chess[r][c] = isBlack ? '@' : '#';

printChess();

if (wasWin(r, c)) {

System.out.println((isBlack ? "黑方" : "白方") + "獲勝,遊戲結束!");

break;

} else {

isBlack = !isBlack;

}

}

}

}// main end!

public static void printChess() { // 打印棋盤

System.out.print(" ");

for (int i = 0; i < chess.length; i++) {

if (i < 10) {

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

} else {

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

}

}// for end!

System.out.println();

for (int i = 0; i < chess.length; i++) {

if (i < 10) {

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

} else {

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

}

for (int j = 0; j < chess.length; j++) {

System.out.print(chess[i][j] + " ");

}

System.out.println();

}

}// printChess end!

public static int fromCharToInt(char c) {// 判斷是否越界

if (c >= '0' && c <= '9') {

return c - 48;

} else {

return c - 'a' + 10;

}

}

public static boolean wasWin(int r, int c) {// 判斷是否贏了

return wasWinAtV(r, c) || wasWinAtE(r, c) || wasWinAtRD(r, c)

|| wasWinAtLD(r, c);

}

public static boolean wasWinAtV(int r, int c) {

char ch = isBlack ? '@' : '#';

int i = r;

while (i >= 0 && chess[i][c] == ch) {

i--;

}

i++;

int num = 0;

while (i < chess.length && chess[i][c] == ch) {

num++;

i++;

}

return num >= 5;

}

public static boolean wasWinAtE(int r, int c) {

char ch = isBlack ? '@' : '#';

int i = c;

while (i >= 0 && chess[r][i] == ch) {

i--;

}

i++;

int num = 0;

while (i < chess.length && chess[r][i] == ch) {

num++;

i++;

}

return num >= 5;

}

public static boolean wasWinAtRD(int r, int c) {

char ch = isBlack ? '@' : '#';

int i = r;

int j = c;

while (i >= 0 && j < chess.length && chess[i][j] == ch) {

i--;

j++;

}

i++;

j--;

int num = 0;

while (i < chess.length && j >= 0 && chess[i][j] == ch) {

i++;

j--;

num++;

}

return num >= 5;

}

private static boolean wasWinAtLD(int r, int c) {

char ch = isBlack ? '@' : '#';

int i = r;

int j = c;

while (i >= 0 && j >= 0 && chess[i][j] == ch) {

i--;

j--;

}

i++;

j++;

int num = 0;

while (i < chess.length && j < chess.length && chess[i][j] == ch) {

i++;

j++;

num++;

}

return num >= 5;

}

}

看看我的啊!

  • 上一篇:無人駕駛的坦克是怎麽樣的?
  • 下一篇:高端+純電全新品牌同時發布,奇瑞今年放的大招有點多!
  • copyright 2024編程學習大全網