Bygglogg, Java projekt - En smart kublista
Hej Sweclockare,
koden jag skriver är helt fri att använda sig av.
Projekt,
Mitt projekt är att skapa en nodlista som formar sig som en kub (Tänk er en rubikskub). Den ska kunna länkas vid 27 olika punkter, Vertikalt, horisontelt och diagonalt. Sedan tänkte jag försöka skapa en a-star search funktion som ska kunna snabbt och effektivt kunna söka sig fram genom kuben efter data.
Bakgrund,
Då jag har ungefär 1-1½ år av programmeringserfarenhet (Själv lärd) kommer jag använda mig av Java för att försöka lösa detta.
Övrigt
Detta är nog det svåraste projektet jag har tagit mig an, men ack så roligt det kan bli.
Jag kommer uppdatera denna tråd med kod som jag implementerar, problem och lösningar. Min tanke är att det kan vara roligt för nya, juniora och även seniora utvecklare att få följa en nybörjare i hur han tänker och gör.
Jag har fått tipset att använda mig av github också, vilket jag kommer lägga till lite senare.
Milstenar,
1. En algoritm, Rekursiv metod eller dylikt för att skapa noderna som en kub. Jag har kollat mycket på hilberts curve, men jag känner att jag har svårt att tolka fram bra fakta när jag har sökt runt. Metoden ska ta fram nästa punkt noden ska skapas.
2. Skapa en metod som kopplar samman noderna. Detta kommer nog bli extremt komplicerat, men definitivt lättare än milsten 1. Listan ska på ett smart och snyggt sätt kunna koppla samman alla sina 27 punkter, när det väl adderas en ny nod.
3. Skapa ett första utkast av en sökfunktion. Den ska ta sig från första till sista noden på ett snabbt och effektivt sätt.
4. Skapa add och remove metoder för noder inom själva kuben. Har inte riktigt bestämt hur jag ska göra här, då jag ej vill sortera hela kuben när något läggs till/tas bort. Till och börja med sätter jag nodens data till null
5. Det tar vi om jag lyckas komma så långt!
Min kod hittils
Hittills har jag bara skapat ett skal och lekt runt lite. Mitt första steg är att ta fram algoritmen för att skapa noderna i form av en kub.
public class CubeList <AnyType>{
private int uniqId = 0;
private int sumDifferential = 0;
private int sumTotal = 0;
// Variables to keep track of the x,y and z position of the node.
private int xAxis=1, yAxis=1, zAxis=1;
//Store the first node and also the one being worked with.
private Node <AnyType> firstNode;
private Node <AnyType> currentNode;
public void add (AnyType data)
{
System.out.println(getFirstNode());
try
{
if (getFirstNode() == null)
{
//Create node nr1.
Node <AnyType> node = new Node<AnyType> (data, uniqId, xAxis, yAxis, zAxis);
uniqId++;
//Connect the first node with this one.
setFirstNode(node);
//Set the newly created node as the current one.
setCurrentNode(node);
}
else
{
//Calculate the position of the node
nextNodePosition(xAxis, yAxis, zAxis);
//Create the new node
Node <AnyType> node = new Node<AnyType> (data, uniqId, xAxis, yAxis, zAxis);
uniqId++;
//Connect the node with other nodes close by.
connectNode(node);
//Change the current node to the new created node.
setCurrentNode(node);
}
}
//Took the first best Exception so the catch statement doesn't complain.
catch (NullPointerException e)
{
e.printStackTrace();
}
}
/**
* This method calculate the position of the next node
*
*/
private void nextNodePosition (int xAxis, int yAxis, int zAxis)
{
}
/**
* This method connect the newly created node, with other nearby nodes
*
*/
private void connectNode (Node <AnyType> node)
{
}
/**
* Just a method to calculate the number of nodes which need to be added between each x³.
* Example, between 2³ and 3³ there is 19 nodes.
*
*
*/
public void rekursivSumAll (int x)
{
if (x < 0)
{
return;
}
else if (x == 0)
{
sumDifferential += 1;
sumTotal += sumDifferential;
System.out.println("The value is: "+ sumDifferential);
return;
}
else
{
sumDifferential += x*6;
rekursivSumAll(x-1);
}
}
/**
* A private class to create a node for the cubeList.
* The nodes store Anytype of data, position of x-,y-,z-axis and pointers to other nearby nodes
*
*
* @author razkan
*
* @param <AnyType>
*
*
*/
private static class Node <AnyType>
{
//The node stored object
private AnyType data;
//X-Axis nodes
private Node <AnyType> xAxisMiddle;
private Node <AnyType> xAxisTop;
private Node <AnyType> xAxisBottom;
private Node <AnyType> xAxisRight;
private Node <AnyType> xAxisLeft;
private Node <AnyType> xAxisTopRightCorner;
private Node <AnyType> xAxisTopLeftCorner;
private Node <AnyType> xAxisBottomRightCorner;
private Node <AnyType> xAxisBottomLeftCorner;
//Y-Axis nodes
private Node <AnyType> yAxisMiddle;
private Node <AnyType> yAxisTop;
private Node <AnyType> yAxisBottom;
private Node <AnyType> yAxisRight;
private Node <AnyType> yAxisLeft;
private Node <AnyType> yAxisTopRightCorner;
private Node <AnyType> yAxisTopLeftCorner;
private Node <AnyType> yAxisBottomRightCorner;
private Node <AnyType> yAxisBottomLeftCorner;
//Z-Axis nodes
private Node <AnyType> zAxisMiddle;
private Node <AnyType> zAxisTop;
private Node <AnyType> zAxisBottom;
private Node <AnyType> zAxisRight;
private Node <AnyType> zAxisLeft;
private Node <AnyType> zAxisTopRightCorner;
private Node <AnyType> zAxisTopLeftCorner;
private Node <AnyType> zAxisBottomRightCorner;
private Node <AnyType> zAxisBottomLeftCorner;
private final int UNIQ_ID;
private int xAxis, yAxis, zAxis;
public Node (AnyType data, int uniq_id, int xAxis, int yAxis, int zAxis)
{
this.data = data;
this.UNIQ_ID = uniq_id;
this.xAxis = xAxis;
this.yAxis = yAxis;
this.zAxis = zAxis;
}
public AnyType getData() {
return data;
}
public void setData(AnyType data) {
this.data = data;
}
public int getUniqId() {
return this.UNIQ_ID;
}
public int getxAxis() {
return xAxis;
}
public void setxAxis(int xAxis) {
this.xAxis = xAxis;
}
public int getyAxis() {
return yAxis;
}
public void setyAxis(int yAxis) {
this.yAxis = yAxis;
}
public int getzAxis() {
return zAxis;
}
public void setzAxis(int zAxis) {
this.zAxis = zAxis;
}
public Node<AnyType> getxAxisMiddle() {
return xAxisMiddle;
}
public void setxAxisMiddle(Node<AnyType> xAxisMiddle) {
this.xAxisMiddle = xAxisMiddle;
}
public Node<AnyType> getxAxisTop() {
return xAxisTop;
}
public void setxAxisTop(Node<AnyType> xAxisTop) {
this.xAxisTop = xAxisTop;
}
public Node<AnyType> getxAxisBottom() {
return xAxisBottom;
}
public void setxAxisBottom(Node<AnyType> xAxisBottom) {
this.xAxisBottom = xAxisBottom;
}
public Node<AnyType> getxAxisRight() {
return xAxisRight;
}
public void setxAxisRight(Node<AnyType> xAxisRight) {
this.xAxisRight = xAxisRight;
}
public Node<AnyType> getxAxisLeft() {
return xAxisLeft;
}
public void setxAxisLeft(Node<AnyType> xAxisLeft) {
this.xAxisLeft = xAxisLeft;
}
public Node<AnyType> getxAxisTopRightCorner() {
return xAxisTopRightCorner;
}
public void setxAxisTopRightCorner(Node<AnyType> xAxisTopRightCorner) {
this.xAxisTopRightCorner = xAxisTopRightCorner;
}
public Node<AnyType> getxAxisTopLeftCorner() {
return xAxisTopLeftCorner;
}
public void setxAxisTopLeftCorner(Node<AnyType> xAxisTopLeftCorner) {
this.xAxisTopLeftCorner = xAxisTopLeftCorner;
}
public Node<AnyType> getxAxisBottomRightCorner() {
return xAxisBottomRightCorner;
}
public void setxAxisBottomRightCorner(Node<AnyType> xAxisBottomRightCorner) {
this.xAxisBottomRightCorner = xAxisBottomRightCorner;
}
public Node<AnyType> getxAxisBottomLeftCorner() {
return xAxisBottomLeftCorner;
}
public void setxAxisBottomLeftCorner(Node<AnyType> xAxisBottomLeftCorner) {
this.xAxisBottomLeftCorner = xAxisBottomLeftCorner;
}
public Node<AnyType> getyAxisMiddle() {
return yAxisMiddle;
}
public void setyAxisMiddle(Node<AnyType> yAxisMiddle) {
this.yAxisMiddle = yAxisMiddle;
}
public Node<AnyType> getyAxisTop() {
return yAxisTop;
}
public void setyAxisTop(Node<AnyType> yAxisTop) {
this.yAxisTop = yAxisTop;
}
public Node<AnyType> getyAxisBottom() {
return yAxisBottom;
}
public void setyAxisBottom(Node<AnyType> yAxisBottom) {
this.yAxisBottom = yAxisBottom;
}
public Node<AnyType> getyAxisRight() {
return yAxisRight;
}
public void setyAxisRight(Node<AnyType> yAxisRight) {
this.yAxisRight = yAxisRight;
}
public Node<AnyType> getyAxisLeft() {
return yAxisLeft;
}
public void setyAxisLeft(Node<AnyType> yAxisLeft) {
this.yAxisLeft = yAxisLeft;
}
public Node<AnyType> getyAxisTopRightCorner() {
return yAxisTopRightCorner;
}
public void setyAxisTopRightCorner(Node<AnyType> yAxisTopRightCorner) {
this.yAxisTopRightCorner = yAxisTopRightCorner;
}
public Node<AnyType> getyAxisTopLeftCorner() {
return yAxisTopLeftCorner;
}
public void setyAxisTopLeftCorner(Node<AnyType> yAxisTopLeftCorner) {
this.yAxisTopLeftCorner = yAxisTopLeftCorner;
}
public Node<AnyType> getyAxisBottomRightCorner() {
return yAxisBottomRightCorner;
}
public void setyAxisBottomRightCorner(Node<AnyType> yAxisBottomRightCorner) {
this.yAxisBottomRightCorner = yAxisBottomRightCorner;
}
public Node<AnyType> getyAxisBottomLeftCorner() {
return yAxisBottomLeftCorner;
}
public void setyAxisBottomLeftCorner(Node<AnyType> yAxisBottomLeftCorner) {
this.yAxisBottomLeftCorner = yAxisBottomLeftCorner;
}
public Node<AnyType> getzAxisMiddle() {
return zAxisMiddle;
}
public void setzAxisMiddle(Node<AnyType> zAxisMiddle) {
this.zAxisMiddle = zAxisMiddle;
}
public Node<AnyType> getzAxisTop() {
return zAxisTop;
}
public void setzAxisTop(Node<AnyType> zAxisTop) {
this.zAxisTop = zAxisTop;
}
public Node<AnyType> getzAxisBottom() {
return zAxisBottom;
}
public void setzAxisBottom(Node<AnyType> zAxisBottom) {
this.zAxisBottom = zAxisBottom;
}
public Node<AnyType> getzAxisRight() {
return zAxisRight;
}
public void setzAxisRight(Node<AnyType> zAxisRight) {
this.zAxisRight = zAxisRight;
}
public Node<AnyType> getzAxisLeft() {
return zAxisLeft;
}
public void setzAxisLeft(Node<AnyType> zAxisLeft) {
this.zAxisLeft = zAxisLeft;
}
public Node<AnyType> getzAxisTopRightCorner() {
return zAxisTopRightCorner;
}
public void setzAxisTopRightCorner(Node<AnyType> zAxisTopRightCorner) {
this.zAxisTopRightCorner = zAxisTopRightCorner;
}
public Node<AnyType> getzAxisTopLeftCorner() {
return zAxisTopLeftCorner;
}
public void setzAxisTopLeftCorner(Node<AnyType> zAxisTopLeftCorner) {
this.zAxisTopLeftCorner = zAxisTopLeftCorner;
}
public Node<AnyType> getzAxisBottomRightCorner() {
return zAxisBottomRightCorner;
}
public void setzAxisBottomRightCorner(Node<AnyType> zAxisBottomRightCorner) {
this.zAxisBottomRightCorner = zAxisBottomRightCorner;
}
public Node<AnyType> getzAxisBottomLeftCorner() {
return zAxisBottomLeftCorner;
}
public void setzAxisBottomLeftCorner(Node<AnyType> zAxisBottomLeftCorner) {
this.zAxisBottomLeftCorner = zAxisBottomLeftCorner;
}
}
private Node<AnyType> getFirstNode() {
return firstNode;
}
private void setFirstNode(Node<AnyType> firstNode) {
this.firstNode = firstNode;
}
private Node<AnyType> getCurrentNode() {
return currentNode;
}
private void setCurrentNode(Node<AnyType> currentNode) {
this.currentNode = currentNode;
}
}