c++

Experience page

Posted on

Updated my experience page as a self reflection on what kind of technical skills I’ve learned here in Japan ( https://kagato0110.wordpress.com/programming-languages/ )

… turns out I know quite a bit (in terms of technology) lol

スクリーンショット 2015-11-02 12.24.44

Advertisements

Find all combinations with repetitions (C++)

Posted on Updated on

One of the most mind boggling (yet very simple) programming problems I’ve found recently was to write a program that displays all combinations of a string with repetitions.

Lets say you have an array like so:

char _chars[] = {'A', 'B', 'C'};

Now write a function that creates the output like so:

AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBB BBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC

So how would we do this?
I would first store the char array into a vector like so:

std::string _tmpStr;
for (int i = 0; i < 3; i++)
{
     _tmpStr += _charArray[i];
}
std::vector<int> pos(_tmpStr.size(), 0);
Hoge::CombinationSequence(_tmpStr, pos, 0);  // call function to find all combinations

And somewhere in the source code I have declared and called the function like so:

/**
 *	可能な全ての組み合わせがを出力する
 */
void Hoge::CombinationSequence(const string& v, std::vector<int>& pos, int n)
{
    for (int i = 0; i < v.size(); i++)
    {
        for (int j = 0; j < v.size(); j++)
        {
            for (int k = 0; k < v.size(); k++)
            {
                cout << v[i] << v[j] << v[k] << " ";
            }
    }
}

Compiling and running this produces the output we expect.

Problem:
What if the input data increases by size?
How do we calculate a string of data that is maybe 100 elements long instead of 3?

Solution:
The simplest solution is to just add more for loops. But having 100 for loops is a pain in the butt. So Instead, we can use recursion, to call the function itself however times necessary. This means that the input data can change size and our function will automatically loop to different sized arrays, achieving a dynamic design.. Like so:

/**
 *	可能な全ての組み合わせがを出力する
 */
void Hoge::CombinationSequence(const string& v, std::vector<int>& pos, int n)
{
    if (n == v.size())
    {
        for (int i = 0; i != n; i++)
	{
	    cout << v[pos[i]];
	}
	cout << " ";
	return;
    }

    // Loop through the vector and update position to whichever element it is pointing to. Then pass that reference back to our function so we know which letter it is referring to.
    for (int i = 0; i != v.size(); i++)
    {
        pos[n] = i;
        Hoge::CombinationSequence(v, pos, n + 1);
    }
}

Hope this helps somebody out there. The solution was quite simple it didn’t even occur to me how simple it was.

By the way,
A, B, C = 3 types of data
3 x 3 = 9
9 x 3 = 27

We have with this example, 27 possible combinations.

‘ReceiveActorBeginOverlap’ marked ‘override’ but does not override any member functions – (Unreal Engine 4 with C++, Mac OSX)

Posted on Updated on

スクリーンショット 2015-09-20 21.15.00

After playing with Cocos2dx and Unity 5, I decided to challenge myself even further by working on a test project (FPS, 3rd person shooter etc) using Unreal Engine 4 with C++.

While working on a first person shooter project, I was in the middle of creating power up / health pick up items. All was going well until I ran into this error message:

‘ReceiveActorBeginOverlap’ marked ‘override’ but does not override any member functions

My source code below:

.h header file

    #pragma once
     #include "GameFramework/Actor.h"
     #include "HealthPack.generated.h"

    UCLASS()
    class TESTFPS_API AHealthPack : public AActor
    {
	    GENERATED_BODY()
	
    public:	
    	AHealthPack();	
	    AHealthPack(const FObjectInitializer& ObjectInitializer);	

	    virtual void 	BeginPlay()  override;
	    virtual void 	Tick( float DeltaSeconds ) override;
	    virtual void 	ReceiveActorBeginOverlap(class AActor* Other)	override;	

And in my .cpp implementation file l declare this method like this:

    void AHealthPack::ReceiveActorBeginOverlap(class AActor* Other)	
    {
     	if (Other != NULL)
	    {
	    	if (CanBePickedUp(Cast<ATestFPSCharacter>(Other)))
	    	{
	    		HandlePickUp(Cast<ATestFPSCharacter>(Other));
	    		HandleEffects();
	    		removeFromWorld();
	    	}
	    }
    }

Turns out that “ReceiveActorBeginOverlap” is not virtual so you cannot override this method (anymore). So to solve this you need to override <strong"NotifyActorBeginOverlap" and then call Super inside this function. Modifying the lines above to below resolves my error:

.h header file:

virtual void 	NotifyActorBeginOverlap(class AActor* Other)	override;

.cpp implementation file:

/**
 *	Check if we can pick up a health pack
 */
void AHealthPack::NotifyActorBeginOverlap(class AActor* Other)	
{
	Super::NotifyActorBeginOverlap(Other);

	if (Other != NULL)
	{
		if (CanBePickedUp(Cast< ATestFPSCharacter >(Other)))
		{
			HandlePickUp(Cast< ATestFPSCharacter >(Other));
			HandleEffects();
			removeFromWorld();
		}
	}
}

Samurai Gunship website finally released!

Posted on

Working at Pamprepps Games in Fukuoka Japan is awesome!
Today the official website for the game I developed for Android (iOS is coming soon) has been released!

スクリーンショット 2015-09-14 11.45.37

Link → http://pamprepps.com/samuraigunship/

Installation Failed: Invalid Argument – iOS Simulator & XCode 6.4

Posted on Updated on

I had this error since yesterday and couldn’t figure out what was causing this.

Tl:dr – Check your Resources folder in your project folder and make sure you don’t have copies. Then add them properly into your workspace in XCode.

The detailed version:
My initial thoughts was my Bundle name, identifier and product name were different but changing either wouldn’t work. But then I ran into this thread:

https://forums.developer.apple.com/thread/7338

Update: Xcode doesn’t like it when you have a referenced directory called ‘Resources’ within your resource bundle.. but instead of complaining about this, you end up wasting half the day trying to resolve all the other issues that it throws up at you instead (see previous two posts).

All is now resolved and working as it should.

Looking at my work tree I had 2 Resources folders. One under my project folder and another inside my proj.ios_mac folder. Not only that I had the contents of my Resources folder copied within proj.ios_mac!

Deleting these copies, selecting “Add files to [project]” eventually did the trick.

Samurai Gunship – A retro flavour 2D casual shooter game!

Posted on Updated on

At long last I am ready to unveil a game I have been developing with Pamprepps Games in Japan for a few months. Thanks to my friend Lee Adams (a good friend from all the way back in the day when we used to play Beyblade and talk about Sonic the hedgehog) for composing the BGM!

An iOS is being released soon. Till then stay tuned!

DL link:
https://play.google.com/store/apps/details?id=com.pamprepps.samuraigames

スクリーンショット 2015-09-02 14.17.38

◆◇◆Samurai Gunship◆◇◆
A simple retro flavour shooter game.
Destroy incoming enemy aliens to score points.

◆◇◆How to play◆◇◆
To destroy enemy targets, set your gunship into position by holding down the left or right arrow buttons. The fuel you carry doesn’t last forever, however you can extend the time you have left by collecting points and by moving through each wave.

“signal 11 (SIGSEGV), code 1” – (Cocos2dx, C++ with Android & Eclipse)

Posted on Updated on

The biggest drawback I’ve been having with Eclipse and cocos2dx (not sure if this also applies to Xcode …) I can think of is the annoying “Signal 11” error. But then I ran into this thread on StackOverFlow:

http://stackoverflow.com/questions/18436383/how-to-check-crash-log-using-android-ndk-in-cocos2d-x

I find the perfect answer in http://www.cocos2d-x.org/boards/6/topics/20437.

The solution is:

When you get errors like this on the LogCat, they are saved on $PROJECT_PATH/obj/local/armeabi where $PROJECT_PATH is the path to your cocos2d-x android project. To symbolicate the messages to something understandable, you can use the ndk-stack tool.

Open up the Terminal (or Cygwin, not sure though) and type in

cd $ANDROID_NDK
adb logcat | ./ndk-stack -sym $PROJECT_PATH/obj/local/armeabi

where:

$ANDROID_NDK is the path to your android NDK

$PROJECT_PATH is the path to your cocos2d-x android project

Wow! I just hit the jackpot! okay well time to switch over to my terminal and try this out. I then get the following crash dump:

********** Crash dump: **********
Build fingerprint: 'samsung/SC-04E/SC-04E:4.4.2/KOT49H/SC04EOMUFOB2:user/release-keys'
pid: 18875, tid: 18897, name: Thread-18051  >>> com.benji.etcetc <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
Stack frame #00  pc 00000000  <unknown>
Stack frame #01  pc 000de0b9  /data/app-lib/com.benji.etcetc/libcocos2dcpp.so (GameScene::onTouchBegan(cocos2d::Touch*, cocos2d::Event*)+232): Routine GameScene::onTouchBegan(cocos2d::Touch*, cocos2d::Event*) at /Users/benji0110/Downloads/cocos2d-x-3.3/etcetc/proj.android/jni/../../Classes/GameScene.cpp:355
Crash dump is completed

Awesome! Finally according to my crash dump I was able to locate where my app crashes at line 355 .. it turns out I had deleted a game object at runtime and read that objects size, but because it was now destroyed the object no longer existed causing the program to terminate.

What a treat to end an awesome Friday^^