java|java五子棋 · 上班摸鱼小游戏

上班实在无聊就用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运行效果
java|java五子棋 · 上班摸鱼小游戏
文章图片

【java|java五子棋 · 上班摸鱼小游戏】

    推荐阅读