Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 19 Oct 2009
Offline Last Active May 20 2015 08:00 AM

Topics I've Started

Casting Pointer to Derived Type

09 November 2014 - 04:31 AM

Eh, this may be kinda stupid, but I have the traditional setup:

class tile {
    // ...

class tilePlayer : public tile {
    // ...

At one point in my code I had a pointer to a tile instance and I wanted to access it as a tilePlayer instance. So I tried something like this:

// map is an array of pointers to tile instances
(tilePlayer*)map[ index ]->PlayerSpecificFunction();

This didn't compile ( "PlayerSpecifcFunction not member of tile" ) so I tried this and it worked:

tilePlayer* p = (tilePlayer*)map[ index ];

Isn't the latter just the same as the former but with more steps? Or am I messing up the syntax?

Can't solve problems without googling?

11 August 2014 - 02:15 PM

To give some context, I'm 17 and I have been programming for a few years.


I was doing a code challenge on codewars.com, (similar to project euler), that involved converting to and from negabinary numbers (base negative 2). I had never heard of, nor even considered numbers with a negative base. But I managed to figure out some patterns in the tables the problem gave me and wrote a nice converter from negabinary to decimal. But the other way around, that was a different story. I tried for hours to figure out a way of converting decimal to negabinary but to no avail. It got reasonably late so I slept on it. The next day I tried for hours again but still couldn't figure out an algorithm. After this I googled it, found a wikipedia page with an algorithm all ready and waiting for me.


I had solved it, albeit with a little "research"...


I want to be the best programmer I can, and I think I'm on the right track by attempting these coding challenges. But I'm a little lost on what I should do when I'm faced with a situation like this. Almost all the problems I'll solve (at least early on) will have been already solved brilliantly by someone else. I'm sure If I was working, my boss wouldn't be impressed if he found out I'd spend the best part of 2 days trying to solve a problem that I could have solved in 2 minutes If I had googled it. But googling it feels like cheating, and I don't get to practice my problem solving abilities.


It seems like the best programmers are able to take problems that no one has solved before and develop solutions on their own.


I guess what I'm asking is how do I get really fucking good at problem solving? By spending massive ammounts of time thinking through problems for myself or by looking at as many past solutions as possible?

Should I break_the_build?

23 June 2014 - 01:53 PM

I'm going to preface this topic with the fact that I've only every worked alone on projects.


In the project I'm working on, I have got to a stage where I've finally decided to refactor one of the systems that a lot of the total code depends on. Refactoring this would probably mean refactoring a lot of other code afterwards. Now I realise that I've got to this scenario through a horrible combination of bad design choices and lack of forethought, but my question is what to do when I find myself in this situation.


Because I'm working alone on this project, it doesn't really matter if I just make a new branch, completely refactor the system and then all of the other code afterwards. This could mean that the project won't actually build for days. Again, not too much of a problem considering that I'm working by my self.


What would be your advice if I encounter this when I'm working with a team of programmers? Assuming that the refactoring work HAS to be done, how can I approach this problem so that I cause minimum disruption to the other programmers?


Thanks for your time.

Int Size, Endianness and Serialisation

14 June 2014 - 01:30 PM

I'm trying to write a function that takes an std::vector of game objects and saves them all as an image.


The game I'm making revolves around placing game objects in a specific way and I wanted users to be able to share different placements easily. Since there won't be much data, I decided that for every game object, each of it's applicable data members would be converted to a colour, and saved in the image as a pixel. To load game states my game would open the image and read each pixel from left to right, top to bottom and re-construct each game object.


The bit that I'm a little stuck on is how to convert a number to a colour, and back again. For example the x coordinate of a specific game object could be 42, I need a way to take 42, and convert it to 4 individual bytes (r,g,b,a).


I've realised that this, by itself, isn't that difficult. But I was wondering If I need to take the platform's integer size and endianness into consideration when I'm building this? And If so what would be a good way to deal with all that?



Javascript #include implemented in Python

03 June 2014 - 11:27 AM

So I was learning and messing around with javascript and html5 canvas when I found out that javascript has no include or import.

you either have to include extra files through the html or do some funky asynchronous loading or get Jquery to do it for you.


So a few hours later I produce this:

# compile.py

# command line utility that takes a javascript file (any text file really)
# as input, goes though it and replaces #include with
# the appropriate js files. Also minimises the resulting 
# large file and saves it.

# minimising and saving not yet implemented

# usage: compile.py file.js

# options:
# -m  minimised the resulting file.
# -o specify output filename
# NOTE - none of these implemented yet

import sys
import argparse

# handle arguments
parser = argparse.ArgumentParser( description="update this later pls" )
parser.add_argument( "filename" )
parser.add_argument( "-m", "--minimise", help="minimises the resultant file", action="store_true" )
parser.add_argument( "-o", "--output", help="filename of the resultant file" )
args = parser.parse_args()

# set options
optInputfile = args.filename
optMinimise = True if args.minimise else False
optOutputfile = args.output if args.output else "COMPILED_" + optInputfile

# extracts the next filename in double or single quotes from a position
def extract_filename ( string, position ):
    curPos = position
    curString = ""
    done = False
    inName = False
    while not done:
        if not inName:
            if string[curPos] == '"' or string[curPos] == "'":
                inName = True
            if string[curPos] != '"' and string[curPos] != "'":
                curString += string[curPos]
                done = True
        curPos += 1
        if curPos-position >= 40:
            # probably a missing " or '
            print( "COMPILE ERROR: #include filename too long. You've probably missed a ' or \"" )
    return curString

# replaces everything with spaces in 'string' from 'position' until 2 ' or " are found
def replace_include_line ( string, position ):
    curPos = position
    done = False
    quoteCount = 0
    curLen = 0
    while not done:
        curLen += 1
        if string[curPos] == '"' or string[curPos] == "'":
            quoteCount += 1
        if quoteCount >= 2:
            done = True
        curPos += 1
    newString = string[:position-1] + string[position+curLen:]
    return newString

# inserts string 'b' into string 'a' at position 'pos'
def string_insert ( a, b, pos ):
    return a[:pos] + b + a[pos:]

# recursive file parsing function
def parse_file ( filename ):
    file = open( filename, 'r' )
    fileStr = file.read()
    count = 0
    pos = 0
    while True:
        # find the next include
        pos = fileStr.find( "#include", pos )
        if pos == -1: break
        # read the filename
        includeFilename = extract_filename( fileStr, pos )
        # remove the #include from file
        fileStr = replace_include_line( fileStr, pos )
        # insert the included file
        fileStr = string_insert( fileStr, parse_file( includeFilename ), pos ) # woah here's where the magic happens
    return fileStr

print( parse_file( optInputfile ) )

It's a command line utility written in Python that parses text files for #include statements and inserts the appropriate files.

It's far from elegant and most of the extra features aren't done yet, at the moment it just prints out the resulting superfile. But the basic functionality of recursively replacing #include with the contents of a file is done.


So in any file that's parsed you can write:

#include "file.ext"

Thoughts? Am I crazy? Wasting my time? Has this been done/solved before?