在Java中将函数分隔为类

时间:2014-08-26 09:05:58

标签: java class

我的教授让我用AI制作棋盘游戏,但我希望AI与主代码分开。我想要做的是将AI.class放到Game.class所在的文件夹中,这样Game.class每次执行下面的代码中的AI()函数时都会调用AI.class。 AI.class也将被执行。我还将一个int数组的值赋给AI.class,AI.class将返回四个整数。 PS我们不允许使用netbeans,我们只允许使用记事本或文本板等。

            @SuppressWarnings("serial")

            public class Game extends JPanel {

            BufferedImage board;
            int[] intinfo = new int[]{486, 539, 591,643,696,749,801,853,907}; 
            int intinfo1;
            int intinfo2;
            int intinfo3;
            int intinfo4;

            char keyPressed;

                //I want this to be on different class
                AI(){
                    int[] info=new int[10]; //the values of this will come from intinfo
                    info1=1; //values of these four info will be returned to the intinfo
                    info2=2;
                    info3=3;
                    info4=4;
                }

                public Game() {
                    KeyListener listener = new KeyListener() {
                        @Override
                        public void keyTyped(KeyEvent e) {
                        }
                        @Override
                        public void keyPressed(KeyEvent e) {
                            keyPressed= e.getKeyChar();
                        }

                        @Override
                        public void keyReleased(KeyEvent e) {
                        }
                    };
                    addKeyListener(listener);
                    setFocusable(true);
                }

                @Override
                public void paint(Graphics g) {
                    super.paint(g);
                    g.drawImage(board, 0, 0,this);
                }

                public static void main(String[] args) throws InterruptedException {
                    JFrame frame = new JFrame("Games of the Generals");
                    Game game = new Game();
                    frame.add(game);
                    frame.setSize(1040, 680);
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    game.repaint();

                    while(true){
                        game.AI();
                        Thread.sleep(50);
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

当然,您需要做的就是

public class Game extends JPanel 
{
    private AI ai;
    int[] intinfo = new int[]{486, 539, 591,643,696,749,801,853,907}; 

    public Game()
    {
        this.ai = new AI();
        ...
    }

    public void execute()
    {
        ai.AI(intinfo);
    }
}

...
                game.repaint();

                while(true)
                {
                    game.execute();
                    Thread.sleep(50);
                }

当然, AI.java

public class AI
{

    int intinfo1;
    int intinfo2;
    int intinfo3;
    int intinfo4;

    public void AI(int[] intinfo)
    {
        int[] info = new int[10]; //the values of this will come from intinfo
        info1=1; //values of these four info will be returned to the intinfo
        info2=2;
        info3=3;
        info4=4;
    }

    ...
}

你需要用javac编译这两个类 - how to compile multiple java source files in command line

相关问题