Permalink
Browse files

AdventureSample - promote VerbResult to enum

  • Loading branch information...
bobbymcr committed May 12, 2018
1 parent bd02be2 commit 73227587149259af696db377673bb30b4e51fe3d
Showing with 42 additions and 38 deletions.
  1. +42 −38 projects/AdventureSample/src/Adventure/adventure.cs
@@ -547,7 +547,7 @@ private void PrintIntro()
INPUT_n("");
}
private int Main()
private VerbResult Main()
{
this.Init();
; // ** THE QUEST **
@@ -586,8 +586,8 @@ private int Main()
CLS();
Dictionary<string, Func<int>> verbRoutines = InitVerbs();
Func<int> unknown = UnknownVerb;
Dictionary<string, Func<VerbResult>> verbRoutines = InitVerbs();
Func<VerbResult> unknown = UnknownVerb;
InitHandlers(verbRoutines);
while (true)
@@ -602,25 +602,29 @@ private int Main()
{
Parser();
int ret = HandleVerb(verbRoutines, unknown);
if (ret == 0)
VerbResult ret = HandleVerb(verbRoutines, unknown);
if (ret == VerbResult.Idle)
{
// NO-OP
}
else if (ret == VerbResult.Proceed)
{
break;
}
else if (ret > 0)
else
{
return ret;
}
}
}
}
private Dictionary<string, Func<int>> InitVerbs()
private Dictionary<string, Func<VerbResult>> InitVerbs()
{
return new Dictionary<string, Func<int>>();
return new Dictionary<string, Func<VerbResult>>();
}
private void InitHandlers(Dictionary<string, Func<int>> verbRoutines)
private void InitHandlers(Dictionary<string, Func<VerbResult>> verbRoutines)
{
AddVerb(verbRoutines, "GO", Go);
AddVerb(verbRoutines, "GET", Get);
@@ -647,14 +651,14 @@ private void InitHandlers(Dictionary<string, Func<int>> verbRoutines)
AddVerb(verbRoutines, "WEA", Wear);
}
private void AddVerb(Dictionary<string, Func<int>> verbRoutines, string v, Func<int> handler)
private void AddVerb(Dictionary<string, Func<VerbResult>> verbRoutines, string v, Func<VerbResult> handler)
{
verbRoutines.Add(v, handler);
}
private int HandleVerb(Dictionary<string, Func<int>> verbRoutines, Func<int> unknown)
private VerbResult HandleVerb(Dictionary<string, Func<VerbResult>> verbRoutines, Func<VerbResult> unknown)
{
Func<int> verbRoutine;
Func<VerbResult> verbRoutine;
if (!verbRoutines.TryGetValue(verb, out verbRoutine))
{
verbRoutine = unknown;
@@ -663,7 +667,7 @@ private int HandleVerb(Dictionary<string, Func<int>> verbRoutines, Func<int> unk
return verbRoutine();
}
private int UnknownVerb()
private VerbResult UnknownVerb()
{
PRINT("I DON'T KNOW HOW TO DO THAT");
return VerbResult.Idle;
@@ -738,7 +742,7 @@ private void Parser()
}
}
private int Go()
private VerbResult Go()
{
int dir;
if (noun == "NOR")
@@ -779,7 +783,7 @@ private int Go()
return Move(dir);
}
private int Move(int dir)
private VerbResult Move(int dir)
{
int next = map[currentRoom, dir];
if ((next > 0) && (next < 128))
@@ -800,7 +804,7 @@ private int Move(int dir)
return VerbResult.Idle;
}
private int Get()
private VerbResult Get()
{
FindRoomForObject();
@@ -839,7 +843,7 @@ private int Get()
return VerbResult.Idle;
}
private int Drop()
private VerbResult Drop()
{
FindRoomForObject();
@@ -857,7 +861,7 @@ private int Drop()
return VerbResult.Idle;
}
private int Inventory()
private VerbResult Inventory()
{
bool atLeastOne = false;
PRINT("YOU ARE CARRYING:");
@@ -878,7 +882,7 @@ private int Inventory()
return VerbResult.Idle;
}
private int Look()
private VerbResult Look()
{
if (noun == "")
{
@@ -889,7 +893,7 @@ private int Look()
return VerbResult.Idle;
}
private int Examine()
private VerbResult Examine()
{
if (noun == "GRO")
{
@@ -931,7 +935,7 @@ private int Examine()
return VerbResult.Idle;
}
private int Quit()
private VerbResult Quit()
{
PRINT_n("ARE YOU SURE YOU WANT TO QUIT (Y/N)");
string quit = INPUT_s("");
@@ -943,7 +947,7 @@ private int Quit()
return VerbResult.Idle;
}
private int PlayAgain()
private VerbResult PlayAgain()
{
while (true)
{
@@ -961,7 +965,7 @@ private int PlayAgain()
}
}
private int Read()
private VerbResult Read()
{
if (noun == "DIA")
{
@@ -1007,7 +1011,7 @@ private int Read()
return VerbResult.Idle;
}
private int Open()
private VerbResult Open()
{
if (noun == "BOX")
{
@@ -1058,7 +1062,7 @@ private int Open()
return VerbResult.Idle;
}
private int Pour()
private VerbResult Pour()
{
bool poured;
if (noun == "SAL")
@@ -1149,7 +1153,7 @@ private bool PourMixture()
return true;
}
private int Climb()
private VerbResult Climb()
{
if (noun == "TRE")
{
@@ -1187,7 +1191,7 @@ private int Climb()
return VerbResult.Idle;
}
private int Jump()
private VerbResult Jump()
{
if ((currentRoom != 7) && (currentRoom != 8))
{
@@ -1210,7 +1214,7 @@ private int Jump()
}
}
private int Dig()
private VerbResult Dig()
{
if ((noun != "HOL") && (noun != "GRO") && (noun != ""))
{
@@ -1237,7 +1241,7 @@ private int Dig()
return VerbResult.Idle;
}
private int Row()
private VerbResult Row()
{
if ((noun != "BOA") && (noun != ""))
{
@@ -1255,7 +1259,7 @@ private int Row()
return VerbResult.Idle;
}
private int Wave()
private VerbResult Wave()
{
if (noun != "FAN")
{
@@ -1286,7 +1290,7 @@ private int Wave()
return VerbResult.Idle;
}
private int Leave()
private VerbResult Leave()
{
if (currentRoom != 13)
{
@@ -1305,7 +1309,7 @@ private int Leave()
}
}
private int Fight()
private VerbResult Fight()
{
if (noun == "")
{
@@ -1334,7 +1338,7 @@ private int Fight()
return VerbResult.Idle;
}
private int Wear()
private VerbResult Wear()
{
if (noun != "GLO")
{
@@ -1353,11 +1357,11 @@ private int Wear()
return VerbResult.Idle;
}
private static class VerbResult
private enum VerbResult
{
public const int Idle = -1;
public const int Proceed = 0;
public const int RestartGame = 1;
public const int EndGame = 2;
Idle,
Proceed,
RestartGame,
EndGame
}
}

0 comments on commit 7322758

Please sign in to comment.