8阶段项目:五子棋(附带源码)

2024-09-08 07:20

本文主要是介绍8阶段项目:五子棋(附带源码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

8阶段项目:五子棋


8.1-技术实现


1.静态变量
静态变量只能定义在类中,不能定义在方法中。静态变量可以在static修饰的方法中使用,也可以在非静态的方法中访问。主要解决在静态方法中不能访问非静态的变量。
2.静态方法
静态方法就相当于一个箱子,只是这个箱子中装的是代码,需要使用这些代码的时候,就把这个箱子放在指定的位置即可。
 

/*** 静态变量和静态方法*/
public class staticTest {//静态变量只能定义在类中,不能定义在方法中public static String name="张三";public static void main(String[] args) {
//        System.out.println(name);
//        System.out.println("张三");
//        System.out.println("男");
//        System.out.println("20");
//        System.out.println("张三");
//        System.out.println("男");
//        System.out.println("20");show();}public static void show(){System.out.println("张三");System.out.println("男");System.out.println("20");}
}

---------------------------分割线--------------------------------------------------------------

8.2-棋盘制作


实现步骤分析

1.制作棋盘

a.使用输入法中的制表符在控制台直接打印出棋盘,然后寻找落子位置的特征
 

/*** 五子棋*/
public class Gobang {public static void main(String[] args) {System.out.println("┌────┬────┬────┬────┬────┬────┬────┬────┬────┐");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");System.out.println("│    │    │    │    │    │    │    │    │    │");System.out.println("└────┴────┴────┴────┴────┴────┴────┴────┴────┘");}
}

b.利用二维数组重新制作棋盘

public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static void main(String[] args) {System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}
}


c.棋盘在玩家使用过程中会反复展示,需要使用方法来进行优化

public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static void main(String[] args) {showChessboard();}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}}


2.落子

a.玩家A、B会交替落子

public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static void main(String[] args) {showChessboard();int totalPosition=chessboard.length*chessboard[0].length;for(int times=0;times<totalPosition;times++){System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");}}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}}


b.落子位置必须是0-100之间的整数,且不能使用已经存在棋子的位置

import java.util.Scanner;public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static char pieceA='○';public static char pieceB='■';public static void main(String[] args) {showChessboard();int totalPosition=chessboard.length*chessboard[0].length;Scanner sc=new Scanner(System.in);for(int times=0;times<totalPosition;times++){System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");while(true){//保证落子成功的一个循环if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入int position=sc.nextInt();if(position>=0&&position<totalPosition){char currentPiece=times%2==0?pieceA:pieceB;int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){System.out.println("非法落子,请重新选择落子位置");continue;}else{chessboard[row][col]=currentPiece;break;}}else{System.out.println("非法落子,请重新选择落子位置");}}else{System.out.println("非法落子,请重新选择落子位置");sc.next();//将Scanner中存储的数据取出,防止死循环}}//落子完成后,棋盘需要重新展示showChessboard();}}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}}


c.落子完成后,需要校验是否获胜

import java.util.Scanner;public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static char pieceA='○';public static char pieceB='■';public static void main(String[] args) {showChessboard();int totalPosition=chessboard.length*chessboard[0].length;Scanner sc=new Scanner(System.in);outer://标记外层循环for(int times=0;times<totalPosition;times++){System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子while(true){//保证落子成功的一个循环if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入int position=sc.nextInt();if(position>=0&&position<totalPosition){int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){System.out.println("非法落子,请重新选择落子位置");continue;}else{chessboard[row][col]=currentPiece;break;}}else{System.out.println("非法落子,请重新选择落子位置");}}else{System.out.println("非法落子,请重新选择落子位置");sc.next();//将Scanner中存储的数据取出,防止死循环}}//落子完成后,棋盘需要重新展示showChessboard();//判断获胜情况,一共4种for(int i=0;i<chessboard.length;i++){for(int j=0;j<chessboard[i].length;j++){//第一种:水平方向上存在同一玩家的连续5颗棋子//(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)boolean case1=(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i][j+1]==currentPiece&&chessboard[i][j+2]==currentPiece&&chessboard[i][j+3]==currentPiece&&chessboard[i][j+4]==currentPiece;//第二种:水平方向上存在同一玩家的连续五颗棋子//(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)boolean case2=(i+4<chessboard.length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j]==currentPiece&&chessboard[i+2][j]==currentPiece&&chessboard[i+3][j]==currentPiece&&chessboard[i+4][j]==currentPiece;//第三种:135度角上存在同一玩家的连续5颗棋子//(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)boolean case3=(i+4<chessboard.length)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j+1]==currentPiece&&chessboard[i+2][j+2]==currentPiece&&chessboard[i+3][j+3]==currentPiece&&chessboard[i+4][j+4]==currentPiece;//第四种:45度角上存在同一玩家的连续五颗棋子//(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)boolean case4=(i>4)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i-1][j+1]==currentPiece&&chessboard[i-2][j+2]==currentPiece&&chessboard[i-3][j+3]==currentPiece&&chessboard[i-4][j+4]==currentPiece;if(case1||case2||case3||case4){System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");break outer;}}}}}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}}


d.棋盘使用完毕还未分出胜负,需要提示

import java.util.Scanner;/*** 五子棋*/
public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static char pieceA='○';//玩家A的棋子public static char pieceB='■';//玩家B的棋子public static int times=0;//记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子public static void main(String[] args) {showChessboard();int totalPosition=chessboard.length*chessboard[0].length;Scanner sc=new Scanner(System.in);outer://标记外层循环while(times<totalPosition){System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子while(true){//保证落子成功的一个循环if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入int position=sc.nextInt();if(position>=0&&position<totalPosition){int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){System.out.println("非法落子,请重新选择落子位置");continue;}else{chessboard[row][col]=currentPiece;break;}}else{System.out.println("非法落子,请重新选择落子位置");}}else{System.out.println("非法落子,请重新选择落子位置");sc.next();//将Scanner中存储的数据取出,防止死循环}}//落子完成后,棋盘需要重新展示showChessboard();//判断获胜情况,一共4种for(int i=0;i<chessboard.length;i++){for(int j=0;j<chessboard[i].length;j++){//第一种:水平方向上存在同一玩家的连续5颗棋子//(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)boolean case1=(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i][j+1]==currentPiece&&chessboard[i][j+2]==currentPiece&&chessboard[i][j+3]==currentPiece&&chessboard[i][j+4]==currentPiece;//第二种:水平方向上存在同一玩家的连续五颗棋子//(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)boolean case2=(i+4<chessboard.length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j]==currentPiece&&chessboard[i+2][j]==currentPiece&&chessboard[i+3][j]==currentPiece&&chessboard[i+4][j]==currentPiece;//第三种:135度角上存在同一玩家的连续5颗棋子//(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)boolean case3=(i+4<chessboard.length)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j+1]==currentPiece&&chessboard[i+2][j+2]==currentPiece&&chessboard[i+3][j+3]==currentPiece&&chessboard[i+4][j+4]==currentPiece;//第四种:45度角上存在同一玩家的连续五颗棋子//(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)boolean case4=(i>4)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i-1][j+1]==currentPiece&&chessboard[i-2][j+2]==currentPiece&&chessboard[i-3][j+3]==currentPiece&&chessboard[i-4][j+4]==currentPiece;if(case1||case2||case3||case4){System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");break outer;}}}times++;}if(times==totalPosition){//说明棋盘已经用完还未分出胜负System.out.println("平局");}}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}}


3.声音特效


为落子、非法落子及获胜添加音效

import javax.print.DocFlavor;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.util.Scanner;/*** 五子棋*/
public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static char pieceA='○';//玩家A的棋子public static char pieceB='■';//玩家B的棋子public static int times=0;//记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子public static void main(String[] args) {showChessboard();int totalPosition=chessboard.length*chessboard[0].length;Scanner sc=new Scanner(System.in);outer://标记外层循环while(times<totalPosition){System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子while(true){//保证落子成功的一个循环if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入int position=sc.nextInt();if(position>=0&&position<totalPosition){int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){System.out.println("非法落子,请重新选择落子位置");playAudio("illegal.wav");continue;}else{chessboard[row][col]=currentPiece;playAudio("fill.wav");break;}}else{System.out.println("非法落子,请重新选择落子位置");playAudio("illegal.wav");}}else{System.out.println("非法落子,请重新选择落子位置");playAudio("illegal.wav");sc.next();//将Scanner中存储的数据取出,防止死循环}}//落子完成后,棋盘需要重新展示showChessboard();//判断获胜情况,一共4种for(int i=0;i<chessboard.length;i++){for(int j=0;j<chessboard[i].length;j++){//第一种:水平方向上存在同一玩家的连续5颗棋子//(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)boolean case1=(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i][j+1]==currentPiece&&chessboard[i][j+2]==currentPiece&&chessboard[i][j+3]==currentPiece&&chessboard[i][j+4]==currentPiece;//第二种:水平方向上存在同一玩家的连续五颗棋子//(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)boolean case2=(i+4<chessboard.length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j]==currentPiece&&chessboard[i+2][j]==currentPiece&&chessboard[i+3][j]==currentPiece&&chessboard[i+4][j]==currentPiece;//第三种:135度角上存在同一玩家的连续5颗棋子//(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)boolean case3=(i+4<chessboard.length)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j+1]==currentPiece&&chessboard[i+2][j+2]==currentPiece&&chessboard[i+3][j+3]==currentPiece&&chessboard[i+4][j+4]==currentPiece;//第四种:45度角上存在同一玩家的连续五颗棋子//(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)boolean case4=(i>4)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i-1][j+1]==currentPiece&&chessboard[i-2][j+2]==currentPiece&&chessboard[i-3][j+3]==currentPiece&&chessboard[i-4][j+4]==currentPiece;if(case1||case2||case3||case4){System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");playAudio("win.wav");break outer;}}}times++;}if(times==totalPosition){//说明棋盘已经用完还未分出胜负System.out.println("平局");}}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}/*** 音效播放* @param fileName*/public static void playAudio(String fileName){URL url =Gobang.class.getResource(fileName);AudioClip clip = Applet.newAudioClip(url);clip.play();try {Thread.sleep(50L);} catch (InterruptedException e) {}}}

-------------------------------分割线-----------------------------------------------

源码

import javax.print.DocFlavor;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.util.Scanner;/*** 五子棋*/
public class Gobang {public static char[][] chessboard={{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}};public static String separator="─────";public static char pieceA='○';//玩家A的棋子public static char pieceB='■';//玩家B的棋子public static int times=0;//记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子public static void main(String[] args) {showChessboard();int totalPosition=chessboard.length*chessboard[0].length;Scanner sc=new Scanner(System.in);outer://标记外层循环while(times<totalPosition){System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子while(true){//保证落子成功的一个循环if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入int position=sc.nextInt();if(position>=0&&position<totalPosition){int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){System.out.println("非法落子,请重新选择落子位置");playAudio("illegal.wav");continue;}else{chessboard[row][col]=currentPiece;playAudio("fill.wav");break;}}else{System.out.println("非法落子,请重新选择落子位置");playAudio("illegal.wav");}}else{System.out.println("非法落子,请重新选择落子位置");playAudio("illegal.wav");sc.next();//将Scanner中存储的数据取出,防止死循环}}//落子完成后,棋盘需要重新展示showChessboard();//判断获胜情况,一共4种for(int i=0;i<chessboard.length;i++){for(int j=0;j<chessboard[i].length;j++){//第一种:水平方向上存在同一玩家的连续5颗棋子//(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)boolean case1=(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i][j+1]==currentPiece&&chessboard[i][j+2]==currentPiece&&chessboard[i][j+3]==currentPiece&&chessboard[i][j+4]==currentPiece;//第二种:水平方向上存在同一玩家的连续五颗棋子//(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)boolean case2=(i+4<chessboard.length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j]==currentPiece&&chessboard[i+2][j]==currentPiece&&chessboard[i+3][j]==currentPiece&&chessboard[i+4][j]==currentPiece;//第三种:135度角上存在同一玩家的连续5颗棋子//(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)boolean case3=(i+4<chessboard.length)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i+1][j+1]==currentPiece&&chessboard[i+2][j+2]==currentPiece&&chessboard[i+3][j+3]==currentPiece&&chessboard[i+4][j+4]==currentPiece;//第四种:45度角上存在同一玩家的连续五颗棋子//(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)boolean case4=(i>4)&&(j+4<chessboard[i].length)&&chessboard[i][j]==currentPiece&&chessboard[i-1][j+1]==currentPiece&&chessboard[i-2][j+2]==currentPiece&&chessboard[i-3][j+3]==currentPiece&&chessboard[i-4][j+4]==currentPiece;if(case1||case2||case3||case4){System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");playAudio("win.wav");break outer;}}}times++;}if(times==totalPosition){//说明棋盘已经用完还未分出胜负System.out.println("平局");}}public static void showChessboard(){System.out.println("   0     1     2     3     4     5     6     7     8     9");for(int i=0;i<chessboard.length;i++){//外层循环控制行System.out.print(i+"   ");for(int j=0;j<chessboard[i].length;j++){//内层循环控制列if(j==chessboard[i].length-1){  //最后一列System.out.print(chessboard[i][j]);}else{System.out.print(chessboard[i][j]+separator);}}System.out.println();if(i<chessboard.length-1){System.out.println("    │     │     │     │     │     │     │     │     │     │");}}}/*** 音效播放* @param fileName*/public static void playAudio(String fileName){URL url =Gobang.class.getResource(fileName);AudioClip clip = Applet.newAudioClip(url);clip.play();try {Thread.sleep(50L);} catch (InterruptedException e) {}}}


 

这篇关于8阶段项目:五子棋(附带源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147482

相关文章

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

使用IntelliJ IDEA创建简单的Java Web项目完整步骤

《使用IntelliJIDEA创建简单的JavaWeb项目完整步骤》:本文主要介绍如何使用IntelliJIDEA创建一个简单的JavaWeb项目,实现登录、注册和查看用户列表功能,使用Se... 目录前置准备项目功能实现步骤1. 创建项目2. 配置 Tomcat3. 项目文件结构4. 创建数据库和表5.

Python项目打包部署到服务器的实现

《Python项目打包部署到服务器的实现》本文主要介绍了PyCharm和Ubuntu服务器部署Python项目,包括打包、上传、安装和设置自启动服务的步骤,具有一定的参考价值,感兴趣的可以了解一下... 目录一、准备工作二、项目打包三、部署到服务器四、设置服务自启动一、准备工作开发环境:本文以PyChar

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

javafx 如何将项目打包为 Windows 的可执行文件exe

《javafx如何将项目打包为Windows的可执行文件exe》文章介绍了三种将JavaFX项目打包为.exe文件的方法:方法1使用jpackage(适用于JDK14及以上版本),方法2使用La... 目录方法 1:使用 jpackage(适用于 JDK 14 及更高版本)方法 2:使用 Launch4j(

Docker集成CI/CD的项目实践

《Docker集成CI/CD的项目实践》本文主要介绍了Docker集成CI/CD的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、引言1.1 什么是 CI/CD?1.2 docker 在 CI/CD 中的作用二、Docke

SpringBoot项目引入token设置方式

《SpringBoot项目引入token设置方式》本文详细介绍了JWT(JSONWebToken)的基本概念、结构、应用场景以及工作原理,通过动手实践,展示了如何在SpringBoot项目中实现JWT... 目录一. 先了解熟悉JWT(jsON Web Token)1. JSON Web Token是什么鬼