Permalink
Browse files

Adding the code for the tower of hanoi game on github

  • Loading branch information...
0 parents commit 56931a7d960201a067c8f3c9e79733ce7eb1563a @aliHafizji committed Apr 20, 2012
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="gen"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+ <classpathentry kind="output" path="bin/classes"/>
+</classpath>
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>TowerOfHanoi</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ApkBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.tutorial.towerofhanoi"
+ android:versionCode="1"
+ android:versionName="1.0" >
+
+ <uses-sdk android:minSdkVersion="4"
+ android:targetSdkVersion="10" />
+
+ <application
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name" >
+ <activity
+ android:name=".TowerOfHanoiActivity"
+ android:label="@string/app_name" >
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
0 README
No changes.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,20 @@
+# To enable ProGuard in your project, edit project.properties
+# to define the proguard.config property as described in that file.
+#
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in ${sdk.dir}/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the ProGuard
+# include property in project.properties.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
@@ -0,0 +1,15 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-10
+android.library.reference.1=../../../AndEngine
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:orientation="vertical" >
+
+ <TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/hello" />
+
+</LinearLayout>
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+ <string name="hello">Hello World, TowerOfHanoiActivity!</string>
+ <string name="app_name">TowerOfHanoi</string>
+
+</resources>
@@ -0,0 +1,198 @@
+package com.tutorial.towerofhanoi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Stack;
+import org.andengine.engine.camera.Camera;
+import org.andengine.engine.options.EngineOptions;
+import org.andengine.engine.options.ScreenOrientation;
+import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
+import org.andengine.entity.scene.Scene;
+import org.andengine.entity.sprite.Sprite;
+import org.andengine.input.touch.TouchEvent;
+import org.andengine.opengl.texture.ITexture;
+import org.andengine.opengl.texture.bitmap.BitmapTexture;
+import org.andengine.opengl.texture.region.ITextureRegion;
+import org.andengine.opengl.texture.region.TextureRegionFactory;
+import org.andengine.ui.activity.SimpleBaseGameActivity;
+import org.andengine.util.adt.io.in.IInputStreamOpener;
+import org.andengine.util.debug.Debug;
+
+import com.tutorial.towerofhanoi.gameelements.Ring;
+
+public class TowerOfHanoiActivity extends SimpleBaseGameActivity {
+
+ private static int CAMERA_WIDTH = 800;
+ private static int CAMERA_HEIGHT = 480;
+
+ private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
+ private Stack<Ring> mStack1, mStack2, mStack3;
+ private Sprite mTower1, mTower2, mTower3;
+
+ public EngineOptions onCreateEngineOptions() {
+ final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
+ return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
+ }
+
+ @Override
+ protected void onCreateResources() {
+ try {
+ ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
+ @Override
+ public InputStream open() throws IOException {
+ return getAssets().open("gfx/background.png");
+ }
+ });
+
+ ITexture towerTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
+
+ @Override
+ public InputStream open() throws IOException {
+ return getAssets().open("gfx/tower.png");
+ }
+ });
+
+ ITexture ring1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
+
+ @Override
+ public InputStream open() throws IOException {
+ return getAssets().open("gfx/ring1.png");
+ }
+ });
+
+ ITexture ring2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
+
+ @Override
+ public InputStream open() throws IOException {
+ return getAssets().open("gfx/ring2.png");
+ }
+ });
+ ITexture ring3 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
+
+ @Override
+ public InputStream open() throws IOException {
+ return getAssets().open("gfx/ring3.png");
+ }
+ });
+ backgroundTexture.load();
+ towerTexture.load();
+ ring1.load();
+ ring2.load();
+ ring3.load();
+
+ this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
+ this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
+ this.mRing1 = TextureRegionFactory.extractFromTexture(ring1);
+ this.mRing2 = TextureRegionFactory.extractFromTexture(ring2);
+ this.mRing3 = TextureRegionFactory.extractFromTexture(ring3);
+
+ this.mStack1 = new Stack<Ring>();
+ this.mStack2 = new Stack<Ring>();
+ this.mStack3 = new Stack<Ring>();
+ } catch (IOException e) {
+ Debug.e(e);
+ }
+ }
+
+ @Override
+ protected Scene onCreateScene() {
+ final Scene scene = new Scene();
+ Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
+
+ mTower1 = new Sprite(0.241f * CAMERA_WIDTH, 0.133f * CAMERA_HEIGHT, this.mTowerTextureRegion, getVertexBufferObjectManager());
+ mTower2 = new Sprite(0.5f * CAMERA_WIDTH, 0.133f * CAMERA_HEIGHT, this.mTowerTextureRegion, getVertexBufferObjectManager());
+ mTower3 = new Sprite(0.756f * CAMERA_WIDTH, 0.133f * CAMERA_HEIGHT, this.mTowerTextureRegion, getVertexBufferObjectManager());
+
+ Ring ring1 = new Ring(1, 139, 174, this.mRing1, getVertexBufferObjectManager()) {
+
+ @Override
+ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
+ if(this.getmStack().peek().getmWeight() != this.getmWeight())
+ return false;
+ this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
+ if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
+ checkForCollisionsWithTowers(this);
+ }
+ return true;
+ }
+ };
+ Ring ring2 = new Ring(2, 118, 212, this.mRing2, getVertexBufferObjectManager()) {
+
+ @Override
+ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
+ if(this.getmStack().peek().getmWeight() != this.getmWeight())
+ return false;
+ this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
+ if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
+ checkForCollisionsWithTowers(this);
+ }
+ return true;
+ }
+ };
+ Ring ring3 = new Ring(3, 97, 255, this.mRing3, getVertexBufferObjectManager()) {
+
+ @Override
+ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
+ if(this.getmStack().peek().getmWeight() != this.getmWeight())
+ return false;
+ this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
+ if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
+ checkForCollisionsWithTowers(this);
+ }
+ return true;
+ }
+ };
+
+ this.mStack1.add(ring3);
+ this.mStack1.add(ring2);
+ this.mStack1.add(ring1);
+
+ ring1.setmStack(mStack1);
+ ring2.setmStack(mStack1);
+ ring3.setmStack(mStack1);
+ ring1.setmTower(mTower1);
+ ring2.setmTower(mTower1);
+ ring3.setmTower(mTower1);
+
+ scene.attachChild(backgroundSprite);
+ scene.attachChild(mTower1);
+ scene.attachChild(mTower2);
+ scene.attachChild(mTower3);
+ scene.attachChild(ring1);
+ scene.attachChild(ring2);
+ scene.attachChild(ring3);
+ scene.registerTouchArea(ring1);
+ scene.registerTouchArea(ring2);
+ scene.registerTouchArea(ring3);
+ scene.setTouchAreaBindingOnActionDownEnabled(true);
+ return scene;
+ }
+
+ private void checkForCollisionsWithTowers(Ring ring) {
+ Stack<Ring> stack = null;
+ Sprite tower = null;
+
+ if(ring.collidesWith(mTower1) && (mStack1.size() == 0 || ring.getmWeight() < mStack1.peek().getmWeight())) {
+ stack = mStack1;
+ tower = mTower1;
+ } else if(ring.collidesWith(mTower2) && (mStack2.size() == 0 || ring.getmWeight() < mStack2.peek().getmWeight())) {
+ stack = mStack2;
+ tower = mTower2;
+ } else if(ring.collidesWith(mTower3) && (mStack3.size() == 0 || ring.getmWeight() < mStack3.peek().getmWeight())) {
+ stack = mStack3;
+ tower = mTower3;
+ } else {
+ stack = ring.getmStack();
+ tower = ring.getmTower();
+ }
+ ring.getmStack().remove(ring);
+ if(stack != null && tower !=null && stack.size() == 0) {
+ ring.setPosition(tower.getX() + tower.getWidth()/2 - ring.getWidth()/2, tower.getY() + tower.getHeight() - ring.getHeight());
+ } else if(stack != null && tower !=null && stack.size() > 0) {
+ ring.setPosition(tower.getX() + tower.getWidth()/2 - ring.getWidth()/2, stack.peek().getY() - ring.getHeight());
+ }
+ stack.add(ring);
+ ring.setmStack(stack);
+ ring.setmTower(tower);
+ }
+}
@@ -0,0 +1,39 @@
+package com.tutorial.towerofhanoi.gameelements;
+
+import java.util.Stack;
+
+import org.andengine.entity.sprite.Sprite;
+import org.andengine.opengl.texture.region.ITextureRegion;
+import org.andengine.opengl.vbo.VertexBufferObjectManager;
+
+public class Ring extends Sprite {
+
+ private int mWeight;
+ private Stack<Ring> mStack; //this represents the stack that this ring belongs to
+ private Sprite mTower;
+
+ public Ring(int weight, float pX, float pY, ITextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) {
+ super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
+ this.mWeight = weight;
+ }
+
+ public int getmWeight() {
+ return mWeight;
+ }
+
+ public Stack<Ring> getmStack() {
+ return mStack;
+ }
+
+ public void setmStack(Stack<Ring> mStack) {
+ this.mStack = mStack;
+ }
+
+ public Sprite getmTower() {
+ return mTower;
+ }
+
+ public void setmTower(Sprite mTower) {
+ this.mTower = mTower;
+ }
+}

0 comments on commit 56931a7

Please sign in to comment.