-2

My professor asked me to make a board game with AI but I want the AI to be separated from the main code. What I want to do is I will just put the AI.class to the same folder where the Game.class is so that Game.class will just call the AI.class every time the AI() function from the code below is executed the AI.class will also be executed. I will also give the values of a int array to AI.class and the AI.class will return four integer. PS we are not allowed to use netbeans, we are only allowed to use notepad or textpad or the like.

            @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);
                    }
                }
            }
LorenzKyle
  • 69
  • 10
  • So try what you just said and see what happens... – rbennett485 Aug 26 '14 at 09:09
  • If you not allowed to get help from IDE why tou think that help from SO is allowed? – talex Aug 26 '14 at 09:12
  • This is more like a java101 question instead of a SO worthy one. You have a specific task but it's not a "problem". Try it yourself first and if you encounter a specific problem during the seperation, come back to SO. – Jacky Cheng Aug 26 '14 at 09:12

1 Answers1

0

Sure, all you need to do is

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);
                }

And of course, 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;
    }

    ...
}

And you need to compile both classes with javac - how to compile multiple java source files in command line

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428