Popitto


NamePopitto JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummaryBasic Pop-It package
upload_time2024-07-05 12:38:56
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.
            # PopIt Game Documentation

## Overview
The `PopIt` game is a simple turn-based game where two players take turns popping elements on a 6x6 board. The game is won ehen one player forces the other to make the last move.

## Constants

- `FIRST = 1`: Represents the first player.
- `SECOND = 2`: Represents the second player.
- `NONE = 0`: Represents an empty spot on the board.
- `POPPED = 1`: Represents a popped spot on the board.
- `NORESULT = 0`: Indicates no result.
- `FIRSTWIN = 1`: Indicates a win for the first player.
- `SECONDWIN = 2`: Indicates a win for the second player.

## PopIt Class

### Initialization

```python
class PopIt:
    def __init__(self, board=None, turn=FIRST):
        if board is None:
            self.board = [[NONE for _ in range(6)] for _ in range(6)]
        else:
            self.board = board
        self.turn = turn
```

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

### Methods

#### makeMove

```python
def makeMove(self, moveRow, numberOfPops):
    new_board = [row[:] for row in self.board]
    for _ in range(numberOfPops):
        for col in range(6):
            if new_board[moveRow][col] == NONE:
                new_board[moveRow][col] = POPPED
                break
    new_turn = 3 - self.turn
    return PopIt(board=new_board, turn=new_turn)
```

- `moveRow`: The row where the move is made.
- `numberOfPops`: The number of pops to make in the specified row.
- Returns a new `PopIt` object with the updated board and turn.

## Functions

### stringToArray
```python
def stringToArray(string):
    if len(string) != 37:
        raise ValueError("Input string must have exactly 37 characters.")
    
    array = []
    for i in range(0, 36, 6):
        row = [int(char) for char in string[i:i+6]]
        array.append(row)
    return array, int(string[36])
```

`string`: The current board position in upi position notation, which is a 1d 37 element array, the first 36 elements is the flattened board position while the last character indicates the turn, 1 for 1st player 2 for 2nd player
- returns a 2d list with the shape of (6, 6), suitable to be fed into the PopPit class to create a PopIt instance

### printPopIt

```python
def printPopIt(PopIt, fancy=False):
  for row in range(6):
    print(f"{row + 1} ", end="")
    for col in range(6):
      if fancy:
        if PopIt.board[row][col] == NONE:
          print("🔲", end="")
        else:
          print("⬛", end="")
      else:
        if PopIt.board[row][col] == NONE:
          print("-", end=" ")
        else:
          print("X", end=" ")
    print()
  if fancy:
    print("   1 2 3 4 5 6")
  else:
    print("  1 2 3 4 5 6")
  print()
```

- `PopIt`: The game state to be printed.
- `fancy`: Wether to print the fancy board or not (Default False)(Not all terminals support it)
- Prints the current state of the game board.

### moveGen

```python
def moveGen(PopIt):
    return [sum(1 for col in row if col == 0) for row in PopIt.board]
```

- `PopIt`: The game state.
- Returns a list indicating the number of available pops in each row.

### makeMove

```python
def makeMove(PopIt, moveRow, numberOfPops):
    for _ in range(numberOfPops):
        for col in range(6):
            if PopIt.board[moveRow][col] == NONE:
                PopIt.board[moveRow][col] = POPPED
                break
    PopIt.turn = 3 - PopIt.turn
    return PopIt
```

- `PopIt`: The game state.
- `moveRow`: The row where the move is made.
- `numberOfPops`: The number of pops to make in the specified row.
- Updates the game state with the specified move and returns the updated state.

### boardFull

```python
def boardFull(PopIt):
    for row in range(6):
        for col in range(6):
            if PopIt.board[row][col] == NONE:
                return False
    return True
```

- `PopIt`: The game state.
- Returns `True` if the board is full, otherwise `False`.

### getResult

```python
def getResult(PopIt):
    if boardFull(PopIt):
        return PopIt.turn
    elif isCheckMate(PopIt):
        return 3 - PopIt.turn
    return NORESULT
```

- `PopIt`: The game state.
- Returns the result of the game (`FIRSTWIN`, `SECONDWIN`, or `NORESULT`).

### isCheckMate

```python
def isCheckMate(PopIt):
    numPops = 0
    for row in range(6):
        for col in range(6):
            if PopIt.board[row][col] == NONE:
                numPops += 1
                if numPops > 1:
                    return False
    return True
```

- `PopIt`: The game state.
- Returns `True` if the game is in a checkmate position, otherwise `False`.

### perfD

```python
def perfD(PopIt, depth):
    if depth == 0:
        return 1
    nodes = 0
    row = 0
    for totalPopsAvail in moveGen(PopIt):
        for numberOfPops in range(1, totalPopsAvail + 1):
            newPopIt = PopIt.makeMove(row, numberOfPops)
            nodes += perfD(newPopIt, depth - 1)
        row += 1
    return nodes
```

- `PopIt`: The game state.
- `depth`: The depth to search.
- Returns the number of nodes at the specified depth.

### perfT

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

- `PopIt`: The game state.
- `maxDepth`: The maximum depth to search.
- Performs a perft (performance test) up to the specified depth and prints the results.

---

# Example Usage
Simple examples making use of the `Popitto` package
## Printing move generation
```python
import Popitto.Framework as Popitto
Pop = Popitto.PopIt()
print(Popitto.moveGen(Pop))
```
## Performing a performance test
```python
import Popitto.Framework as Popitto
Pop = Popitto.PopIt()
Popitto.perfT(Pop, 4)
```

## Simple Pop-It Game
```python
import Popitto.Framework as Popitto
Pop = Popitto.PopIt()
while True:
  Popitto.printPopIt(Pop)
  if Popitto.boardFull(Pop):
    print(f"{'First Player' if Popitto.getResult(Pop) == Popitto.FIRST else 'Second Player'} WON!")
    break

  moveRow = input("Select a row (1-6): ")
  numberOfPops = input("Select number of pops: ")
  Pop = Pop.makeMove(int(moveRow) - 1, int(numberOfPops))
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "Popitto",
    "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/44/19/a941d1b7b27b40f785c6dbf8e05b6af65ffc913fa9d51690b82bec29a858/popitto-0.1.2.tar.gz",
    "platform": null,
    "description": "# PopIt Game Documentation\r\n\r\n## Overview\r\nThe `PopIt` game is a simple turn-based game where two players take turns popping elements on a 6x6 board. The game is won ehen one player forces the other to make the last move.\r\n\r\n## Constants\r\n\r\n- `FIRST = 1`: Represents the first player.\r\n- `SECOND = 2`: Represents the second player.\r\n- `NONE = 0`: Represents an empty spot on the board.\r\n- `POPPED = 1`: Represents a popped spot on the board.\r\n- `NORESULT = 0`: Indicates no result.\r\n- `FIRSTWIN = 1`: Indicates a win for the first player.\r\n- `SECONDWIN = 2`: Indicates a win for the second player.\r\n\r\n## PopIt Class\r\n\r\n### Initialization\r\n\r\n```python\r\nclass PopIt:\r\n    def __init__(self, board=None, turn=FIRST):\r\n        if board is None:\r\n            self.board = [[NONE for _ in range(6)] 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 6x6 matrix representing the game board. Defaults to an empty board if not provided.\r\n- `turn`: Indicates the current player's turn. Defaults to `FIRST`.\r\n\r\n### Methods\r\n\r\n#### makeMove\r\n\r\n```python\r\ndef makeMove(self, moveRow, numberOfPops):\r\n    new_board = [row[:] for row in self.board]\r\n    for _ in range(numberOfPops):\r\n        for col in range(6):\r\n            if new_board[moveRow][col] == NONE:\r\n                new_board[moveRow][col] = POPPED\r\n                break\r\n    new_turn = 3 - self.turn\r\n    return PopIt(board=new_board, turn=new_turn)\r\n```\r\n\r\n- `moveRow`: The row where the move is made.\r\n- `numberOfPops`: The number of pops to make in the specified row.\r\n- Returns a new `PopIt` object with the updated board and turn.\r\n\r\n## Functions\r\n\r\n### stringToArray\r\n```python\r\ndef stringToArray(string):\r\n    if len(string) != 37:\r\n        raise ValueError(\"Input string must have exactly 37 characters.\")\r\n    \r\n    array = []\r\n    for i in range(0, 36, 6):\r\n        row = [int(char) for char in string[i:i+6]]\r\n        array.append(row)\r\n    return array, int(string[36])\r\n```\r\n\r\n`string`: The current board position in upi position notation, which is a 1d 37 element array, the first 36 elements is the flattened board position while the last character indicates the turn, 1 for 1st player 2 for 2nd player\r\n- returns a 2d list with the shape of (6, 6), suitable to be fed into the PopPit class to create a PopIt instance\r\n\r\n### printPopIt\r\n\r\n```python\r\ndef printPopIt(PopIt, fancy=False):\r\n  for row in range(6):\r\n    print(f\"{row + 1} \", end=\"\")\r\n    for col in range(6):\r\n      if fancy:\r\n        if PopIt.board[row][col] == NONE:\r\n          print(\"\u00f0\u0178\u201d\u00b2\", end=\"\")\r\n        else:\r\n          print(\"\u00e2\u00ac\u203a\", end=\"\")\r\n      else:\r\n        if PopIt.board[row][col] == NONE:\r\n          print(\"-\", end=\" \")\r\n        else:\r\n          print(\"X\", end=\" \")\r\n    print()\r\n  if fancy:\r\n    print(\"   1 2 3 4 5 6\")\r\n  else:\r\n    print(\"  1 2 3 4 5 6\")\r\n  print()\r\n```\r\n\r\n- `PopIt`: The game state to be printed.\r\n- `fancy`: Wether to print the fancy board or not (Default False)(Not all terminals support it)\r\n- Prints the current state of the game board.\r\n\r\n### moveGen\r\n\r\n```python\r\ndef moveGen(PopIt):\r\n    return [sum(1 for col in row if col == 0) for row in PopIt.board]\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- Returns a list indicating the number of available pops in each row.\r\n\r\n### makeMove\r\n\r\n```python\r\ndef makeMove(PopIt, moveRow, numberOfPops):\r\n    for _ in range(numberOfPops):\r\n        for col in range(6):\r\n            if PopIt.board[moveRow][col] == NONE:\r\n                PopIt.board[moveRow][col] = POPPED\r\n                break\r\n    PopIt.turn = 3 - PopIt.turn\r\n    return PopIt\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- `moveRow`: The row where the move is made.\r\n- `numberOfPops`: The number of pops to make in the specified row.\r\n- Updates the game state with the specified move and returns the updated state.\r\n\r\n### boardFull\r\n\r\n```python\r\ndef boardFull(PopIt):\r\n    for row in range(6):\r\n        for col in range(6):\r\n            if PopIt.board[row][col] == NONE:\r\n                return False\r\n    return True\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- Returns `True` if the board is full, otherwise `False`.\r\n\r\n### getResult\r\n\r\n```python\r\ndef getResult(PopIt):\r\n    if boardFull(PopIt):\r\n        return PopIt.turn\r\n    elif isCheckMate(PopIt):\r\n        return 3 - PopIt.turn\r\n    return NORESULT\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- Returns the result of the game (`FIRSTWIN`, `SECONDWIN`, or `NORESULT`).\r\n\r\n### isCheckMate\r\n\r\n```python\r\ndef isCheckMate(PopIt):\r\n    numPops = 0\r\n    for row in range(6):\r\n        for col in range(6):\r\n            if PopIt.board[row][col] == NONE:\r\n                numPops += 1\r\n                if numPops > 1:\r\n                    return False\r\n    return True\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- Returns `True` if the game is in a checkmate position, otherwise `False`.\r\n\r\n### perfD\r\n\r\n```python\r\ndef perfD(PopIt, depth):\r\n    if depth == 0:\r\n        return 1\r\n    nodes = 0\r\n    row = 0\r\n    for totalPopsAvail in moveGen(PopIt):\r\n        for numberOfPops in range(1, totalPopsAvail + 1):\r\n            newPopIt = PopIt.makeMove(row, numberOfPops)\r\n            nodes += perfD(newPopIt, depth - 1)\r\n        row += 1\r\n    return nodes\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- `depth`: The depth to search.\r\n- Returns the number of nodes at the specified depth.\r\n\r\n### perfT\r\n\r\n```python\r\ndef perfT(PopIt, maxDepth):\r\n    startTime = time.time()\r\n    totalNodes = 0\r\n    for depth in range(1, maxDepth + 1):\r\n        totalNodes += perfD(PopIt, depth)\r\n        elapsed = time.time() - startTime\r\n        print(\r\n            f\"info string perft depth {depth} nodes {totalNodes} time {int(1000 * elapsed)} nps {int(totalNodes / (elapsed + 0.00000001))}\"\r\n        )\r\n```\r\n\r\n- `PopIt`: The game state.\r\n- `maxDepth`: The maximum depth to search.\r\n- Performs a perft (performance test) up to the specified depth and prints the results.\r\n\r\n---\r\n\r\n# Example Usage\r\nSimple examples making use of the `Popitto` package\r\n## Printing move generation\r\n```python\r\nimport Popitto.Framework as Popitto\r\nPop = Popitto.PopIt()\r\nprint(Popitto.moveGen(Pop))\r\n```\r\n## Performing a performance test\r\n```python\r\nimport Popitto.Framework as Popitto\r\nPop = Popitto.PopIt()\r\nPopitto.perfT(Pop, 4)\r\n```\r\n\r\n## Simple Pop-It Game\r\n```python\r\nimport Popitto.Framework as Popitto\r\nPop = Popitto.PopIt()\r\nwhile True:\r\n  Popitto.printPopIt(Pop)\r\n  if Popitto.boardFull(Pop):\r\n    print(f\"{'First Player' if Popitto.getResult(Pop) == Popitto.FIRST else 'Second Player'} WON!\")\r\n    break\r\n\r\n  moveRow = input(\"Select a row (1-6): \")\r\n  numberOfPops = input(\"Select number of pops: \")\r\n  Pop = Pop.makeMove(int(moveRow) - 1, int(numberOfPops))\r\n```\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Basic Pop-It package",
    "version": "0.1.2",
    "project_urls": null,
    "split_keywords": [
        "python",
        " game",
        " board",
        " boardgame"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "aef0da9bf001cc2e72b324fb0c732b4aa29175358a420ea57674b4f5d9e225fb",
                "md5": "47c2706ad36cf9585d12b920ae404400",
                "sha256": "713e53fa615e3c8a5e5d58f29796d7a0970e756f94f270c4afe0b21388aa5df0"
            },
            "downloads": -1,
            "filename": "Popitto-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "47c2706ad36cf9585d12b920ae404400",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4560,
            "upload_time": "2024-07-05T12:38:54",
            "upload_time_iso_8601": "2024-07-05T12:38:54.771587Z",
            "url": "https://files.pythonhosted.org/packages/ae/f0/da9bf001cc2e72b324fb0c732b4aa29175358a420ea57674b4f5d9e225fb/Popitto-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4419a941d1b7b27b40f785c6dbf8e05b6af65ffc913fa9d51690b82bec29a858",
                "md5": "0f4f712bd8e8471ee0b20f2bf0da2811",
                "sha256": "e4cb723ecc0950f7faae13319f5412cda453755c54cf365cf12b0b0f1943afda"
            },
            "downloads": -1,
            "filename": "popitto-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "0f4f712bd8e8471ee0b20f2bf0da2811",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3932,
            "upload_time": "2024-07-05T12:38:56",
            "upload_time_iso_8601": "2024-07-05T12:38:56.793557Z",
            "url": "https://files.pythonhosted.org/packages/44/19/a941d1b7b27b40f785c6dbf8e05b6af65ffc913fa9d51690b82bec29a858/popitto-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-05 12:38:56",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "popitto"
}
        
Elapsed time: 1.33233s