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

#
I'll try it both ways (Hippo's way and Danpost) although, Dan, we did see 2D arrays but not in Java, I only learned it in C#. I guess it won't be hard to 'convert' it, but could you give a little example of the placing of the chips and/or the usage of the 1, 0 and -1 in a move function? So: Hippo, I tried your way to at least add the pieces.
    public void chipsAdd()
    {
        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(zwarteSchijven[j],"zwart"+zwarteSchijven[j]+".jpg" );
                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(witteSchijven[j],"wit"+witteSchijven[j]+".jpg" );
                for (int i=0; i<10; i++) //columns
                {
                    if ( (i+j) % 2 == 0) 
                    addObject(a,25+50*i,25+50*j);
                }
            }
        }
    }
and the 2 arrays on top:
 private String[] witteSchijven =
        { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"  };

        private String[] zwarteSchijven =
        { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"  };
        
That gives me only this, though:
danpost danpost

2015/1/9

#
I was thinking along these lines (with the Schijven class not being subclassed):
// world instance field
public int[][] bord;
// in constructor
resetGame();
// the 'resetGame' method
public void resetGame()
{
    bord = new int[10][10]; // reset array
    for (int x=0; x<10; x++) // for each column
    { // set representative values
        bord[1-(x%2)][x] = -1;
        bord[3-(x%2)][x] = -1;
        bord[6+(x%2)][x] = 1;
        bord[8+(x%2)][x] = 1;
    }
    showBord();
}
// the 'showBord' method
public void showBord()
{
    removeObjects(getObjects(Schijven.class));
    for (int y=0; y<10; y++) for (int x=0; x<10; x++)
    {
        if (bord[y][x] != 0)
        {
            addObject(new Schijven(bord[y][x]), 25+50*x, 25+50*y);
        }
    }
}
Kryptonous Kryptonous

2015/1/10

#
I'll try it out later this evening, I don't have a lot of time at the moment. But I still do have another week to try to complete that project. It's not nescessary though, but I want to make it so I can see how it works and how I could implement it in future projects.
Super_Hippo Super_Hippo

2015/1/10

#
I am not sure why only the first two columns are filled and not the others. But for my version you don't need these arrays and the constructor of the chips shouldn't need a parameter (unless you combine the chips as one class (for example only the superclass) and have a parameter which represent its color.
Kryptonous Kryptonous

2015/1/13

#
danpost wrote...
I was thinking along these lines (with the Schijven class not being subclassed):
// world instance field
public int[][] bord;
// in constructor
resetGame();
// the 'resetGame' method
public void resetGame()
{
    bord = new int[10][10]; // reset array
    for (int x=0; x<10; x++) // for each column
    { // set representative values
        bord[1-(x%2)][x] = -1;
        bord[3-(x%2)][x] = -1;
        bord[6+(x%2)][x] = 1;
        bord[8+(x%2)][x] = 1;
    }
    showBord();
}
// the 'showBord' method
public void showBord()
{
    removeObjects(getObjects(Schijven.class));
    for (int y=0; y<10; y++) for (int x=0; x<10; x++)
    {
        if (bord[y][x] != 0)
        {
            addObject(new Schijven(bord[y][x]), 25+50*x, 25+50*y);
        }
    }
}
You said 'the schijven class not being subclassed'. How can I then use 2 images, if you're getting the objects from schijven, which is only one object? And how would I write the movement in the class: how can I check if there's an object on that certain square?
danpost danpost

2015/1/13

#
The 'bord' array should contain the current position. Use it for your checks to see what is where. The Schijven class could be written something like this:
import greenfoot.*;
import java.awt.Color;

public class Schijven extends Actor
{
    public static GreenfootImage[][] images = 
    {
        { new GreenfootImage(/** black image filename */), new GreenfootImage(/** white image filename */) },
        { new GreenfootImage(/** reddened black image filename */), new GreenfootImage(/** reddened white image filename */) }
    };

    private int id; // holds color id ('-1' is black and '1' is white)
    
    /** saves color id and sets initial image */
    public Schijven(int kind)
    {
        id = kind;
        setImage(images[0][(1+id)/2]);
    }

    /** if turn is color of piece and clicked on, have world check if can move */
    public void act()
    {
        if (((CheckWorld)getWorld()).turn != id) return; // exit method if not turn for this color piece
        if (Greenfoot.mouseClicked(this)) // if clicked on
        {
            if (!((CheckWorld)getWorld()).madeMove(this)) // if not moved
                setImage(images[1][(1+id)/2]); // turn red
        }
    }
}
where 'turn' is an int field declared in the world class to hold whose turn it currently is and 'madeMove' is a method in the world class that checks to see if the move can be made and if so, makes the move; it also returns a boolean value indication whether the move was possible, and made, or not.
Kryptonous Kryptonous

2015/1/15

#
Ok. I just have a question: I don't really get the formula used here:
setImage(images[0][(1+id)/2]);
I know that the is used to refer to the first dimension (either black or white image). But what is the ? I bet it's needed to chose between the first or second image, right? But where is id declared to be either -1 or 1? Also, I don't really know how I'd declare 'turn'. I could say (a bit like ID) that if it's white's turn, it should be 1, black would be 2 (or -1). But I wouldn't know how to assign the black chips as -1 fe. And what method should I use for 'madeMove' to check for objects/the world edge? I thought of: getOneObjectAtOffset or getObjectsAtOffset and isAtEdge. Are those correct to use? How would I use it?
Super_Hippo Super_Hippo

2015/1/15

#
The id is set when resetGame() is called. The 'bord' gets the 1 and -1 values in the correct places and 'showBoard' then places the chips. It passes the value which is then set to the 'id' field in the constructor.
danpost danpost

2015/1/16

#
Kryptonous wrote...
I just have a question: I don't really get the formula used here:
setImage(images[0][(1+id)/2]);
I know that the is used to refer to the first dimension (either black or white image). But what is the ? I bet it's needed to chose between the first or second image, right? But where is id declared to be either -1 or 1?
As Super_Hippo stated, the 'id' field is set in the constructor of the class (line 17), the value of which is received (see line 15) from the world constructor. If you plug the values of '1' and '-1' into the "formula", '1' will yield '1' and '-1' will yield '0'. These are the index values used for the images.
Also, I don't really know how I'd declare 'turn'. I could say (a bit like ID) that if it's white's turn, it should be 1, black would be 2 (or -1). But I wouldn't know how to assign the black chips as -1 fe. And what method should I use for 'madeMove' to check for objects/the world edge? I thought of: getOneObjectAtOffset or getObjectsAtOffset and isAtEdge. Are those correct to use? How would I use it?
The 'turn' field is simply declared like this (in the CheckWorld class):
public int turn;
It should be set to '1' in the constructor and negated any time a move is made (so yes, values of '1' and '-1'). The world class code I provided earlier in this discussion thread shows how the pieces are passed their color values (see line 26 of that code post). The madeMove method should have a boolean return type and make use of the array to determine if moves are possible. To make a move, simply zero the location in the array where the piece was and set the location in the array where the piece moves to its color value -- then call the 'showBord' method. You have the dimensions of the array to eliminate moves off the edges of the board. The method should return a 'false' value if no legal move was possible for the piece, else make the move as just described and change whose turn it is.
Kryptonous Kryptonous

2015/1/16

#
I fixed the turn-method. That was pretty easy though. I'm still not sure about the getOneObjectAtOffset or getObjectsAtOffset and isAtEdge though. And I think I get the idea about id ;) But the ID also checks if it's either black or white's turn, right? And do I need to refer to 'kind' in the World? EDIT: I didn't see your last post, so nvm this message then. I thought using the array, although I'm not completely sure how. I'll just try multiple things out, if I really can't find it, then I'll ask again. The turncycle is ok.
Kryptonous Kryptonous

2015/1/16

#
    public boolean madeMove()
    {
        if (Greenfoot.mouseClicked(Schijven.class))
        {
            bord[y][x] = 0;
            setLocation(x+25, y+25);
        }
        return false;
    }
I know, it's not really correct. At least I tried, but I don't know how I can get the current location of the chip. I don't even know what the int is, it's not an array, is it a 2D list? But take my piece of code as pseudocode. Then I guess the point is clear. How can I write it so that it's correct?
Super_Hippo Super_Hippo

2015/1/16

#
The int is like a matrix. The first value points to the x'th row and the second points to the y'th column. In line 3 you check if a click on a object of the class Schijven was made. Then you need a reference to the object you clicked on. danpost wrote the following:
if (Greenfoot.mouseClicked(this))
{
    if (!((CheckWorld)getWorld()).madeMove(this))
    //...
So the method is only called if a Schijven object was clicked (Line 3 can be removed) and the object on which was clicked is also passed. You have to change line 1 to this, so the object can be passed:
public boolean madeMove(Schijven schijven)
Then you can call methods on this object. Maybe this already helps you, good luck. :) If you have further questions, just ask.
Kryptonous Kryptonous

2015/1/16

#
Ok, that helps further indeed. Only one more thing: ANY object can be clicked, right, so how can I write down the current location (by using the int) and the setlocation? Because x and y are getting errors, as they don't really mean anything.
public boolean madeMove()
{
        bord[y][x] = 0; //how can I get the location  using the int matrix?
        setLocation(x+25, y+25); //This isn't correct. What would be a correct way to write this?

        //return false; 
}
Super_Hippo Super_Hippo

2015/1/16

#
Every object can be clicked, but only a clicked Schijven object will call this method because the check is detected in the Schijven's act method. You didn't change line 1. This should give you the x and y value of the chip on which was clicked then.
int x = (schijven.getX()-25) / 50; //although I don't think that the -25 is needed
int y = (schijven.getY()-25) / 50;
Another way would be to save the x and y in the Schijven class and update them whenever a chip moved. Then you could use schijven.x if it is public or create a getter method for it. Line 3 should be used only if the chip left its location. After that you should just change the location. First, check if this place is occupied by another chip right now. (So something like if the desired destination is a 0 in the matrix, move there, change this position to the 'id' of the chip, set the old position to 0 and return true.)
Kryptonous Kryptonous

2015/1/16

#
Again, I get what you want to say, but there always seems to be little problems on the road: if I enter this:
    public boolean madeMove()
    {
        int x = (Schijven.getX()-25/50);
int y = (Schijven.getY()-25/50);
        bord[y][x] = 0;
        Greenfoot.setLocation(x+25, y+25);

        return false;
    }
it says that non-static method getX cannot be referenced from a static context. I do know that it can't refer to Schijven.class because it's in world, but why that is, I would now know. Same for the getY. Then: how can I check the id on a desired destination? Like, how would I write a specific destination in the matrix down? Fe like this bord or something? for box 2 on row 3? Or how should I do that? The point is: I know what you mean by
After that you should just change the location. First, check if this place is occupied by another chip right now. (So something like if the desired destination is a 0 in the matrix, move there, change this position to the 'id' of the chip, set the old position to 0 and return true.)
I just don't know how to do it exactly (specifically, I don't know how to use the matrix). It really annoys me, and it bugs me to keep asking. But I don't have another option. With each step, I learn, so that's the good side then!
There are more replies on the next page.
1
2
3