Skip to content

Instantly share code, notes, and snippets.

@zachelko
zachelko / findAllOccurances.cpp
Created April 2, 2010 19:39
findAllOccurrences
QList<int> Hangman::findAllOccurrences(const QChar& theChar, const QString& theStr)
{
QList<int> occurrences;
for (int i = 0; i < theStr.size(); i++)
{
if (theStr.at(i).toUpper() == theChar.toUpper())
{
occurrences.push_back(i);
}
}
@zachelko
zachelko / ASCII-tricks.cpp
Created April 2, 2010 19:45
ASCII tricks
// Add letters to our vector using ASCII conversion (saves typing =p)
for (int i = 65; i < 91; i++)
{
char letter = static_cast<char> (i);
alphabet.push_back(QString(letter));
}
bool Hangman::isAlpha(const QString& key)
{
if (alphabet.size() > 0)
{
return alphabet.contains(key.toUpper());
}
return false;
}
@zachelko
zachelko / scope.cpp
Created April 3, 2010 02:20
Arbitary scope
class Foo
{
public:
Foo() { std::cout << "ctor\n"; }
~Foo() { std::cout << "dtor\n"; }
};
int main()
{
std::vector<Foo> foos;
{
@zachelko
zachelko / reverse1.c
Created April 8, 2010 00:37
Reversing a string the bad way
void reverse(char str[])
{
// Add 1 for the terminating character
int len = strlen(str) + 1;
char temp[len];
// Data resides from indices 0-2
int i = len - 2;
for (; i >= 0; --i)
{
@zachelko
zachelko / reverse2.c
Created April 8, 2010 00:39
Reversing a string the good way
void swap(char str[],
const int firstIndex,
const int secondIndex)
{
char temp = str[firstIndex];
str[firstIndex] = str[secondIndex];
str[secondIndex] = temp;
}
void reverse2(char src[])
@zachelko
zachelko / testcase.cpp
Created April 8, 2010 00:52
Test case
//utils.h
#ifndef UTILS_H
#define UTILS_H
#include
struct Point2d
{
float x;
@zachelko
zachelko / CircularList.hpp
Created April 8, 2010 02:01
Templated list data structure that enables you to toggle 'wrap-around' functionality to achieve a circular list.
// CircularList.hpp
//
// Zach Elko
// 2010
//
// Simple templated data structure that allows you to toggle 'wrap-around' behavior
// to achieve a circular list.
//
#ifndef CIRCULAR_LIST_H
#define CIRCULAR_LIST_H
@zachelko
zachelko / raindrops.py
Created April 8, 2010 04:14
Exclude rain drops below the beaker
# Find the shortest beaker so we know which drops to check for collisions
# against
beakerHeights = []
shortest = 0
for beaker in level.beakers: beakerHeights.append(beaker.rect.y)
beakerHeights.sort()
if (beakerHeights): shortest = beakerHeights[0]
@zachelko
zachelko / fillbeakers.py
Created April 8, 2010 05:18
Progressively filling a container with rain
def render(self, screen):
screen.blit(self.image, (self.rect.x, self.rect.y))
if (self.drops > 0):
x = self.rect.x + 2
rainColor = ()
if (self.full):
level = 1
rainColor = (255,0,0)
else: