gobang

来源:互联网 发布:淘宝花呗商家手续费 编辑:程序博客网 时间:2024/06/06 18:23
import java.util.*;


public class GoBang{

public static final int SIZE = 13 ;
// chesss
private List<Chess> chesss  = new ArrayList<Chess>() ;
//input device
private Scanner input = new Scanner(System.in);

public static final int WHITH = 1 ;// the whith chess player
public static final int BLACK = 2; // the black chess player
public static final String WHITH_STR = "B";
public static final String BLACK_STR = "H";
public static final String NULL_STR = " ";


public static int CURRENT_PLAYER = WHITH ;//current pleay
 
         
// put down one chess
public void putChess(int player){
   while(true){
System.out.print( player == WHITH ? "whith player " : "black player "  );
System.out.println( "   Please input the Address(x , y):");
String str = input.next();
if(str == null || "".equals(str)){
          System.out.println("input not null !");   
          continue ;
}else if(!str.matches("^\\w*\\d{1,2}\\w*,\\w*\\d{1,2}\\w*$")){
          System.out.println("input Format is not correct!");
  continue ;
}else{
  String xy[] = str.split(",");
  int x = Integer.parseInt(xy[0].trim()) ;
  int y = Integer.parseInt(xy[1].trim()) ;
  if(checkAlreadyDown(x , y) != null){
     System.out.println("here already have a chess !");
     continue;
  }else{
    chesss.add(new Chess(x  , y  , player));
    break;
  }
}
   }
}

//print the all chess piecess
public void printChess(){
for(int y = 0 ; y <= SIZE ; y++){
   for(int x = 0 ; x <= SIZE ; x++){
 if(y == 0){
   System.out.print(x + "\t");
                   continue ;
 }
   if(x == 0){
                             System.out.print(y + "\t");
                    continue ;
  }
   Chess chess = checkAlreadyDown(x  , y);
                          if(chess == null){
   System.out.print( NULL_STR +  "\t");
                           }else{
    if(chess.player == WHITH){
         System.out.print( WHITH_STR + "\t");
    }else{
        System.out.print( BLACK_STR + "\t");
    }
  }
}
 System.out.println("\n\n");
}
}


//check is already down in hear
public Chess checkAlreadyDown(int x , int y){
for( int i = 0  ; i < chesss.size() ; i++){
     Chess chess = chesss.get(i);
     if(chess.x == x && chess.y == y){
return chess ;
     }
}
return null ;   
}

//check the player win
public boolean isWiner(int player){
outer : for(int i = 0 ; i < chesss.size() ; i++){
Chess chess = chesss.get(i);
if(chess.player == player){
  int x = chess.x ;
  int y = chess.y ;
  //check the up 
  for(int j = 1 ; j <= 4 ;j++){
   Chess upChess = checkAlreadyDown(x , y - j) ;
   if(upChess != null && upChess.player == player ){
               if(j == 4)
  return true;
   }else{
break; 
   }
            }
  //check up right
for(int j = 1 ; j <= 4 ; j++){
   Chess upChess = checkAlreadyDown(x + j , y - j) ;
                                    if(upChess != null && upChess.player == player ){
      if(j == 4)
 return true ;
   }else{
                                       break;
                                    }
}

 //check right
                                for(int j = 1 ; j <= 4 ; j++){
                                    Chess upChess = checkAlreadyDown(x + j ,y ) ;
                                    if(upChess != null && upChess.player == player ){
if(j == 4)
   return true;
  }else{
                                        break;
                                   }
                                }

 //check right down
                                for(int j = 1 ; j <= 4 ; j++){
                                    Chess upChess = checkAlreadyDown(x + j , y + j) ;
                                    if(upChess != null && upChess.player == player ){
if(j == 4)
  return true ;
                                    }else{
break;
   }
                                }
}else{
continue ;
}
}
return false;
}

public void run(){
  printChess();
  while(true){
if(isWiner(CURRENT_PLAYER)){
   System.out.println( (CURRENT_PLAYER == WHITH ? "which player" : "black player ")  +  " is winer , Game Over!");
                    break;
}
CURRENT_PLAYER = CURRENT_PLAYER == WHITH ? BLACK : WHITH ;
putChess(CURRENT_PLAYER);
printChess();
  }
}

public static void main(String[] args){
   System.out.println( "---------------- Welcome to the gobang ----------------");
   new GoBang().run();


}


}


class Chess{
    public int x ; 
    public int y ;
    public int player ;

    public Chess(){}
    public Chess(int x , int y , int player){
this.x = x ;
this.y = y;
this.player  = player ;
   }


}