五子棋完整代码
public static void main(String[] args){
//遍历棋盘
int[][] game = new int[19][19];
for(int i = 0;i<game.length;i++){
for(int j = 0;j<game[i].length;j++){
System.out.print(game[i][j]+" ");
}
System.out.println();
}
//输入黑子为奇数 白子为偶数;
for(int k=0;k>=0;k++){
if(k%2==0){ //判断为白子
System.out.println("白棋落子:");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行");
int row = scanner.nextInt();
System.out.println("请输入列");
int col = scanner.nextInt();
//重新落子
while(game[row][col]!=0){
System.out.println("此地已经落子,请重新落子:");
int row1=scanner.nextInt();
int col1=scanner.nextInt();
row=row1;
col=col1;
}
if(row>=0&row<game.length&col>=0&col<game[row].length){
game[row][col]=2;
for(int i = 0;i<game.length;i++){
for(int j = 0;j<game[i].length;j++){
System.out.print(game[i][j]+" ");
}
System.out.println();
}
}
}
if(k%2!=0){ //判断为黑子
System.out.println("黑棋落子:");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行");
int row = scanner.nextInt();
System.out.println("请输入列");
int col = scanner.nextInt();
//重新落子
while(game[row][col]!=0){
System.out.println("此地已经落子,请重新落子:");
int row1=scanner.nextInt();
int col1=scanner.nextInt();
row=row1;
col=col1;
}
if(row>=0&row<game.length&col>=0&col<game[row].length){
game[row][col]=1;
for(int i = 0;i<game.length;i++){
for(int j = 0;j<game[i].length;j++){
System.out.print(game[i][j]+" ");
}
System.out.println();
}
}
}
//判断输赢
for (int i = 4; i <15; i++) {
for (int j = 0; j <game[i].length-4; j++) {
if(game[i][j]==2&&game[i][j]==game[i+1][j]&&game[i][j]==game[i+2][j]&&game[i][j]==game[i+3][j]&&game[i][j]==game[i+4][j]
||game[i][j]==2&&game[i][j]==game[i+1][j+1]&&game[i][j]==game[i+2][j+2]&&game[i][j]==game[i+3][j+3]&&game[i][j]==game[i+4][j+4]
||game[i][j]==2&&game[i][j]==game[i][j+1]&&game[i][j]==game[i][j+2]&&game[i][j]==game[i][j+3]&&game[i][j]==game[i][j+4]
||game[i][j]==2&&game[i][j]==game[i-1][j+1]&&game[i][j]==game[i-2][j+2]&&game[i][j]==game[i-3][j+3]&&game[i][j]==game[i-4][j+4] )
{
System.out.println("白棋胜!");
return;
}
if(game[i][j]==1&&game[i][j]==game[i+1][j]&&game[i][j]==game[i+2][j]&&game[i][j]==game[i+3][j]&&game[i][j]==game[i+4][j]
||game[i][j]==1&&game[i][j]==game[i+1][j+1]&&game[i][j]==game[i+2][j+2]&&game[i][j]==game[i+3][j+3]&&game[i][j]==game[i+4][j+4]
||game[i][j]==1&&game[i][j]==game[i][j+1]&&game[i][j]==game[i][j+2]&&game[i][j]==game[i][j+3]&&game[i][j]==game[i][j+4]
||game[i][j]==1&&game[i][j]==game[i-1][j+1]&&game[i][j]==game[i-2][j+2]&&game[i][j]==game[i-3][j+3]&&game[i][j]==game[i-4][j+4] )
{
System.out.println("黑棋胜!");
return;
}
}
}
}