shadwickman 159 Posting Pro in Training

I don't understand that part you added to your post... I eliminated the possibility of rand() duplicating cards in the deck by erasing each index rand() selected from the deck. The range for rand() each time is changed because it gets the new size of the deck (after the previous rindex was erased). At the end, the deck vector is assigned the value of the shuffled vector. This has been working for me so far; no crashes.

Does your way of doing it have something to do with more deep working of C++, and my way will lead to future issues that aren't apparent to me? Because as of now, it works.

shadwickman 159 Posting Pro in Training

Oh! Wow that was a stupid mistake. I didn't realize I had been trying to use deck[ i ] = i; on the vector without a length specified. Another option would be to replace line 32 with deck.push_back( i ); , right?
(I'm new to how vectors work in C++, obviously :P)

shadwickman 159 Posting Pro in Training

Hi, I was wondering what was wrong with the following code. It creates a vector <int> and assigns it the values 0 - 51. Then it shuffles these values and prints it to the screen. When I compile it, I get no errors, but when it runs, it throws an error on Windows and it quits the console window and tells me that it has encountered an error and needed to closed. Any ideas what's wrong?

#include <vector>
#include <iostream>
using namespace std;


// Globals
vector <int> deck;


// Shuffle deck
void shuffleDeck() {
	vector <int> shuffled;
	int s = shuffled.size();
	int decksize = deck.size();
	int rindex;

	while( s < decksize ) {
		rindex = ( rand() % deck.size() );
		shuffled.push_back( deck[ rindex ] );
		deck.erase( deck.begin() + rindex );
		s = shuffled.size();
	}

	deck = shuffled;
}


// Main
int main() {
	// Set deck as 0 through 51
	for( int i = 0; i < 52; i ++ )
		deck[ i ] = i;

	// Shuffle
	shuffleDeck();

	// Show deck
	vector <int>::iterator vit;
	for( vit = deck.begin(); vit != deck.end(); vit ++ )
		cout << *(vit) << endl;

	cin.get();
}
shadwickman 159 Posting Pro in Training

Ok, thanks! I had guessed through C++ 'strictness', there was a boost in performance, as opposed to something like Python (or Ruby, etc.) which is highly interpreted. I think I'll go with the vector method of doing this, as I need to do some serious reading up on the workings of arrays in C++ before attempting to use them extensively. Thanks for the help!

shadwickman 159 Posting Pro in Training

Ok, these are great suggestions, but just out of curiosity, why must C++ be so difficult/strict with this? My favourite language by far is Python, partly due to how utterly simple arrays and associative arrays (dictionaries) are. They can even contain different data types in different indices, making them so versatile. Just still a little confused as to why C++ is so, for lack of a better word, unhelpful when it comes to arrays. Please note, there most likely IS a good reason, just I am ignorant of it at the moment as I'm still fairly new to C++ after spending years with Python.

shadwickman 159 Posting Pro in Training

Hi, I'm really confused now about how to return an int[] from a C++ function. Inside the function, I need to dynamically resize the array, but at the end I want the variable returned to be an array, not a pointer, but I have no idea how to do this:

int findAll( string str, string sub ) {
	int count = 0;
	int *indices = new int[ count ];
	for( int i = 0; i < str.length(); i ++ ) {
		if( str.find( sub, i ) == i ) {
			count += 1;
			int *temp = new int[ count ];
			for( int j = 0; j < count; j ++ )
				temp[ j ] = indices[ j ];
			delete [] indices;
			indices = temp;
		}
	}
	return indices;
}

The problem is that this gives me the error: invalid conversion from `int*' to `int' How do I return the actual array, rather than the pointer? Thanks!

shadwickman 159 Posting Pro in Training

Oh! Ok, sorry about that misunderstanding haha.

shadwickman 159 Posting Pro in Training

Why Painful? Yes thing aren't easy but possible!
You have to state clearly everything where have you failed or what you want to do but have no Idea where to start. From there it is simple to help you! Don't give up so easily!

Um... evstevemd, I don't believe he's giving up. 'Painfully obvious' as in he couldn't believe he didn't think of it before. I know your English isn't perfect, but just to let you know what that expression is.

shadwickman 159 Posting Pro in Training

To use the functions that a module contains, you need to import them first, like Aia showed.
Option #1:

import math
math.sqrt( x )

And option #2 (to import all functions from math):

from math import *
sqrt( x )
shadwickman 159 Posting Pro in Training

I'd personally suggest using wxPython if you haven't chosen a GUI package yet. You'd just need a TextCtrl for the name, a set of CheckBoxes for the different toppings, and a 2 RadioButtons for the choice of bun (so only 1 can be selected), and then a Button which executes the info-gathering function.
You can find wxPython at: http://www.wxpython.org/

(I'm agreeing with jlm699 and evstevemd in that this sounds like an assignment for a class, therefore I'll be happy to debug/suggest ideas for your code, but you'll need to post what you come up with in order for us to help. Your main post lists what you need done as if it's an order to us, but you'll need to actually put some of your own effort into this.)

shadwickman 159 Posting Pro in Training

I've used Panda3D, but never much more than tweaking shaders and such. However, it sounds like you're going to need to make a basic algorithm to calculate the movement for the monster, then apply it to the monster so that it continually finds a path leading to the knight and attempts to follow it. Like paulthom said though, for the part dealing with Panda3D methods/classes, you're better off dealing with the forums made specifically for this 3D engine.

shadwickman 159 Posting Pro in Training

Why don't you try using a wxListBox set with the style flag for allowing only a single item to be selected at once? It's a simple solution if you're using wxPython.

shadwickman 159 Posting Pro in Training

Oh, I didn't think about creating my own str class. That's a great idea and it works wonderfully. Thanks Gribouillis!

shadwickman 159 Posting Pro in Training

Hi, I didn't really know how to word the title, but I was just wondering if it was possible to write a function and make it accessible as if it was any other str function. It was a simple (but useful) few lines which returned a list of all the occurrences of a substring within a string. I'd have to call it by findAll( string, substring ) , but I would like to be able to call it like the built-in find function, i.e. string.findAll( substring ) . Is this possible? Thanks!

shadwickman 159 Posting Pro in Training

YES! Thanks for that threading code, because I have it working perfectly now! Thanks again!

shadwickman 159 Posting Pro in Training

Ah, I didn't think of that. I'll take a look into threading (I've never used it before) to see if it helps. Thanks!

shadwickman 159 Posting Pro in Training

Hi, I have a GUI written with wxPython that contains a form. When the button is clicked, a script is executed to cycle through a specified directory and resize the images to a 50% size (the image manipulation is done with PIL). I used to run this script via the console/terminal, and during the image resizing and such, it would print progress to the screen. This whole process worked fine just running plain from the console, but I've rigged it to print the output to a multi-line text ctrl in the GUI now instead. When I run the script, everything is fine and works, EXCEPT that the GUI freezes during the processing of the images. It will finish the tasks correctly, it just freezes the GUI while doing it. Does anyone know what's happening?

(Yes, the processing is fairly intensive, but when running in a console, messages were still able to be printed during it. It's only now that I've moved the output to a text ctrl on the GUI that I have issues with it.)

shadwickman 159 Posting Pro in Training

I was going to suggest using Python + wxPython to make a text editor like Notepad or Wordpad. But you don't know Python right?

shadwickman 159 Posting Pro in Training

I can try to help with #2. First, change im, im1, im2, etc to strings, so that the command for im looks like im = "image.crop(box)" . The value should be a string. When you want to call that specific command use exec im1 etc for each command.

shadwickman 159 Posting Pro in Training

What laptop is it? And what was originally wrong with it?

shadwickman 159 Posting Pro in Training

All I was able to find was this article, but from what it says, it sounds risky as each platform/distro is quite different and difficult.
http://lists.wxwidgets.org/pipermail/wxpython-users/2007-March/062720.html

lllllIllIlllI commented: Great post, helped, worked, brilliant +1
shadwickman 159 Posting Pro in Training

The one argument you gave it is self .

shadwickman 159 Posting Pro in Training

It also looks like your "getAttributesInfo()" function in the troll class doesn't have "self" passed to it as an argument, and the same goes for the orc one. I think that's what your error is coming from. They should read "def getAttributesInfo( self ):"

shadwickman 159 Posting Pro in Training

Can you please edit the post and put the code within CODE tags? Otherwise indentation gets lost, and as you know, identation is crucial in Python.
(And as a quick tip, you might want to make a base class for all weapons, and then have individual weapon classes extend that base one. Same goes for Actors (player, NPCs, enemies), and then have others extend that base class). Anyways, add the code tags above and then I'll try taking a look through your code.

shadwickman 159 Posting Pro in Training

Can you please put your code in python CODE tags so it has syntax highlighting, but most importantly indentation. Seeing as Python requires indentation, and the above has none, it's hard to sort through at the moment.

shadwickman 159 Posting Pro in Training

Oh, ok. The reason why I stick with 2.5 is because of WConio, and I haven't been able to find out how to get it to work with 2.6 yet. I'd need to modify the .pyd file for it, but I have no idea how to do that (the file looks like gibberish to me when I open it in Windows).
http://newcenturycomputers.net/projects/wconio.html

shadwickman 159 Posting Pro in Training

how do activate to Gedit syntax highlighting and auto complete features?

I went to Edit > Preferences to turn on syntax highlighting, but I don't know how or if it has an auto-completion feature. I just type everything myself and let it deal with syntax highlighting only.

shadwickman 159 Posting Pro in Training

When I try to program with wxPython using IDLE (on Vista), it also gives me this screwed up error about the wx.App() needing to be created first, all the times I run it after the first attempt. I gave up on this after a while and just switched to programming in Notepad++. Or I just boot Linux and use gedit.

shadwickman 159 Posting Pro in Training

I use Notepad++ when programming Python with Windows, and I use plain old gedit when I'm programming Python on Ubuntu.

shadwickman 159 Posting Pro in Training

why don't you upgrade to 2.5? I'm curious if there is special feature or just you love it!

Sorry, I don't understand you. What are you trying to say about Python 2.5? It's what I use, even though Python 2.6 is out now (or the early releases for 3.0).

shadwickman 159 Posting Pro in Training

Ah, brilliant! I didn't know that the exec function could be used to execute a python script... thanks - I learned something new! :D

shadwickman 159 Posting Pro in Training

Ah, I didn't know that myself either. Is there more of a general definition of it? I get what the example does, but I'm just curious about the sort of extent/functions that this term can be used to/for.

shadwickman 159 Posting Pro in Training

I don't know if there's a "proper" method of doing this, but I have the code look something like this:

# function to call
def myFunction():
    print "code here"
    return True


# main program
redo = True
while redo == True:
    redo = myFunction()
    # if myFunction does not return True, then the code will end.

That's a pretty basic, although most likely not "proper" or "correct" way of doing it. Hope that helped!

shadwickman 159 Posting Pro in Training

I've tried that but I get an error saying that the list index is out of range, I'm guessing because everytime you delete a letter, the range changes.

Yes, but it's easy to fix. Here's the updated code:

let = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]

import random

for i in range(3):
    pos = random.randint(0,len(let))
    letter = let[pos]
    print letter,

All I did was change the line with the random function so that it went from 0 to the length of the array, therefore making sure indices that don't exist anymore don't get called.

shadwickman 159 Posting Pro in Training

You could always just add del let[ pos ] after that letter has been printed, thus deleting that letter from the list. It'll never be selected again.

shadwickman 159 Posting Pro in Training

What's the actual question you're asking? You just kinda gave a description and plopped your code out. If this is for a school project (which I assume it is), I'll try to help...

shadwickman 159 Posting Pro in Training

The % operator is used to return the remainder if one number were to be divided by another. So 5 % 2 would return 1, because 2 goes into 5 twice, with 1 left over. Another example is 10 % 2 which would return 2. 4 goes in twice, with 2 left over.
So x % n == 0 just means that there is no remainder (it fits perfectly).
Hope that helped!

shadwickman 159 Posting Pro in Training

Are you actually looking to assign a value to "fibnum"? Because like woooee said, it'll always return None. If you added a line like return sum at the end of the "getfibnum()" function, then "fibnum" would be set to the same value as "sum" within the "getfibnum()" function.
If you only want to call the "getfibnum()" function and not assign a result to a variable, you can just use "getfibnum()" in place of "fibnum = getfibnum()".

shadwickman 159 Posting Pro in Training

I don't have scitools, so I can't test this code, but why don't you try changing the line:

line = x[i], resultat

to

line = str( x[i] ), str( resultat )

Tell me if that makes a difference!

shadwickman 159 Posting Pro in Training

Thanks for pointing that out! Must be the sun was shining in my eyes. I corrected it

No problems! And thanks for this other post you found by shanenin; it has restored my interesting in small-scale logic games (which I had no idea where/how to start work on before).

shadwickman 159 Posting Pro in Training

It's just that the link that Vegaseat posted didn't include the last ')' in it. Here's the corrected one:

http://en.literateprograms.org/Tic_Tac_Toe_(Python)

shadwickman 159 Posting Pro in Training

Wow, I've been wanting to do something like this for a while. Thanks for the link Vegaseat!

shadwickman 159 Posting Pro in Training

Originally, I started Java at age 11 and C++ at age 13, and at the time, I found the strict and very rigid declarations/style of Java and C++ to be a little confusing and overwhelming.
After finding Python, I just came to love its simplicity compared to these other two languages, and the more relaxed form. I just find Python to be much more flexible; but this is a personal thing, as I don't make professional or exceedingly complex programs. All these languages have pros and cons (first one that comes to mind is the speed of C++ vs. Python - where C++ wins in most cases), but I myself found it easier to work with a more lax language when experimenting with ideas I've never tried before (namely an A.I.).
I've still been having fair trouble with a real artificial intelligence, but I haven't worked on anything dealing with that in a few months. This thread makes me want to get back into that though haha!
As Vegaseat mentioned, Python's string processing/manipulation is quite extensive, and I love that. That is also one of the reasons that I've stuck with Python mainly, and dictionaries are incredibly useful. Not sure if that fully answered your question, evstevemd!

shadwickman 159 Posting Pro in Training

Ah! sys.stdout.write() .... I've seen it before but I had no clue what it really was. Thanks for pointing it out, Aia!

shadwickman 159 Posting Pro in Training

Hello! I've run into a slight problem with my script.
As I iterate through a list of strings, I need to print each string, but they all need to print on the same line, so I used this:

for item in myList:
    print item,

The comma puts each item on the same line, but the problem is that it adds a space between the items. Is there any way around this? I cannot use:

out = ""
for item in myList:
    out += item
print out

Depending on the string, the colour in the console gets changed accordingly before printing it, therefore I cannot add them all to one output string and then that print last. Any help is appreciated!

shadwickman 159 Posting Pro in Training

I don't know if this has been mentioned yet (and I'm not going to look through the many pages of this thread to check), but write a simple program to convert a number in our system (base of 10) to a different base - such as binary (2), or hexadecimal (16). Or even a base of 3, 4, 5, e, pi, etc.

EX: a decimal number of 157 to a binary number would be: 10011101

Here's a useful little segment on binary numbers:
http://www.helpwithpcs.com/courses/binary-numbers.htm

shadwickman 159 Posting Pro in Training

I think solsteel's idea about splitting the sentence into a list first is much better (why didn't I think of that?), and he implemented an error catching system. I hope all the responses answer your question Daiosmith!

shadwickman 159 Posting Pro in Training

I'm sorry for the stupid above post which is only advertising a different site.
Anyways, why not try:

sentence = "The quick brown fox jumps over the lazy dog."
search1 = "the" # start word
search2 = "fox" # end word
index1 = sentence.lower().find( search1 ) # search through as lowercase
index2 = sentence.lower().find( search2 )
words = sentence[ index1 + len(search1) : index2 ].split(" ") # split into list of words
for a in range( len(words) ): # remove blank spaces
    try:
        if words[ a ] == "":
            del words[ a ]
    except:
        pass
print words # output

Result: ['quick', 'brown'] It looks fairly long for such a simple function, but all that the for loops does is remove any blank spaces. The result is the list words , which contains the in-between words.
Hope that helps!

shadwickman 159 Posting Pro in Training

What program are you writing your code in? If you're using something like Notepad, that has no syntax highlighting. I personally use IDLE (which came with the download of python), but there are many other code editors out there.

shadwickman 159 Posting Pro in Training

Hi Clipper34. I'm not 100% sure what you're asking specifically, but I use wxPython. I prefer it to Tkinter - just a personal choice. You can go to http://wxpython.org/ to download wxPython and begin using it.

As for positioning a button absolutely, just set it's position during it's creation. Example:

self.my_button = wx.Button( self, id=-1, label="Click me!", pos=(25, 15) )

Or you could use:

self.my_button.SetPosition( (25, 15) )

for after it's creation.


That would set the button at the specified (x, y) coordinates of (25, 15). But you should browse through the wxPython documentation for many, many good examples to get you started with wxPython.

Here's an example app that positions a button at (25, 15) and handles the button click event:

import wx

class MyPanel( wx.Panel ):
    def __init__( self, id, parent ):
        wx.Panel.__init__( self, id, parent, size=(200, 200) )

        self.my_button = wx.Button( self, id=-1, label="Click me!", pos=(25, 15) )
        self.my_button.Bind( wx.EVT_BUTTON, self.clickButton )

    def clickButton( self, event ):
        print "Clicked!"

my_app = wx.App()
my_frame = wx.Frame( parent=None, id=-1, size=(200, 200) )
MyPanel( my_frame, -1 )
my_frame.Show( True )
my_app.MainLoop()

Good luck with your coding!