-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cs
251 lines (221 loc) · 7.17 KB
/
Board.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
namespace Swingy
{
public class Board
{
private int[,] GameBoard;
public int Size { get; set; }
private Hero Hero;
private List<Character> Enemies;
public Board(int size)
{
GameBoard = new int[size, size];
this.Size = size;
}
/*
* Initialise the 2D map
*/
public int[,] InitBoard()
{
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
GameBoard[i, j] = 0;
}
}
return GameBoard;
}
/*
* Place hero on the specified position on the map
*/
public void PlaceHero(Hero hero, int pos)
{
this.Hero = hero;
//Mark the hero position
GameBoard[pos, pos] = 1;
this.Hero.XPosition = pos;
this.Hero.YPosition = pos;
}
/*
* Randomly spread villians over the map
*/
public void SpreadVilians()
{
Enemies = new List<Character>();
Random rnd = new Random();
int randInt = -1;
Character enemy;
string name;
//Iterate through the map
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
//If hero hasn,t been placed on the current position
if (GameBoard[i, j] != 1)
{
//Random integer that decides based on a 1/6 chance whether to place a villian on a current position
randInt = rnd.Next(6);
//Only place villian when randInt is 0
if (randInt == 0)
{
//Generate another random integer to help decide on which villian to place
randInt = rnd.Next(10);
switch (randInt)
{
case 0:
name = "Superbat";
break;
case 1:
case 4:
name = "Ghost";
break;
default:
name = "Demon";
break;
}
//Initialize villian for the current position
enemy = new Character();
enemy.Name = name;
enemy.XPosition = i;
enemy.YPosition = j;
//Create villian, add it to the list of villians and mark it's position on the map
CreateEnemy(enemy);
Enemies.Add(enemy);
GameBoard[i, j] = 2;
}
}
}
}
}
/*
* Create the specified villian and initialise its stats
*/
private void CreateEnemy(Character enemy)
{
int attack, defense, hitPoints;
//Assign stats to the stats based on the name
switch (enemy.Name)
{
case "Demon":
attack = 15;
defense = 3;
hitPoints = 50;
break;
case "Ghost":
attack = 25;
defense = 5;
hitPoints = 100;
break;
default:
attack = 40;
defense = 10;
hitPoints = 150;
break;
}
//Initialise the villian with the assigned stats
enemy.Attack = attack;
enemy.Defense = defense;
enemy.HitPoints = hitPoints;
}
/*
* Change the hero position to the specified coordinates and mark the map based on whether it's a new position or the previous one
*/
public void ChangeHeroPos(int x, int y, bool rev)
{
//If it's the previous position, place back the villian else mark it empty
if (rev)
GameBoard[Hero.XPosition, Hero.YPosition] = 2;
else
GameBoard[Hero.XPosition, Hero.YPosition] = 0;
//Mark the new hero position
GameBoard[x, y] = 1;
//Save the hero's current position as the previous
Hero.XPrevious = Hero.XPosition;
Hero.YPrevious = Hero.YPosition;
Hero.XPosition = x;
Hero.YPosition = y;
}
/*
* Print the map with all the villians and the hero
*/
public void PrintBoard()
{
char icon;
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
switch (GameBoard[j, i])
{
case 1:
icon = 'H';
break;
case 2:
icon = EnemyIcon(j, i);
break;
case 3:
icon = 'X';
break;
default:
icon = '.';
break;
}
Console.Write(icon);
}
Console.WriteLine();
}
}
/*
* Return the character that represents the villian based on the name
*/
private char EnemyIcon(int x, int y)
{
Character enemy = GetEnemy(x, y);
char eChar;
switch (enemy.Name)
{
case "Ghost":
eChar = 'G';
break;
case "Superbat":
eChar = 'S';
break;
default:
eChar = 'V';
break;
}
return eChar;
}
/*
* Return the villian on the specified position
*/
public Character GetEnemy(int x, int y)
{
foreach (Character E in Enemies)
{
if (E.XPosition == x && E.YPosition == y)
return E;
}
//No villian encountered on the specified position
return null;
}
/*
* Mark the specified position with X
*/
public void MarkBattle(int xPos, int yPos)
{
//3 is for character X
GameBoard[xPos, yPos] = 3;
}
/*
* Remove the defeated villian from the enemy list
*/
public void KillEnemy(Character e)
{
Enemies.Remove(e);
}
}
}