ideahub智慧屏設計一個國際象棋游戲

            添加時間:2022-02-27 23:35:13

            來源:

            添加時間:2022-02-27 23:35:13

            評分:

            防偽碼:CMSEASYkz0Nk6M5VQose3D920

            問題陳述:問題是使用面向對象的原則設計一個國際象棋游戲。


            問: Adobe、亞馬遜、微軟等。


            解決方案:

            在面試中會問這些類型的問題,以判斷候選人的面向對象設計技能。所以,首先我們應該考慮類。


            主要課程將是:


            點:一個點代表 8×8 網格中的一個塊和一個可選塊。

            棋子:系統的基本組成部分,每個棋子都將被放置在一個地方。Piece 類是一個抽象類。擴展類(Pawn、King、Queen、Rook、Knight、Bishop)實現了抽象操作。

            棋盤:棋盤是一組 8×8 的盒子,其中包含所有活動棋子。

            玩家:玩家類代表玩游戲的參與者之一。

            移動:表示游戲移動,包含起點和終點。Move 類還將跟蹤做出移動的玩家。

            Game:這個類控制游戲的流程。它跟蹤所有游戲動作,當前輪到哪個玩家,以及游戲的最終結果。

            讓我們看看細節。這些代碼是不言自明的。您可以查看不同類的屬性/變量和方法。


            Spot:表示棋盤上的一個單元格:


            public class Spot {

                private Piece piece;

                private int x;

                private int y;

              

                public Spot(int x, int y, Piece piece)

                {

                    this.setPiece(piece);

                    this.setX(x);

                    this.setY(y);

                }

              

                public Piece getPiece()

                {

                    return this.piece;

                }

              

                public void setPiece(Piece p)

                {

                    this.piece = p;

                }

              

                public int getX()

                {

                    return this.x;

                }

              

                public void setX(int x)

                {

                    this.x = x;

                }

              

                public int getY()

                {

                    return this.y;

                }

              

                public void setY(int y)

                {

                    this.y = y;

                }

            }

            Piece:代表所有棋子的通用功能的抽象類:


            public abstract class Piece {

              

                private boolean killed = false;

                private boolean white = false;

              

                public Piece(boolean white)

                {

                    this.setWhite(white);

                }

              

                public boolean isWhite()

                {

                    return this.white;

                }

              

                public void setWhite(boolean white)

                {

                    this.white = white;

                }

              

                public boolean isKilled()

                {

                    return this.killed;

                }

              

                public void setKilled(boolean killed)

                {

                    this.killed = killed;

                }

              

                public abstract boolean canMove(Board board, 

                                             Spot start, Spot end);

            }

            King:將 King 表示為棋子:


            public class King extends Piece {

                private boolean castlingDone = false;

              

                public King(boolean white)

                {

                    super(white);

                }

              

                public boolean isCastlingDone()

                {

                    return this.castlingDone;

                }

              

                public void setCastlingDone(boolean castlingDone)

                {

                    this.castlingDone = castlingDone;

                }

              

                @Override

                public boolean canMove(Board board, Spot start, Spot end)

                {

                    // we can't move the piece to a Spot that 

                    // has a piece of the same color

                    if (end.getPiece().isWhite() == this.isWhite()) {

                        return false;

                    }

              

                    int x = Math.abs(start.getX() - end.getX());

                    int y = Math.abs(start.getY() - end.getY());

                    if (x + y == 1) {

                        // check if this move will not result in the king

                        // being attacked if so return true

                        return true;

                    }

              

                    return this.isValidCastling(board, start, end);

                }

              

                private boolean isValidCastling(Board board, 

                                                 Spot start, Spot end)

                {

              

                    if (this.isCastlingDone()) {

                        return false;

                    }

              

                    // Logic for returning true or false

                }

              

                public boolean isCastlingMove(Spot start, Spot end)

                {

                    // check if the starting and 

                    // ending position are correct

                }

            }

            騎士:將騎士表示為棋子


            public class Knight extends Piece {

                public Knight(boolean white)

                {

                    super(white);

                }

              

                @Override

                public boolean canMove(Board board, Spot start, 

                                                        Spot end)

                {

                    // we can't move the piece to a spot that has

                    // a piece of the same colour

                    if (end.getPiece().isWhite() == this.isWhite()) {

                        return false;

                    }

              

                    int x = Math.abs(start.getX() - end.getX());

                    int y = Math.abs(start.getY() - end.getY());

                    return x * y == 2;

                }

            }

            同樣,我們可以為其他棋子創建類,如Queen、Pawns、Rooks、Bishops等。


            棋盤:代表棋盤:


            public class Board {

                Spot[][] boxes;

              

                public Board()

                {

                    this.resetBoard();

                }

              

                public Spot getBox(int x, int y)

                {

              

                    if (x < 0 || x > 7 || y < 0 || y > 7) {

                        throw new Exception("Index out of bound");

                    }

              

                    return boxes[x][y];

                }

              

                public void resetBoard()

                {

                    // initialize white pieces

                    boxes[0][0] = new Spot(0, 0, new Rook(true));

                    boxes[0][1] = new Spot(0, 1, new Knight(true));

                    boxes[0][2] = new Spot(0, 2, new Bishop(true));

                    //...

                    boxes[1][0] = new Spot(1, 0, new Pawn(true));

                    boxes[1][1] = new Spot(1, 1, new Pawn(true));

                    //...

              

                    // initialize black pieces

                    boxes[7][0] = new Spot(7, 0, new Rook(false));

                    boxes[7][1] = new Spot(7, 1, new Knight(false));

                    boxes[7][2] = new Spot(7, 2, new Bishop(false));

                    //...

                    boxes[6][0] = new Spot(6, 0, new Pawn(false));

                    boxes[6][1] = new Spot(6, 1, new Pawn(false));

                    //...

              

                    // initialize remaining boxes without any piece

                    for (int i = 2; i < 6; i++) {

                        for (int j = 0; j < 8; j++) {

                            boxes[i][j] = new Spot(i, j, null);

                        }

                    }

                }

            }

            播放器:播放器的抽象類,可以是人也可以是計算機。


            public abstract class Player {

                public boolean whiteSide;

                public boolean humanPlayer;

              

                public boolean isWhiteSide()

                {

                    return this.whiteSide;

                }

                public boolean isHumanPlayer()

                {

                    return this.humanPlayer;

                }

            }

              

            public class HumanPlayer extends Player {

              

                public HumanPlayer(boolean whiteSide)

                {

                    this.whiteSide = whiteSide;

                    this.humanPlayer = true;

                }

            }

              

            public class ComputerPlayer extends Player {

              

                public ComputerPlayer(boolean whiteSide)

                {

                    this.whiteSide = whiteSide;

                    this.humanPlayer = false;

                }

            }

            移動:代表國際象棋移動:


            public class Move {

                private Player player;

                private Spot start;

                private Spot end;

                private Piece pieceMoved;

                private Piece pieceKilled;

                private boolean castlingMove = false;

              

                public Move(Player player, Spot start, Spot end)

                {

                    this.player = player;

                    this.start = start;

                    this.end = end;

                    this.pieceMoved = start.getPiece();

                }

              

                public boolean isCastlingMove()

                {

                    return this.castlingMove;

                }

              

                public void setCastlingMove(boolean castlingMove)

                {

                    this.castlingMove = castlingMove;

                }

            }

            public enum GameStatus {

                ACTIVE,

                BLACK_WIN,

                WHITE_WIN,

                FORFEIT,

                STALEMATE,

                RESIGNATION

            }

            游戲:代表一盤棋:


            public class Game {

                private Player[] players;

                private Board board;

                private Player currentTurn;

                private GameStatus status;

                private List<Move> movesPlayed;

              

                private void initialize(Player p1, Player p2)

                {

                    players[0] = p1;

                    players[1] = p2;

              

                    board.resetBoard();

              

                    if (p1.isWhiteSide()) {

                        this.currentTurn = p1;

                    }

                    else {

                        this.currentTurn = p2;

                    }

              

                    movesPlayed.clear();

                }

              

                public boolean isEnd()

                {

                    return this.getStatus() != GameStatus.ACTIVE;

                }

              

                public boolean getStatus()

                {

                    return this.status;

                }

              

                public void setStatus(GameStatus status)

                {

                    this.status = status;

                }

              

                public boolean playerMove(Player player, int startX, 

                                            int startY, int endX, int endY)

                {

                    Spot startBox = board.getBox(startX, startY);

                    Spot endBox = board.getBox(startY, endY);

                    Move move = new Move(player, startBox, endBox);

                    return this.makeMove(move, player);

                }

              

                private boolean makeMove(Move move, Player player)

                {

                    Piece sourcePiece = move.getStart().getPiece();

                    if (sourcePiece == null) {

                        return false;

                    }

              

                    // valid player

                    if (player != currentTurn) {

                        return false;

                    }

              

                    if (sourcePiece.isWhite() != player.isWhiteSide()) {

                        return false;

                    }

              

                    // valid move?

                    if (!sourcePiece.canMove(board, move.getStart(), 

                                                        move.getEnd())) {

                        return false;

                    }

              

                    // kill?

                    Piece destPiece = move.getStart().getPiece();

                    if (destPiece != null) {

                        destPiece.setKilled(true);

                        move.setPieceKilled(destPiece);

                    }

              

                    // castling?

                    if (sourcePiece != null && sourcePiece instanceof King

                        && sourcePiece.isCastlingMove()) {

                        move.setCastlingMove(true);

                    }

              

                    // store the move

                    movesPlayed.add(move);

              

                    // move piece from the stat box to end box

                    move.getEnd().setPiece(move.getStart().getPiece());

                    move.getStart.setPiece(null);

              

                    if (destPiece != null && destPiece instanceof King) {

                        if (player.isWhiteSide()) {

                            this.setStatus(GameStatus.WHITE_WIN);

                        }

                        else {

                            this.setStatus(GameStatus.BLACK_WIN);

                        }

                    }

              

                    // set the current turn to the other player

                    if (this.currentTurn == players[0]) {

                        this.currentTurn = players[1];

                    }

                    else {

                        this.currentTurn = players[0];

                    }

              

                    return true;

                }

            }


            上一篇業務流程智能
            下一篇
            用戶名 Name
            評論 Comment

            聯系我們

            / CONTACT US

            地 址:四川省成都市航空路豐德國際廣場

            郵政編碼:610000

            電 話:18215660330

            傳 真:18215660330

            手機:18215660330

            郵 箱:179001057@qq.com

            投訴郵 箱:179001057@qq.com

            姓名Name
            標題Title
            郵 箱Emali
            聯系電話Tel
            內容Content
            凤凰彩票