Last active
April 28, 2017 19:23
-
-
Save porthunt/7c89c6abae0aff3aad421f4a55a6e934 to your computer and use it in GitHub Desktop.
A different approach to work on the Text101 challenge from Udemy Unity Course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"cell" "You are on a cell." | |
"sheets_0" "These are some pretty dirt sheets... Did you really sleep on these...?" | |
"mirror" "You see a broken mirror on the floor..." | |
"lock_0" "A locked door. It feels pretty much close for me." | |
"sheets_1" "Awesome, more dirty sheets. I think I have a teammate, but I can't really remember." | |
"cell_mirror" "Okay... what should I do with this? I will just put it on my pocket." | |
"lock_1" "Another lock, great... I think there is something different on this one." | |
"freedom" "Nice! The locker was broken, I'm out of this dirty cell." | |
cell > sheets_0 ('S'; "Sheets") | |
sheets_0 > cell ('R'; "Return") | |
cell > mirror ('M'; "Mirror") | |
mirror > cell ('R'; "Return") | |
cell > lock_0 ('L'; "Lock") | |
lock_0 > cell ('R'; "Return") | |
mirror > cell_mirror ('T'; "Take") | |
cell_mirror > sheets_1 ('S'; "Sheets") | |
sheets_1 > cell_mirror ('R'; "Return") | |
cell_mirror > lock_1 ('L'; "Lock") | |
lock_1 > cell_mirror ('R'; "Return") | |
lock_1 > freedom ('O'; "Open") | |
start: cell | |
end: freedom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"corridor" "You find yourself in a creepy corridor..." | |
"stairs_0" "Right, nothing in here, not even a single lightbulb." | |
"floor" "There is something weird on the floor, should I get it?" | |
"closet_door" "A closet! Oh wait... it is locked." | |
"stairs_1" "More stairs? Oh my... nothing in here." | |
"corridor_1" "Okay, it is a <i>hairclip</i>. What should I do with this?" | |
"in_closet" "Wait... I can use the hairclip to open this closet! There is an office uniform inside!" | |
"stairs_2" "Nothing in here." | |
"corridor_2" "What should I do next? I have to get outta here." | |
"corridor_3" "A lot of officers in here, I need to get out without being noticed." | |
"courtyard" "I'm out!" | |
corridor > stairs_0 ('S'; "Climb stairs") | |
stairs_0 > corridor ('R'; "Return") | |
corridor > floor ('F'; "Look at the floor") | |
floor > corridor ('R'; "Go back") | |
corridor > closet_door ('C'; "Open closet door") | |
closet_door > corridor ('R'; "Return") | |
floor > corridor_1 ('H'; "Hairclip") | |
corridor_1 > in_closet ('P'; "Pick") | |
corridor_1 > stairs_1 ('S'; "Climb stairs") | |
stairs_1 > corridor_1 ('R'; "Return") | |
in_closet > corridor_3 ('D'; "Dress uniform") | |
corridor_3 > in_closet ('U'; "Take out the uniform") | |
in_closet > corridor_2 ('R'; "Return to corridor") | |
corridor_2 > in_closet ('B'; "Go back") | |
corridor_2 > stairs_2 ('R'; "Climb stairs") | |
stairs_2 > corridor_2 ('B'; "Go back") | |
corridor_3 > courtyard ('S'; "Go to the courtyard") | |
start: corridor | |
end: courtyard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class World | |
{ | |
LinkedList<Map> map_list; | |
Map curr_map; | |
public World() | |
{ | |
map_list = new LinkedList<Map>(); | |
Map cell = new Map("Cell Scene"); | |
map_list.AddLast(cell); | |
Map corridor = new Map("Corridor Scene"); | |
map_list.AddLast(corridor); | |
SetStart(cell); | |
SetEnd(corridor); | |
SetCurrentMap(cell); | |
} | |
public void SetCurrentMap(Map m) | |
{ | |
curr_map = m; | |
} | |
public Map CurrentMap() | |
{ | |
return curr_map; | |
} | |
public void SetStart(Map m) | |
{ | |
if (map_list.Contains(m)) | |
map_list.Remove(m); | |
map_list.AddFirst(m); | |
} | |
public Map StartMap() | |
{ | |
return map_list.First.Value; | |
} | |
public bool IsStart(Map m) | |
{ | |
if (m.Equals(StartMap())) | |
return true; | |
else return false; | |
} | |
public void SetEnd(Map m) | |
{ | |
if (map_list.Contains(m)) | |
map_list.Remove(m); | |
map_list.AddLast(m); | |
} | |
public Map EndMap() | |
{ | |
return map_list.Last.Value; | |
} | |
public bool IsEnd(Map m) | |
{ | |
if (m.Equals(EndMap())) | |
return true; | |
else return false; | |
} | |
public Map NextMap() | |
{ | |
curr_map = map_list.Find(curr_map).Next.Value; | |
return curr_map; | |
} | |
public bool HasEnded() | |
{ | |
if (curr_map.Equals(map_list.Last.Value)) | |
return true; | |
else return false; | |
} | |
} | |
public class Map | |
{ | |
private Graph states; | |
public string map_name; | |
public Map(string name) | |
{ | |
this.map_name = name; | |
states = new Graph(); | |
ReadMapFile(); | |
} | |
private void ReadMapFile() | |
{ | |
string line; | |
System.IO.StreamReader file = new System.IO.StreamReader(@"D:\Users\User\Documents\Text101\Assets\maps\" + map_name.ToLower() + ".txt"); | |
while ((line = file.ReadLine()) != "") | |
ReadVertex(line); | |
while ((line = file.ReadLine()) != "") | |
ReadEdge(line); | |
while ((line = file.ReadLine()) != null) | |
ReadSettings(line); | |
} | |
private void ReadVertex(string line) | |
{ | |
string from = line.Split('\t')[0].Replace("\"", "").Trim(); | |
string to = line.Split('\t')[1].Replace("\"", "").Trim(); | |
this.AddState(from, to); | |
} | |
private void ReadEdge(string line) | |
{ | |
string[] vertex_rel = line.Split('\t')[0].Split('>'); | |
string[] edge_info = line.Split('\t')[1].Split(';'); | |
char edge_action = edge_info[0].Replace("'", "").Replace("(", "").ToCharArray()[0]; | |
string edge_action_name = edge_info[1].Replace("\"", "").Replace(")", "").Trim() ; | |
Vertex from = states.Find(vertex_rel[0].Trim()); | |
Vertex to = states.Find(vertex_rel[1].Trim()); | |
if (from != null && to != null) | |
this.Link(edge_action, edge_action_name, from, to); | |
else | |
throw new System.ArgumentException("Invalid input file for " + map_name + ". Please review the Vertex/Edges."); | |
} | |
private void ReadSettings(string line) | |
{ | |
string command = line.Split(':')[0]; | |
string vertex = line.Split(':')[1].Trim(); | |
if (command.Equals("start")) | |
this.SetStart(states.Find(vertex)); | |
if (command.Equals("end")) | |
this.SetEnd(states.Find(vertex)); | |
} | |
public Vertex AddState(string name, string msg) | |
{ | |
Vertex v = states.Insert(name, msg); | |
return v; | |
} | |
public void Link(char action, string action_name, Vertex v, Vertex y) | |
{ | |
states.Link(action, action_name, v, y); | |
} | |
public void SetStart(Vertex v) | |
{ | |
states.SetStart(v); | |
} | |
public Vertex StartPoint() | |
{ | |
return states.GetStart(); | |
} | |
public bool IsStart(Vertex v) | |
{ | |
if (v.Equals(StartPoint())) | |
return true; | |
else return false; | |
} | |
public void SetEnd(Vertex v) | |
{ | |
states.SetEnd(v); | |
} | |
public Vertex EndPoint() | |
{ | |
return states.GetEnd(); | |
} | |
public bool IsEnd(Vertex v) | |
{ | |
if (v.Equals(EndPoint())) | |
return true; | |
else return false; | |
} | |
} | |
public class Graph | |
{ | |
LinkedList<Vertex> vertex_list; | |
internal Graph() | |
{ | |
vertex_list = new LinkedList<Vertex>(); | |
} | |
public Vertex Insert(string name, string msg) | |
{ | |
Vertex v = new Vertex(name, msg); | |
vertex_list.AddLast(v); | |
return v; | |
} | |
public Vertex Find(string vertex_name) | |
{ | |
foreach (Vertex v in vertex_list) | |
{ | |
if (vertex_name.Equals(v.name)) | |
return v; | |
} | |
return null; | |
} | |
public Edge Link(char action, string action_name, Vertex v1, Vertex v2) | |
{ | |
Edge link = new Edge(action, action_name, v1, v2); | |
v1.AddNeighbor(link); | |
return link; | |
} | |
public int Size() | |
{ | |
return vertex_list.Count; | |
} | |
public void SetStart(Vertex v) | |
{ | |
if (vertex_list.Contains(v)) | |
vertex_list.Remove(v); | |
vertex_list.AddFirst(v); | |
} | |
public Vertex GetStart() | |
{ | |
return vertex_list.First.Value; | |
} | |
public void SetEnd(Vertex v) | |
{ | |
if (vertex_list.Contains(v)) | |
vertex_list.Remove(v); | |
vertex_list.AddLast(v); | |
} | |
public Vertex GetEnd() | |
{ | |
return vertex_list.Last.Value; | |
} | |
} | |
public class Vertex { | |
public string name; | |
public string msg; | |
private LinkedList<Edge> neighbors; | |
internal Vertex(string name, string msg) | |
{ | |
this.name = name; | |
this.msg = msg; | |
this.neighbors = new LinkedList<Edge>(); | |
} | |
public void AddNeighbor(Edge e) | |
{ | |
neighbors.AddLast(e); | |
} | |
public LinkedList<Edge> Neighbors() | |
{ | |
return neighbors; | |
} | |
} | |
public class Edge { | |
private Vertex node1; | |
private Vertex node2; | |
public char action; | |
public string action_name; | |
internal Edge(char action, string action_name, Vertex node1, Vertex node2) | |
{ | |
this.action = action; | |
this.action_name = action_name; | |
this.node1 = node1; | |
this.node2 = node2; | |
} | |
public Vertex CurrentVertex() | |
{ | |
return node1; | |
} | |
public Vertex RelatedVertex() | |
{ | |
return node2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class TextController : MonoBehaviour { | |
public Text text; | |
Vertex curr_state; | |
Map map; | |
World world; | |
// Use this for initialization | |
void Start () { | |
world = new World(); | |
map = world.CurrentMap(); | |
curr_state = map.StartPoint(); | |
text.text = Message(curr_state); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (world.HasEnded() && map.IsEnd(curr_state)) | |
{ | |
text.text = "THE END"; | |
} else if (map.IsEnd(curr_state)) | |
{ | |
map = world.NextMap(); | |
curr_state = map.StartPoint(); | |
text.text = Message(curr_state); | |
} | |
else | |
{ | |
foreach (Edge element in curr_state.Neighbors()) | |
if (Input.inputString.ToUpper() == element.action.ToString()) | |
{ | |
curr_state = element.RelatedVertex(); | |
text.text = Message(curr_state); | |
} | |
} | |
} | |
string Message (Vertex state) | |
{ | |
string msg = state.msg + "\n\n"; | |
if (state.Neighbors().Count > 0) | |
msg += "Press "; | |
foreach (Edge element in state.Neighbors()) | |
msg += "<b>" + element.action + "</b> for <i>" + element.action_name + "</i>. "; | |
return msg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment