SHARE
TWEET

Untitled

a guest Oct 18th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Assignment 6 -- Prisoner's Dilemma -- 2ip90
  3.  * part PlayingField
  4.  *
  5.  * @author Stefan Adrian Robu, 1449044
  6.  * @author Silvia Garcia Cabaces, 1446304
  7.  * assignment group 28
  8.  *
  9.  * assignment copyright Kees Huizing
  10.  */
  11.  
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.util.*;
  15. import javax.swing.*;
  16. import javax.swing.Timer;
  17.  
  18. class PlayingField extends JPanel implements ActionListener /* possible implements ... */ {
  19.     private int freqv;
  20.     private boolean alternativeRule = false;
  21.     private static int GRID_SIZE= 50;
  22.     private final static int[] xP = new int[]{1, -1, 0, 0, 1, -1, 1, -1}; //x coordinates for neighbours
  23.     private final static int[] yP = new int[]{0, 0, 1, -1, 1, -1, -1, 1}; //y coordinates for neighbours
  24.     private Patch[][] grid;
  25.     private boolean[][] tempGrid;
  26.     private double alpha; // defection award factor
  27.     private Timer timer;
  28.     private GridLayout gridLayout;
  29.     // random number generator
  30.     private static final long SEED = 37L; // seed for random number generator; any number goes
  31.     public static final Random random = new Random( SEED );
  32.  
  33. PlayingField(int gridSize){
  34.     this.timer= new Timer(1000, (e) -> step());
  35.     int GRID_SIZE = gridSize;
  36.     grid = new Patch[GRID_SIZE][GRID_SIZE];
  37.     for (int x = 0 ;x<GRID_SIZE;x++) {
  38.     for(int y =0;y<GRID_SIZE;y++){
  39.         grid[x][y] = new Patch();
  40.     }
  41.     }
  42. }
  43.     // we generate the grid here
  44. public void gridCreate() {
  45.     for (int x = 0; x < GRID_SIZE; x++) {
  46.         for (int y = 0; y < GRID_SIZE; y++) {
  47.             grid[x][y].setCooperating(random.nextBoolean());
  48.         }
  49.     }
  50. }
  51.     // we generate the neighbours List Here
  52.     public void setNeighbour() {
  53.         for (int i = 0; i < GRID_SIZE; i++) {
  54.             for (int j = 0; j < GRID_SIZE; j++) {
  55.                 ArrayList<Patch> neighboursList = new ArrayList<>();
  56.                 for (int n = 0; n < 8; n++) {
  57.                     neighboursList.add(grid[(i + xP[n] + GRID_SIZE) % GRID_SIZE][(j + yP[n] + GRID_SIZE) % GRID_SIZE]);
  58.                     grid[i][j].setNeighbour(neighboursList);
  59.  
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65. /**
  66.      * calculate and execute one step in the simulation
  67.      */
  68.  
  69.     public void step () {
  70.         for (int x = 0; x < grid.length; x++) {
  71.             for (int y = 0; y < grid[0].length; y++) {
  72.                 grid[x][y].setScore(this.alpha);//returns the score of the patch
  73.             }
  74.         }
  75.         //we compare the score of the neighbours
  76.         for (int x = 0; x < grid.length; x++) {
  77.             for (int y = 0; y < grid[0].length; y++) {
  78.                 ArrayList<Patch> results = new ArrayList<>();
  79.                 double highestScore = grid[x][y].getScore();
  80.                 for (Patch i : grid[x][y].getNeighbours())
  81.                     if (i.getScore() > highestScore) {
  82.                         highestScore = i.getScore();
  83.                     }
  84.                 for (Patch i : grid[x][y].getNeighbours()) {
  85.                     if (i.getScore() == highestScore) {
  86.                         results.add(i);
  87.                     }
  88.                     if ((grid[x][y].getScore() == highestScore)) {
  89.                         results.add(grid[x][y]);
  90.                     }
  91.                     if (highestScore == grid[x][y].getScore() && alternativeRule) { // checks if extra rule is in play
  92.                         grid[x][y].isCooperating();
  93.                     }
  94.                 }
  95.                 results.get(PlayingField.random.nextInt(results.size())).isCooperating();
  96.             }
  97.         }
  98.  
  99.     }
  100.     public void setAlpha(double alpha){
  101.             this.alpha= alpha;
  102.     }
  103.     public double getAlpha() {
  104.         return this.alpha;
  105.     }
  106.     public void setAlternativeRule(boolean alternativeRule) {
  107.         alternativeRule = alternativeRule;
  108.     }
  109.  
  110.     public void setFrequency(int freq){
  111.         freqv = freq;
  112.  
  113.         if(!timer.isRunning()){
  114.             timer= new Timer(1000 / freqv, (e) -> step());
  115.         }
  116.         else{
  117.             timer = new Timer (1000 / freqv, (e) -> step());
  118.             timer.start();
  119.         }
  120.     }
  121.     @Override
  122.     protected void paintComponent(Graphics g){
  123.         for(int x  =0; x < grid.length; x++){
  124.             for(int y = 0; y < grid.length; y++){
  125.                 if(grid[x][y].isCooperating()){
  126.                     g.setColor(Color.blue);
  127.  
  128.                 }
  129.                 else {
  130.                     g.setColor(Color.red);
  131.                 }
  132.  
  133.                 g.fillRect(x*10+40,y*10+20,9,9);
  134.             }
  135.         }
  136.     }
  137.  
  138.     // return grid as 2D array of booleans
  139.     // true for cooperators, false for defectors
  140.     // precondition: grid is rectangular, has non-zero size and elements are non-null
  141.     public boolean[][] getGrid() {
  142.         boolean[][] resultGrid = new boolean[grid.length][grid[0].length];
  143.         for (int x = 0; x < grid.length; x++ ) {
  144.             for (int y = 0; y < grid[0].length; y++ ) {
  145.                 resultGrid[x][y] = grid[x][y].isCooperating();
  146.             }
  147.         }
  148.        
  149.         return resultGrid;
  150.     }
  151.     // sets grid according to parameter inGrid
  152.     // a patch should become cooperating if the corresponding
  153.     // item in inGrid is true
  154.     public void setGrid( boolean[][] inGrid) {
  155.          for (int x = 0; x < grid.length; x++ ) {
  156.             for (int y = 0; y < grid[0].length; y++ ) {
  157.                 grid[x][y].setCooperating(inGrid[x][y]);
  158.             }
  159.         }
  160.     }
  161.  
  162.     public void timeStart(){
  163.             if(!timer.isRunning()) {
  164.                 timer.start();
  165.             }
  166.             else{
  167.                 timer.stop();
  168.             }
  169.     }
  170.     public void stop(){
  171.             timer.stop();
  172.     }
  173.  
  174.     @Override
  175.     public void actionPerformed(ActionEvent e){
  176.         if(e.getSource() == timer){
  177.             step();
  178.         }
  179.     }
  180. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top