C
C#2mo 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ávio2mo 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
casog2mo ago
Have fun
lycian
lycian2mo ago
Which part are you having problem with?
2spooky2play
2spooky2play2mo 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ávio2mo 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
lycian2mo 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ávio2mo ago
Okay thanks.
mtreit
mtreit2mo 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
More Posts
✅ Problem With my code from KeyAuthHey, Im having problems with my code, im trying to implement Keyauth and im getting these error's JPress enter onlyI want to code a loop which only takes spacebar for the code to loop. E.g. While (true) { ConsoleC# with NeovimHey! I'm trying to set up neovim with C# on WSL. Using latest 9.5 nvim. I managed to get OmniSharp (Question to software development interviewers, is it ok to keep portfolio on localhost over prod?Spent a week migrating my websites to the cloud, frontend uploaded to Netlify. Currently delving intRedis or database for cache, distribiuted lock, signalR scale-out?I am evaluating whether to use Redis or alternative methods for caching, distributed locking, and scDiscord.net help - Trying to access a users rolesDoes anybody know a method I can use to access a users discord roles whilst I am logging into my appSeeking community input for project ideas to learn problem-solving and best practicesI recently completed 2 solo projects, a basic notes taking app and a basic e-commerce portal, withouSelecting an overloaded method based on the runtime type of an object by casting it to dynamicIs this something I should do ? Is it a common pattern ? Are there any downsides / unexpected behavi✅ cant assign a value to a variable```cs using System; using System.Collections.Generic; using System.Linq; using System.Reflection.PorEF Core Where on multiple fields## Initial question Hi! I'm currently running into an issue where I try and do something along the