其他分享
首页 > 其他分享> > 2021.1.27笔记

2021.1.27笔记

作者:互联网

创建一个五子棋importjava.util.Scanner;
publicclassDemo101{//1.创建一个棋盘publicstaticString[][]board=newString[15][15];//玩家轮流计数publicstaticintplayer=0;//黑棋和白棋publicstaticStringBLACK_CHESS=“O”;publicstaticStringWHITE_CHESS=“X”;publicstaticScannerinput=newScanner(System.in);publicstaticvoidmain(String[]args){//2.给棋盘的每一个元素要赋值+initBoard();printBoard();//3.开始下棋startGame();}publicstaticvoidstartGame(){while(!isGameOver()){if(player%20){//黑方下棋System.out.println(">>>请黑方下棋");playChess(BLACK_CHESS);}else{//白方下棋System.out.println(">>>请白方下棋");playChess(WHITE_CHESS);}player++;}if(player1515){System.out.println(">>>和棋,游戏结束!");}else{if((player-1)%20){System.out.println(">>>黑胜!游戏结束!");}else{System.out.println(">>>白胜!游戏结束!");}}
}
publicstaticbooleanisGameOver(){for(introw=0;row<board.length;row++){for(intcol=0;col<board[row].length;col++){if(!board[row][col].equals("+")){//向右if(col<11){booleanflag=true;for(intc=col+1;c<=col+4;c++){if(!board[row][col].equals(board[row][c])){flag=false;break;}}if(flag){returntrue;}}//向下if(row<11){booleanflag=true;for(intr=row+1;r<=row+4;r++){if(!board[row][col].equals(board[r][col])){flag=false;break;}}if(flag){returntrue;}}//向右上if(row>3&&col<11){booleanflag=true;for(intr=row-1,c=col+1;c<=col+4;r–,c++){if(!board[row][col].equals(board[r][c])){flag=false;break;}}if(flag){returntrue;}}//向右下if(row<11&&col<11){booleanflag=true;for(intr=row+1,c=col+1;c<=col+4;r++,c++){if(!board[row][col].equals(board[r][c])){flag=false;break;}}if(flag){returntrue;}}
}
}}if(player
15
15){returntrue;}returnfalse;}publicstaticvoidplayChess(Stringchess){System.out.print(">>>请输入棋子坐标:");intx=input.nextInt()-1;inty=input.nextInt()-1;if(!board[x][y].equals("+")){System.out.println(">>>此处已有棋子,请重新下棋!");player–;return;}board[x][y]=chess;printBoard();}publicstaticvoidprintBoard(){System.out.print("");for(inti=1;i<=board[0].length;i++){System.out.printf("%-2d",i);}System.out.println();for(inti=0;i<board.length;i++){System.out.printf("%2d",i+1);for(intj=0;j<board[i].length;j++){System.out.print(board[i][j]+"");}System.out.println();}}publicstaticvoidinitBoard(){for(inti=0;i<board.length;i++){for(intj=0;j<board[i].length;j++){board[i][j]="+";}}}}

标签:2021.1,27,下棋,System,笔记,player,CHESS,println,out
来源: https://blog.csdn.net/weixin_54010073/article/details/113267828