Simple Implementation Method of Java Console Version Gobang
- 2021-08-12 02:46:02
- OfStack
Design a 10*10 chessboard:
Line number and column number are output separately
package yu;
import java.util.Scanner;
public class WuZiQi {
/* ● Chess pieces 1
0 Chess pieces 2
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] [] qipan=new String [10] [10];
// Initialize the chessboard :
for(int k=0;k<qipan.length;k++){
for(int q=0;q<qipan[k].length;q++){
qipan[k][q]="+ ";
}
}
// Output chessboard :
System.out.print(" ");
for(int i=0;i<10;i++){
System.out.print(i+" ");
}
System.out.println();
for(int k=0;k<qipan.length;k++){
System.out.print(k+" ");
for(int q=0;q<qipan[k].length;q++){
System.out.print(qipan[k][q]);
}
System.out.println();
}
Enter coordinates to play chess (x, y) and make fault-tolerant treatment:
Ensure that the input coordinates are (x, y); Subscript out-of-bounds processing; Judging whether there are chess pieces in this coordinate; Make sure the coordinates are entered as numbers.
int x,y;// Save chess coordinates :
Scanner sc=new Scanner(System.in);
boolean flag=true;// Distinguish black and white chess ;
while(true){
System.out.println(" Please enter coordinates to play chess, coordinate format (x,y)");
String str=sc.nextLine();
String [] str1=str.split(",");
// Fault tolerant processing 1
if(str1.length!=2){
System.out.println(" Coordinate input error, please re-enter! ! ");
}else{
// Fault tolerant processing 3
try{
x=Integer.parseInt(str1[0]);
y=Integer.parseInt(str1[1]);
}catch(Exception e){
System.out.println(" Incorrect input of coordinates, please re-enter !!");
continue;
}
// Fault tolerant processing 2-- Subscript out of bounds
if(x>=10||y>=10){
System.out.println(" Incorrect input of coordinates, please re-enter !!");
}else{
// Fault tolerant processing -- Judge whether there are chess pieces in the current position :
// Black and White Chess:
if(qipan[x][y].equals("+ ")){
if(flag){
qipan[x][y]=" ● ";
}else{
qipan[x][y]=" 0 ";
}
flag=!flag;
}else{
System.out.println(" There are already pieces in the current position, please re-enter the coordinates !!");
continue;
}
// Output chessboard :
System.out.print(" ");
for(int i=0;i<10;i++){
System.out.print(i+" ");
}
System.out.println();
for(int k=0;k<qipan.length;k++){
System.out.print(k+" ");
for(int q=0;q<qipan[k].length;q++){
System.out.print(qipan[k][q]);
}
System.out.println();
}
Judge whether there are 5 sub-beads:
8 directions, 4 lines
Above & Below Left side & Right side Left oblique up & Right oblique downward Right oblique upward & Left oblique downward
// Judge whether 5 Zilianzhu :
int count=1;
String currentZiQi=qipan[x][y];// Save the current chess piece ;
// Above judgment :
for(int k=x-1;k>=0;k--){
if(qipan[k][y].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
// Judge below :
for(int k=x+1;k<10;k++){
if(qipan[k][y].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
count=1;// Reset count;
// Judge the left :
for(int k=y-1;k>=0;k--){
if(qipan[x][k].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
// Judge the right side :
for(int k=y+1;k<10;k++){
if(qipan[x][k].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
count=1;
// Judge the upper left hypotenuse :
for(int k=x-1,j=y-1;k>=0&&j>=0;k--,j--){
if(qipan[k][j].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
// Lower right rhombic :
for(int k=x+1,j=y+1;k<10&&j<10;k++,j++){
if(qipan[k][j].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
count=1;
// Lower left rhombic :
for(int k=x-1,j=y+1;k>=0&&j<10;k--,j++){
if(qipan[k][j].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
// Upper right rhombic :
for(int k=x+1,j=y-1;k<10&&j>=0;k++,j--){
if(qipan[k][j].equals(currentZiQi)){
count++;
}else{
break;
}
}
if(count>=5){
System.out.println(currentZiQi+" Win! ! ! ");
break;
}
count=1;
}
}
}
}
}
Summarize