C
C#9mo ago
Owltávio

Trying to simulate a turtle race and getting my ass kicked...

So I have a assignment to make a 5 player turtle race, but I'm having a hard time trying to implement the turtle movement and the race itself, I have been trying to this for 2 days and I feel i'm getting nowhere... Could anyone explain ways that I could implement them into the code? The assignment has some criterias: - must use System.Threading and Thread - race must me 100m - turtle must have a random inicial velocity between 1 and 5 m/s - during the race, the turtle must randomly have its speed increased, decreased or completely stop(sleep) - if a draw happen, the turtle with the least time sleeping wins - and of course, no use of AI, and code can't be copied from the internet Also, I'm using GDB Online, because I'm in windowns 7 hell...
8 Replies
Owltávio
OwltávioOP9mo ago
Here is what I coded so far: https://paste.mod.gg/ttfecjjwrdvn/0
BlazeBin - ttfecjjwrdvn
A tool for sharing your source code with the world!
casog
casog9mo ago
Have fun
lycian
lycian9mo ago
Which part are you having problem with?
2spooky2play
2spooky2play9mo ago
private string Name;
public string name{
get { return Name; }
set { Name = value; }
}
private string Name;
public string name{
get { return Name; }
set { Name = value; }
}
can be shorttened to
public string Name { get; set; }
public string Name { get; set; }
not integral, but nice to know
Owltávio
OwltávioOP9mo ago
Mostly the Run() part, every time I try develop it either not works or it becomes really slow. Also, I don't know if what I'm doing is the correct way or not, so I ge very lost... Oh, thanks. I didn't knew that.
lycian
lycian9mo ago
to do everything in parallel with threads, you can do something like
var tasks = new[] {
Task.Run(() => t1.Run()),
Task.Run(() => t2.Run()),
Task.Run(() => t3.Run()),
Task.Run(() => t4.Run()),
Task.Run(() => t5.Run())
};

Task.WaitAll(tasks);
var tasks = new[] {
Task.Run(() => t1.Run()),
Task.Run(() => t2.Run()),
Task.Run(() => t3.Run()),
Task.Run(() => t4.Run()),
Task.Run(() => t5.Run())
};

Task.WaitAll(tasks);
You'll still need to figure out how to stop the task looping, report winning, and in general make sure every task is running at the same "rate" or use fastest "rate" to determine winner. That's how you get started with the multithreaded portion though If you need to use System.Threading.Thread you can follow https://learn.microsoft.com/en-us/dotnet/standard/threading/using-threads-and-threading#how-to-create-and-start-a-new-thread and do similar
Using threads and threading - .NET
Learn about using threads and threading in .NET, so you can write applications to perform many operations at the same time (multithreading).
Owltávio
OwltávioOP9mo ago
Okay thanks.
mtreit
mtreit9mo ago
Except the assignment says they must use Thread. The course material was probably written before the TPL existed :\
Want results from more Discord servers?
Add your server