https://github.com/brean/connect_four
Four connect logic to use in any other project.
https://github.com/brean/connect_four
Last synced: 7 months ago
JSON representation
Four connect logic to use in any other project.
- Host: GitHub
- URL: https://github.com/brean/connect_four
- Owner: brean
- License: apache-2.0
- Created: 2023-08-05T09:09:53.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-05T12:32:05.000Z (about 2 years ago)
- Last Synced: 2025-01-10T21:47:59.491Z (9 months ago)
- Language: Python
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# connect_four
Four connect logic to use in any other project.# Installation
~~You can use pypi to install:~~
Download this package and then run pip in the downloaded folder:
```
cd connect_four
pip install .
```# Usage
To create a new game simple create a new instance of the `ConnectFour`-class.
You can also configure the size of your game, the starting player and the
number of player. The default is a 7*6 board with 2 players where player "1"
starts:```python
import connect_four
cf = connect_four.ConnectFour(columns=7, rows=6, starting_player=1)
```You can then call `cf.step(...)` to play the next piece, it gets the player and the column as parameter. A game could look like this:
```python
cf.step(1, 3)
cf.step(2, 3)
cf.step(1, 4)
cf.step(2, 4)
cf.step(1, 4)
```You need to check if one of the players won manually after each step:
```python
>>> import connect_four
>>> cf = connect_four.ConnectFour(columns=7, rows=6, starting_player=1)
>>> cf.step(1, 3)
>>> cf.step(2, 3)
>>> cf.step(1, 4)
>>> cf.step(2, 4)
>>> cf.step(1, 5)
>>> cf.step(2, 5)
>>> cf.won()
None
>>> cf.step(1, 6)
>>> cf.won()
1
```
If a player won it returns the players number, while the game is undecided it
returns `None`.If all tiles are played (there are no free spots left) the game is called a draw and the `won` function returns `-1`.
For debugging you can always print the current board:
```python
>>> print(cf.print_board())
# +-------+
# |0000000|
# |0000000|
# |0000000|
# |0000100|
# |0002200|
# |0001100|
# +-------+
```To start a new game (empty the board and reset the starting player) call
`cf.restart_game()`.