decision tree suspended, map enlargened, boxes with names

This commit is contained in:
tubks 2021-06-02 02:29:04 +02:00
parent de2e2b7e00
commit 63c9ebaf0d
7 changed files with 55 additions and 35 deletions

View File

@ -4,6 +4,7 @@ from mesa.visualization.modules.TextVisualization import TextElement
from mesa.visualization.ModularVisualization import ModularServer from mesa.visualization.ModularVisualization import ModularServer
from src.decisiontree import create_model from src.decisiontree import create_model
from src.direction import Direction from src.direction import Direction
from collections import defaultdict
def player_representation(agent): def player_representation(agent):
@ -25,8 +26,14 @@ def player_representation(agent):
portrayal["Shape"] = 'sprites/skeletonArcher.png' portrayal["Shape"] = 'sprites/skeletonArcher.png'
return portrayal return portrayal
grid = CanvasGrid(player_representation, 10, 10, 500, 500) def chest_content(agent):
if agent.isBox:
portrayal = {"Shape": "sprites/coin.jpg",
"Layer": 1}
return portrayal
grid = CanvasGrid(player_representation, 10, 10, 700, 700)
image=CanvasGrid(chest_content, 1, 1, 200, 200)
class PlayerStats(TextElement): class PlayerStats(TextElement):
def render(self, model): def render(self, model):
hp = str(model.get_hp()) hp = str(model.get_hp())
@ -34,6 +41,14 @@ class PlayerStats(TextElement):
gold = str(model.get_gold()) gold = str(model.get_gold())
return "Player hp: {}<br>Current position: {}<br>Player's gold: {}".format( return "Player hp: {}<br>Current position: {}<br>Player's gold: {}".format(
hp, position, gold) hp, position, gold)
# class ChestContent(CanvasGrid):
# def render(self,model):
# grid_state = defaultdict(list)
# portrayal = self.chest_content(agent)
# portrayal["x"] = 0
# portrayal["y"] = 0
# grid_state[portrayal["Layer"]].append(portrayal)
# return
# class FightStats(TextElement): # class FightStats(TextElement):
# def render(self, model): # def render(self, model):
# if model.player.combat: # if model.player.combat:
@ -41,9 +56,9 @@ class PlayerStats(TextElement):
# else: # else:
# return "Not fighting" # return "Not fighting"
server = ModularServer(GameMap, server = ModularServer(GameMap,
[grid, PlayerStats()], [grid,PlayerStats()],
"Map", "Map",
{"x": 10, "y": 10}) {"x": 10, "y": 10})
create_model() #create_model()
server.port = 8081 server.port = 8081
server.launch() server.launch()

View File

@ -59,14 +59,17 @@ class Player(Creature):
'e_armor_magic_protection':nominalize(opponent.get_armor_magic_protection(), 5), 'e_armor_magic_protection':nominalize(opponent.get_armor_magic_protection(), 5),
'e_attack_type':opponent.get_attack_type().upper(), 'e_attack_type':opponent.get_attack_type().upper(),
'strategy':"PASS"}, index=[1]) 'strategy':"PASS"}, index=[1])
with localconverter(robjects.default_converter + pandas2ri.converter): # with localconverter(robjects.default_converter + pandas2ri.converter):
r_dataframe = robjects.conversion.py2rpy(testcase) # # r_dataframe = robjects.conversion.py2rpy(testcase)
robjects.globalenv['r_dataframe']=r_dataframe # # robjects.globalenv['r_dataframe']=r_dataframe
result=robjects.r('predict(fights.id3, r_dataframe)') # # print(r_dataframe)
return result[0] # # result=robjects.r('predict(fights.id3, r_dataframe)')
# # print(result)
# # return result[0]
return 'MELEE'
def describe_situation(self, opponent, strategy): def describe_situation(self, opponent):
situation = pd.DataFrame({# 'p_strength': self.get_strength(), situation = {# 'p_strength': self.get_strength(),
# 'p_agility': self.get_agility(), # 'p_agility': self.get_agility(),
# 'p_wisdom': self.get_wisdom(), # 'p_wisdom': self.get_wisdom(),
# 'p_health': self.get_health(), # 'p_health': self.get_health(),
@ -82,7 +85,7 @@ class Player(Creature):
'e_damage': opponent.get_damage(), 'e_damage': opponent.get_damage(),
'e_armor_defence': opponent.get_armor_defence(), 'e_armor_defence': opponent.get_armor_defence(),
'e_armor_magic_protection': opponent.get_armor_magic_protection(), 'e_armor_magic_protection': opponent.get_armor_magic_protection(),
'e_attack_type': opponent.get_attack_type().upper()}, index=[1]) 'e_attack_type': opponent.get_attack_type().upper()}
return situation return situation
def melee_attack(self, opponent): def melee_attack(self, opponent):
@ -248,7 +251,7 @@ class Player(Creature):
else: else:
self.opponent = cellmates[0] self.opponent = cellmates[0]
self.strategy=self.predict_strategy(self.opponent) self.strategy=self.predict_strategy(self.opponent)
print("Current enemy stats:\n", self.describe_situation(self.opponent, self.strategy)) print("Current enemy stats:\n", self.describe_situation(self.opponent))
print("Strategy returned from the tree: ", self.strategy) print("Strategy returned from the tree: ", self.strategy)
self.fight_or_flight(self.opponent, self.strategy) self.fight_or_flight(self.opponent, self.strategy)
else: else:

View File

@ -18,8 +18,7 @@ x = 10
y = 10 y = 10
step_counter = 0 step_counter = 0
boxes_number = 5 boxes_number = 5
creatures_number = 25 creatures_number = 35
class GameMap(Model): class GameMap(Model):
def __init__(self, x, y): def __init__(self, x, y):
@ -46,7 +45,7 @@ class GameMap(Model):
pass pass
for i in range(self.boxes_number, for i in range(self.boxes_number,
self.boxes_number + self.creatures_number-10): # taki range, żeby każdy agent miał poprawne unique_id self.boxes_number + self.creatures_number-20): # taki range, żeby każdy agent miał poprawne unique_id
# creature = Creature(i, self) # creature = Creature(i, self)
creature = Creature(i, self, "Goblin", creature = Creature(i, self, "Goblin",
roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3), roll_the_dice(3),
@ -59,9 +58,9 @@ class GameMap(Model):
self.schedule.add(creature) self.schedule.add(creature)
else: else:
pass pass
for i in range(self.boxes_number + self.creatures_number-10, self.creatures_number): # taki range, żeby każdy agent miał poprawne unique_id for i in range(self.boxes_number + self.creatures_number-20, self.creatures_number): # taki range, żeby każdy agent miał poprawne unique_id
creature = Creature(i, self, "Skeleton", creature = Creature(i, self, "Skeleton",
roll_the_dice(5), roll_the_dice(5), roll_the_dice(5), roll_the_dice(5), roll_the_dice(5), roll_the_dice(7), roll_the_dice(7), roll_the_dice(7), roll_the_dice(7), roll_the_dice(7),
WR4, A8, roll_the_dice(10)) WR4, A8, roll_the_dice(10))
x = self.random.randrange(self.grid.width) x = self.random.randrange(self.grid.width)

View File

@ -1,9 +1,9 @@
from mesa import Agent from mesa import Agent
from .dice.dice import roll_the_dice from .dice.dice import roll_the_dice
chest_names=['c'+str(i)+'.jpg' for i in range(1,10)]
class Box(Agent): class Box(Agent):
def __init__(self, unique_id, model): def __init__(self, unique_id, model):
super().__init__(unique_id, model) super().__init__(unique_id, model)
self.gold = 3 * roll_the_dice(6) self.gold = 3 * roll_the_dice(6)
self.isBox = True self.isBox = True
@ -12,3 +12,4 @@ class Box(Agent):
def step(self): def step(self):
pass pass

View File

@ -61,8 +61,8 @@ class Creature(Agent):
attack_value = self.agility + roll_the_dice(6) attack_value = self.agility + roll_the_dice(6)
defense_value = opponent.agility defense_value = opponent.agility
damage = attack_value - defense_value damage = attack_value - defense_value
if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.defence > 0): if (damage > 0) and (damage + self.weapon1.get_damage() - opponent.armor.get_defence() > 0):
opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.defence) opponent.health = opponent.health - (damage + self.weapon1.get_damage() - opponent.armor.get_defence())
print("damage done by the monster: ", damage) print("damage done by the monster: ", damage)
def magic_attack(self, opponent): def magic_attack(self, opponent):
attack_value = self.wisdom + roll_the_dice(6) attack_value = self.wisdom + roll_the_dice(6)
@ -74,7 +74,7 @@ class Creature(Agent):
def default_attack(self, opponent): def default_attack(self, opponent):
if self.weapon1.get_type() == "Meele": if self.weapon1.get_type() == "Meele":
self.melee_attack(opponent) self.melee_attack(opponent)
elif self.weapon1.get_type() == "Range": elif self.weapon1.get_type() == "Ranged":
self.range_attack(opponent) self.range_attack(opponent)
else: else:
self.magic_attack(opponent) self.magic_attack(opponent)

View File

@ -7,10 +7,12 @@ def create_model():
robjects.r('WPM("refresh-cache")') robjects.r('WPM("refresh-cache")')
robjects.r('WPM("install-package", "simpleEducationalLearningSchemes")') robjects.r('WPM("install-package", "simpleEducationalLearningSchemes")')
robjects.r('WPM("load-package", "simpleEducationalLearningSchemes")') robjects.r('WPM("load-package", "simpleEducationalLearningSchemes")')
#print("dupa")
robjects.r('ID3<-make_Weka_classifier("weka/classifiers/trees/Id3")') robjects.r('ID3<-make_Weka_classifier("weka/classifiers/trees/Id3")')
robjects.r('fights<-read.arff("C:/Users/X260/Downloads/currentdata.arff")') robjects.r('fights<-read.arff("C:/Users/X260/Downloads/currentdata.arff")')
robjects.r('fights.id3 <- ID3(strategy ~ p_strength + p_agility + p_wisdom+ p_health + ' robjects.r('fights.id3 <- ID3(strategy ~ p_strength + p_agility + p_wisdom+ p_health + '
'p_melee_damage + p_ranged_damage + p_magic_damage + p_armor_defence + p_armor_magic_protection + ' 'p_melee_damage + p_ranged_damage + p_magic_damage + p_armor_defence + p_armor_magic_protection + '
'e_strength+ e_agility + e_wisdom + e_health + e_damage + e_armor_defence + e_armor_magic_protection + ' 'e_strength+ e_agility + e_wisdom + e_health + e_damage + e_armor_defence + e_armor_magic_protection + '
'e_attack_type, data = fights)') 'e_attack_type, data = fights)')
# robjects.r('.jcache(fights.id3)')
# robjects.r('save(fights.id3, file="dectre.rda)')
#todo: zapisać w rpythonie do pliku, raz zrobić i nie odpalać za każdym razem w serverze

View File

@ -14,16 +14,16 @@ WM10 = Weapon("Axe", "Melee", 3)
WM11 = Weapon("Axe", "Melee", 3) WM11 = Weapon("Axe", "Melee", 3)
WM12 = Weapon("Battle axe", "Melee", 4) WM12 = Weapon("Battle axe", "Melee", 4)
WR1 = Weapon("Sling", "Range", 1) WR1 = Weapon("Sling", "Ranged", 1)
WR2 = Weapon("Sling", "Range", 1) WR2 = Weapon("Sling", "Ranged", 1)
WR3 = Weapon("Sling", "Range", 1) WR3 = Weapon("Sling", "Ranged", 1)
WR4 = Weapon("Bow", "Range", 2) WR4 = Weapon("Bow", "Ranged", 2)
WR5 = Weapon("Bow", "Range", 2) WR5 = Weapon("Bow", "Ranged", 2)
WR6 = Weapon("Bow", "Range", 2) WR6 = Weapon("Bow", "Ranged", 2)
WR7 = Weapon("Bow", "Range", 2) WR7 = Weapon("Bow", "Ranged", 2)
WR8 = Weapon("Longbow", "Range", 3) WR8 = Weapon("Longbow", "Ranged", 3)
WR9 = Weapon("Longbow", "Range", 3) WR9 = Weapon("Longbow", "Ranged", 3)
WR10 = Weapon("Crossbow", "Range", 4) WR10 = Weapon("Crossbow", "Ranged", 4)
S1 = Weapon("Push", "Magic", 2) S1 = Weapon("Push", "Magic", 2)
S2 = Weapon("Push", "Magic", 2) S2 = Weapon("Push", "Magic", 2)
@ -61,7 +61,7 @@ A16 = Armor("Magical Plate Armor", 3, 2)
# C10 = Box(A8) # C10 = Box(A8)
# C11 = Box(A12) # C11 = Box(A12)
# C12 = Box(A14) # C12 = Box(A14)
#
# Gracz = Player(1000, self, "Janusz",3,3,3,20,20,WM1,A1,0,WR1,S1) # Gracz = Player(1000, self, "Janusz",3,3,3,20,20,WM1,A1,0,WR1,S1)
# def __init__(self, unique_id, model, n, s, a, w, max_hp, hp, weapon, arm, g): # def __init__(self, unique_id, model, n, s, a, w, max_hp, hp, weapon, arm, g):
# M1 = Creature("Goblin",2,2,1,10,10,WM2,A2,dice(6)) # M1 = Creature("Goblin",2,2,1,10,10,WM2,A2,dice(6))