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

2015/1/9

Checks

1
2
3
Super_Hippo Super_Hippo

2015/1/16

#
Again, change line 1 to this:
public boolean madeMove(Schijven schijven)
Then change the Schijven to schijven again. When calling this method, the object which was clicked is passed and can be used. It could be something like this then:
if (schijven.id == -1)
{
    if (y == 9) return false; //reached bottom already?
    if (x < 9)
    {
        if (bord[y+1][x+1] == 0) //cell empty?
        {
            bord[y+1][x+1] = -1;
            bord[y][x] = 0;
            setLocation(x*50+25,y*50+25); //or showBord();
            return true;
        }
        // check other directions to move
    }
}
The matrix is this at the beginning by the way. {{0,-1,0,-1,0,-1,0,-1}, {-1,0,-1,0,-1,0,-1,0}, {0,-1,0,-1,0,-1,0,-1}, {-1,0,-1,0,-1,0,-1,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0}, {0,1,0,1,0,1,0,1}}
Kryptonous Kryptonous

2015/1/16

#
Thanks! That helped out a lot. Now one more thing, so I made this:
    public boolean madeMove(Schijven schijven)
    {
        if (schijven.id == -1) //Black chips
        {
            int x = (schijven.getX()-25)/50;//-25/50
            int y = (schijven.getY()-25)/50;//-25/50
            if (y == 9) return false; //reached bottom already?
            if (x < 9)
            {
                if (bord[y+1][x+1] == 0) //cell empty?
                {//Leftup
                    bord[y+1][x+1] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation(y+1,x+1); //or showBord();
                    return true;
                }
                if (bord[x+1][y-1] == 0) //cell empty?
                {//Rightup
                    bord[y+1][x-1] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation(y+1,x-1);
                    return true;
                }
                if (bord[y+1][x] == 0) //cell empty?
                {//Up
                    bord[y+1][x] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation(y+1,x);
                    return true;
                }
            }
        }
        
        if (schijven.id == 1) //Whitechips
        {
            int x = (schijven.getX()-25)/50;//-25/50
            int y = (schijven.getY()-25)/50;//-25/50
            if (y == 1) return false; //reached top already?
            if (x < 9)
            {
                if (bord[y-1][x-1] == 0) //cell empty?
                {//Leftup
                    bord[y-1][x-1] = 1;
                    bord[y][x] = 0;
                    schijven.setLocation(y-1,x-1); //or showBord();
                    return true;
                }
                if (bord[y-1][x+1] == 0) //cell empty?
                {//Rightup
                    bord[y-1][x+1] = 1;
                    bord[y][x] = 0;
                    schijven.setLocation(y-1,x+1);
                    return true;
                }
                if (bord[y-1][x] == 0) //cell empty?
                {//Up
                    bord[y-1][x] = 1;
                    bord[y][x] = 0;
                    schijven.setLocation(y-1,x);
                    return true;
                }
            }
        }
    }
For both sides. But it says 'missing return statement'. But I can't put a return statement outside the ifs, otherwise the code is for nothing? Like, I can't just write 'return false;' because then the chips will always be red. How should I fix that?
Super_Hippo Super_Hippo

2015/1/16

#
Write "return false;" at the very end of the method. So it returns false if it didn't move, because if it did move, it would already left the method. You can declare x and y right at the beginning, because it is the same for both sides. You can also add an 'else' at the beginning of line 34.
danpost danpost

2015/1/16

#
In line 27 of the Schijven class code I provided, I called 'madeMove(this)', not 'madeMove()'. The 'this' in the braces passes the piece to the 'madeMove' method in the world class. The 'madeMove' method needs to be declared as follows:
public boolean madeMove(Schijven schijven)
A local variable named 'schijven' will now contain the piece clicked on and you can use 'schijven.getX()' and 'schijven.getY()' to get its location coordinates within the world. The method should not actually set the location of the piece once the move has been determined possible; it should just change the array values to show the new position and call the 'showBord' method. This will reset any reddened pieces back to normal before the other player moves. The matrix is not difficult to work with. Once you have the location in the matrix of the piece passed to the method (using the formulas for 'x' and 'y' that Super_Hippo supplied above, you can determine, by making use of the 'turn' value whether any possible move is directed outside the matrix.
if (turn == 1 && y == 0) return false; // white at top edge
if (turn == -1 && y == 9) return false; // black at bottom edge
Then for the possible moves forward, you would make sure the side edge is not exceeded and check 'bord' value for zero to be able to move there.
if (x+xOffset*turn >= 0 && x+xOffset*turn < 10 && bord[y-turn][x+xOffset*turn] == 0)
The 'xOffset' values of '1', '0', and '-1' will be used for each of the three possible moves.
Kryptonous Kryptonous

2015/1/16

#
            if (x < 9)
            {
                if (bord[y+1][x+1] == 0) //cell empty?
                {//Leftup
                    bord[y+1][x+1] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation((y*50)+25,(x*50)+25); //or showBord();
                    return true;
                }
                else if (bord[x+1][y-1] == 0) //cell empty?
                {//Rightup
                    bord[y+1][x-1] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation((y*50)+25,x-75);
                    return true;
                }
                else if (bord[y+1][x] == 0) //cell empty?
                {//Up
                    bord[y+1][x] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation((y*50)+25,x);
                    return true;
                }
            }
There's probably something wrong with the x/y coordinate formula I use, because only one chip (black only, for now), the middle chip on the first row moves in the right way. If I click another, they move pretty strange. Should I use the showBord function after every single if? EDIT: again, didn't see your post, Dan. So I'll make another method where I declare the setLocation then? and after every if put the showBord?
danpost danpost

2015/1/16

#
Of the code you posted for the 'madeMove' method above (the 64-liner), the 'if' blocks starting at lines 24 and 55 need to be moved outside the current 'if' blocks. The same can be said for the 'if' blocks starting at lines 17 and 48, except that they need to inside a different 'if' block that checks that 'x > 0' instead of 'x < 9'.
danpost danpost

2015/1/16

#
I ended up with the following methods:
/**  checks to see if piece can move and performs appopiate actions on results */
public boolean madeMove(Schijven piece)
{
    int x = piece.getX()/50; // get board column index
    int y = piece.getY()/50; // get row column index
    if (y-turn >= 10 || y-turn < 0) return false; // at end row
    if (bord[y-turn][x] == 0) // in front
        movePiece(x, y, x, y-turn);
    else if (x+turn < 10 && x+turn >= 0 && bord[y-turn][x+turn] == 0) // diag to right
        movePiece(x, y, x+turn, y-turn);
    else if (x-turn < 10 && x-turn >= 0 && bord[y-turn][x-turn] == 0) // diag to left
        movePiece(x, y, x-turn, y-turn);
    else return false; // cannot move
    return true; // moved
}

/** sets and shows the new board position and switches whose turn it is */
private void movePiece(int x0, int y0, int x1, int y1)
{
    bord[y0][x0] = 0;
    bord[y1][x1] = turn;
    showBord();
    turn = -turn;
}
Kryptonous Kryptonous

2015/1/16

#
Like this, then?
    public boolean madeMove(Schijven schijven)
    {
        int x = (schijven.getX()-25)/50;//-25/50
        int y = (schijven.getY()-25)/50;//-25/50
        if (schijven.id == -1) //Black chips
        {
            if (y == 9) return false; //reached bottom already?
            if (x < 9)
            {
                if (bord[y+1][x+1] == 0) //cell empty?
                {//Leftup
                    bord[y+1][x+1] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation((y*50)+25,(x*50)+25); //or showBord();
                    return true;
                }
            }
            if (x > 0)
            {
               if (bord[x+1][y-1] == 0) //cell empty?
                {//Rightup
                    bord[y+1][x-1] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation((y*50)+25,x-75);
                    return true;
                }
            }
                else if (bord[y+1][x] == 0) //cell empty?
                {//Up
                    bord[y+1][x] = -1;
                    bord[y][x] = 0;
                    schijven.setLocation((y*50)+25,x);
                    return true;
                }
        }
It seems you post faster then I do (discard this comment)!
danpost danpost

2015/1/16

#
Remove the 'else' on line 28.
Kryptonous Kryptonous

2015/1/16

#
What's the 'turn' in your method?
danpost danpost

2015/1/16

#
'turn' is a field in the world class that tracks which color has the move.
Kryptonous Kryptonous

2015/1/16

#
But why y-turn? or x-turn?
danpost danpost

2015/1/16

#
For 'y-turn': white, whose 'id' (= turn) is '1', is going up the board and in the upward direction the value of y decreases; black,whose 'id' is '-1' goes down the board in the direction which the value of y increases. The result is the final y of the possible locations to move. For 'x-turn' (also used is 'x+turn'): if I had used 'x-1' and 'x+1' for the possible final x for diagonal moves, the priority of direction would be different for the white and black pieces; by using 'turn' instead of '1', the priorities are matched.
Kryptonous Kryptonous

2015/1/18

#
Still figuring out the turns, but I think that'll work out, eventually. Thanks danpost and Super_Hippo, you really helped me out a lot! I still have to learn so much, though! Many, many thanks!
You need to login to post a reply.
1
2
3