Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public void alternateAI(){
  2. try{
  3. TimeUnit.MILLISECONDS.sleep(500);
  4. } catch (InterruptedException e) {
  5. e.printStackTrace();
  6. }
  7. //make a list of all pieces
  8. //that are taken by the other player
  9. ArrayList<HexButton> hexButtonArrayList = new ArrayList<HexButton>();
  10.  
  11. ArrayList<HexButton> hexButtonChoice = new ArrayList<HexButton>();
  12.  
  13. for (int col = HexPanel.COUNT - 1; col >= 0; col--){
  14. for (int row = HexPanel.COUNT - 1; row >= 0; row--){
  15. if (HexPanel.getGrid()[col][row].getFill_val() == 1){
  16. hexButtonArrayList.add(HexPanel.getGrid()[col][row]);
  17. }
  18. }
  19. }
  20.  
  21. //going through each item in the list
  22. //see if the piece has free spaces in above or below the piece. adding it to another list
  23. for (int x = 0; x < hexButtonArrayList.size(); x++){
  24. HexButton hexButtonCurrent = hexButtonArrayList.get(x);
  25. int hexRow = hexButtonCurrent.getRow();
  26. int hexCol = hexButtonCurrent.getCol();
  27. try{
  28. if (HexPanel.getGrid()[hexCol][hexRow + 1].getFill_val() == 0){
  29. hexButtonChoice.add(HexPanel.getGrid()[hexCol][hexRow + 1]);
  30. }
  31. if(HexPanel.getGrid()[hexCol + 1][hexRow + 1].getFill_val() == 0){
  32. hexButtonChoice.add(HexPanel.getGrid()[hexCol + 1][hexRow + 1]);
  33. }
  34. if(HexPanel.getGrid()[hexCol - 1][hexRow].getFill_val() == 0){
  35. hexButtonChoice.add(HexPanel.getGrid()[hexCol - 1][hexRow]);
  36. }
  37. if(HexPanel.getGrid()[hexCol - 1][hexRow - 1].getFill_val() == 0){
  38. hexButtonChoice.add(HexPanel.getGrid()[hexCol - 1][hexRow - 1]);
  39. }
  40. } catch (ArrayIndexOutOfBoundsException e){
  41. continue;
  42. }
  43.  
  44. }
  45.  
  46. //add a random item to the list
  47. int AIx = (int) (Math.random() * 11);
  48. int AIy = (int) (Math.random() * 11);
  49. while (!availableSpace(AIx, AIy)) {
  50. AIx = (int) (Math.random() * 11);
  51. AIy = (int) (Math.random() * 11);
  52. }
  53. HexButton hexButtonRandom = HexPanel.getGrid()[AIx][AIy];
  54.  
  55. hexButtonChoice.add(hexButtonRandom);
  56.  
  57. //decide randomly which one to play
  58. HexButton hexButtonFinal = hexButtonChoice.get((int) Math.random() * hexButtonChoice.size());
  59. hexButtonFinal.setFill_val(getPlayerTurn());
  60. hexButtonFinal.repaint();
  61. getLog().getLog()
  62. .setText(getLog().getLog().getText() + "PLAYER "
  63. + getPlayerTurn() + " PLAYED\nPOSITION\nROW: " + hexButtonFinal.getRow()
  64. + " COL: " + hexButtonFinal.getCol() + ".\n\n");
  65. checkForVictory();
  66. setPlayerTurn((getPlayerTurn() == 1) ? 2 : 1);
  67. //activate info on other user's game
  68. getGrid().sendInfo((Server.getServerIn() != null) ? Server.getServerClientSocket() : Client.getClientSocket(), hexButtonFinal.getCol(), hexButtonFinal.getRow());
  69. updateSettings(getPlayerTurn());
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement