BracU CSE 422 Ai Assignment 3
BracU CSE 422 Ai Assignment 3
Task-01
class KombatSimulation:
def __init__(self, starting_player):
self.starting_player = starting_player
self.total_rounds = 0
self.game_winner = None
self.round_victors = []
if is_maximizing:
best = float('-inf')
c = 0 #count
while c < 2:
evl = self.alpha_beta_search(dpth + 1, pos * 2 + c, False, av, bv)
best = max(best, evl)
av = max(av, evl)
if bv <= av:
break
c += 1
return best
else:
worst = float('inf')
c=0
while c < 2:
evl = self.alpha_beta_search(dpth + 1, pos * 2 + c, True, av, bv)
worst = min(worst, evl)
bv = min(bv, evl)
if bv <= av:
break
c += 1
return worst
def run_battle(self):
current_turn = self.starting_player
round_counter = 0
while round_counter < 3:
self.total_rounds += 1
victor = self.alpha_beta_search(0, 0, current_turn == 1, float('-inf'), float('inf'))
if victor == -1:
self.round_victors.append("Scorpion")
current_turn = 1
else:
self.round_victors.append("Sub-Zero")
current_turn = 0
round_counter += 1
def show_results(self):
return {
"Game Winner": self.game_winner,
"Total Rounds": self.total_rounds,
"Round Victors": self.round_victors
}
starting_player = 0
battle_sim = KombatSimulation(starting_player)
battle_sim.run_battle()
battle_results = battle_sim.show_results()
Task-02
def pacman_simulation(cost):
def minmax(dpth, iDx, is_maximizing, alphaV, betaV):
if dpth == 3:
return outcomes[iDx]
if is_maximizing:
best_max = float('-inf')
idx = 0
while idx < 2:
gain = minmax(dpth + 1, iDx * 2 + idx, False, alphaV, betaV)
best_max = max(best_max, gain)
alphaV = max(alphaV, gain)
if betaV <= alphaV:
break
idx += 1
return best_max
else:
best_min = float('inf')
idx = 0
while idx < 2:
gain = minmax(dpth + 1, iDx * 2 + idx, True, alphaV, betaV)
best_min = min(best_min, gain)
betaV = min(betaV, gain)
if betaV <= alphaV:
break
idx += 1
return best_min
outcomes = [3, 6, 2, 3, 7, 1, 2, 0]
org_mm = minmax(0, 0, True, float('-inf'), float('inf'))
ls_mx = max(outcomes[0:4])
rs_mx = max(outcomes[4:8])
pacman_simulation(2)
pacman_simulation(5)