[QUOTE=Z_guy;45628435]In my experience it's just a matter of the wrong thing having focus. I usually just click one of the files so it's selected and then press the letter and then it jumps there as expected.[/QUOTE]
It happens to me when I've pressed a letter to find something, then pressed a different letter to find something else. Windows queues up the letters so if you want to find starting with 'adv' instead of just 'a', you can type in 'adv' and it'll work. Other than clicking in the window, I'm not sure how to reset/clear this queue, however.
[QUOTE=SDC_Zuratul;45627602]I'm making a game, and I'd like my character's sprite to change so as to face the mouse. I have a sprite made for every angle, but I'm not sure how I'd write the code to change the active sprite depending on where the mouse is. I wrote a few Java classes before I started using Unity that changed the sprites depending on the last directional arrow you'd pressed, but that's about all I know how to do so far. Haven't found many tutorials explaining in a way I understood, so I figured I'd ask here!
Mockup Image with the sprites arranged around the centre.
[IMG]http://31.media.tumblr.com/e0fcc438425deb21a45afcd2b0612286/tumblr_n9z8oy1o6Z1tfd5wyo1_500.png[/IMG]
Oh! Currently I'm using Unity and C# to code my game. I have another version I was working on in Java using Eclipse but Unity has been a more streamlined process so far.[/QUOTE]
You can use Math.Atan2(y, x) to get the angle between your character and the mouse [code]angle = Math.Atan2(mouse_y - character_y, mouse_x - character_x);[/code]
The angle directly to the right is 0 radians, increasing counterclockwise up to exactly Pi radians, and equally, decresing clockwise up to -Pi radians for an object directly to the left.
Split the circle into eight segments of Pi/4 radians from -Pi to Pi (And remember to account for the fact that it's discontiuous at the left: ]0.75 * Pi .. Pi] is part of the same segment as ]-Pi .. 0.75 * -Pi[ (which is the segment directly to the left)
[QUOTE=Hng;45626306]Codecademy has a good python tutorial that will walk you through the basics. I highly •recommend you go through the full "course" - it will give you a decent grasp of a lot of important things.
[editline]8th August 2014[/editline]
Also [url]www.reddit.com/r/learnpython[/url] is a great community with many people willing to help out.[/QUOTE]
The only downside to this codecademy thing is that it seems to be running an older version of Python with slightly different syntax for things, but otherwise it's great! I might just start with the older version, since it has more tutorials and documentation, and then try to adapt to the newer version once I've got the hang of the basics. I appreciate the suggestion!
[QUOTE=BackwardSpy;45629313]It happens to me when I've pressed a letter to find something, then pressed a different letter to find something else. Windows queues up the letters so if you want to find starting with 'adv' instead of just 'a', you can type in 'adv' and it'll work. Other than clicking in the window, I'm not sure how to reset/clear this queue, however.[/QUOTE]
Oh yeah, you're right. I haven't tested it, but maybe you can press escape to reset it? Otherwise, if you don't want to reach for the mouse I think you can change the selection with the arrow keys to reset it.
[QUOTE=Z_guy;45630688]Oh yeah, you're right. I haven't tested it, but maybe you can press escape to reset it? Otherwise, if you don't want to reach for the mouse I think you can change the selection with the arrow keys to reset it.[/QUOTE]
I just tested it, pressing escape does indeed reset it. And arrow keys also work.
[QUOTE=Big Dumb American;45630503]The only downside to this codecademy thing is that it seems to be running an older version of Python with slightly different syntax for things, but otherwise it's great! I might just start with the older version, since it has more tutorials and documentation, and then try to adapt to the newer version once I've got the hang of the basics. I appreciate the suggestion![/QUOTE]
2.7 probably, pythons pretty much split between 2.7 and the newest release, learning 2.7 is still relevant(I would say more relevant) because it's used more than 3.x, I can't really see that changing any time soon.
[editline]9th August 2014[/editline]
Python is used for a lot of scientific stuff because it is perfect for it, for a while the popular scientific libraries weren't available for 3.x so I assume that's one of the main reasons it is so split now.
[url]http://python3porting.com/differences.html[/url]
There really aren't major changes at the basic syntax level (which is what a beginner is focused on) for python 2.x to 3.x
I did it, I did it!
[img]http://i.imgur.com/r2jUraO.png[/img]
(The randomly generated me is ugly and dumb. :pwn: )
One question, though. Check out this code and let me know if I've been inefficient. I don't know if I did a lot more work than was necessary for this, so please let me know if you have any tips or tricks for a simpler code! Thanks.
[code]__author__ = 'Fatal Error'
from random import randint
ps = (randint(1,6) + randint(1,6) + randint(1,6))
pb = (randint(1,6) + randint(1,6) + randint(1,6))
pp = (randint(1,6) + randint(1,6) + randint(1,6))
pe = (randint(1,6) + randint(1,6) + randint(1,6))
ma = (randint(1,6) + randint(1,6) + randint(1,6))
me = (randint(1,6) + randint(1,6) + randint(1,6))
iq = (randint(1,6) + randint(1,6) + randint(1,6))
spd = (randint(1,6) + randint(1,6) + randint(1,6))
roll1 = randint(1,6)
roll2 = randint(1,6)
roll3 = randint(1,6)
roll4 = randint(1,6)
roll5 = randint(1,6)
roll6 = randint(1,6)
roll7 = randint(1,6)
roll8 = randint(1,6)
name = input("What is your name: ")
print()
print("____________________________________________")
print("Character Name: ", name)
if ps >= 16:
ps = ps + roll1
if roll1 == 6:
ps = (ps + roll1 + randint(1, 6))
print("Physical Strength:", ps)
else:
print("Physical Strength:", ps)
else:
print("Physical Strength:", ps)
if pb >= 16:
pb = pb + roll2
if roll2 == 6:
pb = (pb + roll2 + randint(1, 6))
print("Physical Beauty:", pb)
else:
print("Physical Beauty:", pb)
else:
print("Physical Beauty:", pb)
if pp >= 16:
pp = pp + roll3
if roll3 == 6:
pp = (pp + roll3 + randint(1, 6))
print("Physical Prowess:", pp)
else:
print("Physical Prowess:", pp)
else:
print("Physical Prowess:", pp)
if pe >= 16:
pe = pe + roll4
if roll4 == 6:
pe = (pe + roll4 + randint(1, 6))
print("Physical Endurance:", pe)
else:
print("Physical Endurance:", pe)
else:
print("Physical Endurance:", pe)
if ma >= 16:
ma = ma + roll5
if roll5 == 6:
ma = (ma + roll5 + randint(1, 6))
print("Mental Affinity:", ma)
else:
print("Mental Affinity:", ma)
else:
print("Mental Affinity:", ma)
if me >= 16:
me = me + roll6
if roll6 == 6:
me = (me + roll6 + randint(1, 6))
print("Mental Endurance:", me)
else:
print("Mental Endurance:", me)
else:
print("Mental Endurance:", me)
if iq >= 16:
iq = iq + roll7
if roll7 == 6:
ma = (iq + roll7 + randint(1, 6))
print("Intelligence:", iq)
else:
print("Intelligence:", iq)
else:
print("Intelligence:", iq)
if spd >= 16:
spd = spd + roll8
if roll8 == 6:
spd = (spd + roll8 + randint(1, 6))
print("Speed:", spd)
else:
print("Speed:", spd)
else:
print("Speed:", spd)
print("____________________________________________")[/code]
There is still a lot that I'd like to do, but I'm a bit pleased at myself for having at least figured out this little bit! I guess my next step is working on the skills database, figuring out how to select those skills, and how to apply any attribute bonuses those skills might give to the character's attributes. That sounds very complicated and time consuming, so be at the ready for a lot of annoying questions.
The most glaringly obvious thing to me is how quite often you have something like this:
[code]if roll1 == 6:
ps = (ps + roll1 + randint(1, 6))
print("Physical Strength:", ps)
else:
print("Physical Strength:", ps)[/code]
that bit of text is going to get printed whether the 'if' condition is satisfied or not, so you might as well just print it anyway, like this:
[code]if roll1 == 6:
ps = (ps + roll1 + randint(1, 6))
print("Physical Strength:", ps)[/code]
Hey guys, I'm going to be taking a class on C in two months. I was reading the first chapter of the textbook that I'll need for the class and it makes constant references to unix, such as tools like lint that are "traditionally provided with UNIX" (and I'm guessing Linux as well.) The book does not recommend any IDEs. There is no doubt that my professor will make all of this clear when class starts, but I'd like to get a head start while I have some free time.
Given that Visual Studio doesn't even support C unless you use some weird trickery in a C++ project, should I use a virtual machine running a Linux distribution for programming? I know that C++, in most cases, supports C, but it feels weird. I get the vibe that C is a pain on non-unix OSes like Windows.
I'm fairly comfortable on the Unix command line, and could learn Windows as well if there was an easy solution besides a virtual machine.
What is your guys' setup for programming in C? I don't have very much programming experience at all, I've tried a little bit of C++, Objective C and Python but nothing past something like 'for loops'.
[url=http://www.amazon.com/Programming-Modern-Approach-2nd-Edition/dp/0393979504]"C Programming: A Modern Approach, 2nd Edition" is the book I'll be using.[/url]
-snip found out i forgot brackets whoops :P-
[QUOTE=Cittidel;45633473]Hey guys, I'm going to be taking a class on C in two months. I was reading the first chapter of the textbook that I'll need for the class and it makes constant references to unix, such as tools like lint that are "traditionally provided with UNIX" (and I'm guessing Linux as well.) The book does not recommend any IDEs. There is no doubt that my professor will make all of this clear when class starts, but I'd like to get a head start while I have some free time.
Given that Visual Studio doesn't even support C unless you use some weird trickery in a C++ project, should I use a virtual machine running a Linux distribution for programming? I know that C++, in most cases, supports C, but it feels weird. I get the vibe that C is a pain on non-unix OSes like Windows.
I'm fairly comfortable on the Unix command line, and could learn Windows as well if there was an easy solution besides a virtual machine.
What is your guys' setup for programming in C? I don't have very much programming experience at all, I've tried a little bit of C++, Objective C and Python but nothing past something like 'for loops'.[/QUOTE]
When I had my Operating Systems course I programmed in Windows, using Sublime Text 3 as my editor and Cygwin with gcc as compiler.
[URL]http://www.sublimetext.com/3[/URL]
Looking back it was pretty dumb and I'm probably not gonna do that in the future as Cygwin compiled stuff wasn't working on school's Unix machines and so on. It was a mess of moving files from here to there.
But I'm assuming that the things you're gonna be writing won't be very complicated so such a setup should be okay. There are probably better ways :v:
Edit: Did a Google search and saw this : [url]https://netbeans.org/features/cpp/[/url], you should try it out.
[QUOTE=Swebonny;45633578]Edit: Did a Google search and saw this : [url]https://netbeans.org/features/cpp/[/url], you should try it out.[/QUOTE]
When I was looking around a year or so ago DevC++ was recommended with the warning that it's pretty outdated. I definitely should have searched again, this looks great. Thanks!
In LISP I'm trying to make a function foo(A L) where it will remove every A in list L.
[code]
(defun foo(A L)
(cond ((null L) nil)
(T (cond ((equal A (car L)) (foo (A (cdr L))))
(T (cons (car L) (foo (A (cdr L)))))))
))
[/code]
Doesn't work, I don't know why. The parenthesis look right to me. Is there a problem with my recursive call?
omg I'm so dumb, I put A in parenthesis when recursively calling my function. It should be
[code]
(defun foo(A L)
(cond ((null L) nil)
(T (cond ((equal A (car L)) (foo A (cdr L)))
(T (cons (car L) (foo A (cdr L))))))
))
[/code]
[QUOTE=Cittidel;45633473]Hey guys, I'm going to be taking a class on C in two months. I was reading the first chapter of the textbook that I'll need for the class and it makes constant references to unix, such as tools like lint that are "traditionally provided with UNIX" (and I'm guessing Linux as well.) The book does not recommend any IDEs. There is no doubt that my professor will make all of this clear when class starts, but I'd like to get a head start while I have some free time.
Given that Visual Studio doesn't even support C unless you use some weird trickery in a C++ project, should I use a virtual machine running a Linux distribution for programming? I know that C++, in most cases, supports C, but it feels weird. I get the vibe that C is a pain on non-unix OSes like Windows.
I'm fairly comfortable on the Unix command line, and could learn Windows as well if there was an easy solution besides a virtual machine.
What is your guys' setup for programming in C? I don't have very much programming experience at all, I've tried a little bit of C++, Objective C and Python but nothing past something like 'for loops'.
[url=http://www.amazon.com/Programming-Modern-Approach-2nd-Edition/dp/0393979504]"C Programming: A Modern Approach, 2nd Edition" is the book I'll be using.[/url][/QUOTE]
I used a linux VM with gcc and gedit and it worked a treat, though I would definitely recommend sublime over gedit. Don't use anything that compiles to C++ as a way of programming to C.
C is definitely a pain on windows and I wouldn't bother. Run a virtual machine imo
[QUOTE=BackwardSpy;45633229]The most glaringly obvious thing to me is how quite often you have something like this:
[code]if roll1 == 6:
ps = (ps + roll1 + randint(1, 6))
print("Physical Strength:", ps)
else:
print("Physical Strength:", ps)[/code]
that bit of text is going to get printed whether the 'if' condition is satisfied or not, so you might as well just print it anyway, like this:
[code]if roll1 == 6:
ps = (ps + roll1 + randint(1, 6))
print("Physical Strength:", ps)[/code][/QUOTE]
Oh, thanks! I thought I needed to specify an "else" statement for every "if" statement. I'll fix that right now!
I'm trying to do height map based terrain, in an OpenGL/C++ based project. I can't for the life of me figure out how to read through the pixels in the image, check their color, and set height accordingly.
How should I approach this individual pixel reading? And should I set the terrain heights when I create the vertices or in the vertex shader?
It depends on what you want to do but you can sample your heightmap texture in your vertex shader to offset the vertex. This way makes it dynamic too by just having to buffer in new pixels to your heightmap texture.
Your vertex buffer can be as simple as a position per vertex. The coord for the heightmap can use the XY position of the vertex if you give your shader a size of the terrain varying. If you want you can just put the coord in the vertex buffer if you'd rather not do that. The height gets sampled from your heightmap and used as the Y position of your vertex. Vertex normals can be calculated in shader by sampling the 4 adjacent heights to create a normal vector. I can give you the code for that if you need it.
I'm kinda stuck with OpenGL.
How would you pass multiple data to a vertex shader? I want to pass vec3 Position, vec3 Color, vec3 Normal, vec2 UV and int BlockIndex. It works partially when i pass them as vertex attributes, i can pass position, color and uvs (normals not yet implemented), but for some reason i can't pass the BlockIndex, i get garbage.
[QUOTE=cartman300;45641269]I'm kinda stuck with OpenGL.
How would you pass multiple data to a vertex shader? I want to pass vec3 Position, vec3 Color, vec3 Normal, vec2 UV and int BlockIndex. It works partially when i pass them as vertex attributes, i can pass position, color and uvs (normals not yet implemented), but for some reason i can't pass the BlockIndex, i get garbage.[/QUOTE]
If you get garbage the attribute configuration is wrong, are you sure all data types match?
[QUOTE=Tamschi;45641459]If you get garbage the attribute configuration is wrong, are you sure all data types match?[/QUOTE]
I'm pretty sure.
UV Data, it works just fine
[code]
GL.BindBuffer(BufferTarget.ArrayBuffer, Buffs[2]);
GL.BufferData<Vector2>(BufferTarget.ArrayBuffer,
new IntPtr(UVData.Length * Vector2.SizeInBytes), UVData, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(attrib_uv);
GL.VertexAttribPointer(attrib_uv, 2, VertexAttribPointerType.Float, false, 0, 0);
[/code]
Block index data, it does not work fine
[code]
GL.BindBuffer(BufferTarget.ArrayBuffer, Buffs[3]);
GL.BufferData<uint>(BufferTarget.ArrayBuffer,
new IntPtr(UVData.Length * sizeof(uint)), BlockIndsData, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(attrib_idx);
GL.VertexAttribPointer(attrib_idx, 1, VertexAttribPointerType.UnsignedInt, false, 0, 0);
[/code]
I'm testing it by trying to offset water verts by half a block if block index equals to water index. (adding the index to the Z of vert just makes a huge clusterfuck like [URL="http://i.imgur.com/s1jJK2v.png"]this[/URL])
[editline]9th August 2014[/editline]
Okay i got it working by just using vectors.
[editline]9th August 2014[/editline]
Actually [URL="http://i.imgur.com/aVKcILg.png"]this[/URL] is not my desired result.
[editline]9th August 2014[/editline]
Okay DEFINITELY fixed this time, i must be retarded or something. My block index data length wasn't equal to vertex data length.
Edit; Fixed. Windows is a pain in the ass.
[QUOTE=Big Dumb American;45632598]I did it, I did it!
(The randomly generated me is ugly and dumb. :pwn: )
One question, though. Check out this code and let me know if I've been inefficient. I don't know if I did a lot more work than was necessary for this, so please let me know if you have any tips or tricks for a simpler code! Thanks.
[code]snip[/code]
There is still a lot that I'd like to do, but I'm a bit pleased at myself for having at least figured out this little bit! I guess my next step is working on the skills database, figuring out how to select those skills, and how to apply any attribute bonuses those skills might give to the character's attributes. That sounds very complicated and time consuming, so be at the ready for a lot of annoying questions.[/QUOTE]
Congratulations, you made the computer do something.
In fact, you made the computer do something repeatedly, over and over.
[code]if attributeScore >= 16:
attributeScore = attributeScore + attributeBonus
if attributeBonus == 6:
attributeScore = (attributeScore + attributeBonus + randint(1, 6))
print("Attribute Name:", attributeScore )
else:
print("Attribute Name:", attributeScore )
else:
print("Attribute Name:", attributeScore )[/code]
The other guy advised this:
[code]if attributeScore >= 16:
attributeScore = attributeScore + attributeBonus
if attributeBonus == 6:
attributeScore = (attributeScore + attributeBonus + randint(1, 6))
print("Attribute Name:", attributeScore )[/code]
But you're still repeating that chunk of code and over again, complete with using different variables for every iteration.
Now, it could be that you want to keep track of the attribute variables -- that's ok. The bonus rolls are unimportant though!
Here is my quick attempt at refactoring (meaning 'rearranging or rewriting to make cleaner') your code so that it doesn't repeat itself. I haven't tested this, so you might get some compile errors when you try to run it.
[code]__author__ = 'Fatal Error'
from random import randint
def getDiceSum(sides, number):
"""Returns the result of rolling a generic die repeatedly
sides = the number of sides on the die
number = the number of times to roll the die and add the result"""
assert(number > 0) #you must roll at least 1 die
assert(sides >= 4) #a die must have 4 or more sides
sum = 0 #the sum starts at 0
for ii in range(0, number): #'roll' the die successively
sum += randint(1, sides) #adding the value
return sum #return the sum
def calculateAttributeScore():
"""Returns the result of rolling 3 6-sided die, adding an additional
6-sided if the previous roll was >= 16, and adding a final 6-sided die
if the previous roll equaled 6."""
score = getDiceSum(6, 3) #roll 3 6-sided die
if score >= 16: #if the total is over 16
bonus = getDiceSum(6, 1) #roll a bonus die
score += bonus #add the bonus
if bonus == 6: #if the bonus was a 6
score += getDiceSum(6, 1) #add another bonus die
return score
#declare a list of attributes
#every character has one of these attributes
DEFINED_ATTRIBUTES = ["Physical Strength", #0
"Physical Beauty", #1
"Physical Prowess", #2
"Physical Endurance", #3
"Mental Affinity", #4
"Mental Endurance", #5
"Intelligence", #6
"Speed"] #7
name = input("What is your name: ")
print()
print("____________________________________________")
print("Character Name:", name)
#these are the specific attributes
#generated for this character
charAttributes = []
for attribute in DEFINED_ATTRIBUTES:
score = calculateAttributeScore()
charAttributes.append(score) #add the score to list of attributes
print(attribute, score) #print the attribute and score
#now we can access attributes later, e.g.
#charAttributes[0] will return the value
#of the "Physical Strength" attribute
print("____________________________________________")[/code]
I'm trying to reverse engineer the file format used for storing the packed model data in Farming Simulator 2013. Some of their models are stored in plaintext using some sort of XML - OBJ combination, but models with larger amount of data are compressed into ".i3d.shapes" files, which are just a garbled mess. I've tried shoving one into a hex editor but I can't find anything plaintext nor interesting. I do got access to the exe, and I got Ollydbg installed, but I have absolutely no clue on how I can intersect where the file is being read and then try to decode the assembly from there. Is there any way to find out and then break the process when ollydbg finds out that its opening a file or something?
I've also shoved it into some program that scans the file and compares it to loads of different compression formats, and it got no matches.
[QUOTE=cartman300;45641627]I'm pretty sure.
UV Data, it works just fine
[code]
GL.BindBuffer(BufferTarget.ArrayBuffer, Buffs[2]);
GL.BufferData<Vector2>(BufferTarget.ArrayBuffer,
new IntPtr(UVData.Length * Vector2.SizeInBytes), UVData, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(attrib_uv);
GL.VertexAttribPointer(attrib_uv, 2, VertexAttribPointerType.Float, false, 0, 0);
[/code]
Block index data, it does not work fine
[code]
GL.BindBuffer(BufferTarget.ArrayBuffer, Buffs[3]);
GL.BufferData<uint>(BufferTarget.ArrayBuffer,
new IntPtr(UVData.Length * sizeof(uint)), BlockIndsData, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(attrib_idx);
GL.VertexAttribPointer(attrib_idx, 1, VertexAttribPointerType.UnsignedInt, false, 0, 0);
[/code]
I'm testing it by trying to offset water verts by half a block if block index equals to water index. (adding the index to the Z of vert just makes a huge clusterfuck like [URL="http://i.imgur.com/s1jJK2v.png"]this[/URL])
[editline]9th August 2014[/editline]
Okay i got it working by just using vectors.
[editline]9th August 2014[/editline]
Actually [URL="http://i.imgur.com/aVKcILg.png"]this[/URL] is not my desired result.
[editline]9th August 2014[/editline]
Okay DEFINITELY fixed this time, i must be retarded or something. My block index data length wasn't equal to vertex data length.[/QUOTE]
For future reference: When passing integer data, OpenGL requires you to use VertexAttrib[b]I[/b]Pointer for whatever reason.
So, I'm doing a C++/OpenGL project and I can not for the life of me figure out what needs to be deleted from memory and what doesn't. This is something I have been struggling with for a bit, thanks to my background being entirely in Java/C# that manage memory for you, and googling hasn't helped much. Is there a hard and fast rule for what needs to be freed from memory and what doesn't, either in the general context of C++ or in OpenGL specifically? Any resources or tips would be lovely!
Any raw pointers need to be deleted, but using unique_ptr and shared_ptr will alleviate this. Anything that you create through OpenGL functions (all the generating functions such as glGenBuffers and glCreateShader) should have a complementary delete call(glDeleteBuffers, glDeleteShader). In C++ you don't have to free anything unless you explicitly call new. That's the beauty of RAII.
I have my own question, which I know I should know the answer to. Lets say a function returns some type, maybe a vector
[cpp]std::vector<int> foo(){}[/cpp]
If I want to use that value temporarily, can I just store it in a reference? Or does its lifetime run out as soon as the function returns? Does the const qualifier matter?
[cpp]const std::vector<int> & bar = foo();[/cpp]
Actually I realize now that this will most likely be move optimized, or return value optimized if moving isn't possible so it's a pretty moot question.
Yes you are allowed to assign a returned value to a const reference and it is well-defined behavior, it will be released when it goes out of scope. But it's pretty much pointless because C++11 guarantees move optimization. Even without that guarantee, any semi-competent compiler will be able to optimize the copy out with RVO.
The const-qualifier does matter as without it, it would not compile. Only lvalues can be assigned to a non-const reference, and a if a function returns by value, then that return value is an rvalue (more specifically it's a prvalue).
[QUOTE=Dvd;45644386]Congratulations, you made the computer do something.
- CODE CODE CODE -[/QUOTE]
That's beautiful, thanks so much! I'm looking through this trying to figure out how things work, and I have a couple of questions if you don't mind. Firstly, this bit at the beginning where you wrote the rules for the die caster. I'll put my comments and questions in using the #comment sign, just to make sure I fully understand what and how you're doing this.
[code]def getDiceSum(sides, number): #1
assert(number > 0) #2
assert(sides >= 4) #3
sum = 0 #4
for ii in range(0, number): #5
sum += randint(1, sides) #6
return sum #7
#1: Here you are simply naming the function you're about to define, and asserting that its variables are "sides" and "number," right?
#2: Here you are stating that the number of dice being rolled (or the number of random integers being generated) has to be at least 1
#3: Here you are defining that the dice being rolled must have at least 4 sides (or that 4 is the minimum upper limit for a range of random numbers, meaning the range must be at least 1-4)
#4: This is simply defining the starting point of zero as "sum"
#5: "for ii in range" is saying that the second variable in the the range (the space between parenthesis) is going to be used by the next line? In this case, "number" is directing the action?
#6: A randomly generated number between 1 and the given number of sides is selected, and added to the sum, and then more random integers are generated and added to the sum based on the "number" variable in the previous line? I don't understand the syntax, however. Why is it "sum += randint" and not "sum + randint?" Or is either okay? Or am I missing something?
#7: And finally, the total is given
[/code]
Sorry, I know this probably seems like really basic stuff, but I still really have to examine the commands in order to understand them. If I can figure out the commands you're giving, then that means I can figure out how to use them for myself later! So the end result of this code is that if you plug in, "getDiceSum(sides, number)" anywhere else in the code, it will say, "[I]'sides' defines the upper range of a random integer I need to choose, 'number' defines the total number of random integers I need to choose from within that range, and then I need to add each of those random integers together and spit out the sum.[/I]" Right?
Once I completely understand this section, I'll do a similar routine with the next section. Thanks for all the help!
[QUOTE=Big Dumb American;45653783]
[code]#1: Here you are simply naming the function you're about to define, and asserting that its variables are "sides" and "number," right?[/code][/QUOTE]
Correct.
[QUOTE=Big Dumb American;45653783]
[code]#2: Here you are stating that the number of dice being rolled (or the number of random integers being generated) has to be at least 1
#3: Here you are defining that the dice being rolled must have at least 4 sides (or that 4 is the minimum upper limit for a range of random numbers, meaning the range must be at least 1-4)
#4: This is simply defining the starting point of zero as "sum"[/code][/QUOTE]
Correct. The assert(...) statements help prevent you from accidentally typing something that doesn't make any sense -- there is no such thing as a die with less than 4 sides. A more stringent assert might check that 'sides' is one of the following: 4, 6, 8, 10, 12, 20, or 100.
[QUOTE=Big Dumb American;45653783]
[code]#5: "for ii in range" is saying that the second variable in the the range (the space between parenthesis) is going to be used by the next line? In this case, "number" is directing the action?[/code][/QUOTE]
This is probably the most confusing line for a new programmer. It's easy to look at it in reverse.
[code]for ii in range(0,5):
print("hello")[/code]
This is a for-loop in Python. Everything indented after this is going to be repeated, for some amount of times. How many times will "hello" be printed?
You need to understand that the for-loop in Python is just iterating over every object in a collection. "Ok, but I don't have a collection. I just want to print hello repeatedly!"
Sure, but your collection is the *number* of times that hello will be printed.
The function range(a,b) returns a list of numbers starting at a and ending at b - 1. e.g.: range(0,5) == [0, 1, 2, 3, 4]
Therefore when we loop over every element in that range, we will loop 5 times (the number of elements). "ii" is just a name applied to the current element of the loop -- it will in this case successively refer to 0, 1, 2, 3, and 4 depending on which iteration of the loop is in progress.
Look at it this way:
If you only have one d6, how do you roll 3d6? You roll that one die repeatedly, adding the result as necessary. That's exactly what this code does. It takes a description of the number of sides (>= 4) and the number of die you want to roll (> 0) and will calculate a sum from that based on repeatedly generating random integers.
[QUOTE=Big Dumb American;45653783]
[code]#6: A randomly generated number between 1 and the given number of sides is selected, and added to the sum, and then more random integers are generated and added to the sum based on the "number" variable in the previous line? I don't understand the syntax, however. Why is it "sum += randint" and not "sum + randint?" Or is either okay? Or am I missing something?
#7: And finally, the total is given[/code][/QUOTE]
Correct. The following are equivalent in almost all programming languages:
[code]b += a
b = b + a[/code]
Likewise, you have:
[code]b -= a
b = b - a
b *= a
b = b * a[/code]
et cetera.
Sorry, you need to Log In to post a reply to this thread.