• What Do You Need Help With? V6
    7,544 replies, posted
I just finished learning about recursion in school, and according to my course schedule, that's the last thing I'm learning. Now we're just hammering out tests to practice for our ap exams. Are there any cool things that I can start doing now? I really don't know where to begin.
You now possess the ability to overflow the stack
Little python script, put my problem in pseudocode [code]numlines = len(open('smallTriangle.txt').readlines ( )) lines = [[]*numlines for i in range(numlines)] linecount = 0 with open('smallTriangle.txt', 'r') as f: for x in f: x = x.rstrip() if not x: continue lines[linecount].append(x.split( )) linecount = linecount + 1 numberabove = 0 space = 0 total = 0 for i in range(numlines): total += numberabove #space = whatever place in lines[i-1] numberabove is #e.g. if lines[1] has two elements, and numberabove is at [0], then #i want space to be 0 adjacentnumsbelow = map(int, lines[i][0])[space-1:space+1] numberabove = max(adjacentnumsbelow) print total [/code] Here is smallTriangle.txt [code]3 7 4 2 4 6 8 5 9 3[/code] Basically, I want space to be what place in the array numberabove takes up. Adjacentnumsbelow works by taking the current numbers only directly below it and 1 to the right (corresponds to how it's interpreted, trust me).
How can I create a function that takes the memory address of an array and then prints the contents of it? Should I use pointers or references? Here's my where I got stuck at: [code] #include <Windows.h> #include <iostream> void PrintArray() { // Get the memory address of Arr std::cout << "First element: " << std::endl; // And print the first element } int main() { int Arr[10] = {1,2,3,4,5,6,7,8,9,10}; PrintArray(); // Pass the memory address of Arr here return 0x0; } [/code]
[QUOTE=ollie;40181103]How can I create a function that takes the memory address of an array and then prints the contents of it? Should I use pointers or references? Here's my where I got stuck at: [code] #include <Windows.h> #include <iostream> void PrintArray() { // Get the memory address of Arr std::cout << "First element: " << std::endl; // And print the first element } int main() { int Arr[10] = {1,2,3,4,5,6,7,8,9,10}; PrintArray(); // Pass the memory address of Arr here return 0x0; } [/code][/QUOTE] I'm not a C++ programmer but here's what I did: [cpp] #include "stdafx.h" #include <iostream> void printarray(const char* firstelement) { while(*firstelement != '\0') std::cout << *firstelement++ << std::endl; } int _tmain(int argc, char* argv[]) { char arr[] = {'h','e','l','l','o','\0'}; printarray(&arr[0]); return 0; } [/cpp]
[QUOTE=FlashStock;40181606]I'm not a C++ programmer but here's what I did: [cpp]#include "stdafx.h" #include <iostream> void printarray(const char* firstelement) { while(*firstelement != '\0') std::cout << *firstelement++ << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { char arr[] = {'h','e','l','l','o','\0'}; printarray(&arr[0]); return 0; }[/cpp][/QUOTE] I know this is completely irrelevant, but don't use tchar. Seriously, it'll break your code.
Also, &arr[0] is the same as simply arr, since it's a pointer to begin with.
[QUOTE=account;40182118]Also, &arr[0] is the same as simply arr, since it's a pointer to begin with.[/QUOTE] Ah, cool!
Using [cpp]&arr[0][/cpp] Is often preferred with C++, because that way it's compatible with some of the standard containers like vectors, where the data isn't stored at offset 0 in memory. That way, if you were to change your array to a vector, your code wouldn't break.
There's also [url=http://en.cppreference.com/w/cpp/container/vector/data]data()[/url] if you're using a vector.
[QUOTE=Meatpuppet;40176773]Little python script, put my problem in pseudocode [code]numlines = len(open('smallTriangle.txt').readlines ( )) lines = [[]*numlines for i in range(numlines)] linecount = 0 with open('smallTriangle.txt', 'r') as f: for x in f: x = x.rstrip() if not x: continue lines[linecount].append(x.split( )) linecount = linecount + 1 numberabove = 0 space = 0 total = 0 for i in range(numlines): total += numberabove #space = whatever place in lines[i-1] numberabove is #e.g. if lines[1] has two elements, and numberabove is at [0], then #i want space to be 0 adjacentnumsbelow = map(int, lines[i][0])[space-1:space+1] numberabove = max(adjacentnumsbelow) print total [/code] Here is smallTriangle.txt [code]3 7 4 2 4 6 8 5 9 3[/code] Basically, I want space to be what place in the array numberabove takes up. Adjacentnumsbelow works by taking the current numbers only directly below it and 1 to the right (corresponds to how it's interpreted, trust me).[/QUOTE] I'm not sure what you mean by "space to be what place in the array numberabove" but I'll just tell you you're tackling the problem wrong, you won't end up with the maximum number with that method (if I'm reading this right) edit: I think I get it, I would suggest just doing if (linebelow[index] > linebelow[index + 1]: number above = linebelow[index] space = i else: number above = linebelow[index + 1] space = index + 1 I just solved this problem if you want to see my solution edit: as to why you won't get the right answer, consider this: [code] 1 1 0 1 0 0 1 0 0 100 [/code] You'll stay on the left the whole time and get 4 rather than 101 Actually I really can't tell if you're traversing it that way or not, having "numberabove" as well as "numberbelow" in confusing me
Is Flash is a good place to begin creating games? I already know how to use java but I'm stuck on the actual implementation part, so far I've only been creating shitty little text apps.
Flash is a fairly good place to learn to make games, I guess, but since you already know Java, I'd recommend you work on that instead. [URL="http://zetcode.com/tutorials/javagamestutorial/"]This [/URL]is where I learnt to make games, and I think it's a pretty good tutorial.
I figured it wouldn't be that hard to pick up actionscript, but what you linked to me seems pretty awesome.
[QUOTE=What Up;40198263]I figured it wouldn't be that hard to pick up actionscript, but what you linked to me seems pretty awesome.[/QUOTE] I actually had to make a Sokoban game as a uni project, the example in this tutorial looks a whole lot like the work other project groups did :v:
How would I easily convert a 0 - 1 value determining the clicked coordinates into a number from 0 - 16? I know that each pixel is equal = 0.0625, but I lack the math skills to determine all this on the fly.
[QUOTE=dmillerw;40198415]How would I easily convert a 0 - 1 value determining the clicked coordinates into a number from 0 - 16? I know that each pixel is equal = 0.0625, but I lack the math skills to determine all this on the fly.[/QUOTE] Divide your value with pixel size. Value = 0.5 Pixel = 0.0625 Coordinate = Value / Pixel = 8
..god damn it. I knew that too. I don't know how to math.
Is it just me or is creating a simple TreeView unnecessarily complicated in Qt (or more specifically in PyQt4)? I want to create a view consisting of categories, each containing one list of items. In Gtk it was as simple as adding items consisting of a parent and data, with a parent of None meaning a root item. In Qt I'm still at a loss as to how to implement this. [url=http://www.daniweb.com/software-development/python/threads/312211/pyqt4-treemodel-example]This[/url] appears to be similar to what I need, but it seems outrageous that I would have to implement all of this behavior for something which seems so fundamental to any GUI toolkit :pwn: [editline]7th April 2013[/editline] It seems like there is no standard bookkeeping mechanism for a tree model in Qt? [url=https://qt-project.org/doc/qt-4.8/itemviews-simpletreemodel.html]This[/url] tutorial creates its own TreeItem without deriving from any Qt class, so am I just on my own? [editline]7th April 2013[/editline] I think I spoke too soon, because it appears there is a convenience class, QTreeWidget, which is item based instead and so appears to work much like the Gtk analog.
I'm writing a super simple encryption code. Basically it'll take a letter, shift it by 13 letters, and store it. (N -> A, W -> J, etc) (This has the advantage of decrypting itself, conveniently). Ideally I think I'd use ASCII values (it'd be a simple equation with them), but I'm not entirely sure how to use them. This is in c++, by the way. Any advice? [editline]8th April 2013[/editline] I'll also need to keep capital letters as capital letters and I can't figure outhow to do that without it getting REALLY big. [editline]8th April 2013[/editline] I'll also need to keep capital letters as capital letters and I can't figure outhow to do that without it getting REALLY big.
[cpp]char rot13(char c) { if (c >= 'a' && c <= 'z') c = 'a' + ((c - 'a') + 13) % 26; else if (c >= 'A' && c <= 'Z') c = 'A' + ((c - 'A') + 13) % 26; return c; }[/cpp]
Check whether it's larger than or equal to 'A' and smaller than or equal to 'Z', then add 13 and if the result is larger than 'Z' subtract ('Z'-'A'+1). Same for lower-case letters. In C++ you should be able to use char directly as number but the values depend on your string encoding (but should be the same for [A-Za-z] with the most common ones). Edit: Or the above. Note that these solutions break if the encoding has characters that are longer than a char.
huh. I can just say > 'a' and < 'z' and it'll pick abcdefghijklmnopqrstuvwxyz? (i don't know why i typed taht out) yeah, i need to encrypt full strings (no more than 80 characters), plut rot5 numbers but that'll be way easier. never really fucked around with comparing characters before.
ASCII characters are just 7-bit numbers that can be used in the same way
ah. this is a warmup for regionals or something. i'm almost positive i shouldn't do it but my professor's absolutely insisting that i sign up for them, so whatever i guess. i've got like eight months to learn enough shit.
-snip-
Guys I just wanted to know from fpp, how do you detect collision? I want to make something simple like pong (is it even simple?) in html5 with javascript. I'm sure I cans simply google it, but they're not always correct or the tutorials are just doing wrong. It would be great if there is a right way and a wrong way to do them.
I have zero knowledge of the language, but I'm going to guess that you wouldn't. I'd probably just pick a section of the screen where the paddle can go, then check if it passes that, then check if the paddle's in that range. If it is, rebound. Then again I imagine implementing physics with html isn't particularly simple.
I need some advice on general game class design. Say I want to make a console game where the level is randomly generated in an array and I want to check if the player has hit a wall. I'm confused as how the 'player' communicates with the 'level' since they're 2 separate classes. What I want to do is something like: [code] void player::getInput() { if(<right arrow key is pressed>) { if(level.getBlockAt(posX+1,posY) == <code for empty tile>) posX++; //etc... } } [/code] This piece of code would not work because 'player' knows nothing about 'level' so I can't access its functions. All my objects are instantiated in the main() function (player p1; level l1; while(running) { <run code> }) Should I create a big 'game' class that contains both a player object and a level object? It seems kind of repetitive: int game::getBlockAt(int x, int y) { return l1.getBlockAt(x,y); } thanks
[QUOTE=WhatTheEf;40216303]I need some advice on general game class design. Say I want to make a console game where the level is randomly generated in an array and I want to check if the player has hit a wall. I'm confused as how the 'player' communicates with the 'level' since they're 2 separate classes. What I want to do is something like: [code] void player::getInput() { if(<right arrow key is pressed>) { if(level.getBlockAt(posX+1,posY) == <code for empty tile>) posX++; //etc... } } [/code] This piece of code would not work because 'player' knows nothing about 'level' so I can't access its functions. All my objects are instantiated in the main() function (player p1; level l1; while(running) { <run code> }) Should I create a big 'game' class that contains both a player object and a level object? It seems kind of repetitive: int game::getBlockAt(int x, int y) { return l1.getBlockAt(x,y); } thanks[/QUOTE] You could make sure the class that contains the main() function also has a player and level, and pass on the positions of walls for the collision, or you could pass the walls to the constructor of the player, or you could handle the collision in a seperate class.
Sorry, you need to Log In to post a reply to this thread.