I am pretty far along on my project dealing with singly circular linked lists and am still having trouble with one point. My trouble stems from not being able to/knowing how to call the node before the node containing the first of the four consecutive colors and referring it to the beginning.
Prompt for Marbles game: Place random marbles in a circle and if four of the same color appear, remove them and add four times the color score to the total score.
I would really appreciate if someone could help me figure how to resolve this problem..
Here's the code I'm working on:
public void deleteQuadruples()
{
//if the list has 4 nodes, delete all and make a new head
if (size == 4 && (start.getaMarble().getNumber() == start.getNextLink().getaMarble().getNumber()
&& start.getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getaMarble().getNumber()
&& start.getNextLink().getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getNextLink().getaMarble().getNumber()))
{
sum = 4*start.getaMarble().getNumber();
start = new Link(null, null);
addFirstMarble();
}
//else go thru the list and find whether or not there are four consecutive marbles of the same color/number and if so take them out
else
{
Link tmp = start;
if (tmp == null)
{
return;
}
do
{
/*int colorNumber = tmp.getaMarble().getNumber();
int counter = 1;
tmp = tmp.getNextLink();
if(colorNumber == tmp.getaMarble().getNumber())
{
counter++;
}
if(counter == 4)
{
score += 4*tmp.getaMarble().getNumber();
counter = 1;
//delete the 4 consecutive elements
}
if(tmp.getNextLink() == start && counter == 3 && start.getaMarble().getColor() == tmp.getaMarble().getColor())
{
score += 4*tmp.getaMarble().getNumber();
start = start.getNextLink();
//delete the 4 consecutive elements
}*/
for(Link cursor = start; cursor != end; cursor = cursor.getNextLink)
{
Link temp;
counter = 1;
if(cursor.getaMarble().getNumber() == cursor.getNextLink().getaMarble().getNumber())
{
counter++;
}
if(cursor.getaMarble().getNextLink().getNumber() != cursor.getNextLink().getNextLink().getaMarble().getNumber())
{
counter = 1;
}
if (counter == 4)
{
//deletes the four consecutive nodes with same color
//something.getNextLink() = start;
score += 4* cursor.getaMarble.getNumber();
}
}
}
while (tmp != start);
}
}
I made some changes to your code (it may not compile but will give an idea)
public void deleteQuadruples()
{
if (start == null || size == 3)
return;
//if the list has 4 nodes, delete all and make a new head
if (size == 4 && (start.getaMarble().getNumber() == start.getNextLink().getaMarble().getNumber()
&& start.getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getaMarble().getNumber()
&& start.getNextLink().getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getNextLink().getaMarble().getNumber()))
{
sum = 4*start.getaMarble().getNumber();
start = new Link(null, null);
addFirstMarble();
}
//else go thru the list and find whether or not there are four consecutive marbles of the same color/number and if so take them out
else
{
Node end = start;
while (end.getNextLink().getaMarble().getNumber() != start.getNextLink().getaMarble().getNumber())
{ end = end.getNextLink(); }
// no data
if(end.getNextLink().getaMarble().getNumber() == start.getaMarble().getNumber())
return;
// we have data
else {
sum = 4*start.getaMarble().getNumber();
// loop four times
// set the end.getNextLink() with start.getNextLink()
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.