C463/B551/I400 Artificial Intelligence
Dana Vrajitoru

C463/B551/I400 Homework 2

Due date: Wednesday, September 10, 2025.


Ex. 1. D&D Game

In this homework, we will create a small program simulating a text-based dungeons and dragons style game where one or more human players compete with NPCs. The goal is to create a system containing some semi-intelligent agents and to review OOP in Python. 

Download the file GM.py in your working folder. This contains a class called GM managing a game with a given number of players, out of which some can be human. It goes in a loop where it asks the user if they want to play another round until they say no. 

You will define a class Player that implements a player character that can be either human or artificial. The player has two attributes, strength and intelligence, randomized at the start. The player has a home base that has a given level starting from 1 and a health starting from 5. The player can store resources of type brick, wood, and food. In each round, it can take the following actions: collect resource (randomized outcome), level up the home base, heal, and attack another player. 

a. Defining a Class and a Constructor

In a file called Player.py, import the module random at the top, and then define a class called Player. Before the constructor, add two (static) attributes called strengthRange and intelligenceRange, both initialized as 10, and healthMax and resourceRange as 5. Then define a constructor with the signature:

def __init__(self, human = False):

In the constructor, initialize self.strength and self.intelligence with a call to randint with 1 and strengthRange as parameter for the first one, and similarly for the second. Initialize self.wood, brick, and food as 0. Initialize self.homeBase as 1 and self.health to healthMax

Add a function with the signature

def __str__(self):

where you return a string containing "Human: ## Strength: ## Intelligence: ## Health: ## Home: ##" where the ## are replaced by current values of the attributes. 

b. Action Functions

Define 4 functions for the 4 possible actions with the following signatures and descriptions. In each of these, you can print a message with the result of the action at the end if it helps the game play.

def collect(self):

This function collects a random resource. Run randint between 1 and 3 and collect wood, brick, or food based on the value. The amount to collect should be randomized between 1 and resourceRange. Finding wood or brick increases the strength by 1 and finding food increase the intelligence by 1. 

def build(self):

This function builds up the home base level. You should check if the amount of brick and of wood the player has is at least equal to the level. If it is, then subtract this amount from both of them, and then increase the level by 1. 

def heal(self):

This function heals the player. Let's say that the player's maximum health is equal to 5 plus the level of the home base. So, add to the health an amount that is at most equal to the amount of food available, and that doesn't make it go over the maximum health. For example, if the home base level is 1 and the player has 7 food, and a current health of 2, then he can heal for 4 food and be left with 3 food.

def attack(self, target):

In this function, first check if self and target are not the same object, and then check that the health of the target is not 0 (eliminated). If they are the same object or the target's health is 0, return without doing anything. Otherwise, the capacity of each player is calculated as strength plus twice the intelligence plus the home base level. Generate a random value for each player between 1 and their capacity. The player with the higher number wins. The winning player gets their intelligence increased by 1 while the losing player gets their health decreased by 1. 

c. Play Function

Write a function with the signature

def play(self, players):

where players is a list of other players.  First, if the player's health is 0, the function should return without doing anything. This means that a player whose health reaches 0 is eliminated. 

Otherwise, if the player is human, then ask them to chose one of the 4 actions. You can use numbers from 1 to 4 for the answers. You can get the answer with the function input and then covert it to an integer with int, like

answer = int(input())

If the player is not human, generate the action randomly between 1 and 4. 

Then map the action to one of the 4 functions defined at point b in the order above. If the action is to attack another player, if the self player is human, then ask which player number you want to attack with answers from 0 to the length of the players list len(players) minus 1. If the player is not human, randomize the choice of player to attack. Then call the function attack from self with a single parameter that is the element players[choice] (or whatever you call the variable).

d. Running the Program

You can run the program by just running the module GM.py. In the __main__ part of the code, it contains the code needed to test the program. 

Turn in:

Submit the file Player.py as well as the file GM.py if you modify it.