Programming thread

I didn’t know you could use define like that
You can, but take careful note of all the seemingly unnecessary parentheses that I added, because they're necessary (if not in this case, in some other cases, so it should be done out of habit). #define is a preprocessor directive, and it performs a relatively naive code replacement. It does not evaluate anything. Adding parentheses guarantees that the parameter is atomic and your logic doesn't evaluate to something unexpected.

For example, if you were to write if (numRolls == 1 && WIN_FIRST_ROLL(diceSum)), the preprocessor would replace that with if (numRolls == 1 && ((diceSum) == 7 || (diceSum) == 11)). The && operator has higher precedence than the || operator, which means the outer set of parentheses becomes necessary. If you leave the parentheses off, the && would be evaluated first, so the logic would become if ((numRolls == 1 && (diceSum) == 7) || (diceSum) == 11).

Similarly, it's good practice to be in the habit of writing (X) instead of X, because otherwise you will run into scenarios where you pass something like a + b and the low-priority + operator can allow your argument to be split up incorrectly if you don't wrap parentheses around it to guarantee that the argument is evaluated correctly. a + b * c is not the same as (a + b) * c.
 
People in this thread are talking about computer memory management and the like. Meanwhile, I’m still making little casino like games with the absolute basic logic.
Good work. I'm definitely seeing improvement :)

I really am retarded.
No you're not. You're just learning. Everyone has to start somewhere. Beating yourself up does you no favors.

Besides, retards don't take up learning C. They go for Javascript and use it for everything. You're already way above them in terms of understanding and ability. I have seen all sorts of retards in my time teaching and tutoring, and you sir are not one of them.

I didn’t know you could use define like that.
C's preprocessor is really quite powerful. I often find myself missing it when I use other languages. There's a debate about using #define for constants over const types, and both have their merits for different cases, but I use #define 99% of the time. The logic starts getting into how compilers and shit work, so I'll leave that for later.

As mentioned above, make sure your constants have descriptive names, and use underscores to separate out words in the names when needed. It's good practice to define constants up there for readability and easy modifications, but giving them good names is crucial. You can use the more advanced preprocessor macros like the example you were given, but even basic ones like you defined in your program are seriously handy when used effectively.

Most of the other stuff was covered, so I'll simply add that the if (bal < 1.0) bit and other similar boundary checks should almost always appear at the start of the loop instead of near the end as a rule of thumb. It helps tremendously with preventing introducing overspending problems and out-of-bounds memory reads as you add more stuff later on. Good programming is all about accounting for future poor decision making, both by the programmer and the end users.

Be a bit careful with function macros, because if you do something like putting a rand() call in there, it may be included (and called) many times and different parts of your function will end up with different values from the same "variable". These aren't real functions, they just substitute text into another piece of text and it ends up looking like a function call. Whenever you see a complex expression inside of a macro function, just think a little bit about how it would interact. Most of the time it'll be fine but if you call anything really expensive or non-deterministic your shit will be fucked.
Excellent point. As a rule, always encapsulate more complex macros to avoid issues with braces and semicolons, and never use macros for anything that significantly affects control flow.
 
Last edited:
The preprocessor is a mixed blessing - it's easy to foul things up with it. IMHO something simple like WIN_FIRST_ROLL would be better off as a small function, which the compiler will probably inline anyway.

Also, change
Code:
    int DiceNums[6] = {1,2,3,4,5,6}; //just so we don't deal with a
                                     //dice rolling zero
    //char messages that aren't modified or formated
    char entrance[] = "Test your Luck!";
    char loss[] = "Tough luck! Better luck next time.";
    char win[] = "Congratulations!";
    char SevElvWin[] = "Wow! You\'re really lucky!";
    char promptPlayAgain[] = " Wanna play again?";

to

Code:
    static const int DiceNums[6] = {1,2,3,4,5,6}; //just so we don't deal with a
                                     //dice rolling zero
    //char messages that aren't modified or formated
    static const char entrance[] = "Test your Luck!";
    static const char loss[] = "Tough luck! Better luck next time.";
    static const char win[] = "Congratulations!";
    static const char SevElvWin[] = "Wow! You\'re really lucky!";
    static const char promptPlayAgain[] = " Wanna play again?";

Variables marked static const get placed in the read-only section of your EXE. Otherwise your program will waste time initializing these variables at runtime.
 
I think the kernel style guide outlines a quite sensible policy for macros. The whole document is pretty reasonable imo, and it fits with the intuition I built up over the years. That said, my past kernel work biases me a little.
macros.png
I would copy the text here, but I don't want to fuck up the formatting. It matters a lot when talking about this. Plus, this guide is archived in tons of places already.

Variables marked static const get placed in the read-only section of your EXE. Otherwise your program will waste time initializing these variables at runtime.
I figured compilers recognized constant string literals and optimized them like that automatically, even if you don't do the standard char pointer form for literals. I could be wrong. Regardless, I agree it's important to keep stuff like that in mind.
 
Last edited:
Code:
int DiceNums[6] = {1,2,3,4,5,6};
Extra step for what is the same as
Code:
d1 = rand() % 6 + 1
But on the other hand you could slip in a "cheater" die this way by having one with two sixes ;)
Alternative exercise: make a loaded die that has a 10% chance each of rolling 1-5, and a 50% chance of rolling 6.

Other comments:
bal += (bal *.6);
This could just be bal *= 1.6;. I don't know offhand if an optimizer would do this for you, since floating-point has some amount of daylight between "do what I say" and "do what I mean".
By the way, you might want to try rewriting this to not use floating-point at all for the balance. After all, there's not really any such thing as $10.9438 - in reality they would just round it off and not let you carry "change" in the small decimals. You could just keep track of how many cents the gambler has - $1.00 is 100U and so on. Just format it in the usual way when you print it for the user.

firstRoll = diceSum = d1 = d2 = numRolls = 0;
You don't need to bother initializing d1, d2, and diceSum because you're immediately overwriting them anyway.
 
I recently began learning how to program in HTML, now I want to make Fallout 5 video game what are my next steps? Also I heard about Linux on here and it sounds cool, do I need to upgrade from Windows 10 to Windows 11 to install it? I don't see it on the Store.

Thanks
 
I recently began learning how to program in HTML, now I want to make Fallout 5 video game what are my next steps? Also I heard about Linux on here and it sounds cool, do I need to upgrade from Windows 10 to Windows 11 to install it? I don't see it on the Store.

Thanks
I know you're joking, but you can make a game like Fallout for HTML5.
 
People in this thread are talking about computer memory management and the like. Meanwhile, I’m still making little casino like games with the absolute basic logic. I really am retarded. Anyway here’s my attempt at a craps game in C:
It's been a LOOOOONG time since I wrote any C code of substance but I just wanted to say, at first blush, you really want to pick a consistent case convention for variables. I believe C typically uses snake case for most purposes but I'm of the mind that, as long as you have one standard or other across a project, that's adequate. (For example: if I were a Linux kernel contributor I would use tabs for indentation even though I think it's icky-poo-poo just because the precedent is so massive.)

Anyway you might be interested in Monte Carlo methods, which are used to solve probability problems approximately when a closed-form solution isn't available (or possibly is, but I can't figure it out).
 
  • Agree
Reactions: 419 and y a t s
Jokes aside, a lot of the mainstream game engines out there let you use JS. I know Unity does and I'm pretty sure Unreal does too. Everything else is handled graphically somehow, often with a drag and drop patching setup. So it's not a completely crazy idea for someone with web dev experience to try making games.
 
  • Thunk-Provoking
Reactions: Mr. 0
Jokes aside, a lot of the mainstream game engines out there let you use JS. I know Unity does and I'm pretty sure Unreal does too. Everything else is handled graphically somehow, often with a drag and drop patching setup. So it's not a completely crazy idea for someone with web dev experience to try making games.
Unreal does, and I remember years ago when Epic's Citadel demo was released on HTML5. I tried to find it, but it appears to have been wiped from the internet. Best you can find now is videos on YouTube.
 
  • Like
Reactions: y a t s
I'm going to be doing a lot of stuff to make a graphical representation of stuff using Unity. Before you suggest something else it needs to be Unity and the graphical representation is complex enough that it basically becomes a simple version of a video game.

Any tips or things I should look out for? For context I have programmed a bunch in Python, I have a pretty strong background in a highly technical field, and I have a lot of surface level knowledge about programming concepts.

Any suggestions for tutorials would also be appreciated, though I may get through that stuff before you respond.
 
  • Like
Reactions: y a t s
I'm going to be doing a lot of stuff to make a graphical representation of stuff using Unity. Before you suggest something else it needs to be Unity and the graphical representation is complex enough that it basically becomes a simple version of a video game.

Any tips or things I should look out for? For context I have programmed a bunch in Python, I have a pretty strong background in a highly technical field, and I have a lot of surface level knowledge about programming concepts.

Any suggestions for tutorials would also be appreciated, though I may get through that stuff before you respond.
Without going into PL level detail, what sort of graphical representations? Without much to go on, my recommendations are going to be pretty general.

Unity puts a big emphasis on writing stuff in C#, but you don't have to. You mentioned Python, so perhaps you might like IronPython3. It's essentially Python for the .NET framework. Their Python 3 implementation is still a bit incomplete, but it could help you use existing Python stuff you've already written. I haven't tried it personally, but many others seem to like it. Their Python 2 implementation is more mature, so perhaps consider that if you really need to do a certain thing they don't have in 3. Just an idea.

As for resources, there is a wealth of Q&A content out there that can help if you run into specific issues. Unity comes with a lot of decent tutorials and guides that can quickly get you reasonably comfortable with the workflow. Once you have a good grasp of the graphical part of Unity (models, textures, adding stuff, navigating in the viewport, etc.), the rest of it is regular programming using API documentation.
 
Without going into PL level detail, what sort of graphical representations? Without much to go on, my recommendations are going to be pretty general.
Basically I need to represent 3D models in a way that someone who is not super technical can understand them. The models themselves are pretty simplistic and just by looking at them the operators can understand what is there so I don't need complicated UI elements or a lot of text displays. But I need the operators to be able to move stuff around and have it change the 3D model. It is pretty simplistic compared to a video game but it is more complex than an animation. If it works well and it makes sense I might also implement this into VR/MR.

I already have basically a simplistic Animation that I can convert to FBX really easily. It does what I need the operator to do but I need to make it so the operator can be the one doing those things.

Unity puts a big emphasis on writing stuff in C#, but you don't have to. You mentioned Python, so perhaps you might like IronPython3. It's essentially Python for the .NET framework. Their Python 3 implementation is still a bit incomplete, but it could help you use existing Python stuff you've already written. I haven't tried it personally, but many others seem to like it. Their Python 2 implementation is more mature, so perhaps consider that if you really need to do a certain thing they don't have in 3. Just an idea.
That is a good suggestion. I may not even use IronPython in the end but it could also be a good at bridging gaps in my understanding. It is sometimes better to slowly approach something new rather than completely jump into doing it especially if you start having a lot of problems.

As for resources, there is a wealth of Q&A content out there that can help if you run into specific issues. Unity comes with a lot of decent tutorials and guides that can quickly get you reasonably comfortable with the workflow. Once you have a good grasp of the graphical part of Unity (models, textures, adding stuff, navigating in the viewport, etc.), the rest of it is regular programming using API documentation.
Yeah I've noticed this somewhat. But it is good to know that there are a lot of people doing stuff somewhat similar, that I can learn from rather than having to figure out everything myself

Thanks for your advice and knowledge of this stuff.
 
  • Like
Reactions: y a t s
Basically I need to represent 3D models in a way that someone who is not super technical can understand them. The models themselves are pretty simplistic and just by looking at them the operators can understand what is there so I don't need complicated UI elements or a lot of text displays. But I need the operators to be able to move stuff around and have it change the 3D model. It is pretty simplistic compared to a video game but it is more complex than an animation. If it works well and it makes sense I might also implement this into VR/MR.
If I'm imagining this correctly, this should be pretty straightforward using simple meshes. Once you have that out of the way, mapping it all to controls is a breeze.
 
  • Winner
Reactions: Napoleon III
Back