-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeExplorer_MazeClass
More file actions
205 lines (183 loc) · 6.99 KB
/
MazeExplorer_MazeClass
File metadata and controls
205 lines (183 loc) · 6.99 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace InClassThursNov21_01
{
class Maze
{
//class attributes
private const int SIZE = 20;
private char[,] coords = new char[SIZE, SIZE];
private Random randNum = new Random();
private const int numMaps = 3; //total number of maps in the game
private const int levelIncrement = 5; //level-up difficulty increment
private int treasureFreq = 5; //lower treasureFreq will mean more treasures created
private int pointsToLevelUp = 20; //sets the number of points needed to get to the next level
public int score;
public int moves;
private int randomValue;
private int currentLevel;
//constructor
public Maze(int level)
{
Load(level);
}
//load the level from a file
private void Load(int levelNum)
{
currentLevel = levelNum;
string fileName = "level" + levelNum + ".txt";
StreamReader reader = new StreamReader(fileName);
for (int row = 0; row < SIZE; row++) //loop through the rows of the file
{
if (reader.EndOfStream) break; //error handling
string line = reader.ReadLine();
for (int col = 0; col < SIZE; col++) //loop through every char in the line
{
coords[col, row] = line[col];
}
}
reader.Close();
}
//draw the maze
public void Draw(Hero hero)
{
Header();
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
if (hero.GetX() == col && hero.GetY() == row)
{
AddNewTreasure();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("&");
Console.ForegroundColor = ConsoleColor.Gray;
}
else if (coords[col, row] == '$')
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("$");
Console.ForegroundColor = ConsoleColor.Gray;
}
else { Console.Write(coords[col, row]); }
}
Console.WriteLine();
}
DisplayGameInfo();
LevelUp(hero);
}
//deisplays the header
private void Header()
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Treasure Hunter ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("L: " + currentLevel);
Console.ForegroundColor = ConsoleColor.Gray;
}
//displays relevant game information
private void DisplayGameInfo()
{
//Console.ForegroundColor = ConsoleColor.Yellow;
//Console.WriteLine("Level: " + currentLevel);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\tTreasure: $" + score);
Console.ForegroundColor = ConsoleColor.Gray;
}
//check if a new treasure should spawn in a random spot
private void AddNewTreasure()
{
int numMoves = randNum.Next(0, treasureFreq);
int xCoord = randNum.Next(0, SIZE);
int yCoord = randNum.Next(0, SIZE);
if (PointIsAlsoFree(xCoord, yCoord) && moves == (moves + numMoves))
{
coords[xCoord, yCoord] = '$';
}
}
//checks if it's time to move to a new maze
public void LevelUp(Hero hero)
{
if (currentLevel < numMaps && score >= pointsToLevelUp)
{
TripleTone(1);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Moving up a level... Press any key to continue");
Console.ForegroundColor = ConsoleColor.Gray;
currentLevel += 1;
treasureFreq += levelIncrement; //treasures spawn less frequently in the next level
pointsToLevelUp *= 2; //raise the points needed to move up in the next level
hero.ResetHero();
Load(currentLevel);
}
else if (currentLevel >= numMaps && score >= pointsToLevelUp)
{
Console.Clear();
Console.Write("You won in ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(moves);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" moves with a final treasure value of ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(score);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("!");
Console.Write("Press [Enter] to close the game... ");
TripleTone(3);
Console.ReadLine();
Environment.Exit(0);
}
}
private void TripleTone(int n)
{
for (int i = 0; i < n; i++)
{
Console.Beep(1500, 150);
Console.Beep(1000, 150);
Console.Beep(1500, 150);
}
}
//returns true if x,y is in the grid, otherwise returns false
private Boolean PointInGrid(int x, int y)
{
if (x < 0 || x >= SIZE) return false;
if (y < 0 || y >= SIZE) return false;
return true;
}
//returns true if x,y is a valid position to move to
public Boolean PointIsFree(int x, int y)
{
if (!PointInGrid(x, y)) return false;
if (coords[x, y] == '*')
{
Console.Beep(370, 100);
return false;
}
if (coords[x, y] == '$')//checking to see if the player has moved onto a treasure
{
randomValue = randNum.Next(1, 6);//assigns a random value to the treasure
Console.Beep(32000, 100);
Console.Beep(32000, 100);
score += randomValue;
coords[x, y] = ' ';//clears the treasure once it's been collected
}
return true;
}
public Boolean PointIsAlsoFree(int x, int y)
{
if (!PointInGrid(x, y)) return false;
if (coords[x, y] == '*')
{
return false;
}
return true;
}
public int GetLevel() { return currentLevel; }
public int GetScore() { return score; }
public int GetMoves() { return moves; }
}
}