《Java语言程序设计》编程练习8.9(游戏:#字游戏加强版)
程序说明看一下链接
https://www.cnblogs.com/buyiyangdefengcai/p/11664287.html
一下是在此基础上改进
/** fileName:game.java * 作用:#字游戏 * 创建日期:2019年10月10日 * 修改日期:2020年10月22日 * mail:xuangliang1@live.com */ import java.util.Scanner; public class game_3{ final static int RANKS = 9; /*棋盘行数和列数*/ static String player1 = "●"; static String player2 = "▢"; public static void main(String[] args){ Scanner input = new Scanner(System.in); //棋盘数组 String[][] chess = getInitChessEmu(); /** 结束标志,ture为未有输赢结果,false为已有玩家赢或者平局 */ boolean endMask = true; do { //打印棋盘 chess = emu(chess); //玩家1下棋 System.out.print("请玩家 " + player1 + " 输入横坐标(1 - " + RANKS*RANKS + "): "); int player1Scanner = input.nextInt(); //将玩家1下的结果输入到数组中 while(true){ if((chess[(player1Scanner - 1)/RANKS][(player1Scanner - 1)%RANKS] != player2) && chess[(player1Scanner - 1)/RANKS][(player1Scanner-1)%RANKS] != player1){ chess[(player1Scanner - 1)/RANKS][(player1Scanner - 1)%RANKS] = player1; break; } else{ System.out.print("请玩家 " + player1 + " 重新输入横坐标(1 - " + RANKS*RANKS + "): "); player1Scanner = input.nextInt(); } } //打印棋盘 chess = emu(chess); //判断玩家1游戏结果 switch (maskGameResults(chess, player1)){ case 0: break; case 1: System.out.println("玩家 " + player1 + " 获得胜利!"); endMask = false; break; case -1: System.out.println("游戏平局,请重来!"); endMask = false; break; } //玩家2下棋 if(endMask){ System.out.print("请玩家 " + player2 + " 输入横坐标(1 - " + (RANKS*RANKS) + "): "); int player2Scanner = input.nextInt(); //将玩家2下的结果输入到数组中 while(true){ if ((chess[(player2Scanner - 1)/RANKS][(player2Scanner - 1)%RANKS] != player1) && chess[(player2Scanner-1)/RANKS][(player2Scanner-1)%RANKS] != player2 ){ chess[(player2Scanner - 1)/RANKS][(player2Scanner - 1)%RANKS] = player2; break; } else{ System.out.print("请玩家 " + player2 + " 重新输入横坐标(1 - " + (RANKS*RANKS) + "): "); player2Scanner = input.nextInt(); } } chess = emu(chess); switch (maskGameResults(chess, player2)){ case 0: break; case 2: System.out.println("玩家 " + player2 + " 获得胜利!"); endMask = false; break; case -1: System.out.println("游戏平局,请重来!"); break; } } //打印棋盘 }while(endMask); } /** 显示棋盘 */ public static String[][] emu(String [][] chess){ for(int i = 0; i < RANKS*5 + 3; i++){ if( i%5 == 1 ) System.out.printf("+"); else System.out.printf("-"); } System.out.println(); for(int i = 0; i<RANKS; i++){ for(int j = 0; j<RANKS; j++ ){ System.out.printf(" |%3s", chess[i][j]); } System.out.println(" |"); for( int k = 0; k < RANKS*5+ 3; k++){ if( 1 == k%5 ) System.out.printf("+"); else System.out.print("-"); } System.out.println(); } System.out.println(); return chess; } /** 初始化棋盘,使其不显示NULL */ public static String [][] getInitChessEmu(){ int i =1; String chess[][] = new String [RANKS][RANKS]; for(int a = 0; a< RANKS ; a++){ for(int b = 0; b< RANKS; b++){ chess[a][b] = Integer.toString(i); i++; } } return chess; } /** 判断玩家输赢 */ public static int maskGameResults(String m [][], String player){ //棋子连续数player int numberRow= 0; /* 连续行*/ int numberHig=0; /* 连续列*/ int numberRowHig=0; /*左上*/ int numberRowHigHig=0; /* 左下*/ int numberRowRig = 0; int numberRigHig = 0; int numberLeftHig = 0; /** 判断连续行 和列*/ for(int i = 0; i < RANKS; i++){ for(int j =1; j < RANKS; j++){ if ( (m[i][j-1] ==m[i][j] ) ){ numberRow++; } if ( (m[j-1][i] == m[j][i] ) ){ numberHig++; } if(numberRow == 4 || numberHig == 4 ){ if (player == player1) return 1; return 2; } } numberRow=0; numberHig=0; } numberRow= 0; numberHig=0; /** 判断左上左下 */ for(int i = 0; i < RANKS - 1; i++){ if(m[i][i] == m[i+1][i+1]) numberRow++; else numberRow = 0; if(i != RANKS-2){ if( m[i][i+1] == m[i+1][i+2]) numberRowHig++; else numberRowHig=0; if(m[i+1][i] == m[i+2][i+1]) numberRowHigHig ++; else numberRowHigHig = 0; } //判断右上左上对角线 if(m[i][RANKS-1-i] == m[i+1][RANKS-2-i]) numberRowRig++; else numberRowRig = 0; //判断胜负 if(numberRow == 4 || numberRowHig == 4 || numberRowHigHig == 4 || numberRowRig == 4 || numberLeftHig ==4){ if (player == player1) return 1; return 2; } } /* 判断右下、右上*/ /* 以后再加入 for(int i = 1; i < RANKS-1; i++){ for(int j = 0; j < RANKS-1; j++){ if(m[i][j] == m[i-1][j+1]) numberRow++; else numberRow=0; System.out.print(numberRow); } if(numberRow == 4 ){ if (player == player1) return 1; return 2; } } */ /** 平局判断 */ for(int i = 0; i< RANKS; i++){ for(int j = 0; j<RANKS; j++){ if(m[i][j] != player1 && m[i][j] != player2) return 0; } } return -1; } }