Ok, so I've been away from pretty much all types of programming for the past few months. School took over, and I got a new computer (where I couldn't figure out how to install and configure my IDE's/SDK's). Anyways, I managed to get the Android ADK to work, and I was working on a simple program to get me back into the swing of things -- Towers of Hanoi. (For those of you who don't know what the Towers of Hanoi are, check out the Wikipedia article on it.)
I have three classes right now - Tower Activty, Tower, and Disc. Tower has methods for keeping track of which discs are on it, and Disc has a method for moving the disc to another tower and keeping track of the disc sizes (making sure the rules are followed). Right now, Tower Activity just initializes 4 discs and sets them to the left tower. It's just getting started, but I'd be interested to know what you think!
Tower Activity
Disc
Tower
I have three classes right now - Tower Activty, Tower, and Disc. Tower has methods for keeping track of which discs are on it, and Disc has a method for moving the disc to another tower and keeping track of the disc sizes (making sure the rules are followed). Right now, Tower Activity just initializes 4 discs and sets them to the left tower. It's just getting started, but I'd be interested to know what you think!
Tower Activity
package com.camdroid.towers;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TowersActivity extends Activity {
TextView leftTower;
TextView middleTower;
TextView rightTower;
Tower left = new Tower(1);
Tower t2 = new Tower(2);
Tower t3 = new Tower(3);
Disc d1 = new Disc(1);
Disc d2 = new Disc(2);
Disc d3 = new Disc(3);
Disc d4 = new Disc(4);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Init();
//setContentView(R.layout.main);
}
public void Init()
{
leftTower = (TextView)findViewById(R.id.left_tower);
middleTower = (TextView)findViewById(R.id.middle_tower);
rightTower = (TextView)findViewById(R.id.right_tower);
d1.setTower(left);
d2.setTower(left);
d3.setTower(left);
d4.setTower(left);
ShowDiscs();
}
public void ShowDiscs()
{
leftTower.setText(left.listDiscs());
middleTower.setText(""+t2.listDiscs());
rightTower.setText(t3.listDiscs());
}
}
Disc
package com.camdroid.towers;
public class Disc {
private int size;
Tower tower = new Tower(1);
public Disc(int num)
{
size = num;
}
public boolean canBePlaced(Disc d)
{
if(this.getSize()>d.getSize())
return false;
else return true;
}
public boolean moveDisc(Tower t)
{
if((t.getPosition()>3) || (t.getPosition()<1))
return false;
if(this.canBePlaced(t.getTopDisc()))
this.setTower(t);
return true;
}
public void setTower(Tower t)
{
tower = t;
t.addDisc(this);
}
public int getSize()
{
return size;
}
}
Tower
package com.camdroid.towers;
import java.util.ArrayList;
public class Tower {
private int position;
private ArrayList<Disc> discs = new ArrayList<Disc>();
public Tower(int num)
{
position = num;
}
public int getPosition()
{
return position;
}
public void addDisc(Disc d)
{
discs.add(d);
}
public Disc getTopDisc()
{
return discs.get(0);
}
public String listDiscs()
{
String s="";
int n=0;
// if(discs.size()==0)
// s=""+discs.size();
for(int i=0; i<discs.size();i++)
{
s += discs.get(i).getSize()+"\n";
}
if(discs.size() == 0)
s=""+discs.size();
return s;
}
}
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
← February 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 |
Tags
My Blog Links
Recent Entries
-
Coming Back...on Dec 09 2010 08:45 PM
Search My Blog
2 user(s) viewing
2 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment








|