作者的個人分享網:分享時刻【www.itison.cn】
用法java在控制檯實現五子棋對弈 ,界面雖然醜,但是功能確實基本實現,學習Java SE的時候收錄的經典代碼##
實現五子棋的全部代碼如下,(複製直接可用,但是控制檯的字體要設置爲合適的)
Chess.java:
/**五子棋項目**/
package com.itjob.chess;
import java.util.Scanner;
public class Chess {
static String[][] chess = new String[15][15];
public static void main(String[] args) {
initial();//存儲棋盤部分
showChess();//打印棋盤
playChess();//下棋
while(true){
System.out.println("是否繼續遊戲?(y/n)");
Scanner input = new Scanner(System.in);
String str = input.next();
if (str.equals("y")) {
initial();//存儲棋盤部分
showChess();//打印棋盤
playChess();//下棋
}else{
System.exit(0);
}
}
}
//存儲棋盤部分
private static void initial(){
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
if (i==0 && j==0) {
chess[i][j] = "┏";
}else if(i==0 && j > 0 && j < 14){
chess[i][j] = "┳";
}else if(j < 14 && j > 0 && i==14){
chess[i][j] = "┻";
}else if(i==0 && j==14){
chess[i][j] = "┓";
}else if (i > 0 && i < 14 && j==0) {
chess[i][j] = "┣";
}else if (i > 0 && i < 14 && j==14) {
chess[i][j] = "┫";
}else if (j==0 && i==14) {
chess[i][j] = "┗";
}else if (j==14 && i==14) {
chess[i][j] = "┛";
}else {
chess[i][j] = "╋";
}
}
}
}
//打印棋盤
private static void showChess(){
//打印棋盤
for (int i = 0; i < 15; i++) {
System.out.print(" " + (char)('A' + i) + " ");
}
System.out.println();
for (int i = 0; i < chess.length; i++) {
System.out.print((char)('A' + i));
for (int j = 0; j < chess[i].length; j++) {
if (j < 14) {
System.out.print(chess[i][j] + "━");//加長棋盤
}else{
System.out.print(chess[i][j]);
}
}
System.out.println();
if (i<14) {//加高度
System.out.println(" ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃");
}
}
}
//下棋,落子
private static void playChess(){
Scanner input = new Scanner(System.in);
int i = 0;
int j = 0;
String str = "";
boolean flag = true;
while(true){
String who = flag ? "○":"●";
System.out.print("請" + who + "方落子(行,列)(0退出):");
str = input.nextLine();//接收輸入值
str = str.toUpperCase();//全部則轉換爲大寫
if (str.equals("0")) {
System.exit(0);
}
//除去不輸入異常的bug
if (str.length() != 2) {
System.out.println("輸入的位置不對!請重新輸入...");
continue;
}
//判斷是否爲A到O範圍
if (str.charAt(0) <= 'O' && str.charAt(0) >= 'A' && str.charAt(1) <= 'O' && str.charAt(1) >= 'A' && str.length() == 2) {
i = str.charAt(0) - (int)'A';
j = str.charAt(1) - (int)'A';
}else{
System.out.println("輸入的位置不對!請重新輸入...");
continue;
}
//將黑白子賦值給對應的位置,判斷是否已有棋子
if (!chess[i][j].equals("○") && !chess[i][j].equals("●")) {
chess[i][j] = who;
}else {
System.out.println("此處已經有棋子!請重新輸入...");
continue;
}
showChess();//打印棋盤
if(!isOver(i,j,who)){
break;
}
flag = !flag;
}
}
//判斷輸贏,沒結束則返回true,有結果則返回false(也可以做成返回who的情況,在調用出輸出哪方贏)
private static boolean isOver(int x, int y, String who){
int i = x;
int j = y;
int count = 0;
//判斷橫向向左是否五連
for (j = y; j >= 0 ; j--) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
//判斷橫向向右是否五連
for (j = y; j <= 14 ; j++) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
count--;
if (count >= 5) {
System.out.println(who + "方勝!遊戲結束!!!");
return false;
}
//判斷縱向向上是否五連
i = x;
j = y;
count = 0;
for (i = x; i >= 0 ; i--) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
//判斷縱向向下是否五連
for (i = x; i <= 14 ; i++) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
count--;
if (count >= 5) {
System.out.println(who + "方勝!遊戲結束!!!");
return false;
}
//判斷左上是否五連
count = 0;
for (i = x,j = y; i >= 0 && j >=0 ; i--,j--) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
//判斷右下是否五連
for (i = x,j = y; i < chess.length && j < chess.length ; i++,j++) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
count--;
if (count >= 5) {
System.out.println(who + "方勝!遊戲結束!!!");
return false;
}
//判斷左下是否五連
count = 0;
for (i = x,j = y; i < chess.length && j >=0 ; i++,j--) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
//判斷右上是否五連
for (i = x,j = y; i >= 0 && j < chess.length ; i--,j++) {
if (chess[i][j].equals(who)) {
count++;
}else {
break;
}
}
count--;
if (count >= 5) {
System.out.println(who + "方勝!遊戲結束!!!");
return false;
}
return true;
}
}