connectide


Nameconnectide JSON
Version 0.0.1 PyPI version JSON
download
home_pageNone
SummaryBasic Connect-4 Module
upload_time2024-07-07 08:03:32
maintainerNone
docs_urlNone
authorDragjon
requires_pythonNone
licenseNone
keywords python game board boardgame
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Connectide Game Documentation

## Overview

The `connectide` module provides a class-based implementation of the Connect-4 game. This module includes various functionalities to represent the game board, manage game state, and perform operations like move generation, checking for wins, adding and undoing moves, and running performance tests.

## Constants

- `noPce = 0`: Represents an empty spot on the board.
- `red = 1`: Represents a red piece on the board.
- `yellow = 2`: Represents a yellow piece on the board.

## Board Class

### Initialization

```python
class Board:
    def __init__(self, board=None, turn=red):
        if board is None:
            self.board = [[noPce for _ in range(7)] for _ in range(6)]
        else:
            self.board = board
        self.turn = turn
```

- `board`: A 6x7 matrix representing the game board. Defaults to an empty board if not provided.
- `turn`: Indicates the current player's turn. Defaults to `red`.

### Methods

#### `isConnect4`

```python
def isConnect4(self, row: int, col: int) -> bool:
    board = self.board
    pce = board[row][col]

    if pce is not noPce:
        if row >= 3 and board[row - 1][col] == pce and board[row - 2][col] == pce and board[row - 3][col] == pce:
            return True
        if row <= 2 and board[row + 1][col] == pce and board[row + 2][col] == pce and board[row + 3][col] == pce:
            return True
        if col <= 3 and board[row][col + 1] == pce and board[row][col + 2] == pce and board[row][col + 3] == pce:
            return True
        if col >= 3 and board[row][col - 1] == pce and board[row][col - 2] == pce and board[row][col - 3] == pce:
            return True
        if row >= 3 and col <= 3 and board[row - 1][col + 1] == pce and board[row - 2][col + 2] == pce and board[row - 3][col + 3] == pce:
            return True
        if row <= 2 and col <= 3 and board[row + 1][col + 1] == pce and board[row + 2][col + 2] == pce and board[row + 3][col + 3] == pce:
            return True
        if row >= 3 and col >= 3 and board[row - 1][col - 1] == pce and board[row - 2][col - 2] == pce and board[row - 3][col - 3] == pce:
            return True
        if row <= 2 and col >= 3 and board[row + 1][col - 1] == pce and board[row + 2][col - 2] == pce and board[row + 3][col - 3] == pce:
            return True

    return False
```

- `row`: The row of the square.
- `col`: The column of the square.
- Returns `True` if the square is part of a 4-in-a-row, otherwise `False`.

#### `isCheckmate`

```python
def isCheckmate(self) -> bool:
    for row in range(6):
        for col in range(7):
            if self.isConnect4(row, col):
                return True
    return False
```

- Returns `True` if a 4-in-a-row is present on the board, otherwise `False`.

#### `pseudoLegalMoveGen`

```python
def pseudoLegalMoveGen(self) -> list[int]:
    moves = []
    board = self.board
    for col in range(7):
        if board[0][col] == noPce:
            moves.append(col)
    return moves
```

- Returns a list of pseudo-legal moves (legal columns), which includes moves even after a board is in checkmate.

#### `moveGen`

```python
def moveGen(self) -> list[int]:
    moves = []
    if not self.isCheckmate():
        board = self.board
        for col in range(7):
            if board[0][col] == noPce:
                moves.append(col)
    return moves
```

- Returns a list of legal moves (legal columns).

#### `addPiece`

```python
def addPiece(self, col: int):
    pce = self.turn
    for row in range(6):
        if self.board[row][col] != noPce:
            self.board[row - 1][col] = pce
            self.turn = 3 - pce
            break
        if row == 5 and self.board[row][col] == noPce:
            self.board[row][col] = pce
            self.turn = 3 - pce
            break
```

- `col`: The column to add a piece.

#### `undoMove`

```python
def undoMove(self, col: int):
    for row in range(6):
        if self.board[row][col] != noPce:
            self.board[row][col] = noPce
            self.turn = 3 - self.turn
            break
```

- `col`: The column from which to undo a move.

#### `getNumMoves`

```python
def getNumMoves(self) -> int:
    moves = 0
    for i in range(6):
        for j in range(7):
            if self.board[i][j] != noPce:
                moves += 1
    return moves
```

- Returns the number of pieces on the board.

#### `show`

```python
def show(self, debug: bool = False):
    board = self.board
    for i in range(6):
        for j in range(7):
            pce = board[i][j]
            pceChar = "-" if pce == noPce else "R" if pce == red else "Y"
            print(pceChar, end=" ")
        if i != 5:
            print()
    print("\n-------------")
    print("1 2 3 4 5 6 7 ")
    if debug:
        print(f"Turn: {'red' if self.turn == red else 'yellow'}")
        print(f"Moves Played: {self.getNumMoves()}")
        print(f"Checkmate Status: {'True' if self.isCheckmate() else 'False'}")
    print()
```

- `debug` (optional): Whether to print additional debug information. Defaults to `False`.

#### `chash`

```python
def chash(self) -> str:
    fen = ""
    for i in range(len(self.board)):
        for j in range(len(self.board[0])):
            fen += str(self.board[i][j])
    return fen + str(self.turn)
```

- Returns a simple hash of the board position.

#### `flatten`

```python
def flatten(self) -> list[int]:
    flat = []
    for i in range(len(self.board)):
        for j in range(len(self.board[0])):
            flat.append(self.board[i][j])
    return flat
```

- Returns the flattened board array as a 1D list.

#### `parse`

```python
def parse(self, string: str):
    i = 0
    j = 0
    count = 0

    for char in string:
        count += 1
        if count == 43:
            self.turn = int(char)
            break
        if i == 7:
            i = 0
            j += 1

        self.board[j][i] = int(char)
        i += 1
```

- `string`: The string board representation created by `chash`.

#### `perfD`

```python
def perfD(self, depth: int) -> int:
    if depth == 0:
        return 1
    nodes = 0
    legals = self.moveGen()
    for col in legals:
        self.addPiece(col)
        nodes += self.perfD(depth - 1)
        self.undoMove(col)
    return nodes
```

- `depth`: The depth to search to.
- Returns the number of nodes at a specified depth.

#### `perfT`

```python
def perfT(self, maxDepth: int):
    startTime = time.time()
    for depth in range(1, maxDepth + 1):
        nodes = self.perfD(depth)
        elapsed = time.time() - startTime
        print(
            f"info string perft depth {depth} time {int(elapsed*1000)} nodes {nodes} nps {int(nodes / (elapsed + 0.000000001))}"
        )
```

- `maxDepth`: The maximum depth to run for this test.
- Initiates a performance test.

### Example Usage

```python
board = Board()
board.show()
board.parse("0000000000000000000000000000000000000000012")
board.perfT(7)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "connectide",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "python, game, board, boardgame",
    "author": "Dragjon",
    "author_email": "<magiciandragjon@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/0f/fb/89d64975b9d291375229daaa16755820098c5aa7c9da7db2f0ba2c0bdff2/connectide-0.0.1.tar.gz",
    "platform": null,
    "description": "# Connectide Game Documentation\r\n\r\n## Overview\r\n\r\nThe `connectide` module provides a class-based implementation of the Connect-4 game. This module includes various functionalities to represent the game board, manage game state, and perform operations like move generation, checking for wins, adding and undoing moves, and running performance tests.\r\n\r\n## Constants\r\n\r\n- `noPce = 0`: Represents an empty spot on the board.\r\n- `red = 1`: Represents a red piece on the board.\r\n- `yellow = 2`: Represents a yellow piece on the board.\r\n\r\n## Board Class\r\n\r\n### Initialization\r\n\r\n```python\r\nclass Board:\r\n    def __init__(self, board=None, turn=red):\r\n        if board is None:\r\n            self.board = [[noPce for _ in range(7)] for _ in range(6)]\r\n        else:\r\n            self.board = board\r\n        self.turn = turn\r\n```\r\n\r\n- `board`: A 6x7 matrix representing the game board. Defaults to an empty board if not provided.\r\n- `turn`: Indicates the current player's turn. Defaults to `red`.\r\n\r\n### Methods\r\n\r\n#### `isConnect4`\r\n\r\n```python\r\ndef isConnect4(self, row: int, col: int) -> bool:\r\n    board = self.board\r\n    pce = board[row][col]\r\n\r\n    if pce is not noPce:\r\n        if row >= 3 and board[row - 1][col] == pce and board[row - 2][col] == pce and board[row - 3][col] == pce:\r\n            return True\r\n        if row <= 2 and board[row + 1][col] == pce and board[row + 2][col] == pce and board[row + 3][col] == pce:\r\n            return True\r\n        if col <= 3 and board[row][col + 1] == pce and board[row][col + 2] == pce and board[row][col + 3] == pce:\r\n            return True\r\n        if col >= 3 and board[row][col - 1] == pce and board[row][col - 2] == pce and board[row][col - 3] == pce:\r\n            return True\r\n        if row >= 3 and col <= 3 and board[row - 1][col + 1] == pce and board[row - 2][col + 2] == pce and board[row - 3][col + 3] == pce:\r\n            return True\r\n        if row <= 2 and col <= 3 and board[row + 1][col + 1] == pce and board[row + 2][col + 2] == pce and board[row + 3][col + 3] == pce:\r\n            return True\r\n        if row >= 3 and col >= 3 and board[row - 1][col - 1] == pce and board[row - 2][col - 2] == pce and board[row - 3][col - 3] == pce:\r\n            return True\r\n        if row <= 2 and col >= 3 and board[row + 1][col - 1] == pce and board[row + 2][col - 2] == pce and board[row + 3][col - 3] == pce:\r\n            return True\r\n\r\n    return False\r\n```\r\n\r\n- `row`: The row of the square.\r\n- `col`: The column of the square.\r\n- Returns `True` if the square is part of a 4-in-a-row, otherwise `False`.\r\n\r\n#### `isCheckmate`\r\n\r\n```python\r\ndef isCheckmate(self) -> bool:\r\n    for row in range(6):\r\n        for col in range(7):\r\n            if self.isConnect4(row, col):\r\n                return True\r\n    return False\r\n```\r\n\r\n- Returns `True` if a 4-in-a-row is present on the board, otherwise `False`.\r\n\r\n#### `pseudoLegalMoveGen`\r\n\r\n```python\r\ndef pseudoLegalMoveGen(self) -> list[int]:\r\n    moves = []\r\n    board = self.board\r\n    for col in range(7):\r\n        if board[0][col] == noPce:\r\n            moves.append(col)\r\n    return moves\r\n```\r\n\r\n- Returns a list of pseudo-legal moves (legal columns), which includes moves even after a board is in checkmate.\r\n\r\n#### `moveGen`\r\n\r\n```python\r\ndef moveGen(self) -> list[int]:\r\n    moves = []\r\n    if not self.isCheckmate():\r\n        board = self.board\r\n        for col in range(7):\r\n            if board[0][col] == noPce:\r\n                moves.append(col)\r\n    return moves\r\n```\r\n\r\n- Returns a list of legal moves (legal columns).\r\n\r\n#### `addPiece`\r\n\r\n```python\r\ndef addPiece(self, col: int):\r\n    pce = self.turn\r\n    for row in range(6):\r\n        if self.board[row][col] != noPce:\r\n            self.board[row - 1][col] = pce\r\n            self.turn = 3 - pce\r\n            break\r\n        if row == 5 and self.board[row][col] == noPce:\r\n            self.board[row][col] = pce\r\n            self.turn = 3 - pce\r\n            break\r\n```\r\n\r\n- `col`: The column to add a piece.\r\n\r\n#### `undoMove`\r\n\r\n```python\r\ndef undoMove(self, col: int):\r\n    for row in range(6):\r\n        if self.board[row][col] != noPce:\r\n            self.board[row][col] = noPce\r\n            self.turn = 3 - self.turn\r\n            break\r\n```\r\n\r\n- `col`: The column from which to undo a move.\r\n\r\n#### `getNumMoves`\r\n\r\n```python\r\ndef getNumMoves(self) -> int:\r\n    moves = 0\r\n    for i in range(6):\r\n        for j in range(7):\r\n            if self.board[i][j] != noPce:\r\n                moves += 1\r\n    return moves\r\n```\r\n\r\n- Returns the number of pieces on the board.\r\n\r\n#### `show`\r\n\r\n```python\r\ndef show(self, debug: bool = False):\r\n    board = self.board\r\n    for i in range(6):\r\n        for j in range(7):\r\n            pce = board[i][j]\r\n            pceChar = \"-\" if pce == noPce else \"R\" if pce == red else \"Y\"\r\n            print(pceChar, end=\" \")\r\n        if i != 5:\r\n            print()\r\n    print(\"\\n-------------\")\r\n    print(\"1 2 3 4 5 6 7 \")\r\n    if debug:\r\n        print(f\"Turn: {'red' if self.turn == red else 'yellow'}\")\r\n        print(f\"Moves Played: {self.getNumMoves()}\")\r\n        print(f\"Checkmate Status: {'True' if self.isCheckmate() else 'False'}\")\r\n    print()\r\n```\r\n\r\n- `debug` (optional): Whether to print additional debug information. Defaults to `False`.\r\n\r\n#### `chash`\r\n\r\n```python\r\ndef chash(self) -> str:\r\n    fen = \"\"\r\n    for i in range(len(self.board)):\r\n        for j in range(len(self.board[0])):\r\n            fen += str(self.board[i][j])\r\n    return fen + str(self.turn)\r\n```\r\n\r\n- Returns a simple hash of the board position.\r\n\r\n#### `flatten`\r\n\r\n```python\r\ndef flatten(self) -> list[int]:\r\n    flat = []\r\n    for i in range(len(self.board)):\r\n        for j in range(len(self.board[0])):\r\n            flat.append(self.board[i][j])\r\n    return flat\r\n```\r\n\r\n- Returns the flattened board array as a 1D list.\r\n\r\n#### `parse`\r\n\r\n```python\r\ndef parse(self, string: str):\r\n    i = 0\r\n    j = 0\r\n    count = 0\r\n\r\n    for char in string:\r\n        count += 1\r\n        if count == 43:\r\n            self.turn = int(char)\r\n            break\r\n        if i == 7:\r\n            i = 0\r\n            j += 1\r\n\r\n        self.board[j][i] = int(char)\r\n        i += 1\r\n```\r\n\r\n- `string`: The string board representation created by `chash`.\r\n\r\n#### `perfD`\r\n\r\n```python\r\ndef perfD(self, depth: int) -> int:\r\n    if depth == 0:\r\n        return 1\r\n    nodes = 0\r\n    legals = self.moveGen()\r\n    for col in legals:\r\n        self.addPiece(col)\r\n        nodes += self.perfD(depth - 1)\r\n        self.undoMove(col)\r\n    return nodes\r\n```\r\n\r\n- `depth`: The depth to search to.\r\n- Returns the number of nodes at a specified depth.\r\n\r\n#### `perfT`\r\n\r\n```python\r\ndef perfT(self, maxDepth: int):\r\n    startTime = time.time()\r\n    for depth in range(1, maxDepth + 1):\r\n        nodes = self.perfD(depth)\r\n        elapsed = time.time() - startTime\r\n        print(\r\n            f\"info string perft depth {depth} time {int(elapsed*1000)} nodes {nodes} nps {int(nodes / (elapsed + 0.000000001))}\"\r\n        )\r\n```\r\n\r\n- `maxDepth`: The maximum depth to run for this test.\r\n- Initiates a performance test.\r\n\r\n### Example Usage\r\n\r\n```python\r\nboard = Board()\r\nboard.show()\r\nboard.parse(\"0000000000000000000000000000000000000000012\")\r\nboard.perfT(7)\r\n```\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Basic Connect-4 Module",
    "version": "0.0.1",
    "project_urls": null,
    "split_keywords": [
        "python",
        " game",
        " board",
        " boardgame"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8a769458425f280620f64aba7e131b338cb43e0500dc5d03c2fa7378270d59fd",
                "md5": "52e50beef111ebce335e3a890d82a7c8",
                "sha256": "9726816e615369932ca059d637ccd10f5720cc19da3ef492d5a85eac1bf4cd2e"
            },
            "downloads": -1,
            "filename": "connectide-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "52e50beef111ebce335e3a890d82a7c8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6056,
            "upload_time": "2024-07-07T08:03:30",
            "upload_time_iso_8601": "2024-07-07T08:03:30.747565Z",
            "url": "https://files.pythonhosted.org/packages/8a/76/9458425f280620f64aba7e131b338cb43e0500dc5d03c2fa7378270d59fd/connectide-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0ffb89d64975b9d291375229daaa16755820098c5aa7c9da7db2f0ba2c0bdff2",
                "md5": "fb0cd98642eeed33b07bd0584892a110",
                "sha256": "b3de8896fba955fbc3f0b3ed2ab3c820dd0d67c14cee44a3ac3c34b540226260"
            },
            "downloads": -1,
            "filename": "connectide-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "fb0cd98642eeed33b07bd0584892a110",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5354,
            "upload_time": "2024-07-07T08:03:32",
            "upload_time_iso_8601": "2024-07-07T08:03:32.474080Z",
            "url": "https://files.pythonhosted.org/packages/0f/fb/89d64975b9d291375229daaa16755820098c5aa7c9da7db2f0ba2c0bdff2/connectide-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-07 08:03:32",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "connectide"
}
        
Elapsed time: 0.27901s