This site requires JavaScript, please enable it in your browser!
Greenfoot back
Celesterra
Celesterra wrote ...

2019/6/7

Button Function

1
2
Celesterra Celesterra

2019/6/7

#
I am trying to make the buttons work for my scenario. I am trying to get them to function (and I have called it) but, they still don't work. I was trying to implement Super_Hippo's code into these buttons (addNumbers for check and removeRows for clear. I still can't figure out how to do it or where to call the function. I want the 'g' key for check and the 'j' key for clear. Please help! World Code I want In the Button Check
private void addNumber(int i)
{
    //Spawns in a number from the ArrayList to the world in the correct place
    addObject(numbers.get(i), i%9*(cellSize) + cellSize/2, topSpace + i/9*(cellSize) + cellSize/2);
}
    
private void addNumbers()
{
    int size = numbers.size();
        
    //Adds all unsolved numbers to the ArrayList
    for(int i = 0; i < size; i++)
    {
        if(!numbers.get(i).getSolved()) 
        {
            numbers.add(new Number(numbers.get(i).getValue()));
        }
    }
        
    //Spawns in all the numbers from the ArrayList
    for(int i = size; i < numbers.size(); i++)
    {
        addNumber(i);
    }
}
World Code I want in the Button Clear
private void removeRow(int row)
{
    //Removes all of the numbers in a row
    for(int i = 0; i < 9; i++)
    {
        removeObject(numbers.get(row*9+(8-i)));
        numbers.remove(row*9+(8-i));
    }
        
    //Moves up the rows beneath the removed row
    for(int i = row*9; i < numbers.size(); i++)
    {
        numbers.get(i).changeHeight(- cellSize);
    }
}
    
private void removeRows(int row1, int row2)
{
    //If two entire rows have a value of -1, remove the second one first
    if(row1 == -1)
    {
        if(row2 == -1) 
        {
            return;
        }
            
        removeRow(row2);
    }
        
    //If one entire row has a value of -1, remove it
    else
    {
        if(row2 == -1)
        {
            removeRow(row1);
            return;
        }
            
        removeRow(row1>row2 ? row1 : row2);
        removeRow(row1>row2 ? row2 : row1);
    }
}
If you need anymore code, please ask and I will provide. They will be inside if statements checking if one of those keys were pressed. Thanks in advance!
Super_Hippo Super_Hippo

2019/6/7

#
First of all, you commented the "removeRows" method wrong. -1 means that the row is not finished. Otherwise, the variable is the row'th row… Well, so if row1=5 and row2=-1, then only the sixth ("index" = 5) is removed. (Using () in the following paragraph because the other ones aren't working outside code tags.) At the end of the "solve" method, I call the "removeRows" method. finished(0) is true if the line in which the first clicked number was is completely solved. Then "row(0)" (the row in which the number was) will be passed to the "removeRows" method. Otherwise, -1 is passed (line not finished).
removeRows(finished[0]?row[0]:-1, finished[1]?row[1]:-1);
At the beginning of the removeRows method, you can see the "return" statement which is executed without any other action if both parameters are -1. So basically, all numbers in a row needs to have the value 0 to make a row solved. However, that is not checked in that method. It is checked in "solve". If you want the user to be able to manually add new numbers and not only automatically when nothing is possible, you could do it like this:
private boolean gPressed = false;

//in act

if (gPressed != Greenfoot.isKeyDown("g"))
{
    gPressed = !gPressed;
    if (gPressed)
    {
        addNumbers();
    }
}
Checking if anything is possible manually could be done similar. Maybe you want to display a possible pair. (I don't see another reason for that at least, since numbers are added automatically when no pairs are possible.) To do that, you can look at the "matchPossible" method. At the positions where "return true" is called, a possible match is found. You could either tell number i and j to change its image to show the user that there is a pair, or you could store i and j in a list and find every possible pair and then take a random pair to display as a hint. Maybe you also have to prevent someone from just using the hint all the time.
Celesterra Celesterra

2019/6/7

#
Check is to "check" if the puzzle is still solvable. Anyways, got that to work. Now, I need to work on the clear and hint button. I actually started to work on the hint button and got half of it done. Here is the hint code (in the world). Hint:
if(hPressed != Greenfoot.isKeyDown("h"))
{
    hPressed = !hPressed;
            
    if(hPressed)
    {
        if(matchPossible())
        {
            //add highlight for one number
            //add highlight for matching number
        }
                
        if(!matchPossible())
        {
            addObject(new BtnLight(), 66, 19);
        }
    }
}
Celesterra Celesterra

2019/6/10

#
Is there any possible way to make the clear and hint button work? The clear one is so difficult because I have no idea how to call the removeRows line in the solve method and the hint one needs to highlight only one match. Also, how do I detect the end of the game when there are no more numbers or if two matchPossibles in a row (meaning a puzzle is unsolvable) and ends the game ? I was trying to figure that out but, can't seem to figure it out...
Super_Hippo Super_Hippo

2019/6/10

#
Celesterra wrote...
Is there any possible way to make the clear and hint button work? The clear one is so difficult because I have no idea how to call the removeRows line in the solve method and the hint one needs to highlight only one match.
What is the "clear" button supposed to do? Is it to remove solved lines manually instead of doing it automatically? For one match, you can have a similar method like matchPossible but instead of returning true/false, you return i and j to return the first possible pair. Then you just highlight i and j. Or as said in the last message, you could add all of them to a list and get one random pair.
Also, how do I detect the end of the game when there are no more numbers or if two matchPossibles in a row (meaning a puzzle is unsolvable) and ends the game ?
I think you can adjust the end of the pressedNumber method to this:
            if (match)
            { //if nothing unsolved is between them
                solve(number);
                if (!matchPossible())
                {
                    addNumbers();
                    if (!matchPossible)
                    {
                        //end game
                    }
                }
            }
Celesterra Celesterra

2019/6/10

#
The clear button is a manual row remover. Also, the code you suggested does work, however it works at the wrong time (I didn't add the automatic part, since I want the player to add the numbers manually). I don't think it is in the right spot but I am going to try it in the act method under the check button.
Celesterra Celesterra

2019/6/10

#
And I have yet to try the hint code you suggested.
danpost danpost

2019/6/10

#
You cannot instantly get a row number from the user. This suggests that you will have to code something separately in the act method to acquire the chosen row. You could add a "row selector" object into the world and have the user use the up, down and enter keys to move and choose. This object, or some other object should cover the entire world window so no clicks on other objects will be detected. The World instance paintOrder method can be used to ensure that coverage is achieved. The object can highlight the rows and the row highlighted can be changed with arrow key detection and the object and chosen row can be removed upon enter key detection.
Super_Hippo Super_Hippo

2019/6/10

#
(Line 7 had to be the same as line 9, so with the () in the end. Just in case you have a "matchPossible" boolean and it didn't give you an error.)
Celesterra Celesterra

2019/6/11

#
I did the hint code and found that using i and j highlights the same number. How do I make it highlight it's match?
    public void hiMatch()
    {
        for(int i = 0; i < numbers.size(); i++)
        {
            //Makes sure already solved numbers can't be matched again
            if(numbers.get(i).getSolved()) 
            {
                continue;
            }
            
            //Checks the numbers horizontally for matches
            for(int j = i+1; j < numbers.size(); j++)
            {
                //Ignore all solved numbers
                if(numbers.get(j).getSolved()) 
                {
                    continue;
                }
                
                //If there is a match found, set a highlight on a match
                if(numbers.get(i).getValue() == numbers.get(j).getValue()
                    || numbers.get(i).getValue() + numbers.get(j).getValue() == 10)
                { 
                    int ix = numbers.get(i).getX();
                    int iy = numbers.get(i).getY();
                    addObject(hi, ix, iy);
                    
                    int jx = numbers.get(j).getX();
                    int jy = numbers.get(j).getY();
                    addObject(hi, jx, jy);
                }
                
                //If there isn't a match, break the loop
                else 
                {
                    break;
                }
            }
            
            //Checks the numbers vertically for matches
            for(int j = i+9; j < numbers.size(); j+=9)
            {
                //Ignores any solved numbers
                if(numbers.get(j).getSolved()) 
                {
                    continue;
                }
                
                //If there is a match found, set a highlight on a match
                if(numbers.get(i).getValue() == numbers.get(j).getValue()
                    || numbers.get(i).getValue() + numbers.get(j).getValue() == 10)
                {
                    int ix = numbers.get(i).getX();
                    int iy = numbers.get(i).getY();
                    addObject(hi, ix, iy);
                    
                    int jx = numbers.get(j).getX();
                    int jy = numbers.get(j).getY();
                    addObject(hi, jx, jy);
                }
                
                //If there isn't a match, break the loop
                else 
                {
                    break;
                }
            }
        }
    }
danpost danpost

2019/6/11

#
Well, certainly the one highlight object, hi, cannot cover both numbers (see lines 55 and 59). You will need to create a second highlight object to cover one of them with.
Celesterra Celesterra

2019/6/11

#
That worked! But my clear button still doesn't work. I have an idea of how to make it work, but I don't know if it will work. Is there any way I could go through the rows and detect any rows that have a value of 0 (solved number) throughout the entire row? And how would I remove all of those rows, no matter how many there are?
danpost danpost

2019/6/11

#
You could make a new type object to do that. Add it at the top row and call a method in it to proceed. If its image goes across a single row, it can check its intersecting numbers and covers. If same number of each, remove them. Drop down a row and repeat until all rows have been check; then remove self..
Celesterra Celesterra

2019/6/11

#
How would I set that up exactly?
danpost danpost

2019/6/12

#
Celesterra wrote...
How would I set that up exactly?
Create a new subclass of Actor; name it RowRemover. Change the word act to go. Put code in the method to iterate through the 12 rows, make checks and act upon results; then remove self. Override the addedToWorld method to set its image:
protected void addedToWorld(World world)
{
    GreenfootImage img = new GreenfootImage(world.getWidth(), 2);
    setImage(img);
}
You would then use the following to remove the rows from some Actor subclass:
RowRemover rowRemover = new RowRemover();
getWorld().addObject(rowRemover, << center of width of world >> , << top row y >>);
rowRemover.go();
If in a World subclass, just drop getWorld(). from line 2.
There are more replies on the next page.
1
2