我似乎无法让我的代码显示我在NetBeans中的TicTacToe游戏的得分和总数。如何显示这些变量,以及更新玩家得分并添加到玩家总数?
这是我到目前为止所得到的:
public class TicTacToe extends JFrame implements ActionListener
{
private static boolean xTurn = true;
public Player player1;
public Player player2;
private static JButton[] buttonArray = new JButton[9];
private static int[] winsArray =
{
7, 56, 448, 73, 146, 292, 273, 84
};
public TicTacToe()//constructor
{
player1 = new Player(GameUtil.promptPlayersName());
player2 = new Player(GameUtil.promptPlayersName());
//set marker
player1.setPlayerMarker('X');
player2.setPlayerMarker('O');
JPanel gameBoard = new JPanel();
gameBoard.setLayout(new GridLayout(6, 3));
int myBinary = 1;
for (int index = 0; index < 9; index++)
{
buttonArray[index] = new JButton("");
buttonArray[index].addActionListener(this);
buttonArray[index].setActionCommand("" + myBinary);
buttonArray[index].setText("");
gameBoard.add(buttonArray[index]);
myBinary = myBinary * 2;
}
//labels
gameBoard.add(new JLabel("Players"));
gameBoard.add(new JLabel("Score"));
gameBoard.add(new JLabel("Total"));
gameBoard.add(new JLabel("X"));
gameBoard.add(new JLabel("" + player1.getPlayerScore()));
gameBoard.add(new JLabel("" + player1.getPlayerTotal()));
gameBoard.add(new JLabel("O"));
gameBoard.add(new JLabel("" + player2.getPlayerScore()));
gameBoard.add(new JLabel("" + player2.getPlayerTotal()));
this.add(gameBoard);
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
System.out.println(player1.getPlayerName());
System.out.println(player2.getPlayerName());
}
@Override
public void actionPerformed(ActionEvent e)//listener
{
System.out.println(e);
JButton pressedButton = (JButton) e.getSource();
{
if (pressedButton.getText() == "")//X stays X & O stays O
{
if (xTurn)
{
pressedButton.setText("X");
if (player1.getPlayerMarker() == 'X')
{
player1.addPlayerTotal(Integer.parseInt(e.getActionCommand()));
System.out.println(player1.getPlayerTotal());//debug
} else
{
player2.addPlayerTotal(Integer.parseInt(e.getActionCommand()));
System.out.println(player2.getPlayerTotal());//debug
}
xTurn = false;
} else
{
pressedButton.setText("O");
if (player1.getPlayerMarker() == 'O')
{
player1.addPlayerTotal(Integer.parseInt(e.getActionCommand()));
System.out.println(player1.getPlayerTotal());//debug
} else
{
player2.addPlayerTotal(Integer.parseInt(e.getActionCommand()));
System.out.println(player2.getPlayerTotal());//debug
}
xTurn = true;
}
}
}
//debug
System.out.println("Player 1: " + player1.getPlayerName() + " Total: " + player1.getPlayerTotal());
System.out.println("Player 2: " + player2.getPlayerName() + " Total: " + player2.getPlayerTotal());
//check if either player won
System.out.println("P1 Winner: " + check4Winner(player1.getPlayerTotal()));
System.out.println("P2 Winner: " + check4Winner(player2.getPlayerTotal()));
//if either player won
if(check4Winner(player1.getPlayerTotal()))
{
JOptionPane.showMessageDialog(null, player1.getPlayerName() + " WON");
System.out.println(player1.getPlayerName() + " WON");//debug
updatePlayerScore(player1);
resetGame();
}
if(check4Winner(player2.getPlayerTotal()))
{
JOptionPane.showMessageDialog(null, player2.getPlayerName() + " WON");
System.out.println(player2.getPlayerName() + " WON");//debug
updatePlayerScore(player2);
resetGame();
}
//System.out.println("OK");
}
public void resetGame()
{
for (int i = 0; i < 9; i++)
{
buttonArray[i].setText("");
}
player1.setPlayerTotal(0);
player2.setPlayerTotal(0);
}
public static void main(String[] args)
{
//TicTacToe game = new TicTacToe();
new TicTacToe().setVisible(true);
}
public static boolean check4Winner(int total)
{
for (int i = 0; i < winsArray.length; i += 1)
{
if ((winsArray[i] & total) == winsArray[i])
{
return true;
}
}
return false;
}
public void updatePlayerScore(Player score)
{
for(int i = 0; i < 9; i++)
{
player1.updatePlayerScore(+ 1);
player2.updatePlayerScore(+ 1);
}
}