Home > database >  Java 2 d array write simple gobang game
Java 2 d array write simple gobang game

Time:10-29

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) {
//lateral
for(int i=0; iIf (arr [x] [I] equals (chess) & amp; & Arr [x] [I + 1] equals (chess)
& & Arr [x] [I + 2] equals (chess) & amp; & Arr [x] [I + 3] equals (chess)
& & Arr [x] [I + 4] equals (chess)) {
return true;
}
}
//longitudinal
for(int i=0; iIf (arr [I] [y] equals (chess) & amp; & Arr [y]. [I + 1] equals (chess)
& & Arr [y]. [I + 2] equals (chess) & amp; & Arr [I + 3] [y] equals (chess)
& & Arr [y]. [I + 4] equals (chess)) {
return true;
}
}
//inclined down
If (x & gt;=y) {
int j=0;
For (int I=x - y; i If (arr [I] [j] equals (chess) & amp; & Arr [I + 1] [j + 1] equals (chess)
& & Arr [I + 2) + 2 [j]. J equals (chess) & amp; & Arr/I + 3 + 3 [j]. J equals (chess)
& & Arr [I + 4] + 4 [j]. J equals (chess)) {
return true;
}
J++;
}
} else {
int i=0;
For (int j=y - x; J & lt; Arr. Length - 5; J++) {
If (arr [I] [j] equals (chess) & amp; & Arr [I + 1] [j + 1] equals (chess)
& & Arr [I + 2) + 2 [j]. J equals (chess) & amp; & Arr/I + 3 + 3 [j]. J equals (chess)
& & Arr [I + 4] + 4 [j]. J equals (chess)) {
return true;
}
i++;
}
}
//inclined upward
int i=0;
For (int k=x + y - 2; K & gt;=0; K -) {
If (k - 4 & gt; 0 {
If (arr [k]. [I] equals (chess) & amp; & Arr [I + 1] [k - 1]. The equals (chess)
& & Arr [I + 2] [k - 2] equals (chess) & amp; & 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="+";
Arr=new String String [] [] [15] [15].
//create a board
for(int i=0; ifor(int j=0; jIf (j<9) {
Arr [I] [j]=xiaogezi;
} else {
Arr [I] [j]=dagezi;
}

}
}
//chess
While (true) {
sc=new Scanner(System.in);
Int x, y, m, n;
//black go
System. The out. Print (" please enter the coordinates of black: ");
X=sc. NextInt ();
Y=sc. NextInt ();
While (true) {
If (x<1 | | x> 15 | | y> 15 | | y<1) {
System. The out. Print (" beyond the border, please input again: ");
X=sc. NextInt ();
Y=sc. NextInt ();
} else {
break;
}
}
While (true) {
If (xiaogezi. Equals (arr [1] x [1] y) | | dagezi. Equals (arr [1] x [1] y)) {
Arr [1] x [1] y=black;
break;
} else {
System. The out. Print (" the location have pieces, please move later again: ");
X=sc. NextInt ();
Y=sc. NextInt ();
}
If (x<1 | | x> 15 | | y> 15 | | y<1) {
System. The out. Print (" beyond the border, please input again: ");
X=sc. NextInt ();
Y=sc. NextInt ();
}
}
for(int i=0; iIf (i<9) {
System. The out. Print (" "+ (I + 1) +" ");
} else {
System. The out. Print (" "+ (I + 1) +" ");
}
}
System.out.println();
for(int i=0; iIf (i<9) {
System. The out. Print (" "+ (I + 1));
} else {
System. The out. Print (I + 1);
}
for(int j=0; jSystem. The out. Print (arr [I] [j]);
}
System.out.println();
}
If (win (1 x, y - 1, black, arr)) {
System. Out.println (" black win!" );
break;
}
Walk//white
System. The out. Print (" please input the coordinates of white: ");
M=sc nextInt ();
N=sc. NextInt ();
While (true) {
If (m<1 | | m> 15 | | n> 15 | | n<1) {
System. The out. Print (" beyond the border, please input again: ");
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. The out. Print (" the location have pieces, please move later again: ");
M=sc nextInt ();
N=sc. NextInt ();
}
If (m<1 | | m> 15 | | n> 15 | | n<1) {
System. The out. Print (" beyond the border, please input again: ");
M=sc nextInt ();
N=sc. NextInt ();
}
}
for(int i=0; iIf (i<9) {
System. The out. Print (" "+ (I + 1) +" ");
} else {
System. The out. Print (" "+ (I + 1) +" ");
}
}
System.out.println();
for(int i=0; iIf (i<9) {
System. The out. Print (" "+ (I + 1));
} else {
System. The out. Print (I + 1);
}
for(int j=0; jSystem. The out. Print (arr [I] [j]);
}
System.out.println();
}
If (win (m - 1, n - 1, white, arr)) {
System. Out.println (" white win!" );
break;
}

}
}

CodePudding user response:

Friendship support
Prev:svga
Next:GPRMAX
  • Related