-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
94 lines (74 loc) · 2.64 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Author: Bishal Sarang
"""
This file contains all the game states like board config, goats_in_hand, goats_killed, current_turn etc
"""
from utilities import can_move
from collections import namedtuple
from configparser import ConfigParser
from assets import Assets
parser = ConfigParser()
Player = namedtuple("Player", "role type")
INF = float("inf")
class Game(object):
def __init__(self):
self.asset = Assets()
parser.read(self.asset.configuration_path)
self.current_turn = 'Goat'
self.winner = None
self.goats_in_hand = 20
self.goats_killed = 0
self.player_1 = Player("Goat", parser.get("settings", "Goat"))
self.player_2 = Player("Tiger", parser.get("settings", "Tiger"))
self.depth = 4
self.set_difficulty()
self.ai = None
self.role = dict()
self.role["Goat"] = "Human"
self.role["Tiger"] = "Human"
if self.player_1.type == "AI":
self.ai = "Goat"
self.role["Goat"] = "AI"
elif self.player_2.type == "AI":
self.ai = "Tiger"
self.role["Tiger"] = "AI"
self.grid = None
self.board_init()
def set_difficulty(self):
if parser.get("settings", "difficulty") == "Medium":
self.depth = 4
elif parser.get("settings", "difficulty") == "Hard":
self.depth = 5
else:
self.depth = 3
def reload_config(self):
self.player_1 = Player("Goat", parser.get("settings", "goat"))
self.player_2 = Player("Tiger", parser.get("settings", "tiger"))
@classmethod
def reset(cls):
cls.__init__()
def is_two_player_mode(self):
return self.player_1.type == "Human" and self.player_2.type == "Human"
def get_role(self):
return self.role[self.current_turn]
def board_init(self):
self.grid = [['_' for _ in range(5)] for _ in range(5)]
# Place tiger at corners
self.grid[0][0] = self.grid[0][4] = self.grid[4][0] = self.grid[4][4] = 'T'
def switch_turn(self):
if self.current_turn == 'Goat':
self.current_turn = 'Tiger'
else:
self.current_turn = 'Goat'
def is_game_over(self):
if self.goats_killed >= 5:
self.winner = "Tiger"
return True
for i in range(5):
for j in range(5):
if self.grid[i][j] == 'T':
if can_move(self.grid, i, j):
return False
self.winner = "Goat"
return True
def __repr__(self):
return f"Current Turn {self.current_turn} | Goats in hand {self.goats_in_hand} "