Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Board.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,22 +426,32 @@ def generate_move_list(self, rdm=True):
return move_list

def _get_empty_positions(self):
"""
Returns all the empty positions(points) in the board.
"""
return [i.get_index(i.coord) for i in self.points if i.state.name == 'E']

def _is_closed(self, position):
"""
Returns True if the position is closed else False.
--------------------------------------------------
Closed means that the position is empty and surrounded
by all the neighbouring goats. In addition, no tigers
can access the empty position by capturing.
"""

for i in self._move_connections[position]:
if self.points[i].state.name in {'T', 'E'}:
return False
return True
all_goat_neighbours = any([not self.points[i].state.name in {'T', 'E'} for i in self._move_connections[position]])

capture_tiger_present = any([self.points[i].state.name == 'T' for i in self._capture_connections[position]])

return all_goat_neighbours and not capture_tiger_present

@property
def no_of_closed_spaces(self):
closed_spaces = 0
for i in self._get_empty_positions():
if self._is_closed(i):
closed_spaces += 1
return closed_spaces
"""
Return the number of closed spaces in the board.
"""
return len([True for i in self._get_empty_positions() if self._is_closed(i)])

def copy(self):
board = Board()
Expand Down
2 changes: 1 addition & 1 deletion Engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def evaluate(self, depth=0):
winner = self.board.winner
if not winner:
return 300 * self.board.movable_tigers() + 700 * self.board.deadGoats\
- 100 * self.board.no_of_closed_spaces - depth
- 700 * self.board.no_of_closed_spaces - depth

if winner == Board.Player.G:
return -Engine.INF
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Bagh-Chal
Bag-Chal is the 2-player strategic board game consisting of tigers and goats originated in Nepal. Played on 5 by 5 grid, the game has 20 goats and 4 tigers in the starting of the game. Tigers aim to capture the goat and goat aim to restrict legal movement of tiger. When none of the tigers are given legal movement, goats win whereas death of 5 goats make the tiger winner.
Regarding rules and more information of Bag-Chal, please [click here](https://en.wikipedia.org/wiki/Bagh-Chal).

## About This Repository:
The Bagh-Chal game here is built in Python using Tkinter GUI library. Here, users can play with computer either as tiger or goat at different difficulty. For AI, we have implemented minimax algorithm with alpha beta pruning.

## Dependencies
The only dependency as of now is tkinter. Install tkinter by executing the following commands in the terminal.
```
$ sudo apt-get update
$ sudo apt-get install python3-tk
```

## Usage
First clone the repository.
```
git clone https://github.com/code-geek/pybaghchal
```
Then, go to the project directory and run ui.py.
```
cd baghchal (if name of project directory is baghchal)
./ui.py
or python3 ui.py
```

The code here is modular so as to make it readable and easy to maintain.
The modules serves different purpose as follows:
- Board.py: Module describing board parameters and methods.
- Engine.py: Module containing AI engine for the game.
- Game.py: Module that lets us play the game in terminal. This helps for the game to be integrated across different other platforms.
- Point.py: Module containing class for the point. It contains helper methods and attributes that a point in Bagh-Chal board game represents.
- tests.py: Testing module using unittest python module.
- ui.py: UI created using tkinter and imports AI logic from Engine.py. Board logic is imported from Board.py.
- ui.conf: Configuration file for playing options as goat/tiger and level of difficulty.

## Further Improvements:
- The entire code is written in Python so in higher difficulty(which mean higher depth so higher computation time), the AI takes long time to make the move. We could implement AI engine using low level programming language(like C++). That helps a lot to make the AI make move faster.
- We have used minimax algorithm with alpha beta pruning for AI implementation. We could use reinforcement learning further to make stronger AI.
4 changes: 4 additions & 0 deletions uiconf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[game]
ai = tiger
aistrength = 5