-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameControl.frm
132 lines (118 loc) · 3.78 KB
/
GameControl.frm
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
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} GameControl
Caption = "UserForm1"
ClientHeight = 5070
ClientLeft = 0
ClientTop = -465
ClientWidth = 7065
OleObjectBlob = "GameControl.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "GameControl"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub btnGo_Click()
On Error GoTo errHandler
Dim Vx As Integer
Vx = cmbVx.Value
Dim Vy As Integer
Vy = cmbVy.Value
Dim x As Integer
Dim y As Integer
Dim intCase As Integer
Dim MoveMe As Range
If LabelP1.Visible = True Then
intCase = 1
Else: intCase = 2
End If
Select Case intCase
Case 1
'Speed
x = GameBoardSheet.Range("A100") + Vx
y = GameBoardSheet.Range("A101") + Vy
GameBoardSheet.Range("A100") = x
GameBoardSheet.Range("A101") = y
'Move
With Cells(Int(CurrentRow.Value), Int(CurrentCol.Value))
.ClearContents
.Interior.ColorIndex = xlNone
'Excel uses (rows,cols) notation, so Y direction is first
'We're using (-y) so that positive 1 moves upward
Set MoveMe = .Offset(-y, x)
End With
If MoveMe.Interior.ColorIndex = xlNone Then
MoveMe = "P1"
MoveMe.Interior.ColorIndex = 8
Range("A102") = MoveMe.Row
Range("A103") = MoveMe.Column
Else: GoTo WinLose
End If
'set up form for next player
LabelP1.Visible = False
LabelP2.Visible = True
CurrentX.Text = Range("A200")
CurrentY.Text = Range("A201")
CurrentRow.Text = Range("A202")
CurrentCol.Text = Range("A203")
Exit Sub
'Player 2 turn
Case 2
'Speed
x = GameBoardSheet.Range("A200") + Vx
y = GameBoardSheet.Range("A201") + Vy
GameBoardSheet.Range("A200") = x
GameBoardSheet.Range("A201") = y
'Move
With Cells(Int(CurrentRow.Value), Int(CurrentCol.Value))
.ClearContents
.Interior.ColorIndex = xlNone
Set MoveMe = .Offset(-y, x)
End With
If MoveMe.Interior.ColorIndex = xlNone Then
MoveMe = "P2"
MoveMe.Interior.ColorIndex = 3
Range("A202") = MoveMe.Row
Range("A203") = MoveMe.Column
Else: GoTo WinLose
End If
'set up form for next player
LabelP2.Visible = False
LabelP1.Visible = True
CurrentX.Text = Range("A100")
CurrentY.Text = Range("A101")
CurrentRow.Text = Range("A102")
CurrentCol.Text = Range("A103")
Exit Sub
End Select
'TODO: Create function
WinLose:
If MoveMe.Interior.ColorIndex = xlAutomatic Then
MsgBox ("You Win!")
MoveMe = "P1"
MoveMe.Interior.ColorIndex = 6
Else: MsgBox ("Whoops, you crashed!")
End If
Unload GameControl
Exit Sub
'TODO: Create Function
errHandler:
MsgBox ("Please select your values")
End Sub
Private Sub UserForm_Initialize()
'Placement of Form - works well on some machines, not perfect on others
Me.StartUpPosition = 0
Me.Top = Application.Top + 25
Me.Left = Application.Left + 30
'Populate the combobox lists with an array upon initialization - this way they will always retain the values I set here
cmbVx.List = Array("-1", "0", "1")
cmbVy.List = Array("-1", "0", "1")
'Player1 goes first
LabelP1.Visible = True
LabelP2.Visible = False
CurrentRow.Text = Range("A102").Value
CurrentCol.Text = Range("A103").Value
CurrentX.Text = Range("A100").Value
CurrentY.Text = Range("A101").Value
End Sub