-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameplay.vb
614 lines (515 loc) · 20.2 KB
/
Gameplay.vb
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
'Gameplay is the main class for the game.
Public Class Gameplay
' Array of dice objects
Private diceArray() As Dice
' Array of player objects
Private players() As Player
' Is game forced or not
Private isForced As Boolean
' Is game maxi or normal
Private isMaxi As Boolean
' Scoreboard for game
Public ScoreBoardTable As ScoreBoardControll
' Index to current player having turn
Private currentPlayer As Integer
' Each dice have their own index. Example: If two dices with value two exist than currentDiceScore(1) has value 2.
Private currentDiceScore As Integer()
' Rolls left in turn
Private rollsLeft As Integer
' Are scoreboard active
Private scoreBoardActive As Boolean
' Are event for holding dice active
Private dicesActive As Boolean
' Amount of rows in scoreboard
Private rows As Integer
' Bonus limit
Private bonusLimit As Integer
' Bonus score
Private bonusScore As Integer
' Amount of round played
Private roundPlayed As Integer
' Counts each scorecategory
Private forcedCount As Integer
' True if game is finished
Private gameIsFinished As Boolean
' Constructor with important arguments for new game.
Public Sub New(ByVal dicePicBoxes As PictureBox(), ByVal playersName As String(), ByVal isForced As Boolean, ByVal isMaxi As Boolean, ByVal ScoreBoardControll As ScoreBoardControll)
Me.isForced = isForced
Me.isMaxi = isMaxi
Me.ScoreBoardTable = ScoreBoardControll
gameIsFinished = False
rows = ScoreBoardTable.table.RowCount - 1
bonusLimit = 63
If isMaxi Then
bonusScore = 100
If Not isForced Then
bonusLimit = 84
End If
Else
bonusScore = 50
End If
'Init dice objects and store Pictureboxes for dices in them.
initDices(dicePicBoxes)
'Init player objects and store players name in them.
initPlayers(playersName)
'Choose next turn. True as arugement means its first turn in game.
nextTurn(True)
End Sub
' Get ready for next turn
Private Sub nextTurn(ByRef isFirstTurn As Boolean)
If isFirstTurn Then
'First player in players(0) starts playing. If we use -1 as currentplayer next player will be 0.
currentPlayer = 0
roundPlayed += 1
forcedCount += 1
ElseIf currentPlayer + 1 < players.Length
currentPlayer = currentPlayer + 1
Else
' Check if game is finished
isGameFinished()
currentPlayer = 0
roundPlayed += 1
forcedCount += 1
End If
If Not gameIsFinished Then
'Remove hold from all dices
resetDiceHold()
'Highlight current player
ScoreBoardTable.MarkCurrentPlayer(currentPlayer)
'Deactivate scoreboard before first roll
scoreBoardActive = False
'Deactivate dices before first roll
dicesActive = False
rollsLeft = 3
End If
End Sub
' Function for initialize player objects
Private Sub initPlayers(ByVal playersName As String())
ReDim players(playersName.Length - 1)
'Loop through players name and use them for arguments for new player objects.
For i = 0 To playersName.Length - 1
Dim scoreBoard(rows, 1) As Integer
players(i) = New Player(playersName(i), scoreBoard)
Next i
ScoreBoardTable.setPlayerNames(playersName)
End Sub
' Function for init dices
Private Sub initDices(ByVal dicePicBoxes As PictureBox())
ReDim diceArray(dicePicBoxes.Length - 1)
' Redim with 5 because both normal and maxi have highest possible dice score as 6
ReDim currentDiceScore(5)
'Loop through Pictureboxes for dices and use them as arguments for dice objects.
For i = 0 To dicePicBoxes.Length - 1
diceArray(i) = New Dice(dicePicBoxes(i))
Next i
End Sub
' Change dice color when mouse enter or leave
Public Sub ChangeDiceColor(ByVal sender As Object, ByVal shouldChange As Boolean)
'Cast sender object to PictureBox
Dim thisPictureBox As PictureBox = CType(sender, PictureBox)
'Get tagName from PictureBox. Tagnames are 1,2,3,4,5 and 6
Dim tagName As String = thisPictureBox.Tag
'Tagname is string and we need to convert to 32 bit integer
Dim tagNameTwo As Integer = Convert.ToInt32(thisPictureBox.Tag)
'Check if current dice is at hold or not.
If GetDiceHold(tagNameTwo - 1) = False Then
'If its not at hold we change color.
diceArray(tagNameTwo - 1).ChangeColor(shouldChange)
End If
End Sub
'This function is called from YatzyForm when Roll button are clicked
Public Sub Roll()
'Set currentDiceScore values back to default value 0
Array.Clear(currentDiceScore, 0, currentDiceScore.Length)
'Scoreboard is active after first roll
scoreBoardActive = True
rollsLeft -= 1
'Loop through all dice objects and roll them!
For i = 0 To diceArray.Length - 1
diceArray(i).Roll()
Dim diceValue As Integer = diceArray(i).DiceValue
currentDiceScore(diceValue - 1) += 1
Next i
'Change hold dices back to normal color if its last roll
' If rollsLeft = 0 Then
' resetDiceHold()
'End If
If rollsLeft < 1 Then
dicesActive = False
If isForced Then
forcedChoose()
End If
Else
dicesActive = True
End If
End Sub
' Dice hold is reset after each players turn
Private Sub resetDiceHold()
For i = 0 To diceArray.Length - 1
diceArray(i).DiceHold = False
diceArray(i).ChangeColor(False)
Next i
End Sub
' This is called when player click submit in forced game
Public Sub forcedChoose()
'Skip sum and bonus
If forcedCount = 7 Then
forcedCount = 9
End If
Dim textLabel As Label = ScoreBoardTable.table.GetControlFromPosition(0, forcedCount)
Dim tagNameTwo As Integer = Convert.ToInt32(textLabel.Tag)
checkScore(tagNameTwo, forcedCount)
End Sub
'This function is used when dice Pictureboxes are clicked. Toggle between hold and unhold.
Public Sub ToggleDiceHold(sender As Object)
'Cast sender object to PictureBox
Dim thisPictureBox As PictureBox = CType(sender, PictureBox)
'Tagname is string and we need to convert to 32 bit integer
Dim tagName As Integer = Convert.ToInt32(thisPictureBox.Tag)
diceArray(tagName - 1).ToggleHold()
End Sub
' Check if score combination choosed by player are valid. If not score combination is stroked
Function checkScore(ByVal scoreTag As Integer, ByVal scoreIndexRow As Integer)
' Return false if score combination was invalid
Dim isAllowed As Boolean = False
Dim currentP As Player = players(currentPlayer)
' Test if its allowed to set current score
If currentP.isScoreValid(scoreIndexRow) Then
Dim calcScore As Integer = calculateScore(scoreTag)
currentP.updateScore(scoreIndexRow, calcScore)
ScoreBoardTable.upDatePlayersScore(currentPlayer, currentP.playersScore)
' Check if its bonustime
checkBonus()
' Next players turn
nextTurn(False)
isAllowed = True
End If
Return isAllowed
End Function
' Check if game is finished and show message box if thats true
Private Sub isGameFinished()
' 3 is because sum, bonus and total
Dim scoreCombis As Integer = rows - 3
If roundPlayed = scoreCombis Then
gameIsFinished = True
'Deactivate scoreboard
scoreBoardActive = False
'Deactivate dices
dicesActive = False
Dim winner(,) As Integer = findWinner()
Dim highscoreText As String = getHighScoreText(winner)
Dim winText As String = ""
Dim pos As Integer = 1
For i = 0 To winner.GetLength(0) - 1
Dim name As String = players(winner(i, 1)).getName
winText &= pos & ". " & name & " " & winner(i, 0) & Environment.NewLine
pos += 1
Next
MessageBox.Show(winText & Environment.NewLine & highscoreText)
End If
End Sub
' Function for creating highscore text
Private Function getHighScoreText(ByVal sortedPlayers As Integer(,))
Dim highScoreText As String = ""
Dim highScoreObj As Highscore = New Highscore()
Dim yatzyType As Integer = 0
If isMaxi And Not isForced Then yatzyType = 1
If Not isMaxi And isForced Then yatzyType = 2
If Not isMaxi And Not isForced Then yatzyType = 3
For i = 0 To sortedPlayers.GetLength(0) - 1
Dim name As String = players(sortedPlayers(i, 1)).getName
Dim totalScore As String = sortedPlayers(i, 0)
Dim currentDate As String = DateTime.Now.ToString("dd/MM/yy")
Dim hSArgument = New String() {name, totalScore, currentDate}
Dim position As Integer = highScoreObj.writeScore(hSArgument, yatzyType)
If position > 0 Then highScoreText = highScoreText & name & " got " & position & ".place" & Environment.NewLine
Next
If Not String.IsNullOrEmpty(highScoreText) Then
highScoreText = "New highscore!" & Environment.NewLine & highScoreText
End If
Return highScoreText
End Function
' Function to calculate and write out winners
Private Function findWinner()
Dim winner(players.Length - 1, 2) As Integer
For i = 0 To players.Length - 1
Dim currentP As Player = players(i)
Dim totalScore As Integer = currentP.getTotalScore()
winner(i, 0) = totalScore
winner(i, 1) = i
'Console.WriteLine(i)
'Console.WriteLine(totalScore)
currentP.updateScore(currentP.playersScore.GetLength(0) - 1, totalScore)
ScoreBoardTable.upDatePlayersScore(i, currentP.playersScore)
Next i
Return sort2d(winner)
End Function
'Selection sorting of winners
Private Function sort2d(ByVal intArray As Integer(,))
Dim scoreArray(intArray.GetLength(0)) As Integer
Dim indexArray(intArray.GetLength(0)) As Integer
For i = 0 To intArray.GetLength(0) - 1
scoreArray(i) = intArray(i, 0)
indexArray(i) = intArray(i, 1)
Next
Array.Sort(scoreArray, indexArray)
Array.Reverse(scoreArray)
Array.Reverse(indexArray)
For i = 0 To intArray.GetLength(0) - 1
intArray(i, 0) = scoreArray(i)
intArray(i, 1) = indexArray(i)
Next
Return intArray
End Function
' Check if current player have bonus
Private Sub checkBonus()
Dim currentP As Player = players(currentPlayer)
If (currentP.leftForBonusCalc = 0) Then
' Index 7 equals to sum field
currentP.updateScore(7, currentP.bonusPoints)
' Index 8 equals to bonus field
If (currentP.bonusPoints >= bonusLimit) Then
currentP.updateScore(8, bonusScore)
Else
currentP.stroke(8)
End If
ScoreBoardTable.upDatePlayersScore(currentPlayer, currentP.playersScore)
' To avoid double bonus calculation
currentP.leftForBonusCalc = -1
End If
End Sub
' Function to calculate score. Argument are an choosen dice combination
' Return value is best possible score from current dice position.
' Example: If caclulateScore(8) are called and dice values
' are 1, 2, 2, 4, 4 return value would be 8.
' Argument 8 equals to pair and best score from combination are 4 + 4 = 8
Private Function calculateScore(ByVal scoreIndex As Integer)
Dim bestScore As Integer = 0
Select Case scoreIndex
' NORMAL AND MAXI YATZY COMBINATIONS
' Ones
Case 1
bestScore = currentDiceScore(0)
' Twos
Case 2
bestScore = currentDiceScore(1) * 2
' Threes
Case 3
bestScore = currentDiceScore(2) * 3
' Fours
Case 4
bestScore = currentDiceScore(3) * 4
' Fives
Case 5
bestScore = currentDiceScore(4) * 5
' Sixes
Case 6
bestScore = currentDiceScore(5) * 6
' Skipping some indexes because 7 are Sum and 8 are bonus
' 1 Pair
Case 9
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) >= 2 Then
'Highest value is allways the last one
bestScore = 2 * (i + 1)
End If
Next
' 2 Pairs
Case 10
'Lets loop backword from heighst value to lowest.
'Stop when to pairs are found. Thats the heigest possible score.
Dim limit As Integer = 0
For i = currentDiceScore.Length - 1 To 0 Step -1
If currentDiceScore(i) >= 2 Then
bestScore += 2 * (i + 1)
limit += 1
End If
If limit = 2 Then
Exit For
End If
Next
' If one or no pairs was found we have to set it back to 0
If limit < 2 Then
bestScore = 0
End If
' 3 of a kind
Case 11
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) >= 3 Then
'Highest value is allways the last one
bestScore = 3 * (i + 1)
End If
Next
' 4 of a kind
Case 12
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) >= 4 Then
bestScore = 4 * (i + 1)
End If
Next
' Small straight
Case 13
Dim limit As Integer = 0
For i = 0 To 4
If currentDiceScore(i) > 0 Then
bestScore += (i + 1)
limit += 1
End If
Next
If Not limit = 5 Then
bestScore = 0
End If
' Large straight
Case 14
Dim limit As Integer = 0
For i = 1 To 5
If currentDiceScore(i) > 0 Then
bestScore += i + 1
limit += 1
End If
Next
If Not limit = 5 Then
bestScore = 0
End If
' House (Maxi Yatzy also use this as Cabin)
Case 15
Dim foundPair As Boolean
Dim foundThreeOfAKind As Boolean
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) = 2 Then
bestScore += 2 * (i + 1)
foundPair = True
ElseIf currentDiceScore(i) = 3 Then
bestScore += 3 * (i + 1)
foundThreeOfAKind = True
End If
Next
If Not foundPair Or Not foundThreeOfAKind Then
bestScore = 0
End If
' Chance
Case 16
For i = 0 To currentDiceScore.Length - 1
bestScore += (currentDiceScore(i) * (i + 1))
Next
' Yatzy
Case 17
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) = diceArray.Length Then
If isMaxi Then
bestScore = 100
Else
bestScore = 50
End If
End If
Next
' Skipping 18 because thats Total
' MAXI YATZY SCORE COMBINATIONS
' Three pairs
Case 19
'Lets loop backword from heighst value to lowest.
'Stop when to pairs are found. Thats the heigest possible score.
Dim limit As Integer = 0
For i = currentDiceScore.Length - 1 To 0 Step -1
If currentDiceScore(i) >= 2 Then
bestScore += 2 * (i + 1)
limit += 1
End If
If limit = 3 Then
Exit For
End If
Next
' If one or no pairs was found we have to set it back to 0
If limit < 3 Then
bestScore = 0
End If
' 5 of a kind
Case 20
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) >= 5 Then
bestScore = 5 * (i + 1)
End If
Next
' Full straight
Case 21
Dim limit As Integer = 0
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) > 0 Then
bestScore += i + 1
limit += 1
End If
Next
If Not limit = 6 Then
bestScore = 0
End If
' House in maxi yatzy
Case 22
Dim limit As Integer = 0
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) = 3 Then
bestScore += 3 * (i + 1)
limit += 1
End If
Next
If Not limit = 2 Then
bestScore = 0
End If
' Tower
Case 23
Dim foundPair As Boolean
Dim foundThreeOfAKind As Boolean
For i = 0 To currentDiceScore.Length - 1
If currentDiceScore(i) = 2 Then
bestScore += 2 * (i + 1)
foundPair = True
ElseIf currentDiceScore(i) = 4 Then
bestScore += 4 * (i + 1)
foundThreeOfAKind = True
End If
Next
If Not foundPair Or Not foundThreeOfAKind Then
bestScore = 0
End If
End Select
Return bestScore
End Function
' Function to read if dice are at hold
Private Function GetDiceHold(ByVal diceIndex As Integer)
Return diceArray(diceIndex).DiceHold
End Function
' Function for reading rolls left
Public ReadOnly Property rollsLeftValue() As Integer
Get
' Gets the property value.
Return rollsLeft
End Get
End Property
' Function for reading if scoreboard is active
Public ReadOnly Property isScoreboardActive() As Integer
Get
' Gets the property value.
Return scoreBoardActive
End Get
End Property
' Function for reading if scoreboard is active
Public ReadOnly Property isDicesActive() As Integer
Get
' Gets the property value.
Return dicesActive
End Get
End Property
' Read if game is finished
Public ReadOnly Property isGameFinishedValue() As Integer
Get
' Gets the property value.
Return gameIsFinished
End Get
End Property
' Read if game is forced
Public ReadOnly Property isGameForced() As Boolean
Get
' Gets the property value.
Return isForced
End Get
End Property
End Class