Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f260c3

Browse files
Merge pull request #3 from Suraj1127/master
Add README.md
2 parents c5c8f1c + 9a611e0 commit 7f260c3

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

‎Board.py‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,22 +426,32 @@ def generate_move_list(self, rdm=True):
426426
return move_list
427427

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

431434
def _is_closed(self, position):
435+
"""
436+
Returns True if the position is closed else False.
437+
--------------------------------------------------
438+
Closed means that the position is empty and surrounded
439+
by all the neighbouring goats. In addition, no tigers
440+
can access the empty position by capturing.
441+
"""
432442

433-
for i in self._move_connections[position]:
434-
if self.points[i].state.name in {'T', 'E'}:
435-
return False
436-
return True
443+
all_goat_neighbours = any([not self.points[i].state.name in {'T', 'E'} for i in self._move_connections[position]])
444+
445+
capture_tiger_present = any([self.points[i].state.name == 'T' for i in self._capture_connections[position]])
446+
447+
return all_goat_neighbours and not capture_tiger_present
437448

438449
@property
439450
def no_of_closed_spaces(self):
440-
closed_spaces = 0
441-
for i in self._get_empty_positions():
442-
if self._is_closed(i):
443-
closed_spaces += 1
444-
return closed_spaces
451+
"""
452+
Return the number of closed spaces in the board.
453+
"""
454+
return len([True for i in self._get_empty_positions() if self._is_closed(i)])
445455

446456
def copy(self):
447457
board = Board()

‎Engine.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def evaluate(self, depth=0):
2222
winner = self.board.winner
2323
if not winner:
2424
return 300 * self.board.movable_tigers() + 700 * self.board.deadGoats\
25-
- 100 * self.board.no_of_closed_spaces - depth
25+
- 700 * self.board.no_of_closed_spaces - depth
2626

2727
if winner == Board.Player.G:
2828
return -Engine.INF

‎README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Bagh-Chal
2+
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.
3+
Regarding rules and more information of Bag-Chal, please [click here](https://en.wikipedia.org/wiki/Bagh-Chal).
4+
5+
## About This Repository:
6+
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.
7+
8+
## Dependencies
9+
The only dependency as of now is tkinter. Install tkinter by executing the following commands in the terminal.
10+
```
11+
$ sudo apt-get update
12+
$ sudo apt-get install python3-tk
13+
```
14+
15+
## Usage
16+
First clone the repository.
17+
```
18+
git clone https://github.com/code-geek/pybaghchal
19+
```
20+
Then, go to the project directory and run ui.py.
21+
```
22+
cd baghchal (if name of project directory is baghchal)
23+
./ui.py
24+
or python3 ui.py
25+
```
26+
27+
The code here is modular so as to make it readable and easy to maintain.
28+
The modules serves different purpose as follows:
29+
- Board.py: Module describing board parameters and methods.
30+
- Engine.py: Module containing AI engine for the game.
31+
- Game.py: Module that lets us play the game in terminal. This helps for the game to be integrated across different other platforms.
32+
- Point.py: Module containing class for the point. It contains helper methods and attributes that a point in Bagh-Chal board game represents.
33+
- tests.py: Testing module using unittest python module.
34+
- ui.py: UI created using tkinter and imports AI logic from Engine.py. Board logic is imported from Board.py.
35+
- ui.conf: Configuration file for playing options as goat/tiger and level of difficulty.
36+
37+
## Further Improvements:
38+
- 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.
39+
- We have used minimax algorithm with alpha beta pruning for AI implementation. We could use reinforcement learning further to make stronger AI.

‎uiconf‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[game]
2+
ai = tiger
3+
aistrength = 5
4+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /