Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ChessGame {
- String[][] board = new String[8][8];
- public int z = 0;
- boolean nextPlayer = true;
- public void turnPlayer() {
- nextPlayer = !nextPlayer;
- }
- public void fillBoard() {
- //Pawns
- for (int k = 0; k < 8; k++) {
- board[1][k] = "p";
- board[6][k] = "P";
- }
- // Knights
- board[0][1] = "n";
- board[0][6] = "n";
- board[7][1] = "N";
- board[7][6] = "N";
- //Bishops
- board[0][2] = "b";
- board[0][5] = "b";
- board[7][2] = "B";
- board[7][5] = "B";
- // Rooks
- board[0][0] = "r";
- board[0][7] = "r";
- board[7][0] = "R";
- board[7][7] = "R";
- //Queens
- board[0][3] = "q";
- board[7][3] = "Q";
- //Kings
- board[0][4] = "k";
- board[7][4] = "K";
- // Knights
- board[0][1] = "n";
- board[0][6] = "n";
- board[7][1] = "N";
- board[7][6] = "N";
- // quick check if everything was done correctly
- for (int i = 0; i < 8; i++) {
- if (i != 0) {
- System.out.print(" / ");
- }
- //System.out.print("/");
- for (int j = 0; j < 8; j++) {
- if (board[i][j] == null) {
- board[i][j] = " ";
- } // replace all cells with a " " <- for empty cell
- if (board[i][j].equals(" ")) {
- z++;
- if (z == 8) {
- System.out.print(z);
- z = 0;
- }
- } // if the cell is an empty cell then z++
- else { // otherwise there are two specific opportunites
- if (z == 0) { // if the counter z = 0 then print the board with the pieces
- System.out.print(board[i][j] + " " ); // and set z = 0 again
- z = 0;
- } else {
- // System.out.print(z); // if the counter is not equal 0 then print z and the board with the current cell
- z = 0;
- System.out.print(board[i][j] + " " );
- }
- }
- }
- }
- }
- public void switchPlayer(){
- if (nextPlayer) { // Player w
- fillBoard();
- System.out.print(" w");
- }
- if (!nextPlayer){ // Player b
- fillBoard();
- System.out.print(" b");
- }
- }
- public static void main (String[] args) {
- ChessGame test = new ChessGame();
- test.switchPlayer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement