Advertisement
ausbrumm96

ChessGameplay

Apr 7th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package chess;
  2.  
  3. import boardgame.*;
  4.  
  5. import java.util.HashSet;
  6.  
  7. public class ChessGameplay extends TwoPlayerGame {
  8.     ChessBoard board;
  9.     public ChessGameplay(Player player1, Player player2){
  10.         super(player1, player2);
  11.         board = initBoard( 8 );
  12.     }
  13.  
  14.     @Override
  15.     protected void menu() {
  16.         System.out.println( "\t\t ----------------------" );
  17.         System.out.println( "\t\t|        Chess        |" );
  18.         System.out.println( "\t\t ----------------------" );
  19.         System.out.println( "Rules:" );
  20.         System.out.println( "1. Enter your moves in algebraic notation" );
  21.         System.out.println( "2. Put the other player in checkmate" );
  22.         System.out.println( "3. Type 'help' to get a list of available moves" );
  23.         System.out.println( "4. Type 'quit' or 'exit' to exit the game.\n" );
  24.     }
  25.  
  26.     ChessBoard initBoard(int size) {
  27.         ChessBoard new_board = new ChessBoard( size );
  28.         new_board.init();
  29.         return new_board;
  30.     }
  31.     @Override
  32.     public void run() {
  33.         ChessBoard testBoard;
  34.         testBoard = board.clone();
  35.         HashSet<ChessMove> initialMoves;
  36.  
  37.         menu();
  38.         Boolean inCheck = false;
  39.         while(true) {
  40.             board.print();
  41.             initialMoves = board.generateMoves( currentPlayer );
  42.             initialMoves = testBoard.removeBadMoves(initialMoves, currentPlayer);
  43.             if(inCheck){
  44.  
  45.                 if(initialMoves.size() == 0){
  46.                     System.out.print( "Checkmate!" );
  47.                     playerWins( otherPlayer );
  48.                     break;
  49.                 }
  50.                 System.out.println( "Check" );
  51.             }
  52.             while (true) {
  53.                 playerPrompt( currentPlayer );
  54.                 String input = Application.input.nextLine();
  55.                 if (input.equalsIgnoreCase( "exit" ) || input.equalsIgnoreCase( "quit" ))
  56.                     exitGame();
  57.                 if(input.equalsIgnoreCase( "help" )){
  58.                     board.printAvailableMoves( initialMoves );
  59.                     continue;
  60.                 }
  61.                 if (board.tryPlayingPosition( currentPlayer, input)) {
  62.                     break;
  63.                 }
  64.             }
  65.             nextPlayer();
  66.             System.out.println(  );
  67.             stalemate();
  68.  
  69.             if(board.playerInCheck( otherPlayer)){
  70.                 inCheck = true;
  71.             }else{
  72.                 inCheck = false;
  73.             }
  74.         }
  75.     }
  76.  
  77.     private void playerWins(Player player) {
  78.         System.out.println( "\nCongratulations, " +
  79.                 (player.getName().equalsIgnoreCase("player 1" ) ? "Uppercase" : "lowercase")
  80.                 + "! You won!\n" );
  81.         System.exit( 0 );
  82.     }
  83.  
  84.  
  85.     private void playerPrompt(Player currentPlayer) {
  86.         String tmp = "";
  87.         if(currentPlayer.getColor() == Colors.WHITE)
  88.             tmp = "Lowercase";
  89.         else tmp = "Uppercase";
  90.         System.out.println( currentPlayer.getName() + "'s turn. Your piece is " + tmp);
  91.         System.out.println( "Enter your move in algebraic notation(a6-b5): " );
  92.     }
  93.  
  94.     public void stalemate(){
  95.         if(board.generateMoves( currentPlayer ).size() == 0 || board.generateMoves( otherPlayer ).size() == 0){
  96.             System.out.println( "It's a stalemate." );
  97.             System.exit( 0 );
  98.         }
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement