利于二维数组写一个五子棋,棋盘14x14

package com.lanou.homework0;


import java.util.Scanner;


public class WuZiQi {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[ ][ ] map = new int [14][14];

for(int i = 0 ;i < map.length ; i++){

for(int j = 0 ; j < map[i].length ; j++){

System.out.print(map[i][j] +  "  ");

}

System.out.println();

}

while(true){

int m;

int n;

do{

do{System.out.println("请输入白棋的横坐标");

m = scanner.nextInt();

}while(m >= map.length || m < 0);

do{System.out.println("请输入白棋的纵坐标");

n = scanner.nextInt();

}while(n >= map[m].length || n < 0);

}while(map[m][n] == 1 || map[m][n] == 2);

map [m][n] = 1;

for(int i = 0 ;i < map.length ; i++){

for(int j = 0 ; j < map[i].length ; j++){

System.out.print(map[i][j] +  "  ");

}

System.out.println();

}

//判断条件

for(int h=0;h <= 5;h++){

for(int k=0; k <= 5;k++){        //遍历一次左上的6x6数组   首先确定一个起始点

   

if(map[h][k] == 1 && map[h][k] ==map[h+1][k+1]&&map[h][k] ==map[h+2][k+2]&&map[h][k] ==map[h+3][k+3]&&map[h][k] ==map[h+4][k+4]

||map[h][k] == 1 && map[h][k] ==map[h+1][k]&&map[h][k] ==map[h+2][k]&&map[h][k] ==map[h+3][k]&&map[h][k] ==map[h+4][k]

||map[h][k] == 1 && map[h][k] ==map[h][k+1]&&map[h][k] ==map[h][k+2]&&map[h][k] ==map[h][k+3]&&map[h][k] ==map[h][k+4]

){

System.out.println("白棋获胜" );

return;

}

}

}

for(int h=4;h <= 9;h++){

for(int k=0; k <= 5;k++){

if(map[h][k] == 1 && map[h][k] ==map[h-1][k+1]&&map[h][k] ==map[h-2][k+2]&&map[h][k] ==map[h-3][k+3]&&map[h][k] ==map[h-4][k+4]){

System.out.println("白棋获胜了");

return;

}

}

}

int x;

int y;

do{

do{

System.out.println("请输入黑棋的横坐标");

x = scanner.nextInt();

}while(x >= map.length ||x < 0);

do{

System.out.println("请输入黑棋的纵坐标");

y = scanner.nextInt();

}while(y >= map[x].length || y < 0);

}while(map[x][y] == 1 || map[x][y] == 2);

map [x][y] = 2;

for(int i = 0 ;i < map.length ; i++){

for(int j = 0 ; j < map[i].length ; j++){

System.out.print(map[i][j] +  "  ");

}

System.out.println();

}

//判断条件

for(int h=0;h <= 5;h++){

for(int k=0; k <= 5;k++){

    float sum4=0;

float sum5=0;

float sum6=0;

for(int j = 0;j <= 4;j++){

sum4=sum4+map[h+j][k];

sum5 = sum5+map[h][k+j];

sum6 =sum6+ map[h+j][k+j];

}

if( sum4/5== 2 || sum5/5 ==2 || sum6/5 == 2){

System.out.println("黑棋获胜");

return;

}

}

}

for(int h=4;h <= 9;h++){

for(int k=0; k <= 5;k++){

float sum=0;

for(int j = 0;j <= 4;j++){

sum = sum+map[h-j][k+j];

}

if(sum/5 == 2){

System.out.println("黑棋获胜了");

return;

}

}

}

}

}

}



你可能感兴趣的:(java,二维数组)