package wuziqi;
import java.util.Scanner;
public class Wuziqi {
private static Scanner sc;
public static boolean win(int x,int y,String chess,String[][] arr) {
//橫向
for(int i=0;i<arr.length-5;i++) {
if(arr[x][i].equals(chess)&&arr[x][i+1].equals(chess)
&&arr[x][i+2].equals(chess)&&arr[x][i+3].equals(chess)
&&arr[x][i+4].equals(chess)) {
return true;
}
}
//縱向
for(int i=0;i<arr.length-5;i++) {
if(arr[i][y].equals(chess)&&arr[i+1][y].equals(chess)
&&arr[i+2][y].equals(chess)&&arr[i+3][y].equals(chess)
&&arr[i+4][y].equals(chess)) {
return true;
}
}
//斜向下
if (x >= y) {
int j = 0;
for (int i = x - y; i < arr.length - 5; i++) {
if (arr[i][j].equals(chess) && arr[i + 1][j + 1].equals(chess)
&& arr[i + 2][j + 2].equals(chess) &&arr[i + 3][j + 3].equals(chess)
&& arr[i + 4][j + 4].equals(chess)) {
return true;
}
j++;
}
} else {
int i = 0;
for (int j = y - x; j < arr.length - 5; j++) {
if (arr[i][j].equals(chess) && arr[i + 1][j + 1].equals(chess)
&& arr[i + 2][j + 2].equals(chess) && arr[i + 3][j + 3].equals(chess)
&& arr[i + 4][j + 4].equals(chess)) {
return true;
}
i++;
}
}
//斜向上
int i = 0;
for (int k = x + y - 2; k >= 0; k--) {
if(k-4>0) {
if (arr[i][k].equals(chess) && arr[i + 1][k - 1].equals(chess)
&& arr[i + 2][k - 2].equals(chess) && arr[i + 3][k - 3].equals(chess)
&& arr[i + 4][k - 4].equals(chess)) {
return true;
}
i++;
}
}
return false;
}
public static void main(String[] args) {
String black=" . ";
String white=" 。 ";
String xiaogezi=" + ";
String dagezi=" + ";
String[][] arr = new String[15][15];
//創建棋盤
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr.length;j++) {
if(j<9) {
arr[i][j]=xiaogezi;
}else {
arr[i][j]=dagezi;
}
}
}
//下棋
while(true) {
sc = new Scanner(System.in);
int x ,y,m,n;
//黑棋走
System.out.print("請輸入黑棋的坐標:");
x=sc.nextInt();
y=sc.nextInt();
while(true) {
if(x<1||x>15||y>15||y<1) {
System.out.print("超出邊界,請重新輸入:");
x=sc.nextInt();
y=sc.nextInt();
}else {
break;
}
}
while(true) {
if(xiaogezi.equals(arr[x-1][y-1])||dagezi.equals(arr[x-1][y-1])){
arr[x-1][y-1]=black;
break;
}else {
System.out.print("該位置有棋子,請重新落子:");
x=sc.nextInt();
y=sc.nextInt();
}
if(x<1||x>15||y>15||y<1) {
System.out.print("超出邊界,請重新輸入:");
x=sc.nextInt();
y=sc.nextInt();
}
}
for(int i=0;i<arr.length;i++) {
if(i<9) {
System.out.print(" "+(i+1)+" ");
}else {
System.out.print(" "+(i+1)+" ");
}
}
System.out.println();
for(int i=0;i<arr.length;i++) {
if(i<9) {
System.out.print(" "+(i+1));
}else {
System.out.print(i+1);
}
for(int j=0;j<arr.length;j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
if(win(x-1,y-1,black,arr)) {
System.out.println("黑棋獲勝!");
break;
}
//白棋走
System.out.print("請輸入白棋的坐標:");
m=sc.nextInt();
n=sc.nextInt();
while(true) {
if(m<1||m>15||n>15||n<1) {
System.out.print("超出邊界,請重新輸入:");
m=sc.nextInt();
n=sc.nextInt();
}else {
break;
}
}
while(true) {
if(xiaogezi.equals(arr[m-1][n-1])||dagezi.equals(arr[m-1][n-1])){
arr[m-1][n-1]=white;
break;
}else {
System.out.print("該位置有棋子,請重新落子:");
m=sc.nextInt();
n=sc.nextInt();
}
if(m<1||m>15||n>15||n<1) {
System.out.print("超出邊界,請重新輸入:");
m=sc.nextInt();
n=sc.nextInt();
}
}
for(int i=0;i<arr.length;i++) {
if(i<9) {
System.out.print(" "+(i+1)+" ");
}else {
System.out.print(" "+(i+1)+" ");
}
}
System.out.println();
for(int i=0;i<arr.length;i++) {
if(i<9) {
System.out.print(" "+(i+1));
}else {
System.out.print(i+1);
}
for(int j=0;j<arr.length;j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
if(win(m-1,n-1,white,arr)) {
System.out.println("白棋獲勝!");
break;
}
}
}
uj5u.com熱心網友回復:
友情支持一下轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/149595.html
標籤:應用實例
上一篇:svga
下一篇:GPRMAX
