/media/sda-magnetic/david/Extern-Magnetic-2022-06-29/Extern01/Dokumente-2020-11-16/disk10-ab-2020-01-10/02-debian-pc2-work/informatik/java-real/chess/chessv5/Chess.java


public class Chess {
    static final int xmin = 0;
    static final int ymin = 0;
    static final int xmax = 7;
    static final int ymax = 7;
    static final int xn = 8;
    static final int yn = 8;
    
    static final int north = 0;
    static final int east = 1;
    static final int south = 2;
    static final int west = 3;
    
    static final boolean lives = true;
    static final boolean death = false;
    
    static final boolean black = true;
    static final boolean white = false;
    
    Chess() {
        Board brd = new Board();
        brd.printBoard();
    
        
    }    
        
    class Board {
        Figure [][] board = new Figure [yn][xn];
        Gambler wht;
        Gambler blk;
        
        Board () {
            int i, j;
            
            for (i = ymin;  i <= ymax;  i++)
                for (j = xmin;  j <= xmax;  j++)
                    new Empty (i, j);
            
            this.wht = new Gambler (white);
            this.blk = new Gambler (black);
        
        }
        
        void printBoard () {
            int i, j;
            
            for (i = ymin;  i <= ymax;  i++) {
                for (j = xmin;  j <= xmax;  j++)
                    System.out.print(board[i][j].getfullname() + " ");
                System.out.println ();
            }
        }

        class Gambler {
            boolean color;
            Gambler (boolean color) {
                this.color = color;
                
                if (color == white) {
                    Rook rook1 =  new Rook (0, 0, white);
                    Rook rook2 = new Rook (0, 7, white);
                    Knight knight1 = new Knight (0, 1, white);
                    Knight knight2 = new Knight (0, 6, white);
                    Bishop bishop1 = new Bishop (0, 2, white);
                    Bishop bishop2 = new Bishop (0, 5, white);
                    King king = new King (0, 3, white);;
                    Queen queen = new Queen (0, 4, white);
                    Pawn pawn1 = new Pawn (1, 0, white);
                    Pawn pawn2 = new Pawn (1, 1, white);
                    Pawn pawn3 = new Pawn (1, 2, white);
                    Pawn pawn4 = new Pawn (1, 3, white);
                    Pawn pawn5 = new Pawn (1, 4, white);
                    Pawn pawn6 = new Pawn (1, 5, white);
                    Pawn pawn7 = new Pawn (1, 6, white);
                    Pawn pawn8 = new Pawn (1, 7, white);
                    
                    //Bishop test = new Bishop (2, 2, white);
                    //System.out.println (test.make_a_move (south, east, 4, 4)); 
                }
                else if (color == black) {
                    Rook rook1 =  new Rook (7, 0, black);
                    Rook rook2 = new Rook (7, 7, black);
                    Knight knight1 = new Knight (7, 1, black);
                    Knight knight2 = new Knight (7, 6, black);
                    Bishop bishop1 = new Bishop (7, 2, black);
                    Bishop bishop2 = new Bishop (7, 5, black);
                    King king = new King (7, 3, black);;
                    Queen queen = new Queen (7, 4, black);
                    Pawn pawn1 = new Pawn (6, 0, black);
                    Pawn pawn2 = new Pawn (6, 1, black);
                    Pawn pawn3 = new Pawn (6, 2, black);
                    Pawn pawn4 = new Pawn (6, 3, black);
                    Pawn pawn5 = new Pawn (6, 4, black);
                    Pawn pawn6 = new Pawn (6, 5, black);
                    Pawn pawn7 = new Pawn (6, 6, black);
                    Pawn pawn8 = new Pawn (6, 7, black);                
                    Bishop test = new Bishop (2, 2, white);
                    Rook test2 = new Rook (4,4, white);
                    //Bishop test2 = new Bishop (4, 4, white);
/*                    System.out.println (board[2][2].make_a_move (south, west, 2, 2));
                    System.out.println (board[4][4].make_a_move (south, east, 1, 0)); 
                    System.out.println (board[6][7].make_a_move (north, east, 1, 0));
                    System.out.println (board[5][7].make_a_move (north, east, 1, 0));
                    System.out.println (board[4][7].make_a_move (north, east, 1, 0));
                    System.out.println (board[3][7].make_a_move (north, west, 1, 0));
                    System.out.println (board[2][7].make_a_move (north, west, 1, 1));*/

                }
            }
        }
        
        class Figure {
            int x;
            int y;
            boolean alive;
            boolean color;
            boolean empty;
            String name;
        
            int getx() {return x;}
            int gety() {return y;}
            boolean isalive() {return alive;}
            void setdeath() {this.alive = death;}
            void setalive() {this.alive = lives;}
            void setxy (int y, int x) {this.y = y; this.x = x;board[y][x] = this;}
            boolean isset () {return !empty;}
            void setempty () {this.empty = true;}
            void setnotempty () {this.empty = false;}
            int succx (int direastwest) {
                if (direastwest == west) 
                    return -1;
                return +1;
            }
            int succy (int dirnorthsouth) {
                if (dirnorthsouth == north)
                    return -1;
                return +1;
            }
            String getname () {return name;}
            String getfullname () {
                String cl;
                if (empty == true)
                    cl = "";
                else if (color == white)
                    cl = "white";
                else
                    cl = "black";
                return cl + " " + name;
            }
            void setname (String name) {this.name = name;}
            boolean getcolor () {return color;}
            void setcolor (boolean color) {this.color = color;}
            
            boolean make_a_move_bishop (int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                boolean ok = true;
                int dirx = 1;
                int diry = 1;
                int newx;
                int newy;   
                
                int startx;
                int starty;
                int endx;
                int endy;
                int i, j;
                
                int oldx = getx();
                int oldy = gety();
                
                if (inorthsouth == ieastwest) {
                    dirx = succx (direastwest);
                    diry = succy (dirnorthsouth);
                                        
                    newx = getx() + (ieastwest * dirx);
                    newy = gety() + (inorthsouth * diry);
                    
                    if ((newx < xmin) || (newx > xmax) || (newy < ymin) || (newy > ymax))
                        ok = false;
                    if (ok) {
                        if (newx > getx()) {
                            startx = newx;
                            endx = getx();
                        }
                        else {
                            startx = getx();
                            endx = newx;
                        }
                        if (newy > gety ()) {
                            starty = newy;
                            endy = gety();
                        }
                        else {
                            starty = gety();
                            endy = newy;
                        }
                            
                        for (i = starty-1, j = startx-1;  (i > endy) && (j > endx);  i--, j--)
                            if (board[i][j].isset())
                                ok = false;
                        if (board[newy][newx].isset()) {
                            if (board[newy][newx].getcolor() == this.getcolor())
                                ok = false;
                        }
                        if (ok) {
                            setxy (newy,newx);
                            new Empty (oldy, oldx);
                        }
                    }
                }
                else
                    ok = false;
                return ok;
            }
            
            
            boolean make_a_move_rook (int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                boolean ok = true;
                int dirx = 1;
                int diry = 1;
                int newx;
                int newy;   
                
                int startx;
                int starty;
                int endx;
                int endy;
                int i, j;
                
                int oldx = getx();
                int oldy = gety();
                
                
                if ((inorthsouth == 0) && (ieastwest != 0)) {
                    
                    dirx = succx (direastwest);
                                        
                    newx = getx() + (ieastwest * dirx);
                    newy = gety();
                    
                    if ((newx < xmin) || (newx > xmax) || (newy < ymin) || (newy > ymax))
                        ok = false;
                    if (ok) {
                        if (newx > getx()) {
                            startx = newx;
                            endx = getx();
                        }
                        else {
                            startx = getx();
                            endx = newx;
                        }
                            
                        for (i = gety(), j = startx-1;  j > endx;  j--)
                            if (board[i][j].isset())
                                ok = false;
                        if (board[newy][newx].isset()) {
                            if (board[newy][newx].getcolor() == this.getcolor())
                                ok = false;
                        }
                        if (ok) {
                            setxy (newy,newx);
                            new Empty (oldy, oldx);
                        }
                    }
                }
                else if ((inorthsouth != 0) && (ieastwest == 0)) {
                    diry = succy (dirnorthsouth);
                                        
                    newx = getx();
                    newy = gety() + (inorthsouth * diry);
                    
                    
                    if ((newx < xmin) || (newx > xmax) || (newy < ymin) || (newy > ymax))
                        ok = false;
                    if (ok) {
                        if (newy > gety()) {
                            starty = newy;
                            endy = gety();
                        }
                        else {
                            starty = gety();
                            endy = newy;
                        }
                            
                        for (i = starty - 1, j = getx();  i > endy;  i--)
                            if (board[i][j].isset())
                                ok = false;
                        if (board[newy][newx].isset()) {
                            if (board[newy][newx].getcolor() == this.getcolor())
                                ok = false;
                        }
                        if (ok) {
                            setxy (newy,newx);
                            new Empty (oldy, oldx);
                        }
                    }
                }
                
                
                
                else
                    ok = false;
                return ok;
            }
            
            boolean make_a_move_queen( int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                if (make_a_move_bishop(dirnorthsouth, direastwest, inorthsouth, ieastwest))
                    return true;
                else if (make_a_move_rook(dirnorthsouth, direastwest, inorthsouth, ieastwest))
                    return true;
                return false;
            }

            boolean make_a_move_king(int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                if ((inorthsouth > 1) || (ieastwest > 1))
                    return false;
                else if (make_a_move_bishop(dirnorthsouth, direastwest, inorthsouth, ieastwest))
                    return true;
                else if (make_a_move_rook(dirnorthsouth, direastwest, inorthsouth, ieastwest))
                    return true;
                return false;
            }
            
            boolean make_a_move_pawn( int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                int dirx, diry, newx, newy;
                int oldy = gety();
                int oldx = getx();
            
                dirx = succx (direastwest);
                diry = succy (dirnorthsouth);
                                        
                newx = getx() + (ieastwest * dirx);
                newy = gety() + (inorthsouth * diry);
            
                if ((newx < xmin) || (newx > xmax) || (newy < ymin) || (newy > ymax))
                    return false;
                if (this.color == white) {
                    if (dirnorthsouth != south)
                        return false;
                    if ((inorthsouth > 1) || (ieastwest) > 1) 
                        return false;
                    else if (ieastwest == 0) {
                        if (!board[gety()+1][getx()].isset()) {
                            setxy(gety()+1, getx());
                            new Empty (oldy, oldx);
                            return true;
                        }
                    }
                    else if (ieastwest == 1) {
                        if (direastwest == west)
                            if (board[gety()+1][getx()-1].isset() && board[gety()+1][getx()-1].getcolor() == black) {
                                setxy (gety()+1, getx()-1);
                                new Empty (oldy, oldx);
                                return true;
                            }
                        else if (direastwest == east)
                            if (board[gety()+1][getx()+1].isset() && board[gety()+1][getx()+1].getcolor() == black) {
                                setxy (gety()+1, getx()+1);
                                new Empty (oldy, oldx);
                                return true;
                            }
                    }
                }
                else if (this.color == black) {
                    if (dirnorthsouth != north)
                        return false;
                    if ((inorthsouth > 1) || (ieastwest) > 1)
                        return false;
                    else if (ieastwest == 0) {
                        if (!board[gety()-1][gety()].isset()) {
                            setxy (gety()-1, getx());
                            new Empty (oldy, oldx);
                            return true;
                        }
                    }
                    else if (ieastwest == 1) {
                        if (direastwest == west) {
                            if (board[gety()-1][getx()-1].isset() && board[gety()-1][getx()-1].getcolor() == white) {
                                setxy (gety()-1,getx()-1);
                                new Empty (oldy, oldx);
                                return true;
                            }
                        }
                        else if (direastwest == east)
                            if (board[gety()-1][getx()+1].isset() && board[gety()-1][getx()+1].getcolor() == white) {
                                setxy (gety()-1,getx()+1);
                                new Empty (oldy, oldx);
                                return true;
                            }
                    }
                }
                return false;
            }
            
             boolean make_a_move (int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                boolean retval = false;
                if (this instanceof Rook)
                    retval = make_a_move_rook (dirnorthsouth, direastwest, inorthsouth, ieastwest);
                if (this instanceof Bishop)
                    retval = make_a_move_bishop (dirnorthsouth, direastwest, inorthsouth, ieastwest);
                if (this instanceof Queen)
                    retval = make_a_move_queen (dirnorthsouth, direastwest, inorthsouth, ieastwest);
                if (this instanceof King)
                    retval = make_a_move_queen (dirnorthsouth, direastwest, inorthsouth, ieastwest);
                if (this instanceof Pawn)
                    retval = make_a_move_pawn (dirnorthsouth, direastwest, inorthsouth, ieastwest);

                return retval;
             }

            
        }
    
        class Pawn extends Figure {
            Pawn (int y, int x, boolean color) {
                setalive();
                setname ("pawn");
                setcolor (color);
                setnotempty();
                setxy (y, x);
            }    
        }
        class Rook extends Figure {
            Rook(int y, int x, boolean color) {
                setalive();
                setname ("rook");
                setcolor (color);
                setnotempty();
                setxy (y, x);
            }
            boolean make_a_move(int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                    return make_a_move_rook (dirnorthsouth, direastwest, inorthsouth, ieastwest);
            }
        }
        class Knight extends Figure {
            Knight (int y,  int x, boolean color) {
                setalive();
                setname ("knight");
                setcolor (color);
                setnotempty();
                setxy (y, x);
            }    
        }
        class Bishop extends Figure {
            Bishop (int y,  int x, boolean color) {
                setalive();
                setname ("bishop");
                setcolor (color);
                setnotempty();
                setxy (y, x);
            }
            boolean make_a_move(int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                return make_a_move_bishop (dirnorthsouth, direastwest, inorthsouth, ieastwest);
            }

        }
        class King extends Figure {
            King (int y,  int x, boolean color) {
                setalive();
                setname ("king");
                setcolor (color);
                setnotempty();
                setxy (y, x);
            }
            boolean make_a_move(int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                    return make_a_move_king (dirnorthsouth, direastwest, inorthsouth, ieastwest);
            }

        }
        class Queen extends Figure {
            Queen (int y,  int x, boolean color) {
                setalive();
                setname ("queen");
                setcolor (color);
                setnotempty();
                setxy (y, x);
            }
            boolean make_a_move(int dirnorthsouth, int direastwest, int inorthsouth, int ieastwest) {
                    return make_a_move_queen (dirnorthsouth, direastwest, inorthsouth, ieastwest);
            }
        }
        class Empty extends Figure {
            Empty (int y, int x) {
                setalive();
                setname ("empty");
                setxy (y, x);
                setempty();
            }
        }
    }
}