-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.vb
More file actions
650 lines (574 loc) · 23.7 KB
/
Application.vb
File metadata and controls
650 lines (574 loc) · 23.7 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
Imports System
Imports System.IO
Imports System.Convert
Module Module1
Public RNoGen As New Random()
Sub Main()
Dim ThisGame As New Breakthrough
ThisGame.PlayGame()
Console.ReadLine()
End Sub
Class Breakthrough
Private Deck As CardCollection
Private Hand As CardCollection
Private Sequence As CardCollection
Private Discard As CardCollection
Private Locks As List(Of Lock)
Private Score As Integer
Private GameOver As Boolean
Private CurrentLock As Lock
Private LockSolved As Boolean
Sub New()
Deck = New CardCollection("DECK")
Hand = New CardCollection("HAND")
Sequence = New CardCollection("SEQUENCE")
Discard = New CardCollection("DISCARD")
Score = 0
LoadLocks()
End Sub
Public Sub PlayGame()
Dim MenuChoice As String
If Locks.Count > 0 Then
GameOver = False
CurrentLock = New Lock()
SetupGame()
While Not GameOver
LockSolved = False
While Not LockSolved And Not GameOver
Console.WriteLine()
Console.WriteLine("Current score: " & Score)
Console.WriteLine(CurrentLock.GetLockDetails())
Console.WriteLine(Sequence.GetCardDisplay())
Console.WriteLine(Hand.GetCardDisplay())
MenuChoice = GetChoice()
Select Case MenuChoice
Case "D"
Console.WriteLine(Discard.GetCardDisplay())
Case "U"
Dim CardChoice As Integer = GetCardChoice()
Dim DiscardOrPlay As String = GetDiscardOrPlayChoice()
If DiscardOrPlay = "D" Then
MoveCard(Hand, Discard, Hand.GetCardNumberAt(CardChoice - 1))
GetCardFromDeck(CardChoice)
ElseIf DiscardOrPlay = "P" Then
PlayCardToSequence(CardChoice)
End If
End Select
If CurrentLock.GetLockSolved() Then
LockSolved = True
ProcessLockSolved()
End If
End While
GameOver = CheckIfPlayerHasLost()
End While
Else
Console.WriteLine("No locks in file.")
End If
End Sub
Private Sub ProcessLockSolved()
Score += 10
Console.WriteLine("Lock has been solved. Your score is now: " & Score)
While Discard.GetNumberOfCards() > 0
MoveCard(Discard, Deck, Discard.GetCardNumberAt(0))
End While
Deck.Shuffle()
CurrentLock = GetRandomLock()
End Sub
Private Function CheckIfPlayerHasLost() As Boolean
If Deck.GetNumberOfCards() = 0 Then
Console.WriteLine("You have run out of cards in your deck. Your final score is: " & Score)
Return True
Else
Return False
End If
End Function
Private Sub SetupGame()
Dim Choice As String
Console.Write("Enter L to load a game from a file, anything else to play a new game:> ")
Choice = Console.ReadLine().ToUpper()
If Choice = "L" Then
If Not LoadGame("game1.txt") Then
GameOver = True
End If
Else
CreateStandardDeck()
Deck.Shuffle()
For Count = 1 To 5
MoveCard(Deck, Hand, Deck.GetCardNumberAt(0))
Next
AddDifficultyCardsToDeck()
Deck.Shuffle()
CurrentLock = GetRandomLock()
End If
End Sub
Private Sub PlayCardToSequence(ByVal CardChoice As Integer)
If Sequence.GetNumberOfCards() > 0 Then
If Hand.GetCardDescriptionAt(CardChoice - 1)(0) <> Sequence.GetCardDescriptionAt(Sequence.GetNumberOfCards() - 1)(0) Then
Score += MoveCard(Hand, Sequence, Hand.GetCardNumberAt(CardChoice - 1))
GetCardFromDeck(CardChoice)
End If
Else
Score += MoveCard(Hand, Sequence, Hand.GetCardNumberAt(CardChoice - 1))
GetCardFromDeck(CardChoice)
End If
If CheckIfLockChallengeMet() Then
Console.WriteLine()
Console.WriteLine("A challenge on the lock has been met.")
Console.WriteLine()
Score += 5
End If
End Sub
Private Function CheckIfLockChallengeMet() As Boolean
Dim SequenceAsString As String = ""
For Count = Sequence.GetNumberOfCards() - 1 To Math.Max(0, Sequence.GetNumberOfCards() - 3) Step -1
If SequenceAsString.Length > 0 Then
SequenceAsString = ", " & SequenceAsString
End If
SequenceAsString = Sequence.GetCardDescriptionAt(Count) & SequenceAsString
If CurrentLock.CheckIfConditionMet(SequenceAsString) Then
Return True
End If
Next
Return False
End Function
Private Sub SetupCardCollectionFromGameFile(ByVal LineFromFile As String, ByVal CardCol As CardCollection)
Dim SplitLine As List(Of String)
Dim CardNumber As Integer
If LineFromFile.Length > 0 Then
SplitLine = LineFromFile.Split(",").ToList()
For Each Item In SplitLine
If Item.Length = 5 Then
CardNumber = ToInt32(Item(4))
Else
CardNumber = ToInt32(Item.Substring(4, 2))
End If
If Item.Substring(0, 3) = "Dif" Then
Dim CurrentCard As New DifficultyCard(CardNumber)
CardCol.AddCard(CurrentCard)
Else
Dim CurrentCard As New ToolCard(Item(0), Item(2), CardNumber)
CardCol.AddCard(CurrentCard)
End If
Next
End If
End Sub
Private Sub SetupLock(ByVal Line1 As String, ByVal Line2 As String)
Dim SplitLine As List(Of String)
SplitLine = Line1.Split(";").ToList()
For Each Item In SplitLine
Dim Conditions As New List(Of String)
Conditions = Item.Split(",").ToList()
CurrentLock.AddChallenge(Conditions)
Next
SplitLine = Line2.Split(";").ToList()
For Count = 0 To SplitLine.Count - 1
If SplitLine(Count) = "Y" Then
CurrentLock.SetChallengeMet(Count, True)
End If
Next
End Sub
Private Function LoadGame(ByVal FileName As String) As Boolean
Dim LineFromFile As String
Dim LineFromFile2 As String
Try
Using MyStream As New StreamReader(FileName)
LineFromFile = MyStream.ReadLine()
Score = ToInt32(LineFromFile)
LineFromFile = MyStream.ReadLine()
LineFromFile2 = MyStream.ReadLine()
SetupLock(LineFromFile, LineFromFile2)
LineFromFile = MyStream.ReadLine()
SetupCardCollectionFromGameFile(LineFromFile, Hand)
LineFromFile = MyStream.ReadLine()
SetupCardCollectionFromGameFile(LineFromFile, Sequence)
LineFromFile = MyStream.ReadLine()
SetupCardCollectionFromGameFile(LineFromFile, Discard)
LineFromFile = MyStream.ReadLine()
SetupCardCollectionFromGameFile(LineFromFile, Deck)
End Using
Return True
Catch
Console.WriteLine("File not loaded")
Return False
End Try
End Function
Private Sub LoadLocks()
Dim FileName As String = "locks.txt"
Dim LineFromFile As String
Dim Challenges As List(Of String)
Locks = New List(Of Lock)
Try
Using MyStream As New StreamReader(FileName)
LineFromFile = MyStream.ReadLine()
While Not LineFromFile Is Nothing
Challenges = LineFromFile.Split(";").ToList()
Dim LockFromFile As New Lock
For Each C In Challenges
Dim Conditions As New List(Of String)
Conditions = C.Split(",").ToList()
LockFromFile.AddChallenge(Conditions)
Next
Locks.Add(LockFromFile)
LineFromFile = MyStream.ReadLine()
End While
End Using
Catch
Console.WriteLine("File not loaded")
End Try
End Sub
Private Function GetRandomLock() As Lock
Return Locks(RNoGen.Next(0, Locks.Count))
End Function
Private Sub GetCardFromDeck(ByVal CardChoice As Integer)
If Deck.GetNumberOfCards() > 0 Then
If Deck.GetCardDescriptionAt(0) = "Dif" Then
Dim CurrentCard As Card = Deck.RemoveCard(Deck.GetCardNumberAt(0))
Console.WriteLine()
Console.WriteLine("Difficulty encountered!")
Console.WriteLine(Hand.GetCardDisplay())
Console.Write("To deal with this you need to either lose a key ")
Console.Write("(enter 1-5 to specify position of key) or (D)iscard five cards from the deck:> ")
Dim Choice As String = Console.ReadLine()
Console.WriteLine()
Discard.AddCard(CurrentCard)
CurrentCard.Process(Deck, Discard, Hand, Sequence, CurrentLock, Choice, CardChoice)
End If
End If
While Hand.GetNumberOfCards() < 5 And Deck.GetNumberOfCards() > 0
If Deck.GetCardDescriptionAt(0) = "Dif" Then
MoveCard(Deck, Discard, Deck.GetCardNumberAt(0))
Console.WriteLine("A difficulty card was discarded from the deck when refilling the hand.")
Else
MoveCard(Deck, Hand, Deck.GetCardNumberAt(0))
End If
End While
If Deck.GetNumberOfCards() = 0 And Hand.GetNumberOfCards() < 5 Then
GameOver = True
End If
End Sub
Private Function GetCardChoice() As Integer
Dim Choice As String
Dim Value As Integer
Do
Console.Write("Enter a number between 1 and 5 to specify card to use:> ")
Choice = Console.ReadLine()
Loop Until Integer.TryParse(Choice, Value)
Return Value
End Function
Private Function GetDiscardOrPlayChoice() As String
Dim Choice As String
Console.Write("(D)iscard or (P)lay?:> ")
Choice = Console.ReadLine().ToUpper()
Return Choice
End Function
Private Function GetChoice() As String
Console.WriteLine()
Console.Write("(D)iscard inspect, (U)se card:> ")
Dim Choice As String = Console.ReadLine().ToUpper()
Return Choice
End Function
Private Sub AddDifficultyCardsToDeck()
For Count = 1 To 5
Deck.AddCard(New DifficultyCard())
Next
End Sub
Private Sub CreateStandardDeck()
Dim NewCard As Card
For Count = 1 To 5
NewCard = New ToolCard("P", "a")
Deck.AddCard(NewCard)
NewCard = New ToolCard("P", "b")
Deck.AddCard(NewCard)
NewCard = New ToolCard("P", "c")
Deck.AddCard(NewCard)
Next
For Count = 1 To 3
NewCard = New ToolCard("F", "a")
Deck.AddCard(NewCard)
NewCard = New ToolCard("F", "b")
Deck.AddCard(NewCard)
NewCard = New ToolCard("F", "c")
Deck.AddCard(NewCard)
NewCard = New ToolCard("K", "a")
Deck.AddCard(NewCard)
NewCard = New ToolCard("K", "b")
Deck.AddCard(NewCard)
NewCard = New ToolCard("K", "c")
Deck.AddCard(NewCard)
Next
End Sub
Private Function MoveCard(ByVal FromCollection As CardCollection, ByVal ToCollection As CardCollection, ByVal CardNumber As Integer) As Integer
Dim Score As Integer = 0
If FromCollection.GetName() = "HAND" And ToCollection.GetName() = "SEQUENCE" Then
Dim CardToMove As Card = FromCollection.RemoveCard(CardNumber)
If CardToMove IsNot Nothing Then
ToCollection.AddCard(CardToMove)
Score = CardToMove.GetScore()
End If
Else
Dim CardToMove As Card = FromCollection.RemoveCard(CardNumber)
If CardToMove IsNot Nothing Then
ToCollection.AddCard(CardToMove)
End If
End If
Return Score
End Function
End Class
Class Challenge
Protected Condition As List(Of String)
Protected Met As Boolean
Sub New()
Met = False
End Sub
Public Function GetMet() As Boolean
Return Met
End Function
Public Function GetCondition() As List(Of String)
Return Condition
End Function
Public Sub SetMet(ByVal NewValue As Boolean)
Met = NewValue
End Sub
Public Sub SetCondition(ByVal NewCondition As List(Of String))
Condition = NewCondition
End Sub
End Class
Class Lock
Protected Challenges As New List(Of Challenge)
Public Overridable Sub AddChallenge(ByVal Condition As List(Of String))
Dim C As New Challenge
C.SetCondition(Condition)
Challenges.Add(C)
End Sub
Private Function ConvertConditionToString(ByVal C As List(Of String)) As String
Dim ConditionAsString As String = ""
For Pos = 0 To C.Count - 2
ConditionAsString &= C(Pos) & ", "
Next
ConditionAsString &= C(C.Count - 1)
Return ConditionAsString
End Function
Public Overridable Function GetLockDetails() As String
Dim LockDetails As String = Environment.NewLine & "CURRENT LOCK" & Environment.NewLine & "------------" & Environment.NewLine
For Each C In Challenges
If C.GetMet() Then
LockDetails &= "Challenge met: "
Else
LockDetails &= "Not met: "
End If
LockDetails &= ConvertConditionToString(C.GetCondition()) & Environment.NewLine
Next
LockDetails &= Environment.NewLine
Return LockDetails
End Function
Public Overridable Function GetLockSolved() As Boolean
For Each C In Challenges
If Not C.GetMet() Then
Return False
End If
Next
Return True
End Function
Public Overridable Function CheckIfConditionMet(ByVal Sequence As String) As Boolean
For Each C In Challenges
If Not C.GetMet() And Sequence = ConvertConditionToString(C.GetCondition()) Then
C.SetMet(True)
Return True
End If
Next
Return False
End Function
Public Overridable Sub SetChallengeMet(ByVal Pos As Integer, ByVal Value As Boolean)
Challenges(Pos).SetMet(Value)
End Sub
Public Overridable Function GetChallengeMet(ByVal Pos As Integer) As Boolean
Return Challenges(Pos).GetMet()
End Function
Public Overridable Function GetNumberOfChallenges() As Integer
Return Challenges.Count
End Function
End Class
Class Card
Protected CardNumber, Score As Integer
Protected Shared NextCardNumber As Integer = 1
Sub New()
CardNumber = NextCardNumber
NextCardNumber += 1
Score = 0
End Sub
Public Overridable Function GetScore() As Integer
Return Score
End Function
Public Overridable Sub Process(ByVal Deck As CardCollection, ByVal Discard As CardCollection, ByVal Hand As CardCollection, ByVal Sequence As CardCollection, ByVal CurrentLock As Lock, ByVal Choice As String, ByVal CardChoice As Integer)
End Sub
Public Overridable Function GetCardNumber() As Integer
Return CardNumber
End Function
Public Overridable Function GetDescription() As String
If CardNumber < 10 Then
Return " " & CardNumber.ToString()
Else
Return CardNumber.ToString()
End If
End Function
End Class
Class ToolCard
Inherits Card
Protected ToolType As String
Protected Kit As String
Sub New(ByVal T As String, ByVal K As String)
MyBase.New()
ToolType = T
Kit = K
SetScore()
End Sub
Public Sub New(ByVal T As String, ByVal K As String, ByVal CardNo As Integer)
ToolType = T
Kit = K
CardNumber = CardNo
SetScore()
End Sub
Private Sub SetScore()
Select Case ToolType
Case "K"
Score = 3
Case "F"
Score = 2
Case "P"
Score = 1
End Select
End Sub
Public Overrides Function GetDescription() As String
Return ToolType & " " & Kit
End Function
End Class
Class DifficultyCard
Inherits Card
Protected CardType As String
Sub New()
MyBase.New()
CardType = "Dif"
End Sub
Public Sub New(ByVal CardNo As Integer)
CardType = "Dif"
CardNumber = CardNo
End Sub
Public Overrides Function GetDescription() As String
Return CardType
End Function
Public Overrides Sub Process(ByVal Deck As CardCollection, ByVal Discard As CardCollection, ByVal Hand As CardCollection, ByVal Sequence As CardCollection, ByVal CurrentLock As Lock, ByVal Choice As String, ByVal CardChoice As Integer)
Dim ChoiceAsInteger As Integer
If Integer.TryParse(Choice, ChoiceAsInteger) Then
If ChoiceAsInteger >= 1 And ChoiceAsInteger <= 5 Then
If ChoiceAsInteger >= CardChoice Then
ChoiceAsInteger -= 1
End If
If ChoiceAsInteger > 0 Then
ChoiceAsInteger -= 1
End If
If Hand.GetCardDescriptionAt(ChoiceAsInteger)(0) = "K" Then
Dim CardToMove As Card = Hand.RemoveCard(Hand.GetCardNumberAt(ChoiceAsInteger))
Discard.AddCard(CardToMove)
Return
End If
End If
End If
Dim Count As Integer = 0
While Count < 5 And Deck.GetNumberOfCards() > 0
Dim CardToMove As Card = Deck.RemoveCard(Deck.GetCardNumberAt(0))
Discard.AddCard(CardToMove)
Count += 1
End While
End Sub
End Class
Class CardCollection
Protected Cards As New List(Of Card)
Protected Name As String
Sub New(ByVal N As String)
Name = N
End Sub
Public Function GetName() As String
Return Name
End Function
Public Function GetCardNumberAt(ByVal X As Integer) As Integer
Return Cards(X).GetCardNumber()
End Function
Public Function GetCardDescriptionAt(ByVal X As Integer) As String
Return Cards(X).GetDescription()
End Function
Public Sub AddCard(ByVal C As Card)
Cards.Add(C)
End Sub
Public Function GetNumberOfCards() As Integer
Return Cards.Count
End Function
Public Sub Shuffle()
Dim TempCard As Card
Dim RNo1, RNo2 As Integer
For Count = 1 To 10000
RNo1 = RNoGen.Next(0, Cards.Count)
RNo2 = RNoGen.Next(0, Cards.Count)
TempCard = Cards(RNo1)
Cards(RNo1) = Cards(RNo2)
Cards(RNo2) = TempCard
Next
End Sub
Public Function RemoveCard(ByVal CardNumber As Integer) As Card
Dim CardFound As Boolean = False
Dim Pos As Integer = 0
Dim CardToGet As Card = Nothing
While Pos < Cards.Count And Not CardFound
If Cards(Pos).GetCardNumber() = CardNumber Then
CardToGet = Cards(Pos)
CardFound = True
Cards.RemoveAt(Pos)
End If
Pos += 1
End While
Return CardToGet
End Function
Private Function CreateLineOfDashes(ByVal Size As Integer) As String
Dim LineOfDashes As String = ""
For Count = 1 To Size
LineOfDashes &= "------"
Next
Return LineOfDashes
End Function
Public Function GetCardDisplay() As String
Dim CardDisplay As String = Environment.NewLine & Name & ":"
If Cards.Count = 0 Then
Return CardDisplay & " empty" & Environment.NewLine & Environment.NewLine
Else
CardDisplay &= Environment.NewLine & Environment.NewLine
End If
Dim LineOfDashes As String
Const CardsPerLine As Integer = 10
If Cards.Count > CardsPerLine Then
LineOfDashes = CreateLineOfDashes(CardsPerLine)
Else
LineOfDashes = CreateLineOfDashes(Cards.Count)
End If
CardDisplay &= LineOfDashes & Environment.NewLine
Dim Complete As Boolean = False
Dim Pos As Integer = 0
While Not Complete
CardDisplay &= "| " & Cards(Pos).GetDescription() & " "
Pos += 1
If Pos Mod CardsPerLine = 0 Then
CardDisplay &= "|" & Environment.NewLine & LineOfDashes & Environment.NewLine
End If
If Pos = Cards.Count Then
Complete = True
End If
End While
If Cards.Count Mod CardsPerLine > 0 Then
CardDisplay &= "|" & Environment.NewLine
If Cards.Count > CardsPerLine Then
LineOfDashes = CreateLineOfDashes(Cards.Count Mod CardsPerLine)
End If
CardDisplay &= LineOfDashes & Environment.NewLine
End If
Return CardDisplay
End Function
End Class
End Module