我刚刚挖掘了几年前我做过的一个java项目,它是关于一个国际象棋游戏,我发现了一个奇怪的行为:
程序启动,窗口打开,所有按钮都放好,但游戏无法启动。我不明白的是,添加一个简单(无用)的打印将使游戏开始。
因此,只有下一条打印线时,程序才会进入while
。
public static void main(String[] args){
//The white pieces are at bottom
int gioco = 0;
while (!(gioco == 1)){
new Window();
//creating, initializing and first printing of the board
final Board scacchiera = new Board();
Configuration.initialization(scacchiera);
//System.out.println(scacchiera.toString());
while (!scacchiera.getCheckmate()){
//THE NEXT LINE IS THE ONE
//if it is not there, the program doesn't work
//everything inside the println is ok
System.out.println(sem);
if (sem){//Waiting the inputs from the window
sem = false;
if(scacchiera.getChessman(x, y).getColour() == turn){
if (scacchiera.move(scacchiera, scacchiera.getChessman(x, y), newX, newY)){
turn = (turn == Colour.bianco) ? Colour.nero : Colour.bianco;
MyFrame.generateBoard(scacchiera);
}
//System.out.println(scacchiera.toString());
}
else{
JOptionPane.showMessageDialog(null, "It's not your turn.", "Turn error", JOptionPane.WARNING_MESSAGE);
}
}
}
JOptionPane option = new JOptionPane ("CheckMate!\n"
+ "Do you want to play another game?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
JDialog dialog = null;
if (turn == Colour.bianco){
dialog = option.createDialog(null,"Black side won!");
}
else{
dialog = option.createDialog(null,"White side won!");
}
dialog.pack();
dialog.setVisible(true);
gioco = ((Integer)option.getValue()).intValue(); //saving the choice: 0: yes 1: no
if(gioco == 1){
JOptionPane.showMessageDialog(null, "See you soon!");
System.exit(0);
}
//TODO
//se voglio fare un'altra partita devo prima chiudere la finestra della partita precedente?
}
}
为什么?