3
\$\begingroup\$

I have an assignment in programming using Java, which is about a marble game. When you're entering a marble the marble has a color and a value. When there's 4 of the same color in a row it will be "gone" and the value will be calculated. It is like the Zuma games.

Here's a picture to describe the games:

Fourteen turns of the game

As you can see, when there are 4 or more marbles with the same color, it will pop and the value will be counted. The requirement is to use a linked list and I am using a singly linked list. My teacher says my code needs some improvements but I'm already stuck and don't know how to improve it.

public class marblegame {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        Marble text = new Marble();
        String loop = in.nextLine();
        int loop2 = Integer.parseInt(loop);
        int score = 0;

        for(int i = 0; i < loop2; i++) 
        {

            int number = in.nextInt();
            int number2 = in.nextInt();
            text.add(number,number2);
            score = text.combo();

        }

        System.out.println (score);

    }
}


class Node 
{

    Node next;

    int content;

    public Node() {
        next = null;

    }
    public Node(int content) {
        this.content = content;
        next = null;
    }

}

class Marble {
    Node head;
    Node pointer;
    int combo=0;

    public Marble() {
        head = new Node();
        pointer = head;
    }

    public boolean isEmpty() 
    {
        return(head.next == null);
    }

    public void delete(int from, int to)
    {
        Node pointer = head.next;
        Node nstart, nend, naftend;
        nstart = null;
        nend = null;
        naftend = null;

        for (int i = 0; i<= to; i++) {
            if (pointer == null) {
                break;
            }

            if (i+1 == from) {
                nstart = pointer;
            }

            if (i == to-1){
                nend = pointer;
                break;
            }

            pointer = pointer.next;
        }

        if (nend != null) {
            naftend = nend.next;
        }

        if (nstart != null){ 
            nstart.next = naftend;
        } else if (nend == null) {
            // both nstart and nend is null, then empty the list
            head.next = null;
        } else {
            nend.next = null;
            head.next = naftend;
        }
    }

    public void add(int posisi, int color)
    {
        Node newNode = new Node(color);
        int i=0;
        pointer=head;
        if(head.next != null)
        {
            while (pointer.next != null && posisi != i)
            {
                pointer=pointer.next;
                i++;
            }
            newNode.next=pointer.next;
            pointer.next=newNode;

        }
        else 
        {
            head.next=newNode;
        }
    }

    public int length () {
        int i = 0;
        Node pointer = head.next;
        while (pointer != null) {
            pointer = pointer.next;
            i+=1;
        }

        return i;
    }

    public void print()
    {
     pointer = head;

     while(pointer.next != null)
            {
            pointer = pointer.next;
            System.out.print(" - " + pointer.content);

            }
     System.out.println ();

    }
    public int combo()
    {   
        Node pointerStart=head.next;
        int score = 0;
        boolean hasCombo = false;

        int i=0;
        while(pointerStart!=null)
        {
            int currentcontent=pointerStart.content;
            int total_same=0;

            Node next_pointer=pointerStart;
            while(next_pointer!=null&&next_pointer.content==pointerStart.content)
            {
                next_pointer=next_pointer.next;
                total_same+=1;
            }
            if(total_same>=4)
            {
                this.delete (i, i+total_same);
                i = i+1;
                //System.out.println ("Sisa : " + this.length ());
                score = 10 * total_same * ((int) Math.pow (combo + 1, 3));
                hasCombo = true;
                this.combo += 1;
                break;
            } else{
                i = i + total_same;
            }
            pointerStart=next_pointer;

        }
        if (hasCombo == true) {
            score += this.combo (); 
        }
        else {
            score -= 11 * this.length ();
        }
        return score;  

    }       

}
\$\endgroup\$
1
  • \$\begingroup\$ You already implicitly stated this is homework, so I added the homework tag. While this isn't SO, this and this are applicable. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Nov 30 '17 at 17:52
1
\$\begingroup\$

First off, it's completely unclear how the program works. It seems to take multiple integers as input, but

  • your description doesn't explain what they are
  • the program doesn't give any prompts
  • the program doesn't contain any comments that would describe them
  • the variable names don't give any information what they are for

As a beginner you should have all four points covered, even if all on them together may be a bit redundant.


Generally your variable and class names are poorly chosen. Why is the main object variable called text? Why is the main class that handles multiple marbles called singular Marble, while the class that actually represents a single marble called Node. (If you called it Node because it should represent a part of generic linked list - which is fine - then why isn't there a "(linked) list" class?


In your description you say that a marble has a color and a value, however in your code your Node only contains a single value (generically named content so it's unclear what it represents). On the other hand the image seem to indicate that color and value are the same thing.


Make sure you understand the task and what the teacher expects from you. "More improvements" is very unspecific. Talk to him and find out. I assume that the focus is on implementing a linked list? In that case do that (implement a linked list) that is independent from the game mechanics and then add the game on top.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.