♕ Phase 0 ‐ Chess Moves - softwareconstruction240/softwareconstruction GitHub Wiki

In this part of the Chess Project, you will implement a basic representation of the game of chess. This includes the setting up of the board and defining how pieces move.

Note

Review the Game of Chess instruction to learn how to set up the board and how each of the pieces move.

Starter Code

To begin building your chess application, you must first follow the instructions in the Getting Started Document.

This provides you with an IntelliJ project that contains the following three modules.

Phase Description
Server A program that handles network requests to create and play games. It stores games persistently in a database and sends out notifications to all the players of a game.
Client The command line program that players use to create and play a game of chess. The client communicates with your server over the network in order to play a game with other clients.
Shared A code library that contains the rules and representation of a chess game that both the client and server use to exercise and validate game play.

Modules

In this phase you will implement the shared code that defines the board, pieces, and the rules of chess. In later phases you will use the code you create to create a fully playable game.

The starter code contains the classes defined by the following diagram. However, instead of a full implementation, the methods all simply throw an exception if invoked. The classes are found in the shared/src/main/java/chess directory.

classDiagram

    class ChessGame {
        getTeamTurn():TeamColor
        validMoves(ChessPosition):Collection~ChessMove~
        makeMove(ChessMove)

        isInCheck(TeamColor):Boolean
        isInCheckmate(TeamColor):Boolean
        isInStalemate(TeamColor):Boolean

        setBoard(ChessBoard)
        getBoard():ChessBoard
    }
    ChessGame --> InvalidMoveException


    class ChessBoard {
        addPiece(ChessPosition, ChessPiece)
        getPiece(ChessPosition):ChessPiece
        resetBoard()
    }
    ChessGame "1" o-- ChessBoard

    class ChessMove {
        getStartPosition():ChessPosition
        getEndPosition():ChessPosition
        getPromotionPiece():PieceType
    }

    class ChessPosition {
        getRow():int
        getColumn():int
    }
    ChessMove o-- "2" ChessPosition

    class ChessPiece {
        getTeamColor():TeamColor
        getPieceType():PieceType
        pieceMoves(ChessBoard, ChessPosition):Collection~ChessMove~
    }

    ChessBoard o-- "*" ChessPiece
Loading

Note

You are not limited to this representation. However, you must not change the existing class names or method signatures since they are used by the pass off tests. You will likely need to add new classes and methods to complete the work required by this phase.

Chess Classes

ChessGame

This class serves as the top-level management of the chess game. It is responsible for executing moves as well as recording the game status.

Important

Although the ChessGame class is presented now, it will not be used until Phase 1. ChessGame. Related tests will be added to the working directory later.

ChessBoard

This class stores all the uncaptured pieces in a Game. It needs to support adding and removing pieces for testing, as well as a resetBoard() method that sets the standard Chess starting configuration.

ChessPiece

This class represents a single chess piece, with its corresponding type and team color. It contains the PieceType enumeration that defines the different types of pieces.

public enum PieceType {
    KING,
    QUEEN,
    BISHOP,
    KNIGHT,
    ROOK,
    PAWN
}

ChessPiece implements rules that define how a piece moves independent of other chess rules such as check, stalemate, or checkmate.

Key Methods

  • pieceMoves: Called on a particular piece and being provided its position and the board as context, this method returns all moves the piece can legally make. ChessPiece.validMoves accounts for the type of piece and the locations of enemy and friendly pieces blocking movement paths. As the precursor to ChessGame.validMoves, this method provides most of the behavior of the chess moves, except it does not honor whose turn it is or check if the king is being attacked.

ChessMove

This class represents a possible move a chess piece could make. It contains the starting and ending positions. It also contains a field for the type of piece a pawn is being promoted to. If the move would not result in a pawn being promoted, the promotion type field should be null.

ChessPosition

This represents a location on the chessboard. This should be represented as a row number from 1-8, and a column number from 1-8. For example, (1,1) corresponds to the bottom left corner (which in chess notation is denoted a1). (8,8) corresponds to the top right corner (h8 in chess notation).

Note

If you implement your Chess Board with an array or a similar data structure accessed with an index, remember that those indexes start with 0, where Chess Positions should go from 1 to 8. Make sure you account for the difference between these two numbers and how you translate between them, such as using a Chess Position to get a Piece from the Board.

Object Overrides

In order for the tests to pass, you are required to override the equals() and hashCode() methods in your class implementations as necessary. This includes the ChessPosition, ChessPiece, ChessMove, and ChessBoard classes in particular. To do this automatically in IntelliJ, right click on the class code and select Code > Generate... > equals() and hashCode(). It is a good idea to generate these methods fairly early. However, IntelliJ will not be able to generate them properly until you have added the required fields to the class.

In most cases, the default methods provided by IntelliJ will suffice. However, there are cases when the generated code will not completely work. You should fully understand all code in your project, even code that was generated for you. Take the time to carefully review and debug what the generated code does. Add a breakpoint, inspect the variables, and step through each line.

The tests and autograder rely on these methods in order to compare your objects. If you can't pass any tests even though the output seems the same check to make sure that these methods are created and working properly.

To understand why we need to override the equals() and hashCode() methods, see the instruction page on Java Object Class.

Tip

Debugging is often much easier if you also override the toString() method and return a concise representation of the object. This is not required, but highly recommended. This can be generated in the same way that the equals() and hashcode() were.

Testing

The test cases found in src/test/java/passoff/chess/piece contain a collection of tests that assert the correct movement of individual pieces.

To run the tests, you can click the play icon next to an individual test, or you can right click on a package or class and select Debug or Debug Tests in …

Debug Test

Recommended Development Process

For this project, you are free to implement the classes described above in whatever order you choose. However, it is suggested that you follow the principles of test driven development. Each test is designed for a specific aspect of the overall project. You should look at what each test needs to function, and build piece by piece to the functionality of the project. A good process for this is to:

  • Identify a test that you feel is the simplest or next in the process.
  • Add the code for the functionality
  • Run the test, debug, and continue implementing until the test passes.
  • Ensure you did not break any tests you previously passed.
  • Commit your changes to GitHub. Smaller changes will be much easier to follow and verify for this project.
  • Repeat the above process for each of the tests until they all pass.

If you are confused at what a test is doing reread the specs to understand what functionality it is looking for, and you can ask for clarification on Slack or to a TA.

Code Quality

You want to write quality code that promotes consistency and readability for all team members. Code quality is discussed in a future instruction topic, and you will be graded on quality starting with phase 3. However, you can use the auto grader at any time to check your chess repositories quality. The rubric used to evaluate code quality is found here: Rubric.

Code Quality

☑ Deliverable

Pass Off, Submission, and Grading

All of the tests in your project must succeed in order to complete this phase.

To pass off this assignment use the course auto-grading tool. If your code passes then your grade will automatically be entered in Canvas.

Grading Rubric

Important

You are required to commit to GitHub with every minor milestone. For example, after you successfully pass a test. This should result in a commit history that clearly details your work on this phase. If your Git history does not demonstrate your efforts then your submission may be rejected.

Category Criteria Points
GitHub History At least 8 GitHub commits evenly spread over the assignment period that demonstrate proof of work Prerequisite
Functionality All pass off test cases succeed 125
Total 125

Videos (21:22)

Note

The "Phase 0 Overview" video contains some outdated setup information. Between timestamps 0:51 and 2:40, an older and more complicated way of setting up the repo is demonstrated. This section of the video can be skipped.

⚠️ **GitHub.com Fallback** ⚠️