Skip to content

Instantly share code, notes, and snippets.

@ppolsinelli

ppolsinelli/Memory.cs Secret

Created Aug 19, 2018
Embed
What would you like to do?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Memory
{
float decay;
Dictionary<string, float> episodes = new Dictionary<string, float>();
public Memory(float decay)
{
this.decay = decay;
}
public int Count()
{
return episodes.Count;
}
public List<string> AllEpisodes()
{
return episodes.Keys.ToList();
}
public float EpisodeCurrentMemory(string episode)
{
if (!episodes.Keys.Contains(episode))
return 0;
return episodes[episode];
}
public void AddEpisode(string episodeLabel)
{
Dictionary<string, float> episodesRefreshed = new Dictionary<string, float>();
foreach (var episodeInMem in episodes.Keys)
{
float currentMemory = episodes[episodeInMem] - decay;
if (currentMemory > 0)
{
episodesRefreshed[episodeInMem] = currentMemory;
}
}
episodesRefreshed[episodeLabel] = 1;
episodes = episodesRefreshed;
}
public string GetMostFresh(List<string> possibleEpisodes, bool addChosenAsEpisode)
{
if (possibleEpisodes == null || possibleEpisodes.Count == 0)
return null;
float minRequiredToJoin = 1;
foreach (var possibleEpisode in possibleEpisodes)
{
if (!episodes.ContainsKey(possibleEpisode))
{
minRequiredToJoin = 0;
break;
}
minRequiredToJoin = Mathf.Min(minRequiredToJoin, episodes[possibleEpisode]);
}
List<string> candidates = new List<string>();
foreach (var possibleEpisode in possibleEpisodes)
{
if (!episodes.ContainsKey(possibleEpisode))
{
candidates.Add(possibleEpisode);
}
else
{
if (episodes[possibleEpisode] <= minRequiredToJoin)
{
candidates.Add(possibleEpisode);
}
}
}
candidates.Shuffle();
return candidates[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.