importjava.util.Scanner;
publicclassFiveChess{
staticchar[][]chess=newchar[16][16];
staticbooleanisBlack=true;
publicstaticvoidmain(String[]args){
System.out.println(=======五子棋游戏=======);
System.out.println();
for(inti=0;i<chess.length;i++){
for(intj=0;j<chess[i].length;j++){
chess[i][j]='*';
}
}//forend!
Scannersca=newScanner(System.in);
printChess();
while(true){
System.out.println();
System.out.println(请+(isBlack?黑方:白方)+落子:);
Stringstr=sca.next();
intr=fromCharToInt(str.charAt(0));
intc=fromCharToInt(str.charAt(1));
if((r15)||(c15)){
System.out.println(输入位置错误,请重新输入!);
continue;
}elseif(chess[r][c]!='*'){
System.out.println(该位置有子,请重新输入!);
continue;
}else{
chess[r][c]=isBlack?'@':'#';
printChess();
if(wasWin(r,c)){
System.out.println((isBlack?黑方:白方)+获胜,游戏结束!);
break;
}else{
isBlack=!isBlack;
}
}
}
}//mainend!
publicstaticvoidprintChess(){//打印棋盘
System.out.print();
for(inti=0;i<chess.length;i++){
if(i<10){
System.out.print(+i);
}else{
System.out.print(+(char)('a'+i-10));
}
}//forend!
System.out.println();
for(inti=0;i<chess.length;i++){
if(i<10){
System.out.print(i+);
}else{
System.out.print((char)('a'+i-10)+);
}
for(intj=0;j<chess.length;j++){
System.out.print(chess[i][j]+);
}
System.out.println();
}
}//printChessend!
publicstaticintfromCharToInt(charc){//判断是否越界
if(c>='0'&&c<='9'){
returnc-48;
}else{
returnc-'a'+10;
}
}
publicstaticbooleanwasWin(intr,intc){//判断是否赢了
returnwasWinAtV(r,c)||wasWinAtE(r,c)||wasWinAtRD(r,c)
||wasWinAtLD(r,c);
}
publicstaticbooleanwasWinAtV(intr,intc){
charch=isBlack?'@':'#';
inti=r;
while(i>=0&&chess[i][c]==ch){
i--;
}
i++;
intnum=0;
while(i<chess.length&&chess[i][c]==ch){
num++;
i++;
}
returnnum>=5;
}
publicstaticbooleanwasWinAtE(intr,intc){
charch=isBlack?'@':'#';
inti=c;
while(i>=0&&chess[r][i]==ch){
i--;
}
i++;
intnum=0;
while(i<chess.length&&chess[r][i]==ch){
num++;
i++;
}
returnnum>=5;
}
publicstaticbooleanwasWinAtRD(intr,intc){
charch=isBlack?'@':'#';
inti=r;
intj=c;
while(i>=0&&j<chess.length&&chess[i][j]==ch){
i--;
j++;
}
i++;
j--;
intnum=0;
while(i=0&&chess[i][j]==ch){
i++;
j--;
num++;
}
returnnum>=5;
}
privatestaticbooleanwasWinAtLD(intr,intc){
charch=isBlack?'@':'#';
inti=r;
intj=c;
while(i>=0&&j>=0&&chess[i][j]==ch){
i--;
j--;
}
i++;
j++;
intnum=0;
while(i<chess.length&&j<chess.length&&chess[i][j]==ch){
i++;
j++;
num++;
}
returnnum>=5;
}
}
看看我的啊!