MahJong
Single player MahJong Solitaire game.
- Supports undo/redo and seeded games using a game number to allow for setting up the board the same across games.
- Tiles removed are displayed on a tile pane on the right side.
- Tournament mode disables undo/redo and discard display.
Under The Hood
Tile
- Tile class stores the information to paint the tiles and acts as a superclass for the different types of tiles (circle, bamboo, etc).
- Tile subclasses have their own constructors/paint method depending on their type.
- Attributes posX, posY, posZ are used for determining where a tile is to be drawn on the board. More on that in MahJongBoard.
- Booleans for tileOnTop, tileOpenRight, tileOpenLeft used to determine if a Tile can be clicked.
- Boolean visible determines if a Tile is to be drawn and selected determines if to draw the selected version of the tile
- Point boardLocation is set after drawing to place the tile back on the board after undoing.
- Getters and setters provided for all private variables
- Matches method provided to determine if a tile matches another, different tile. Matches method in Tile fully functional for all types of Tile thanks to polymorphism.
TileDeck
- TileDeck class creates a deck as an ArrayList of Tile objects.
- Provides method for shuffling with no arguments for shuffling randomly (not actually used in the MahJong file, more on that later)
- Provides method for shuffling based on a number (used for repeat games).
- Provides method for dealing a single card. If the deck isn't empty it removes a card from the deck and returns it
TileRow & TileLayer
- Data structure to set up the layout of the board
-
TileRow
- Contains an ArrayList of Tiles that make up a single row
- Constructor takes arguments (TileDeck deck, int tilesInRow, double xStart, double yVal, int zVal)
- TileDeck deck - Where to pull the tiles from
- int tilesInRow - How many Tiles to put in the row, used for the loop in constructor
- double xStart - Where on the board in a tile grid the first tile belongs
- double yVal - Where on the y-axis on the board the row belongs
- int zVal - What layer the row belongs on
- The loop in the constructor calls setXYZ in tile and sets based on where x is in the loop and the given yVal and zVal
TileLayer
- Contains an ArrayList of layerRows
- Constructor takes no arguments, layerRows is public and added to in MahJongModel (will be update to private later)
MahJongModel
- This is where the magic of the board data structure happens
- Note: Currently the z-indexing across the project is inconsistent and will eventually be refactored.
- Contains a TileLayer attribute for each layer which is then added to an ArrayList of TileLayers
- Also has a TileLayer for leftExtra and rightExtras since it's easier to deal with those tiles separately
- Contains a Tile attribute with getters/setters for storing a tile when it's clicked. This is used for program flow control in MahJong to determine how the program should act when a tile is clicked. Set to null if no tile is selected.
- Contains TileDeck attribute that will be the deck for the game.
- Constructor creates a deck, shuffles it using provided gameNumber, and calls formLayers().
formLayers Method
- Creates ArrayList of tileLayers and stores in tileLayers var.
- The forming of layers is heavily commented, refer to those comments for exact formatting. General format is create tileRows and add to tileLayer then set the right/left side open or set tile on top where applicable
- This function gets the model completely ready to be displayed by MahJongBoard and used by MahJong
MahJong and MahJongBoard
- MahJong is the driver and has nested class MahJongBoard. Nesting it makes it easier to get everything displaying smoothly.
MahJongBoard
- MahJongBoard extends JPanel and implements MouseListener, adding a listener to each of the tiles as they are added to the board.
- Has a MahJongModel attribute which is used for getting the deck and layout
- ImageIcon and Image are used to display the background image, nothing special.
-
- Constructor requires a gameNumber (passed to it in MahJong, more on that later) and creates a new MahJongModel based on the number before calling drawBackground.
- drawBackground method gets image from resources in a try catch and displays it, nothing special
DrawBoard() Method
- DrawBoard() is called by paintComponent every time there is a repaint.
- First checks to see if 144 tiles have been discarded (winning condition)
- Temporary variables for TileLayer, TileRow, and Tile for drawing
- Draws edge tiles then tileLayers iteratively. Again, refer to the heavy commentation for a deeper understanding.
DrawTile(Tile t)
- DrawTile() is called from drawBoard()
- First it checks if the visible variable in Tile is true, if so it removes then adds a mouselistener back (needs to be fixed) then adds it to the board.
DrawDiscards()
- This uses a variable in MahJong, cardPanels, to get the discards that need to be displayed and adds them to cardColumn, another varibale in MahJong.
- First it gets rid of all the tiles in the discard then redraws them based on cardPanels