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

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

如何用Docker运行Django项目

本章教程,介绍如何用Docker创建一个Django,并运行能够访问。 一、拉取镜像 这里我们使用python3.11版本的docker镜像 docker pull python:3.11 二、运行容器 这里我们将容器内部的8080端口,映射到宿主机的80端口上。 docker run -itd --name python311 -p

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

工厂ERP管理系统实现源码(JAVA)

工厂进销存管理系统是一个集采购管理、仓库管理、生产管理和销售管理于一体的综合解决方案。该系统旨在帮助企业优化流程、提高效率、降低成本,并实时掌握各环节的运营状况。 在采购管理方面,系统能够处理采购订单、供应商管理和采购入库等流程,确保采购过程的透明和高效。仓库管理方面,实现库存的精准管理,包括入库、出库、盘点等操作,确保库存数据的准确性和实时性。 生产管理模块则涵盖了生产计划制定、物料需求计划、

SpringBoot项目是如何启动

启动步骤 概念 运行main方法,初始化SpringApplication 从spring.factories读取listener ApplicationContentInitializer运行run方法读取环境变量,配置信息创建SpringApplication上下文预初始化上下文,将启动类作为配置类进行读取调用 refresh 加载 IOC容器,加载所有的自动配置类,创建容器在这个过程

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免