上班實在無聊就用java寫個五子棋小游戲
編譯成class檔案后可在cmd視窗中運行
下面附上原始碼:
import java.util.Scanner;
public class FiveStepsChess {
/**
* 棋盤大小
*/
private static final int THEBOARD_SIZE = 16;
/**
* 定義棋盤
*/
private static final String[][] THEBOARD = new String[THEBOARD_SIZE][THEBOARD_SIZE];
private static final Scanner SC = new Scanner(System.in);
private static String WINNER = "";
public static void main(String[] args) {
// 進入游戲
play();
}
// 游戲開始方法
public static void play() {
printRules();
for (int i = 0; i < THEBOARD.length; i++) {
for (int j = 0; j < THEBOARD[i].length; j++) {
THEBOARD[i][j] = " ";
}
}
System.out.println("--------棋盤展開--------");
while (true) {
printedBoard();
if (victoryConditions()) {
System.out.println("對局結束:" + WINNER);
break;
}
System.out.println("黑子下:");
p1();
printedBoard();
if (victoryConditions()) {
System.out.println("對局結束:" + WINNER);
break;
}
System.out.println("白子下:");
p2();
}
System.out.println("是否繼續游戲!");
System.out.println("輸入yes繼續輸入no或其他字符退出游戲!");
String yorn = SC.next();
if (yorn.toUpperCase().equals("YES")) {
System.out.println("游戲繼續!");
play();
}
System.out.println("已退出");
}
// 列印游戲規則
public static void printRules() {
System.out.println("歡迎來到五子棋小游戲!");
System.out.println("1、對局雙方各執一色棋子,5、黑方的第一枚棋子可下在棋盤任意交叉點上,");
System.out.println("2、空棋盤開局,");
System.out.println("3、黑先、白后,交替下子,每次只能下一子,");
System.out.println("4、棋子下在棋盤的空白點上,棋子下定后,不得向其它點移動,不得從棋盤上拿掉或拿起另落別處,");
System.out.println("5、黑方的第一枚棋子可下在棋盤任意交叉點上,");
}
// 勝利條件
public static boolean victoryConditions() {
boolean pj = true;
String qz = "";
for (int i = 1; i < THEBOARD.length; i++) {
qz = "";
// 水平方向
for (int j = 1; j < THEBOARD_SIZE; j++) {
if (THEBOARD[i][j].equals(" ")) {
pj = false;
}
qz += THEBOARD[i][j];
}
if (winner(qz)) {
return true;
}
// 垂直方向
qz = "";
for (int j = 1; j < THEBOARD_SIZE; j++) {
qz += THEBOARD[j][i];
}
if (winner(qz)) {
return true;
}
}
// 左上部分
for (int i = 1; i < THEBOARD_SIZE; i++) {
qz = "";
for (int j = 0; j < i; j++) {
qz += THEBOARD[i - j][j + 1];
}
if (winner(qz)) {
return true;
}
}
// 右下部分
for (int i = THEBOARD_SIZE - 1; i >= 1; i--) {
qz = "";
for (int j = i; j < THEBOARD_SIZE; j++) {
qz += THEBOARD[j][(THEBOARD_SIZE - 1) - (j - i)];
}
if (winner(qz)) {
return true;
}
}
// 右上部分
for (int i = 1; i < THEBOARD_SIZE; i++) {
qz = "";
for (int j = 1; j <= i; j++) {
qz += THEBOARD[j][(THEBOARD_SIZE - i - 1) + j];
}
if (winner(qz)) {
return true;
}
}
// 左下部分
for (int i = 1; i < THEBOARD_SIZE; i++) {
qz = "";
for (int j = 1; j <= i; j++) {
qz += THEBOARD[(THEBOARD_SIZE - 1) - (i - j)][j];
}
if (winner(qz)) {
return true;
}
}
if (pj) {
WINNER = "該局平局";
return true;
}
return false;
}
// 判斷勝者
public static boolean winner(String qz) {
if (qz.contains("00000")) {
WINNER = "勝者為持白棋者";
return true;
}
if (qz.contains("11111")) {
WINNER = "勝者為持黑棋者";
return true;
}
return false;
}
// 黑放棋子
public static void p1() {
System.out.println("選擇旗子位置");
System.out.println("輸入橫向:");
int x = SC.nextInt();
System.out.println("輸入縱向:");
int y = SC.nextInt();
if (x >= THEBOARD.length || y >= THEBOARD.length) {
System.out.println("不可超過棋盤大小最大長度為" + (THEBOARD.length - 1));
p1();
} else if (!THEBOARD[x][y].equals(" ")) {
System.out.println("該位置已存在棋子");
p1();
} else {
THEBOARD[x][y] = "1";
}
}
// 白放棋子
public static void p2() {
System.out.println("選擇旗子位置");
System.out.println("輸入橫向:");
int x = SC.nextInt();
System.out.println("輸入縱向:");
int y = SC.nextInt();
if (x >= THEBOARD.length || y >= THEBOARD.length) {
System.out.println("不可超過棋盤大小最大長度為" + (THEBOARD.length));
p2();
} else if (!THEBOARD[x][y].equals(" ")) {
System.out.println("該位置已存在棋子");
p2();
} else {
THEBOARD[x][y] = "0";
}
}
// 列印棋盤
public static void printedBoard() {
System.out.print(" ");
if (THEBOARD_SIZE > 9) {
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
}
} else {
for (int i = 1; i < THEBOARD_SIZE; i++) {
System.out.print(i + " ");
}
}
for (int i = 10; i < THEBOARD.length; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 1; i < THEBOARD.length; i++) {
if (i < 10) {
System.out.print(i + " ");
} else {
System.out.print(i);
}
for (int j = 1; j < THEBOARD[i].length; j++) {
if (THEBOARD[i][j].equals("0")) {
System.out.print(" o ");
} else if (THEBOARD[i][j].equals("1")) {
System.out.print(" * ");
} else {
System.out.print(" + ");
}
}
System.out.println();
}
}
}
cmd運行效果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302582.html
標籤:其他
上一篇:cocos creator 2.4.6 加載json檔案 初始化游戲 - 初學者
下一篇:Unity筆記-08
