repl.it
@ThomasAFaulkner/

Pre Release

C#

No description

fork
loading
Files
  • main.cs
  • main.exe
main.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Skeleton Program code for the AQA AS Unit 1 SAM
// this code should be used in conjunction with the Preliminary Material
// written by the AQA COMP1 Programmer Team
// developed in the Visual C# 2008 Express programming environment

// Version 1.1

using System;
using System.IO;

namespace CSPreASSkelton
{
    class Program
    {
        public const int NO_OF_TRAPS = 2;
        public const int N_S_DISTANCE = 5;
        public const int W_E_DISTANCE = 7;

        public struct CellReference
        {
            public int NoOfCellsSouth;
            public int NoOfCellsEast;
        }

        static void Main(string[] args)
        {
            char[,] Cavern = new char[N_S_DISTANCE, W_E_DISTANCE];
            int Choice = 0;
            Boolean MonsterAwake = false;
            CellReference FlaskPosition = new CellReference();
            CellReference MonsterPosition = new CellReference();
            CellReference PlayerPosition = new CellReference();
            CellReference[] TrapPositions = new CellReference[NO_OF_TRAPS];
            while (Choice != 9)
            {
                DisplayMenu();
                Choice = GetMainMenuChoice();
                switch (Choice)
                {
                    case 1:
                        SetUpGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake, true);
                        PlayGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake);
                        break;
                    case 2:
                        LoadGame(TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake);
                        SetUpGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake, false);
                        PlayGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake);
                        break;
                    case 3:
                        SaveGame(TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
                        break;
                    case 4:
                        SetUpTrainingGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake);
                        PlayGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake);
                        break;
                }
            }
        }

        public static void DisplayMenu()
        {
            Console.WriteLine("MAIN MENU");
            Console.WriteLine();
            Console.WriteLine("1.  Start new game");
            Console.WriteLine("2.  Load game");
            Console.WriteLine("3.  Save game");
            Console.WriteLine("4.  Play training game");
            Console.WriteLine("9.  Quit");
            Console.WriteLine();
            Console.WriteLine("Please enter your choice: ");
        }

        public static int GetMainMenuChoice()
        {
            int Choice;
            Choice = int.Parse(Console.ReadLine()); // Takes the main menu choice and                                                  performs the action described.
            Console.WriteLine();
            return Choice;
        }

        public static void ResetCavern(char[,] Cavern)
        {
            int Count1;
            int Count2;
            for (Count1 = 0; Count1 < N_S_DISTANCE; Count1++)    // Resets the Board 
            {
                for (Count2 = 0; Count2 < W_E_DISTANCE; Count2++)
                {
                    Cavern[Count1, Count2] = ' ';
                }
            }
        }

        public static CellReference GetNewRandomPosition()
        {
            CellReference Position = new CellReference();
            Random rnd = new Random();
            do
            {
                // a random coordinate of (0,0) needs to be rejected as this is the starting position of the player
                Position.NoOfCellsSouth = rnd.Next(0, N_S_DISTANCE); 
                Position.NoOfCellsEast = rnd.Next(0, W_E_DISTANCE);
            } while (Position.NoOfCellsSouth == 0 && Position.NoOfCellsEast == 0);
            return Position;
        }
                
        public static void SetPositionOfItem(char[,] Cavern, ref CellReference ObjectPosition, char Item, Boolean NewGame)
        {
            CellReference Position = new CellReference();
            if (NewGame && Item != '*')
            {
                do   //Randomises the location of the item to be collected by the player
                {
                    Position = GetNewRandomPosition();
                } while (Cavern[Position.NoOfCellsSouth, Position.NoOfCellsEast] != ' ');
                ObjectPosition = Position;
            }
            Cavern[ObjectPosition.NoOfCellsSouth, ObjectPosition.NoOfCellsEast] = Item;
        }

        public static void SetUpGame(char[,] Cavern, CellReference[] TrapPositions, ref CellReference MonsterPosition,
                     ref CellReference PlayerPosition, ref CellReference FlaskPosition, ref bool MonsterAwake, bool NewGame)
        {
            int Count;
            ResetCavern(Cavern);
            if (NewGame)
            {
                PlayerPosition.NoOfCellsSouth = 0;
                PlayerPosition.NoOfCellsEast = 0; //Sets up the positions of the traps
                MonsterAwake = false;
            }
            for (Count = 0; Count < NO_OF_TRAPS; Count++)
            {
                SetPositionOfItem(Cavern, ref TrapPositions[Count], 'T', NewGame);
            }
            SetPositionOfItem(Cavern, ref MonsterPosition, 'M', NewGame);
            SetPositionOfItem(Cavern, ref FlaskPosition, 'F', NewGame);
            SetPositionOfItem(Cavern, ref PlayerPosition, '*', NewGame);
        }

        public static void SetUpTrainingGame(char[,] Cavern, CellReference[] TrapPositions, ref CellReference MonsterPosition, ref CellReference PlayerPosition, ref CellReference FlaskPosition, ref Boolean MonsterAwake)
        {
            ResetCavern(Cavern);
            PlayerPosition.NoOfCellsSouth = 2;
            PlayerPosition.NoOfCellsEast = 4;
            MonsterAwake = false;
            TrapPositions[0].NoOfCellsSouth = 1;
            TrapPositions[0].NoOfCellsEast = 6; // Sets up the Training game with pre-defined variables
            TrapPositions[1].NoOfCellsSouth = 3;
            TrapPositions[1].NoOfCellsEast = 4;
            MonsterPosition.NoOfCellsSouth = 0;
            MonsterPosition.NoOfCellsEast = 3;
            FlaskPosition.NoOfCellsSouth = 4;
            FlaskPosition.NoOfCellsEast = 5;
            SetUpGame(Cavern, TrapPositions, ref MonsterPosition, ref PlayerPosition, ref FlaskPosition, ref MonsterAwake, false);
        }

        public static void LoadGame(CellReference[] TrapPositions, ref CellReference MonsterPosition, ref CellReference PlayerPosition, ref CellReference FlaskPosition, ref Boolean MonsterAwake)
        {
            string Filename;
            BinaryReader br;
            Console.Write("Enter the name of the file to load: ");
            Filename = Console.ReadLine();
            Console.WriteLine();
            br = new BinaryReader(new FileStream(Filename, FileMode.Open));
            TrapPositions[0].NoOfCellsSouth = br.ReadInt32();
            TrapPositions[0].NoOfCellsEast = br.ReadInt32();
            TrapPositions[1].NoOfCellsSouth = br.ReadInt32();  //Loads the file from a pre-defined set of variables                                                                        in a text file.
            TrapPositions[1].NoOfCellsEast = br.ReadInt32();
            MonsterPosition.NoOfCellsSouth = br.ReadInt32();
            MonsterPosition.NoOfCellsEast = br.ReadInt32();
            PlayerPosition.NoOfCellsSouth = br.ReadInt32();
            PlayerPosition.NoOfCellsEast = br.ReadInt32();
            FlaskPosition.NoOfCellsSouth = br.ReadInt32();
            FlaskPosition.NoOfCellsEast = br.ReadInt32();
            MonsterAwake = br.ReadBoolean();
            br.Close();
        }

        public static void SaveGame(CellReference[] TrapPositions, CellReference MonsterPosition, CellReference PlayerPosition, CellReference FlaskPosition, Boolean MonsterAwake)
        {
            string Filename;
            BinaryWriter bw;
            Console.Write("Enter new file name: ");
            Filename = Console.ReadLine();
            Console.WriteLine();
            bw = new BinaryWriter(new FileStream(Filename, FileMode.Create));
            bw.Write(TrapPositions[0].NoOfCellsSouth);
            bw.Write(TrapPositions[0].NoOfCellsEast);
            bw.Write(TrapPositions[1].NoOfCellsSouth);
            bw.Write(TrapPositions[1].NoOfCellsEast); //Saves the game to a txt file with all the variables stored
            bw.Write(MonsterPosition.NoOfCellsSouth);
            bw.Write(MonsterPosition.NoOfCellsEast);
            bw.Write(PlayerPosition.NoOfCellsSouth);
            bw.Write(PlayerPosition.NoOfCellsEast);
            bw.Write(FlaskPosition.NoOfCellsSouth);
            bw.Write(FlaskPosition.NoOfCellsEast);
            bw.Write(MonsterAwake);
            bw.Close();
        }

        public static void DisplayCavern(char[,] Cavern, Boolean MonsterAwake)
        {
            int Count1;
            int Count2;
            for (Count1 = 0; Count1 < N_S_DISTANCE; Count1++)
            {
                Console.WriteLine(" ------------- ");
                for (Count2 = 0; Count2 < W_E_DISTANCE; Count2++)
                {
                    if (Cavern[Count1, Count2] == ' ' || Cavern[Count1, Count2] == '*' || ((Cavern[Count1, Count2] == 'M') && MonsterAwake))
                    {
                        Console.Write("|" + Cavern[Count1, Count2]); //Coordinates moved by the player.
                    }
                    else
                    {
                        Console.Write("| ");
                    }
                }
                Console.WriteLine("|");
            }
            Console.WriteLine(" ------------- ");
            Console.WriteLine();
        }

        public static void DisplayMoveOptions()
        {
            Console.WriteLine();
            Console.WriteLine("Enter N to move NORTH");
            Console.WriteLine("Enter E to move EAST");
            Console.WriteLine("Enter S to move SOUTH"); //Displays the available moves for the player after the board has been displayed.
            Console.WriteLine("Enter W to move WEST");
            Console.WriteLine("Enter M to return to the Main Menu");
            Console.WriteLine();
        }

        public static char GetMove()
        {
            char Move;
            Move = char.Parse(Console.ReadLine()); //takes the value of the readline and assigns it to the players next move
            Console.WriteLine();
            return Move;
        }

        public static void MakeMove(char[,] Cavern, char Direction, ref CellReference PlayerPosition)
        {
            Cavern[PlayerPosition.NoOfCellsSouth, PlayerPosition.NoOfCellsEast] = ' ';
            switch (Direction)
            {
                case 'N':
                    PlayerPosition.NoOfCellsSouth = PlayerPosition.NoOfCellsSouth - 1;
                    break;
                case 'S':
                    PlayerPosition.NoOfCellsSouth = PlayerPosition.NoOfCellsSouth + 1;
                    break;
                case 'W':  //makes the move by the player and changes the coordinate of the player
                    PlayerPosition.NoOfCellsEast = PlayerPosition.NoOfCellsEast - 1;
                    break;
                case 'E':
                    PlayerPosition.NoOfCellsEast = PlayerPosition.NoOfCellsEast + 1;
                    break;
            }
            Cavern[PlayerPosition.NoOfCellsSouth, PlayerPosition.NoOfCellsEast] = '*';
        }

        public static Boolean CheckValidMove(CellReference PlayerPosition, char Direction)
        {
            Boolean ValidMove;
            ValidMove = true;
            if (!(Direction == 'N' || Direction == 'S' || Direction == 'W' || Direction == 'E' || Direction == 'M'))
            {  //This checks to see if the move made by the player is valid and can be done. If valid, the move is made.
                ValidMove = false;
            }
            return ValidMove;
        }

        public static Boolean CheckIfSameCell(CellReference FirstCellPosition, CellReference SecondCellPosition)
        {
            Boolean InSameCell = false;
            if (FirstCellPosition.NoOfCellsSouth == SecondCellPosition.NoOfCellsSouth && FirstCellPosition.NoOfCellsEast == SecondCellPosition.NoOfCellsEast)
            {
                InSameCell = true; 
            }
            return InSameCell;
        }

        public static void DisplayWonGameMessage()
        {
            Console.WriteLine("Well Done!  You have found the flask containing the Styxian potion.");
            Console.WriteLine("You have won the game of MONSTER!");
            Console.WriteLine();  //Displays the message announcing the player has won the game.
        }

        public static void DisplayTrapMessage()
        {
            Console.WriteLine("Oh no!  You have set off a trap.  Watch out, the monster is now awake!");
            Console.WriteLine(); //Announces to the player that a trap has been set off.
        }

        public static void MoveFlask(char[,] Cavern, CellReference NewCellForFlask, ref CellReference FlaskPosition)
        {
            Cavern[NewCellForFlask.NoOfCellsSouth, NewCellForFlask.NoOfCellsEast] = 'F';
            Cavern[FlaskPosition.NoOfCellsSouth, FlaskPosition.NoOfCellsEast] = ' '; // Moves the flask each time the game begins.
            FlaskPosition = NewCellForFlask;
        }

        public static void MakeMonsterMove(char[,] Cavern, ref CellReference MonsterPosition, ref CellReference FlaskPosition, CellReference PlayerPosition)
        {
            CellReference OriginalMonsterPosition = new CellReference();
            Boolean MonsterMovedToSameCellAsFlask = false;
            OriginalMonsterPosition.NoOfCellsSouth = MonsterPosition.NoOfCellsSouth;
            OriginalMonsterPosition.NoOfCellsEast = MonsterPosition.NoOfCellsEast;
            Cavern[MonsterPosition.NoOfCellsSouth, MonsterPosition.NoOfCellsEast] = ' ';
            if (MonsterPosition.NoOfCellsSouth < PlayerPosition.NoOfCellsSouth)
            {
                MonsterPosition.NoOfCellsSouth = MonsterPosition.NoOfCellsSouth + 1;
            }
            else if (MonsterPosition.NoOfCellsSouth > PlayerPosition.NoOfCellsSouth)
            {
                MonsterPosition.NoOfCellsSouth = MonsterPosition.NoOfCellsSouth - 1; //makes the monster move each time a player moves in the game
            }
            else if (MonsterPosition.NoOfCellsEast < PlayerPosition.NoOfCellsEast)
            {
                MonsterPosition.NoOfCellsEast = MonsterPosition.NoOfCellsEast + 1;
            }
            else if (MonsterPosition.NoOfCellsEast > PlayerPosition.NoOfCellsEast)
            {
                MonsterPosition.NoOfCellsEast = MonsterPosition.NoOfCellsEast - 1;
            }
            MonsterMovedToSameCellAsFlask = CheckIfSameCell(MonsterPosition, FlaskPosition);
            if (MonsterMovedToSameCellAsFlask)
            {
                MoveFlask(Cavern, OriginalMonsterPosition, ref FlaskPosition);
            }
            Cavern[MonsterPosition.NoOfCellsSouth, MonsterPosition.NoOfCellsEast] = 'M';
        }

        public static void DisplayLostGameMessage()
        {
            Console.WriteLine("ARGHHHHHH!  The monster has eaten you.  GAME OVER.");
            Console.WriteLine("Maybe you will have better luck the next time you play MONSTER!");
            Console.WriteLine(); //Announces to the player that they have lost the game.
        }
        
        public static void PlayGame(char[,] Cavern, CellReference[] TrapPositions, ref CellReference MonsterPosition, ref CellReference PlayerPosition, ref CellReference FlaskPosition, ref Boolean MonsterAwake)
        {
            Boolean Eaten = false;
            Boolean FlaskFound = false;
            Boolean ValidMove = false;
            int Count = 0;
            char MoveDirection = ' ';
            DisplayCavern(Cavern, MonsterAwake);
            while (!(Eaten || FlaskFound || MoveDirection == 'M'))
            {
                ValidMove = false;
                while (!ValidMove) //sets the variables and begins the game.
                {
                    DisplayMoveOptions();
                    MoveDirection = GetMove();
                    ValidMove = CheckValidMove(PlayerPosition, MoveDirection);
                }
                if (MoveDirection != 'M')
                {
                    MakeMove(Cavern, MoveDirection, ref PlayerPosition);
                    DisplayCavern(Cavern, MonsterAwake);
                    FlaskFound = CheckIfSameCell(PlayerPosition, FlaskPosition);
                    if (FlaskFound)
                    {
                        DisplayWonGameMessage();
                    }
                    Eaten = CheckIfSameCell(MonsterPosition, PlayerPosition);
                    if (!MonsterAwake && !FlaskFound && !Eaten)
                    {
                        MonsterAwake = CheckIfSameCell(PlayerPosition, TrapPositions[0]);
                        if (!MonsterAwake)
                        {
                            MonsterAwake = CheckIfSameCell(PlayerPosition, TrapPositions[1]);
                        }
                        if (MonsterAwake)
                        {
                            DisplayTrapMessage();
                            DisplayCavern(Cavern, MonsterAwake);
                        }
                    }
                    if (MonsterAwake && !Eaten && !FlaskFound)
                    {
                        Count = 0;
                        while (Count < 2 && !Eaten)
                        {
                            MakeMonsterMove(Cavern, ref MonsterPosition, ref FlaskPosition, PlayerPosition);
                            Eaten = CheckIfSameCell(MonsterPosition, PlayerPosition);
                            Console.WriteLine();
                            Console.WriteLine("Press Enter key to continue");
                            Console.ReadLine();
                            DisplayCavern(Cavern, MonsterAwake);
                            Count = Count + 1;
                        }
                    }
                    if (Eaten)
                    {
                        DisplayLostGameMessage();
                    }
                }
            }
        }

    }
}






//Moving diagonally e.g. NE, NW

// Validation on valid move

//Tracking number of moves 

//Calculate distance between player and monster

//Add a scoring system

//Scan for a trap (e.g. 1 block in all directions from player) 

//Add rocks into some spaces (these cannot be entered)

//Add a new item “spawn new monster”

?