import javax.jws.soap.SOAPBinding; import java.util.Scanner; /** *五子棋 * 胜利条件: * 五子棋棋盘为一个10 x 10的棋盘,五子棋玩家共2个(这里分别称为A和B),A在棋盘上落子后,B再 * 落子,依次往复,直到一方胜利或者棋盘空间用完为止。判断胜利的条件是一条直线或者任意斜线上同 * 时存在A或者B的连续5颗棋子(见下图)。 * 分析: * 1、先制作一个10*10的二维数组 * 2、落子 * a. 玩家A、B会交替落子 * b. 落子位置必须是0~100之间的整数,且不能使用已经存在棋子的位置 * c. 落子完成后,需要校验是否获胜 * d. 棋盘使用完毕还未分出胜负,需要提示 */ public class example { public static char[][] chessboard = new char[][] { {'┌', '┬', '┬', '┬', '┬', '┬', '┬', '┬', '┬', '┐'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'}, {'└', '┴', '┴', '┴', '┴', '┴', '┴', '┴', '┴', '┘'}, }; public static String separator="————"; public static int times=0;//下棋的次数 public static void main(String[] args) { //落子 char playA='○';char playB='■'; //A、B轮流落子 Scanner sc=new Scanner(System.in); win: for(;times<chessboard.length*chessboard[0].length;++times)//A、B落子的总次数 { if(times==0) showChessboard(); System.out.println(times%2==0?"棋手A请落子":"棋手B请落子"); char play=times%2==0?playA:playB; if(sc.hasNextInt()) { int o = sc.nextInt(); int row = o / chessboard.length; int lie = o % chessboard[0].length; //判断是否胜利 if(chessboard[row][lie]==playA||chessboard[row][lie]==playB) { System.out.println("落子错误,请重新输入"); times--; continue; } chessboard[row][lie] = play; showChessboard(); for(int i=0;i<chessboard.length-4;++i) { for(int j=0;j<chessboard[0].length-4;++j) { boolean case1=//180度 i不变 j加一 j<chessboard[0].length-4&& chessboard[i][j]==play&& chessboard[i][j+1]==play&& chessboard[i][j+2]==play&& chessboard[i][j+3]==play&& chessboard[i][j+4]==play; boolean case2=//90度 i加一 j不变 i<chessboard.length-4&& chessboard[i][j]==play&& chessboard[i+1][j]==play&& chessboard[i+2][j]==play&& chessboard[i+3][j]==play&& chessboard[i+4][j]==play; boolean case3=//135度 i加一 j加一 i<chessboard.length-4&& j<chessboard[0].length-4&& chessboard[i][j]==play&& chessboard[i+1][j+1]==play&& chessboard[i+2][j+2]==play&& chessboard[i+3][j+3]==play&& chessboard[i+4][j+4]==play; boolean case4=//45度 i减一 j加一 i>4&& j<chessboard[0].length-4&& chessboard[i][j]==play&& chessboard[i+1][j-1]==play&& chessboard[i+2][j-2]==play&& chessboard[i+3][j-3]==play&& chessboard[i+4][j-4]==play; if(case1||case2||case3||case4) { System.out.println(times%2==0?"棋手A胜利":"棋手B胜利"); break win; } } } } else { System.out.println("落子错误,请重新输入"); times--; sc.next(); continue; } } if(times==100) 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]+separator); else System.out.println(chessboard[i][j]); } if(i<chessboard.length-1) System.out.println(" | | | | | | | | | |"); } }//打印棋盘的函数 }
java 五子棋实现(IDEA)
于 2023-07-15 09:57:07 首次发布