【发布时间】:2014-10-19 13:13:03
【问题描述】:
我的教授让我用 AI 制作棋盘游戏,但我希望 AI 与主代码分开。我想要做的就是将 AI.class 放到 Game.class 所在的同一个文件夹中,这样每次执行下面代码中的 AI() 函数时,Game.class 都会调用 AI.class AI.class 也将被执行。我还将 int 数组的值提供给 AI.class,AI.class 将返回四个整数。 PS我们不能用netbeans,只能用notepad或者textpad之类的。
@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);
}
}
}
【问题讨论】:
-
所以试试你刚才说的,看看会发生什么......
-
如果您不允许从 IDE 获得帮助,为什么您认为允许来自 SO 的帮助?
-
这更像是一个 java101 问题,而不是一个非常有价值的问题。你有一个特定的任务,但这不是一个“问题”。先自己尝试,如果在分离过程中遇到特定问题,请返回 SO。