Advertisement
Guest User

Chessgame

a guest
Nov 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. public class ChessGame {
  2.  
  3.     String[][] board = new String[8][8];
  4.     public int z = 0;
  5.  
  6.     boolean nextPlayer = true;
  7.  
  8.  
  9.     public void turnPlayer() {
  10.         nextPlayer = !nextPlayer;
  11.     }
  12.  
  13.  
  14.     public void fillBoard() {
  15.  
  16.  
  17.         //Pawns
  18.         for (int k = 0; k < 8; k++) {
  19.             board[1][k] = "p";
  20.             board[6][k] = "P";
  21.         }
  22.  
  23.         // Knights
  24.         board[0][1] = "n";
  25.         board[0][6] = "n";
  26.         board[7][1] = "N";
  27.         board[7][6] = "N";
  28.  
  29.         //Bishops
  30.         board[0][2] = "b";
  31.         board[0][5] = "b";
  32.         board[7][2] = "B";
  33.         board[7][5] = "B";
  34.  
  35.         // Rooks
  36.         board[0][0] = "r";
  37.         board[0][7] = "r";
  38.         board[7][0] = "R";
  39.         board[7][7] = "R";
  40.  
  41.         //Queens
  42.         board[0][3] = "q";
  43.         board[7][3] = "Q";
  44.  
  45.         //Kings
  46.         board[0][4] = "k";
  47.         board[7][4] = "K";
  48.  
  49.  
  50.         // Knights
  51.         board[0][1] = "n";
  52.         board[0][6] = "n";
  53.         board[7][1] = "N";
  54.         board[7][6] = "N";
  55.  
  56.  
  57.         // quick check if everything was done correctly
  58.  
  59.  
  60.  
  61.  
  62.             for (int i = 0; i < 8; i++) {
  63.  
  64.                 if (i != 0) {
  65.                     System.out.print(" / ");
  66.                 }
  67.  
  68.                 //System.out.print("/");
  69.  
  70.                 for (int j = 0; j < 8; j++) {
  71.  
  72.                     if (board[i][j] == null) {
  73.                         board[i][j] = " ";
  74.                     } // replace all cells with a " " <- for empty cell
  75.  
  76.                     if (board[i][j].equals(" ")) {
  77.                         z++;
  78.                         if (z == 8) {
  79.                             System.out.print(z);
  80.                             z = 0;
  81.                         }
  82.                     }         // if the cell is an empty cell then z++
  83.  
  84.                     else {                                        // otherwise there are two specific opportunites
  85.  
  86.  
  87.                         if (z == 0) {                              // if the counter z = 0 then print the board with the pieces
  88.                             System.out.print(board[i][j] +  " " );  // and set z = 0 again
  89.                             z = 0;
  90.  
  91.                         } else {
  92.                             //   System.out.print(z);                     // if the counter is not equal 0 then print z and the board with the current cell
  93.                             z = 0;
  94.                             System.out.print(board[i][j] + " " );
  95.  
  96.                         }
  97.  
  98.                     }
  99.  
  100.  
  101.                 }
  102.  
  103.             }
  104.  
  105.  
  106.         }
  107.  
  108.  
  109.  
  110.  
  111.     public void switchPlayer(){
  112.         if (nextPlayer) {               // Player w
  113.             fillBoard();
  114.             System.out.print(" w");
  115.         }
  116.  
  117.         if (!nextPlayer){               // Player b
  118.             fillBoard();
  119.             System.out.print(" b");
  120.         }
  121.  
  122.     }
  123.  
  124.  
  125.  
  126.  
  127.     public static void main (String[] args) {
  128.         ChessGame test = new ChessGame();
  129.  
  130.         test.switchPlayer();
  131.     }
  132.  
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement