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

2015/1/9

Checks

1
2
3
Kryptonous Kryptonous

2015/1/9

#
So I'm making a more simple/basic version of Checks. The only thing I want to achieve is the following: I want an object to check if there's an object in front of it OR in front of it on either the left/right side OR at the edge of the world, if not, the object can move. Code I use: Arrays: 4 arrays for each row, every check has a number.
So with some symbols (x is the given object, o are the other objects) If this scenario would happen: ooo x => x turns red. Because he can't move any way (left and right and bottom are NOT included. If this would happen: oo| x (| is the edge): x'd also turn red. because it can't move, again. IF this'd happen or any variation: oo oo o x OR x OR x OR x then it's possible for x to move diagonally up or just move in front of it.
My pseudocode:
    public void move()
    {
        if (getOneObjectAtOffset(50, 0, Zwarteschijf || Witteschijf))
        {
            ;
        }
        // if collides with an object 50p in front of it OR diagonally 50p up and 50p to either left or right, item becomes red.
        
    } 
The 'Witteschijf' and 'Zwarteschijf' are the black and white checks on either side of the board. Here's an image: this will clearify a lot, I think. No possibility: Possibilities to move
Super_Hippo Super_Hippo

2015/1/9

#
How is the moving controlled? Do you click a ship and then click on the position where you want to go? If yes, maybe you would check the mouse clicks in the world subclass and call a method on the ship object.
//in the world

// if a mouse click occurred on the square above the selected ship. You can simplify that. No need to have every direction separated from each other. Just as an example here
if (selectedShip.move(1)) //if it can move, it moves and returns true (see method at the end)
{
    //other player's turn
}
else
{
    //select the ship if it is from the same player. If you want to do that, get the object that the mouse clicked on to find the ship or change the 'move' method to return the object (right now it only returns if it moved). Otherwise do nothing (or deselect the ship, as you wish)
}

//put the following method in the Schijven class
//as an alternative, you can also create a method for every direction, but I think it is easier like this
public boolean move(int direction)
{
    int dx=0, dy=0;
    switch(direction) //of course you can change which number should be which direction
    {
        case 0: dx=-50; dy=-50; break; //up+left
        case 1:         dy=-50; break; //up
        case 2: dx= 50; dy=-50; break; //up+right
        case 3: dx=-50; dy= 50; break; //down+left
        case 4:         dy= 50; break; //down
        case 5: dx= 50; dy= 50; break; //down+right
    }
    if (getOneObjectAtOffset(dx,dy, Schijven.class) != null)
    {
        setLocation(getX()+dx,getY()+dy);
        return true;
    }
    return false;
}
Kryptonous Kryptonous

2015/1/9

#
Sorry, forgot to mention that. The goal was to enter a letter (from the row) and a number (from the column), which would then select THAT chip on that location. That chip would then try to move forward (or downwards, in case of the black chips), preferably diagonally, and then preferably to the right side. But as I used an array for each chip, I can't really use that method. So, if someone would be so kind: could you explain me how to 1. Give every 'block' on the board a letter for the row and a number for the column and how to enter those, so I can select the chip on the baord on that location. 2. How to make it go forward/downward, preferably diagonally (the possibilities are already stated in the first post (see the images). Thanks! And Supper_Hippo, thanks for the explanation too, but I didn't really get where I should put the put the 'if (selectedShip.move(1))' and how to implement it.
danpost danpost

2015/1/9

#
What do you mean by 'I used an array for each chip'? Explain or show how you do this, please.
Kryptonous Kryptonous

2015/1/9

#
    private String[] witteSchijvenRij1 =
        { "1", "2", "3", "4", "5" };
    private String[] witteSchijvenRij2 =
        { "6", "7", "8", "9", "10"};
    private String[] witteSchijvenRij3 =
        { "11", "12", "13", "14", "15"};
    private String[] witteSchijvenRij4 =
        { "16", "17", "18", "19", "20" };

    private String[] zwarteSchijvenRij1 =
        { "A", "B", "C", "D", "E" }; 
    private String[] zwarteSchijvenRij2 =
        {"F", "G", "H", "I", "J" }; 
    private String[] zwarteSchijvenRij3 =
        { "K", "L", "M", "N", "O" }; 
    private String[] zwarteSchijvenRij4 =
        { "P", "Q", "R", "S", "T"}; 
Again, 'Zwarteschijven' = black chips, 'Witteschijven' = White chips
for(int i = 0; i < witteSchijvenRij1.length; i++) {
            Witteschijf schijf = new Witteschijf(witteSchijvenRij1[i], "wit"+witteSchijvenRij1[i]+".jpg");
            addObject(schijf, i*100 + 75, 475);
        }
        for(int i = 0; i < witteSchijvenRij2.length; i++) {
            Witteschijf schijf = new Witteschijf(witteSchijvenRij2[i], "wit"+witteSchijvenRij2[i]+".jpg");
            addObject(schijf, i*100 + 25, 425);
        }
        for(int i = 0; i < witteSchijvenRij3.length; i++) {
            Witteschijf schijf = new Witteschijf(witteSchijvenRij3[i], "wit"+witteSchijvenRij3[i]+".jpg");
            addObject(schijf, i*100 + 75, 375);
        }
        for(int i = 0; i < witteSchijvenRij4.length; i++) {
            Witteschijf schijf = new Witteschijf(witteSchijvenRij4[i], "wit"+witteSchijvenRij4[i]+".jpg");
            addObject(schijf, i*100 + 25, 325);
        }

        // make the black keys
        for(int i = 0; i < zwarteSchijvenRij1.length; i++) {
            Zwarteschijf schijf = new Zwarteschijf(zwarteSchijvenRij1[i], "zwart" + zwarteSchijvenRij1[i] + ".jpg");
            addObject(schijf, i*100 + 75, 25);
        }
        for(int i = 0; i < zwarteSchijvenRij2.length; i++) {
            Zwarteschijf schijf = new Zwarteschijf(zwarteSchijvenRij2[i], "zwart" + zwarteSchijvenRij2[i] + ".jpg");
            addObject(schijf, i*100 + 25, 75);
        }
        for(int i = 0; i < zwarteSchijvenRij3.length; i++) {
            Zwarteschijf schijf = new Zwarteschijf(zwarteSchijvenRij3[i], "zwart" + zwarteSchijvenRij3[i] + ".jpg");
            addObject(schijf, i*100 + 75, 125);
        }
        for(int i = 0; i < zwarteSchijvenRij4.length; i++) {
            Zwarteschijf schijf = new Zwarteschijf(zwarteSchijvenRij4[i], "zwart" + zwarteSchijvenRij4[i] + ".jpg");
            addObject(schijf, i*100 + 25, 175);
        }
'Rij' = Row, so I guess you get the point. Check the image in the first post.
danpost danpost

2015/1/9

#
I see what you mean -- you used arrays to add the chips into the world, which is fine. However there should be a much easier, and much more useful, way then how you accomplished it -- and we will address that. Before we do, however, I am puzzled over the images used for the 40 chips. Are not the initial images of the 20 black chips the same -- as well as the initial images of the 20 white chips?
Kryptonous Kryptonous

2015/1/9

#
Yes, they are. Checkers uses all the same chips, right. The only problem really is that if I'd move a black chip (top of the image) to a black background, it looks AS IF it's a white background, but it's only the image of the piece. Here they are: (white) (black) White-red: used if the piece can't go anywhere Black-red: used if the piece can't go anywhere
Super_Hippo Super_Hippo

2015/1/9

#
Well, I don't think that it makes sense to have those arrays for the chips. (Sorry that I thought ship, "schijf" was close to "schiff" which is "ship" in German...). The chips will move and then they aren't on the same position anymore. If you want to choose the chip with keyboard input and not with just clicking on them, you could either create a pop up box in which you have to write a String or you can use something like this: (I use that for one of my games to check for cheat input. You probably would rename that, but to give you an idea how it is possible.)
    private String cheat;
    public void act()
    {
        String currentKey = Greenfoot.getKey();
        if (currentKey != null)
        {
            if ("space".equals(currentKey)) currentKey = " ";
            
            if ("backspace".equals(currentKey)) cheat = cheat.substring(0, cheat.length() - 1);
            else if ("enter".equals(currentKey)) checkCheat();
            else if (cheat==null) cheat = currentKey;
            else cheat = cheat + currentKey;
        }
    }
Do you want to have it like that you can only pick the chip, but you can't control where it will move? Edit: You could create the images by coding it. Then they could be transparent to show the real background.
Kryptonous Kryptonous

2015/1/9

#
Super_Hippo wrote...
Do you want to have it like that you can only pick the chip, but you can't control where it will move?
Yes, pretty much. I don't really understand much of the cheat-function though (and about the ship: schijf is dutch for piece). Dan: I checked out your Chess Dos game, it looked nice and very similar to my project. Only difference: mine is like a tiny little bit of that complex game you wrote. I only started using Java (in Greenfoot) this year, so I don't know that much yet. I don't know other imports other then greenfoot.(...). In terms of the book-scenarios you get with Greenfoot, the last one we did was the asteroids, I believe (as well as the piano, baby and worms). I bet you get the point. I'm glad there're people like you and Hippo around to help!
Super_Hippo Super_Hippo

2015/1/9

#
This cheat-function is in the world act method. So whenever you press a key, it is added to the string named 'cheat'. When 'enter' is pressed, the 'checkCheat' method is called which checks if the entered string matches a cheat (and reset it afterwards again). In your case, you would check if it contains a letter and a number and choose the chip on that location.
Kryptonous Kryptonous

2015/1/9

#
Super_Hippo wrote...
This cheat-function is in the world act method. So whenever you press a key, it is added to the string named 'cheat'. When 'enter' is pressed, the 'checkCheat' method is called which checks if the entered string matches a cheat. In your case, you would check if it contains a letter and a number and choose the chip on that location.
Ok, I get what you mean. But how can it contain a letter AND a number? as those are 2 keypresses, right? Or will it then check on combinations?
Super_Hippo Super_Hippo

2015/1/9

#
'cheat' is the string, 'currentKey' is the pressed key which will be added to the cheat string. So for example you press an 'h', 'cheat' is then "h". If you press 'i' then, it will be added to the existing string which is "hi" then. If you press 'enter' then, it will check the string which it is right now → "hi". Strings can also contain numbers, that isn't a problem.
Kryptonous Kryptonous

2015/1/9

#
Okay, seems pretty clear. Now, I still need a way to insert the pieces and to move them. Any ideas?
Super_Hippo Super_Hippo

2015/1/9

#
I think this should work for placing them.
for (int j=0; j<10; j++) //rows
{
    if (j==4 || j==5) continue; //don't place something in the middle
    Actor a;
    if (j<4) //black on top
    {
        a=new Zwarteschijf();
        for (int i=0; i<10; i++) //columns
        {
            if ( (i+j) % 2 == 1) addObject(a,25+50*i,25+50*j);
        }
    }
    else //white at bottom
    {
        a=new Witteschijf();
        for (int i=0; i<10; i++) //columns
        {
            if ( (i+j) % 2 == 0) addObject(a,25+50*i,25+50*j);
        }
    }
}
danpost danpost

2015/1/9

#
Still, all the initial images for black are the same; and all the initial images for white are the same. I suggest you change the background image to light the black square a bit. Also, it would not be difficult to create the images needed for the chips (to have transparency around the outside of the chip images instead of either a solid white or black color). Getting back to the arrays -- it would be much more useful to use a double array for the board itself. It could hold references to the chips themselves, but then it would be much easier to work with if you used int values in the array -- one (1) for a white chip, zero (0) for an empty square and negative one (-1) for a black chip. This is useful because the value itself can be used to determine the direction the chip can move. Also, you will not need to subclass the Schijven class. The double array can be initialized with the starting position and used to add the chips into the world. Then during gameplay it can be used to track the current position and, therefore, it can be used to find possible moves easily.
There are more replies on the next page.
1
2
3