Have a problem making battleship game. I created 2 panels with game decks for player and opponent. enter image description here When i click on opponents tile and miss i want programm to choose random tile on player deck and make a move.
All opponents deck buttons placed inside List of button. I use actionlistener for opponents deck Program choose random tile on player deck
public void buttonPress(){
OpponentDeckGUI odGUI = new OpponentDeckGUI();
PlayerDeckAggregator pda = new PlayerDeckAggregator();
ArrayList<JButton> localList = new ArrayList<>();
for(JButton button : odGUI.getOpponentCommonButtonList()){
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(odGUI.getOpponentPinkButtonList().contains(button)){
button.setBackground(Color.PINK);
}else{
button.setBackground(Color.CYAN);
}
localList.add(button);
}
});
odGUI.getOpponentPinkButtonList().remove(button);
}
}
public void randomChooser(){
PlayerDeckCreationListener pdcl = new PlayerDeckCreationListener();
PlayerDeckGUI pdGUI = new PlayerDeckGUI();
int randomTile = (int) (Math.random()*pdGUI.getPlayerCommonButtonList().size());
JButton localButton = pdGUI.getPlayerCommonButtonList().get(randomTile);
if(pdcl.getPlayerDeckList().contains(localButton)){
localButton.setBackground(Color.RED);
}
else {
localButton.setBackground(Color.CYAN);
}
pdGUI.getPlayerCommonButtonList().remove(localButton);
}
give me advise, how to make a queue work when i click button on opponents deck and get a response on player deck.
Found solution. I placed method that starts when i click and dont get on opponents boat inside actionPerformed part.
public void buttonListCreator(){
OpponentDeckGUI odGUI = new OpponentDeckGUI();
for(JButton button : odGUI.getOpponentCommonButtonList()){
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(odGUI.getOpponentPinkButtonList().contains(button)){
button.setBackground(Color.PINK);
}
else{
button.setBackground(Color.CYAN);
PlayerDeckAggregator pda = new PlayerDeckAggregator();
pda.randomChooser();
}
}
});
}
}
So now, when i click on opponens deck and miss, i get program turn to check tile on players deck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.