-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
135 lines (114 loc) · 4.06 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Labyrinth
{
class Game
{
public Game(Random rand, Ladder ladder)
{
Labyrinth labyrinth = new Labyrinth(rand);
UserInputAndOutput.PrintWelcomeMessage();
int movesCount = 0;
string input = "";
while (!IsGameOver(labyrinth) && input != "restart")
{
UserInputAndOutput.PrinyLabyrinth(labyrinth);
input = UserInputAndOutput.GetInput();
ProccessInput(input, labyrinth, ref movesCount, ladder);
}
if (input != "restart")
{
Console.WriteLine("Congratulations! You escaped in {0} moves.",
movesCount);
if (ladder.ResultQualifiesInLadder(movesCount))
{
Console.WriteLine(
UserInputAndOutput.ENTER_NAME_FOR_SCOREBOARD_MSG);
string name = Console.ReadLine();
ladder.AddResultInLadder(movesCount, name);
}
}
Console.WriteLine();
}
private bool IsGameOver(Labyrinth labyrinth)
{
bool isGameOver = false;
int currentRow = labyrinth.currentCell.Row;
int currentCol = labyrinth.currentCell.Col;
if (currentRow == 0 ||
currentCol == 0 ||
currentRow == Labyrinth.LABYRINTH_SIZE - 1 ||
currentCol == Labyrinth.LABYRINTH_SIZE - 1)
{
isGameOver = true;
}
return isGameOver;
}
private bool TryMove(string direction, Labyrinth labyrinth)
{
bool moveDone = false;
switch (direction)
{
case "u":
moveDone =
labyrinth.TryMove(labyrinth.currentCell, Direction.Up);
break;
case "d":
moveDone =
labyrinth.TryMove(labyrinth.currentCell, Direction.Down);
break;
case "l":
moveDone =
labyrinth.TryMove(labyrinth.currentCell, Direction.Left);
break;
case "r":
moveDone =
labyrinth.TryMove(labyrinth.currentCell, Direction.Right);
break;
default:
Console.WriteLine(UserInputAndOutput.INVALID_MOVE_MSG);
break;
}
if (moveDone == false)
{
Console.WriteLine(UserInputAndOutput.INVALID_MOVE_MSG);
}
return moveDone;
}
private void ProccessInput(string input, Labyrinth labyrinth,
ref int movesCount, Ladder ladder)
{
string inputToLower = input.ToLower();
switch (inputToLower)
{
case "u":
case "d":
case "l":
case "r":
//fallthrough
bool moveDone =
TryMove(inputToLower, labyrinth);
if (moveDone == true)
{
movesCount++;
}
break;
case "top":
ladder.PrintLadder();
break;
case "exit":
Console.WriteLine(UserInputAndOutput.GOODBYE_MSG);
Environment.Exit(0);
break;
case "restart":
break;
default:
string errorMessage = UserInputAndOutput.INVALID_COMMAND_MSG;
Console.WriteLine(errorMessage);
break;
}
}
}
}