0% found this document useful (0 votes)
156 views4 pages

BracU CSE 422 Ai Assignment 3

Uploaded by

MD RADWAN AHAMED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views4 pages

BracU CSE 422 Ai Assignment 3

Uploaded by

MD RADWAN AHAMED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSE422_01_Lab_Assignment03_Turnitin_Summer2024

Name : MD Radwan Ahamed | Student ID : 22101801

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 = []

def alpha_beta_search(self, dpth, pos, is_maximizing, av, bv):


if dpth == 5:
return self.get_leaf_value(pos)

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 get_leaf_value(self, pos):


CSE422_01_Lab_Assignment03_Turnitin_Summer2024
Name : MD Radwan Ahamed | Student ID : 22101801
leaf_utilities = [-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1,
1, -1, 1, -1, 1, -1, 1, -1, 1]
return leaf_utilities[pos]

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

self.game_winner = "Scorpion" if self.round_victors.count("Scorpion") >


self.round_victors.count("Sub-Zero") else "Sub-Zero"

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()

print(f"Game Winner: {battle_results['Game Winner']}")


print(f"Total Rounds Played: {battle_results['Total Rounds']}")
for i, victor in enumerate(battle_results['Round Victors']):
print(f"Winner of Round {i + 1}: {victor}")
CSE422_01_Lab_Assignment03_Turnitin_Summer2024
Name : MD Radwan Ahamed | Student ID : 22101801

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])

left_magic = ls_mx - cost


right_magic = rs_mx - cost
CSE422_01_Lab_Assignment03_Turnitin_Summer2024
Name : MD Radwan Ahamed | Student ID : 22101801

if left_magic > right_magic:


updated_minimax = left_magic
chosen_direction = "left"
else:
updated_minimax = right_magic
chosen_direction = "right"

if updated_minimax > org_mm:


print(f"New minimax value is {updated_minimax}. Pacman moves
{chosen_direction} and uses dark magic")
else:
print(f"Minimax value is {org_mm}. Pacman does not use dark magic")

pacman_simulation(2)
pacman_simulation(5)

You might also like