Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void alternateAI(){
- try{
- TimeUnit.MILLISECONDS.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- //make a list of all pieces
- //that are taken by the other player
- ArrayList<HexButton> hexButtonArrayList = new ArrayList<HexButton>();
- ArrayList<HexButton> hexButtonChoice = new ArrayList<HexButton>();
- for (int col = HexPanel.COUNT - 1; col >= 0; col--){
- for (int row = HexPanel.COUNT - 1; row >= 0; row--){
- if (HexPanel.getGrid()[col][row].getFill_val() == 1){
- hexButtonArrayList.add(HexPanel.getGrid()[col][row]);
- }
- }
- }
- //going through each item in the list
- //see if the piece has free spaces in above or below the piece. adding it to another list
- for (int x = 0; x < hexButtonArrayList.size(); x++){
- HexButton hexButtonCurrent = hexButtonArrayList.get(x);
- int hexRow = hexButtonCurrent.getRow();
- int hexCol = hexButtonCurrent.getCol();
- try{
- if (HexPanel.getGrid()[hexCol][hexRow + 1].getFill_val() == 0){
- hexButtonChoice.add(HexPanel.getGrid()[hexCol][hexRow + 1]);
- }
- if(HexPanel.getGrid()[hexCol + 1][hexRow + 1].getFill_val() == 0){
- hexButtonChoice.add(HexPanel.getGrid()[hexCol + 1][hexRow + 1]);
- }
- if(HexPanel.getGrid()[hexCol - 1][hexRow].getFill_val() == 0){
- hexButtonChoice.add(HexPanel.getGrid()[hexCol - 1][hexRow]);
- }
- if(HexPanel.getGrid()[hexCol - 1][hexRow - 1].getFill_val() == 0){
- hexButtonChoice.add(HexPanel.getGrid()[hexCol - 1][hexRow - 1]);
- }
- } catch (ArrayIndexOutOfBoundsException e){
- continue;
- }
- }
- //add a random item to the list
- int AIx = (int) (Math.random() * 11);
- int AIy = (int) (Math.random() * 11);
- while (!availableSpace(AIx, AIy)) {
- AIx = (int) (Math.random() * 11);
- AIy = (int) (Math.random() * 11);
- }
- HexButton hexButtonRandom = HexPanel.getGrid()[AIx][AIy];
- hexButtonChoice.add(hexButtonRandom);
- //decide randomly which one to play
- HexButton hexButtonFinal = hexButtonChoice.get((int) Math.random() * hexButtonChoice.size());
- hexButtonFinal.setFill_val(getPlayerTurn());
- hexButtonFinal.repaint();
- getLog().getLog()
- .setText(getLog().getLog().getText() + "PLAYER "
- + getPlayerTurn() + " PLAYED\nPOSITION\nROW: " + hexButtonFinal.getRow()
- + " COL: " + hexButtonFinal.getCol() + ".\n\n");
- checkForVictory();
- setPlayerTurn((getPlayerTurn() == 1) ? 2 : 1);
- //activate info on other user's game
- getGrid().sendInfo((Server.getServerIn() != null) ? Server.getServerClientSocket() : Client.getClientSocket(), hexButtonFinal.getCol(), hexButtonFinal.getRow());
- updateSettings(getPlayerTurn());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement