-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cs
More file actions
186 lines (163 loc) · 4.92 KB
/
Copy pathMap.cs
File metadata and controls
186 lines (163 loc) · 4.92 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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Assignment6
{
public class Map : IEnumerable
{
public object[,] map;
public Map(int w, int h)
{
map = new object[w, h];
for(int x = 0; x < w; x++)
{
for(int y = 0; y > h; y++)
{
map[x, y] = null;
}
}
}
//Fill in monsters or item or null with position info, player initial position[0,0] is null
public void FillIn(int monAmt, int bombAmt, int potionAmt)
{
Random posX = new Random();
Random posY = new Random();
int monOnMap = 0;
int bombOnMap = 0;
int potionOnMap = 0;
//Fill in Monster
for (int i = 0; i < map.Length; i++)
{
int x = posX.Next(1, map.GetLength(0));//Generate random position x
int y = posY.Next(1, map.GetLength(1));//Generate random position y
if (monOnMap == monAmt)
{
break;
}
else
{
if (map[x, y] == null)
{
Monster mon = new Monster();
map[x, y] = mon;
monOnMap += 1;
Console.WriteLine("AddMon");
}
}
}
//Fill in Bomb
for(int i = 0; i < map.Length; i++)
{
int x = posX.Next(1, map.GetLength(0));//Generate random position x
int y = posY.Next(1, map.GetLength(1));//Generate random position y
if (bombOnMap == bombAmt)
{
break;
}
else
{
if (map[x, y] == null)
{
Item bomb = new Item(Item.bomb);
map[x, y] = bomb;
bombOnMap += 1;
Console.WriteLine("AddBomb");
}
}
}
//Fill in Potion
for(int i = 0; i < map.Length; i++)
{
int x = posX.Next(1, map.GetLength(0));//Generate random position x
int y = posY.Next(1, map.GetLength(1));//Generate random position y
if (potionOnMap == potionAmt)
{
break;
}
else
{
if (map[x, y] == null)
{
Item potion = new Item(Item.potion);
map[x, y] = potion;
potionOnMap += 1;
Console.WriteLine("AddPotion");
}
}
}
//Fill in int 0 while null, prevents "NullReferenceException"
for(int i = 0; i < map.GetLength(0); i++)
{
int a = map.GetLength(1);
for(int j = 0; j < map.GetLength(1); j++)
{
if(map[i, j] == null)
{
map[i, j] = 0;
Console.WriteLine("Set0");
}
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public MapEnum GetEnumerator()
{
return new MapEnum(map);
}
}
//Implement IEnumerator of Map
public class MapEnum : IEnumerator
{
public object[,] map;
//player initial position
int posX = 0;
int posY = 0;
public MapEnum(object[,] objList)
{
map = objList;
}
//move on x dir
public bool MoveNext()
{
posX += 1;
Console.WriteLine($"{posX}, {posY}");
return (posX < map.GetLength(0) && posY < map.GetLength(1));
}
//move on y dir
public bool MoveDown()
{
posY += 1;
Console.WriteLine($"{posX}, {posY}");
return (posX < map.GetLength(0) && posY < map.GetLength(1));
}
public void Reset()
{
posX = 0;
posY = 0;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public object Current
{
get
{
try
{
return map[posX, posY];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}