Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TayVeeTwo
- {
- class MainClass
- {
- static Random r;
- static String lastInput = "";
- static String lastOutput = "";
- static String currentInput = "";
- static String currentOutput = "";
- static Vocabulary vocabulary;
- static bool inquire = false;
- static void Main(string[] args)
- {
- vocabulary = new Vocabulary();
- r = new Random(123456);
- Console.WriteLine("Hello. Feed me memes XD");
- do
- {
- //First gather input
- currentInput = Console.ReadLine();
- //Before moving on you can check for escape chars
- if (String.IsNullOrWhiteSpace(currentInput)) continue;
- inquire = (r.Next(10) % 2 == 0);
- //Then Evaluate the input
- vocabulary.Evaluate(currentInput);
- //Before moving on you can check for special evaluations
- if (inquire && !String.IsNullOrWhiteSpace(lastInput)) vocabulary.Inquire(lastInput, currentInput);
- //Then respond to the user
- currentOutput = vocabulary.Respond(r).text;
- //Before moving on set last input and output for later use
- lastInput = currentInput;
- lastOutput = currentOutput;
- Console.WriteLine(currentOutput + " XD");
- } while (currentInput != "/quit" && currentInput != "/exit");
- Console.WriteLine("Exiting... Goodbye XD");
- System.Threading.Thread.Sleep(2000);
- }
- }
- class Vocabulary
- {
- List<Entry> entries;
- public Vocabulary(List<Entry> entries)
- {
- this.entries = entries;
- }
- public Vocabulary()
- {
- this.entries = new List<Entry>(50000);
- }
- public Entry Evaluate(String input)
- {
- Entry entry = new Entry(input ,false);
- Boolean exists = entries.Any(e => e.text == input);
- if (exists) entry = entries.Find(e => e.text == input);
- else entries.Add(entry);
- entry.value+=2;
- return entry;
- }
- public void Inquire(String question , String response)
- {
- Entry qentry = Evaluate(question);
- Entry rentry = Evaluate(response);
- if(qentry.responses.All(e => e.text == rentry.text))//Answer already exists, increase its score
- {
- qentry.responses.Find(e => e.text == rentry.text).value+=2;
- }
- else//Create a new answer and add it to the entry
- {
- Entry resp = new Entry(response, true);
- qentry.responses.Add(resp);
- }
- }
- public Entry Respond(Random r)
- {
- Entry response = new Entry("/null",false);
- int i = 0;
- if (entries.Count == 0) return new Entry("Im all out of memes",false);
- do
- {
- i=r.Next(entries.Count);
- if (entries[i].value > 0)
- response = entries[i];
- } while (response.text=="/null");
- entries[i].value--;
- return response;
- }
- public Entry Answer(ref Entry toAnswer)
- {
- Entry bestResponse = toAnswer.responses[0];
- int id = 0;
- for(int i = 0; i < toAnswer.responses.Count; i++)
- {
- if(bestResponse.value < toAnswer.responses[i].value)
- {
- bestResponse = toAnswer.responses[i];
- id = i;
- }
- }
- bestResponse.value--;
- toAnswer.responses[id] = bestResponse;
- return bestResponse;
- }
- }
- class Entry
- {
- public static Entry NOENTRY = new Entry("",true);
- public String text;
- public int value;
- public List<Entry> responses;
- public Entry(String entry, Boolean asResponse)
- {
- this.text = entry;
- value = 0;
- if (asResponse) responses = new List<Entry>(0);
- else responses = new List<Entry>(100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement