Sub start()
Dim rng As Range
Dim sim As Worksheet
Set sim = Sheets("sim")
sim.Range("B4:F4").Select
Selection.ClearContents
Range("A1").Select
frm1.Show
If frm1.Label2.Caption = 999 Then
Exit Sub
End If
For t = 1 To frm1.TextBox1.Value
sim.Range("A8:I2000").Select
Selection.ClearContents
sim.Range("A1").Select
flag = 999
i = 1
While flag > 1
If i = 1 Then
v = 4
Else
Set rng = sim.Range(Cells(i + 6, 6), Cells(i + 6, 9))
Maximum = Application.WorksheetFunction.Max(rng)
v = Application.WorksheetFunction.CountIf(rng, ">0")
End If
flTemp = 1
While flTemp = 1
atacker = Int(Rnd() * 4) + 1
If (sim.Cells(i + 6, 5 + atacker) > 0) Then
flTemp = 999
atackName = sim.Cells(1, 1 + atacker)
End If
Wend
flTemp = 1
While flTemp = 1
target = Int(Rnd() * 4) + 1
If (sim.Cells(i + 6, 5 + target) > 0 And target <> atacker) Then
flTemp = 999
targetName = sim.Cells(1, 1 + target)
End If
Wend
sim.Cells(i + 7, 1) = i
sim.Cells(i + 7, 2) = atackName
sim.Cells(i + 7, 3) = targetName
If (i = 1) Then
sim.Cells(i + 7, 4) = sim.Cells(2, 1 + target)
sim.Cells(i + 7, 5) = sim.Cells(3, 1 + atacker)
sim.Cells(i + 7, 6) = sim.Cells(2, 2)
sim.Cells(i + 7, 7) = sim.Cells(2, 3)
sim.Cells(i + 7, 8) = sim.Cells(2, 4)
sim.Cells(i + 7, 9) = sim.Cells(2, 5)
If sim.Cells(2, 1 + target) - sim.Cells(i + 7, 5) < 0 Then
sim.Cells(i + 7, 5 + target) = 0
Else
sim.Cells(i + 7, 5 + target) = sim.Cells(2, 1 + target) - sim.Cells(i + 7, 5)
End If
Else
sim.Cells(i + 7, 4) = sim.Cells(i + 6, 5 + target)
sim.Cells(i + 7, 5) = sim.Cells(3, 1 + atacker)
sim.Cells(i + 7, 6) = sim.Cells(i + 6, 6)
sim.Cells(i + 7, 7) = sim.Cells(i + 6, 7)
sim.Cells(i + 7, 8) = sim.Cells(i + 6, 8)
sim.Cells(i + 7, 9) = sim.Cells(i + 6, 9)
If sim.Cells(i + 6, 5 + target) - sim.Cells(i + 7, 5) < 0 Then
sim.Cells(i + 7, 5 + target) = 0
Else
sim.Cells(i + 7, 5 + target) = sim.Cells(i + 6, 5 + target) - sim.Cells(i + 7, 5)
End If
End If
'sim.Cells(i + 8) = i
Set rng = sim.Range(Cells(i + 7, 6), Cells(i + 7, 9))
Maximum = Application.WorksheetFunction.Max(rng)
v = Application.WorksheetFunction.CountIf(rng, ">0")
If v < 2 Then
flag = 1
tSim = 0
For k = 1 To 4
If sim.Cells(i + 7, 5 + k) > 0 Then
winner = sim.Cells(7, 5 + k)
wc = k + 1
End If
tSim = tSim + sim.Cells(4, 1 + k)
Next k
sim.Cells(4, wc) = sim.Cells(4, wc) + 1
sim.Cells(4, 6) = tSim + 1
End If
i = i + 1
Wend
Next t
MsgBox ("Done!")
End Sub