Skip to content

Latest commit

 

History

History
60 lines (37 loc) · 2.44 KB

Core_API.md

File metadata and controls

60 lines (37 loc) · 2.44 KB

2048Bae Core API

The 2048Bae Core API provides access to the underlying logic and functionality of the 2048 game implemented in Python with Cython. It allows developers to interact with the game grid, perform moves, and check game states.

Core Overview

The core of the 2048Game consists of a Cython class called SquareMatrix, which represents the square matrix used in the game. This class provides methods for initializing the matrix, making moves in different directions, checking for valid moves, calculating the total score, and more.

SquareMatrix Class

The SquareMatrix class represents a square matrix used in the game. It provides the following functionalities:

  • Initialization: Initialize the square matrix with a list of lists representing the initial state of the matrix.

  • Indexing: Retrieve the value at a specific index in the matrix.

  • Valid Move Checks: Check for valid moves in different directions (left, right, up, down).

  • Making Moves: Move the elements in the matrix according to the specified direction.

  • Calculating Score: Calculate the total score of the matrix.

  • Checking Value Existence: Check if a particular value exists in the matrix.

Optional Features

Customizing Move Probabilities

The move method allows developers to specify custom probabilities for generating new elements during a move. This feature enables developers to customize the game mechanics and difficulty level.

Specifying Empty Cell Representation

Developers can specify a value to represent empty cells in the matrix. This feature is useful for variations of the 2048 game that allow for special representations of empty cells.

Usage

To use the 2048Game Core API, developers can integrate the SquareMatrix class into their Python applications. They can create instances of the class, perform moves, check game states, and customize game mechanics as needed.

# Example usage of the SquareMatrix class
from lib.matrix_core import SquareMatrix, Direction

# Initialize the matrix with a list of lists
initial_state = [[0, 2, 0, 2],
                 [0, 4, 0, 4],
                 [0, 0, 8, 8],
                 [0, 0, 0, 0]]
matrix = SquareMatrix(initial_state)

# Make a move in the left direction
matrix.move(Direction.LEFT)

# Check if the game is over
if matrix.valid_check(0):
    print("Game Over!")

# Calculate the total score of the matrix
total_score = matrix.score()
print("Total Score:", total_score)