Water rising and descending - game-development

im in a process of making a roblox game. I want to make the water rise and descend again down. I think i did something wrong with coding,(its my second day of coding and i cant find any video on it) im not sure what. So everytime i run roblox after 1st descending and starting of 2nd game, it freezes,water dissapears and it throws a error: Script timeout: exhausted allowed execution time. Here is my code:
wait(21)
t = 17
repeat
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, 1, 0)
wait(2)
until
t == 0
wait(1)
t = 17
repeat
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, -1, 0)
wait(0.5)
until
t == 0
wait(9)
t = 17
while true do
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, 1, 0)
end
wait(2)
while false do
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, -1, 0)
end

Script timeout: exhausted allowed execution time.
Because the code never stops looping here:
while true do
//...
end
This will continue to loop forever. (Well, for as long as true is true. And I can't think of a condition in which it wouldn't be.)
Contrast that with your loops above:
t = 17
repeat
t = t-1
//...
until
t == 0
In this case the loop explicitly repeats "until t equals 0", and each iteration of the loop brings t one step closer to 0.
Modify your later loops to have similar structures and conditions. For example:
t = 17
while t > 0 do
t = t-1
//...
end
Note also that this loop will never execute:
while false do
//...
end
Because false is never true. Now is a good time to take a step back and consider the logic of what you want these loops to do. Just like with your first two loops, modify your second two loops to indicate how many times they should repeat.

Related

Quickest way to determine if a number is a prime number or not VB

I have been working on this for quite a bit now, I have a task of creating a program which calculates if a number entered by a user is prime number or not, the program calculates the time taken and displays this to the user, however I have found two method, one takes more time than the other but it produce accurate numbers, the other one calculates very quickly however it is wrong, I am hoping if someone can help me and tell me the quickest way of calculating this, here are my two codes
Code1:
Dim ch As String
ch = "y"
While ch = "y"
If (num Mod 2 = 0) Then
Console.WriteLine("Is not a prime number!")
Else
Console.WriteLine("Is a prime number!")
End If
Code2:
check = 1 'initilizing a check point to use it in the program to determine prime number
Dim Value As Long
Console.Write(vbLf & "Enter a number To check Whater it is Prime or Not :")
Value = Long.Parse(Console.ReadLine())
start_time = Now
Dim ch As ULong
ch = 0
Dim i As ULong
i = 2
While (i <= Value / 2)
If (Value Mod i = 0) Then
ch = 1
Exit While
End If
i = i + 1
End While
If (ch = 0) Then
Console.WriteLine("Prime Number")
Else
Console.WriteLine("Not Prime Number")
End If
There are a great many prime testers out there, many of them on this site. For checking a single number I use a faster variant of your Code2 with a little extra checking. Here is the pseudocode:
boolean function isPrime(num)
//1, 0 and negatives cannot be prime.
if (num < 2) then
return false
endif
// 2 is the only even prime.
if (num MOD 2 = 0) then
return (num = 2)
endif
// Check for odd factors.
limit <- sqrt(num)
for (factor <- 3; factor <= limit; factor <- factor + 2) do
if (num MOD factor = 0) then
return false
endif
endfor
// If we reach this point then the number is prime.
return true
endfunction
As #user448810 said, you should use the square root of your target number as the limit of your testing loop. You can basically halve the number of tests you do by treating even numbers separately. Once you have taken out the even numbers, then you only have to test odd factors: 3, 5, 7, ...

Program freezes with specific numbers

This code is supposed to take a number and find it's prime factors.
Why does this code work with numbers like 2345 (which returns 5, 7, 67 like its supposed to) but it doesn't work with numbers like 500 and 800?
EDIT: when I say it doesn't work, the program simply does nothing and/or freezes on button click. I then have to stop the program in vb.
Dim number As Double = txtNum.Text
Dim var As Double = 2
Dim result As Double
If number > 1 Then
lst1.Items.Clear()
lst1.Items.Add("The prime factors of " & number & ":")
Do While number > 1
result = number / var
If result = Int(result) Then
lst1.Items.Add(var)
number = result
End If
var = var + 1
Loop
Else
lst1.Items.Clear()
lst1.Items.Add("Let try that again...")
End If
The problem was caused because 500 divided by 2 is 250, divided by 5 is 50 ... and then the program continued on by dividing by 6 instead of attempting to divide by 5 again. Once var reached 10, number became 5 and no further divisions ever allowed it to reach 1 - thus an infinite loop was created.
By changing
If result = Int(result) Then
lst1.Items.Add(var)
number = result
End If
var = var + 1
to
If result = Int(result) Then
lst1.Items.Add(var)
number = result
Else
var = var + 1
End If
it correctly made multiple divisions by the same prime factor when necessary.

Calling Another Code

I am trying to figure out another way to write this line. Currently, I have it to where if any of the ranges AA2:AA7 = 1 then call code OneLineItem. Issue is, the parameter I need set is if only one of those cells equals 1 and only one other cell to be greater than 1. I.e. AA2 = 1 and AA7=200 (for example). A problem i'm running into is that AA2 = 1, AA3 = 100, AA7 = 200. However I just need one cell to equal 1 and another cell to be >1 and everything else to be 0. If that criteria is met, then call code OneLineItem. Thank You.
If ActiveSheet.Range("AA2") = 1 Or ActiveSheet.Range("AA3") = 1 Or
ActiveSheet.Range("AA4") = 1 Or ActiveSheet.Range("AA5") = 1 Or
ActiveSheet.Range("AA6") = 1 Or _
ActiveSheet.Range("AA7") = 1 Then
Call OneLineItem
Else
There are 6 numbers so:
1 should be 1
1 should be greater than 1
4 should be 0
so we can use COUNTIF() to find if it follows the pattern
Dim OneTrue As Boolean
Dim MoreTrue As Boolean
Dim RestTrue As Boolean
RestTrue = Application.WorksheetFunction.CountIf([AA2:AA7], 0) = 4 [AA2:AA7].Cells.Count - 2
OneTrue = Application.WorksheetFunction.CountIf([AA2:AA7], 1) = 1
MoreTrue = Application.WorksheetFunction.CountIf([AA2:AA7], ">1") = 1
If RestTrue And OneTrue And MoreTrue Then
Call OneLineItem
End If
Another method would be to nest the IF:
IF Application.WorksheetFunction.CountIf([AA2:AA7], 0) = [AA2:AA7].Cells.Count - 2 Then
IF Application.WorksheetFunction.CountIf([AA2:AA7], 1) = 1 Then
'we do not need the third, If the others are true then the last must be true.
'Unless you can have negative numbers. Then you can add the third.
Call OneLineItem
End If
End If
The advantage to the second is that it only does the COUNTIFs necessary till it find a False return, then it does not do any more. while the first does all three no matter what.

Dance Dance Revolution Game

http://i44.tinypic.com/znlvra.png
So I got this game like dance dance revo it wors Ok it says good and perfect when you hit an arrow i got a problem with the perfect hit it works depends on the speed on the arrows but I got an option easy, medium and hard basically easy is the slowest and hard is the fastest in the medium speed it I never ever hit a perfect so I'm asking if you guys have a better method to get the perfect or help me improve this code:
'Perfect Hit
If (RArrowDown.Top = ArrowDown.Top) And RArrowDown.Visible = True Then
TimerMain.Enabled = False
Hit += 1
Score += 20
RArrowDown.Visible = False
Perfect.Visible = True
TimerPerfect.Enabled = True
Combo_Counter += 1
RandomTime_1.Enabled = True
'End If
'Good Hit
ElseIf (RArrowDown.Top <= ArrowDown.Top + ArrowDown.Height) And (ArrowDown.Top <= RArrowDown.Top + RArrowDown.Height) And RArrowDown.Visible = True Then
TimerMain.Enabled = False
Hit += 1
Score += 10
RArrowDown.Visible = False
Good.Visible = True
TimerGood.Enabled = True
Combo_Counter += 1
RandomTime_1.Enabled = True
End If
I think a better way is to get a value indicating how far off from perfect the keypress is (I think it is very unliekly that someone actually gets it perfectly), then act on this value. You can then tweak the bands that give you a perfect or good hit:
Dim offset as Integer
If [Right Arrow Pressed]
offset = RarrowDown.Top - ArrowDown.Top
End If
Select Case offset
case is 0 to 3
'perfect
case 4 to 10
'good
case 11 to 20
'poor
case else
'missed
End Select

Verify Gamefield VB.NET

So I'm developing a minesweeper game and im assigning the mines, but I've got to check where are the mines now, in order to generate the numbers. The problem is that when I'm verifying the columns and lines I need the program not to get out of the game field.
Here's how my code looks like now:
Public Sub avisinhos(ByVal line, ByVal column)
If mat(line, column) = 0 Then
mat(line, column) = -1
numbandeiras = numbandeiras + 1
End If
For auxlinha = -1 To 1
For auxcolumn = -1 To 1
Next
Next
End Sub
How do I create a IF function to verify that I don't get out of the game field?
Best regards, joao.
pseudo code
int linestart = -1;
int lineend = 1;
int colstart = -1;
int colend = 1;
Assuming a 10 x 10 grid (zero based)
if line < 2 linestart = 0
if line > 8 lineend = 0
if column < 2 colstart = 0
if column > 8 colend = 0
For auxlinha = linestart To lineend
For auxcolumn = colstart To colend
// check
Next
Next
Personally though I wouldn't bother with the loops, they add very little to nothing
HasMineAbove = (line > 1) and (gamefield[line -1,column] = MinePresentValue
would be my approach, do it all in one.
Not to mention the huge potential confusion when auxlinha and auxcolumn are both zero...
I'm not sure exactly what your code is saying. It's a bit cryptic since you're using abbreviations and all lowercase names. You might want to try camelCasing and spelling out the words more completely, intellisense is your friend. =)
But coding style aside, if you are trying to loop through a limited range of values, you can keep your values bounded by using the modulus operator (%). For example, if you need to keep you values between 0-7 and you end up with a value of 12, just take the modulus of 8 to loop back to within range with a value of 4:
12 % 8 = 4
9 % 8 = 1
15 % 8 = 7
24 % 8 = 0
I realize this doesn't answer your specific question, but it's a handy technique might find useful.