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

2018/6/4

How do I make each player play once?

MyName MyName

2018/6/4

#
has two class tank
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class TanqueMarrom extends Actor
{
    public String equipe = "TanquePreto";
    public static String turno=null;
    public void act() 
    {

        if(Greenfoot.mouseClicked(this))
        {   
            getWorld().removeObjects(getWorld().getObjects(EspacoM.class));
            checkY();
            checkX();
            checkY2();
            checkX2();;
            ((Tabuleiro)getWorld()).X = getX();
            ((Tabuleiro)getWorld()).Y = getY();
            // movimentaPara(getX(), getY());
            // atualizacaoEspaco();

            getWorld().removeObjects(getWorld().getObjectsAt(getX(),getY(),EspacoM.class));
        }
    } 

    public void checkX()
    {
        boolean permitir = true;
        int a =-1;
        int b=0;
        while(getOneObjectAtOffset(a,b,null)==null&&a+getX()>=0)
        {
            getWorld().addObject(new EspacoM(),getX()+a, getY()+b);
            a--;
        }
        if(getOneObjectAtOffset(a,b,TanquePreto.class)!=null)
            getWorld().addObject(new EspacoM(),getX()+a, getY()+b);
        int x = 1;
        int y = 0;
        while(getOneObjectAtOffset(x,y,null)==null&&x<11)
        {
            getWorld().addObject(new EspacoM(),getX()+x, getY()+y);
            x++;
        }
        if(getOneObjectAtOffset(x,y,TanquePreto.class)!=null)
            getWorld().addObject(new EspacoM(),getX()+x, getY()+y);
    }

    public void checkY()
    {
        int c=0;
        int d=1;
        while(getOneObjectAtOffset(c,d,null)==null&&d<0)
        {
            getWorld().addObject(new EspacoM(),getX()+c, getY()+d);
            d++;
        }
        if(getOneObjectAtOffset(c,d,TanquePreto.class)!=null)
            getWorld().addObject(new EspacoM(),getX()+c, getY()+d);

        int e=0;
        int f=-1;
        while(getOneObjectAtOffset(e,f,null)==null&&f+getY()>=0)
        {
            getWorld().addObject(new EspacoM(),getX()+e, getY()+f);
            f--;
        }
        if(getOneObjectAtOffset(e,f,TanquePreto.class)!=null)
            getWorld().addObject(new EspacoM(),getX()+e, getY()+f);

    }

    public void checkX2()
    {

        int a =-1;
        int b=-1;
        while(getOneObjectAtOffset(a,b,null)==null&&a+getX()>=0&&b+getY()>=0)
        {
            getWorld().addObject(new EspacoM(),getX()+a, getY()+b);
            a--;
            b--;
        }
        if(getOneObjectAtOffset(a,b,TanquePreto.class)!=null&&a+getX()>=0&&b+getY()>=0)
            getWorld().addObject(new EspacoM(),getX()+a, getY()+b);

        int x = 1;
        int y = 1;
        while(getOneObjectAtOffset(x,y,null)==null&&getX()+x<11&&getY()+y<0)
        {
            getWorld().addObject(new EspacoM(),getX()+x, getY()+y);
            x++;
            y++;
        }
        if(getOneObjectAtOffset(x,y,TanquePreto.class)!=null)
            getWorld().addObject(new EspacoM(),getX()+x, getY()+y);

    }

    public void checkY2()
    {
        int c=-1;
        int d=0;
        while(getOneObjectAtOffset(c,d,null)==null&&d+getY()<11&&c+getX()>=0)
        {
            getWorld().addObject(new EspacoM(),getX()+c, getY()+d);
            d--;
            c--;
        }
        if(getOneObjectAtOffset(c,d,TanquePreto.class)!=null&&d+getY()<11&&c+getX()>=0)
            getWorld().addObject(new EspacoM(),getX()+c, getY()+d);

        int e=1;
        int f=-1;
        while(getOneObjectAtOffset(e,f,null)==null&&f+getY()>=-1&&e+getX()<11)
        {
            getWorld().addObject(new EspacoM(),getX()+e, getY()+f);
            f--;
            e++;
        }
        if(getOneObjectAtOffset(e,f,TanquePreto.class)!=null&&f+getY()>=-1&&e+getX()<11)
            getWorld().addObject(new EspacoM(),getX()+e, getY()+f);

    }

    public void turno() //aque e :a vez do tanque que joga
    {
        if(TanqueMarrom.turno == "TanqueMarrom")
            TanqueMarrom.turno = "TanquePreto";
        else
            TanqueMarrom.turno = "TanqueMarrom"; 
    }

    public boolean adversario(TanqueMarrom tanque)  //inimigo
    {
        if(tanque.equipe != equipe)
        {
            return true;
        }
        return false;
    }
}
danpost danpost

2018/6/4

#
MyName wrote...
How do I make each player play once? has two class tank << Code Omitted >>
You should probably move the turno field to a neutral location where both tanks can use one field instead of two different fields that do not know what each one is doing. Your World subclass would be a logical place where the world can control whose turn it is.
MyName MyName

2018/6/4

#
but my code is not working! Would you have any tips?
Yehuda Yehuda

2018/6/4

#
Did you think of the tip given to you above (I didn't but that's because I don't know the scenario).
MyName MyName

2018/6/4

#
yes but the error when i call the methods !
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Tabuleiro here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tabuleiro extends World
{

    /**
     * Constructor for objects of class Tabuleiro.
     * 
     */
    int X = 0;
    int Y = 0;
    int A=0;
    int B=0;
    public String equipe = "TanquePreto";
    public static String turno=null;

    public Tabuleiro()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(11, 11, 57);

        addObject(new Rosa(),4,4);
        addObject(new Rosa(),5,4);
        addObject(new Rosa(),6,4);
        addObject(new Rosa(),4,5);
        addObject(new Rosa(),5,5);
        addObject(new Rosa(),5,6);
        addObject(new Rosa(),4,6);
        addObject(new Rosa(),6,5);
        addObject(new Rosa(),6,6);

        addObject(new TanquePreto(),1,0);

        addObject(new TanquePreto(), 1, 0);
        addObject(new TanquePreto(), 2, 0);
        addObject(new TanquePreto(), 3, 0);
        addObject(new TanquePreto(), 4, 0);

        addObject(new TanquePreto(), 0, 1);
        addObject(new TanquePreto(), 2, 1);
        addObject(new TanquePreto(), 3, 1);

        addObject(new TanquePreto(), 0, 2);
        addObject(new TanquePreto(), 1, 2);
        addObject(new TanquePreto(), 2, 2);

        addObject(new TanquePreto(), 0, 3);
        addObject(new TanquePreto(), 1, 3);

        addObject(new TanquePreto(), 0, 4);

        addObject(new TanqueComandantePreto(), 0, 0);

        addObject(new Preto(),0,5);
        addObject(new Preto(),2,5);
        addObject(new Preto(),5,2);
        addObject(new Preto(),7,2);
        addObject(new Preto(),8,3);
        addObject(new Preto(),2,7);
        addObject(new Preto(),8,7);
        addObject(new Preto(),7,8);
        addObject(new Preto(),5,8);
        addObject(new Preto(),5,10);
        addObject(new Preto(),9,9);
        addObject(new Preto(),3,8);
        addObject(new Preto(),5,0);
        addObject(new Preto(),1,1);
        addObject(new Preto(),2,3);
        addObject(new Preto(),3,2);
        addObject(new Preto(),9,1);
        addObject(new Preto(),10,5);
        addObject(new Preto(),8,5);
        addObject(new Preto(),1,9);

        addObject(new TanqueMarrom(), 9, 10);
        addObject(new TanqueMarrom(), 8, 10);
        addObject(new TanqueMarrom(), 7, 10);
        addObject(new TanqueMarrom(), 6, 10);

        addObject(new TanqueMarrom(), 10, 9);
        addObject(new TanqueMarrom(), 8, 9);
        addObject(new TanqueMarrom(), 7, 9);

        addObject(new TanqueMarrom(), 10, 8);
        addObject(new TanqueMarrom(), 9, 8);
        addObject(new TanqueMarrom(), 8, 8);

        addObject(new TanqueMarrom(), 10, 7);
        addObject(new TanqueMarrom(), 9, 7);
        addObject(new TanqueMarrom(), 10, 6);
        addObject(new TanqueComandanteMarrom(),11,11);

    }
    public void move(int x, int y)
    {

        List<TanqueMarrom> man = getObjectsAt(X,Y,TanqueMarrom.class);

        Actor man2 = man.get(0);
        if(man.size() > 0)
            man2.setLocation(x,y);
        if(getObjectsAt(x,y,TanquePreto.class).size()>0){

            removeObjects(getObjectsAt(x,y,TanquePreto.class));
        }
    }

    public void move1(int x, int y)
    {

        List<TanquePreto> ma = getObjectsAt(A,B,TanquePreto.class);

        Actor man = ma.get(0);
        if(ma.size() > 0)
            man.setLocation(x,y);
        if(getObjectsAt(x,y,TanqueMarrom.class).size()>0){

            removeObjects(getObjectsAt(x,y,TanqueMarrom.class));
        }

    }

    public void turno() //aque e :a vez do tanque que joga
    {
        if(TanqueMarrom.turno == "TanqueMarrom")
            TanqueMarrom.turno = "TanquePreto";
        else
            TanqueMarrom.turno = "TanqueMarrom"; 
    }

    public boolean adversario(TanqueMarrom tanque)  //inimigo
    {
        if(tanque.equipe != equipe)
        {
            return true;
        }
        return false;
    } 

   
}
danpost danpost

2018/6/5

#
Well, the turno method should now be this:
public void turno() //aque e :a vez do tanque que joga
{
    if ( "TanqueMarrom".equals(turno))
        turno = "TanquePreto";
    else
        turno = "TanqueMarrom"; 
}
If you still get an error (or if you do in the future), do not just say you are getting one -- show what error output you are being given.
You need to login to post a reply.