

import java.awt.*;
import java.util.*;
import java.awt.event.*;


//  Example for array of "non-booleans"!

 class Tournaments extends Frame implements WindowListener, MouseListener

   {

     private static Tournaments arena;
     private static final int STARTWIDTH = 600;  // Initial size of
     private static final int STARTHEIGHT = 500; //  window.
     private static int width;      //  current width of frame (pixels)
     private static int height;     //  current height of frame (pixels)

     private static prisonerA prisoners[];
     private static final int MAX_NUM_PRISONERS = 30;
     private static int num_prisoners;
     private static int single_round_scores[];
     private static int double_round_scores[];
     private static int triple_round_scores[];
     private static int hundred_round_scores[];
     private static int single_round_self_scores[];
     private static int five_round_self_scores[];
     private static int ten_round_self_scores[];

     private static Graphics g;
     private static boolean stopped;



     public static void main(String [] arguments) throws Exception
        {

          arena = new Tournaments();


          arena.play_round_robin(1,single_round_scores); //
          arena.play_round_robin(2,double_round_scores);
          arena.play_round_robin(3,triple_round_scores);
          arena.play_round_robin(100,hundred_round_scores);

          arena.play_selves(1,single_round_self_scores);
          arena.play_selves(5,five_round_self_scores);
          arena.play_selves(10,ten_round_self_scores);
          arena.paint(g);    // Re-paint everything.
         }


//  constructor

     public Tournaments()
        {
           super("  WELCOME to the Prisoners' Dilemma ARENA  ");  //  Create and name window
           setSize(STARTWIDTH,STARTHEIGHT); //  Set initial size
           setBackground(Color.yellow.brighter()); // Initial back. color
           setVisible(true);                //  Display it!b

           addWindowListener(this); //  Tell Java runner that this frame
           addMouseListener(this);  //   obj will handle Window events
                                    //   and mouse events, so give them
                                    //   to us!
           stopped = false;

           prisoners = new prisonerA[MAX_NUM_PRISONERS];
           single_round_scores = new int[MAX_NUM_PRISONERS];
           double_round_scores = new int[MAX_NUM_PRISONERS];
           triple_round_scores = new int[MAX_NUM_PRISONERS];
           hundred_round_scores = new int[MAX_NUM_PRISONERS];
           single_round_self_scores = new int[MAX_NUM_PRISONERS];
           five_round_self_scores = new int[MAX_NUM_PRISONERS];
           ten_round_self_scores = new int[MAX_NUM_PRISONERS];

           g = getGraphics();

           prisoners[0] = new Steph();
           prisoners[1] = new Nathan();
           prisoners[2] = new MakiPlayer();
           prisoners[3] = new greenfire();
           prisoners[4] = new Problem();
           prisoners[5] = new Nicola();
           prisoners[6] = new game();
           prisoners[7] = new dr_x();
           prisoners[8] = new mikes_Prisioner();
           prisoners[9] = new Thomas();
 //          prisoners[10] = new CalebsPrisoner();
           prisoners[10] = new Kuempel();
           prisoners[11] = new TB();
           prisoners[12] = new Prisoner();

           num_prisoners = 13;




        }

     public void paint(Graphics g)
        {
//         width = getWidth();   //  Window might have re-sized.  So get
//          height = getHeight();  //  latest dimensions.
          width = getSize().width;
          height = getSize().height;

          g.clearRect(0,0,600,600);  // Erase the
                                                         // entire active
                                                         //  area



          for(int i=0; i< num_prisoners; i++)
             {
              g.drawString(" NAME           SCORE AGAINST:   OTHERS(1 rnd) (2 rnds) (3 rnds)  (100 rnds)   SELF",
                            23, 40);
              g.drawString(prisoners[i].getName() +  ":  ",20,60+i*15);
              g.drawString(" "+ single_round_scores[i] + "        "
                           + double_round_scores[i] + "         "
                           + triple_round_scores[i] + "          "
                           + hundred_round_scores[i] + "         "
                           + single_round_self_scores[i] + "         "
                           + five_round_self_scores[i] + "         "
                           + ten_round_self_scores[i],
                           200, 60+i*15);
              }


         }



      private void play_round_robin(int num_rounds, int [] scores)
         {

            for(int i =0; i<num_prisoners; i++)   // For eqach prisoner, play
                for(int j=0; j<num_prisoners; j++)  //  against every prisoner
                   if(i!=j)
                      for(int k=0; k<num_rounds; k++) //  NUM_ROUNDS times.
                        play_one_round(i,j, scores);
          }

      private void play_selves(int num_rounds, int [] scores)
         {

            for(int i =0; i<num_prisoners; i++)   // For eqach prisoner, play
                {                                  //  against themselves
                    for(int k=0; k<num_rounds; k++) //  NUM_ROUNDS times.
                        play_one_round(i,i, scores);
                  scores[i] = (int) scores[i]/2;
                 }
          }



//   Use payoff matrix from handout.

      private void play_one_round(int A, int B, int [] scores)
           {
             boolean Amove = prisoners[A].your_move(prisoners[B].getName());
             boolean Bmove = prisoners[B].your_move(prisoners[A].getName());

             if(!Amove)  // if A cooperates
                if(!Bmove)  //  and if B cooperates
                   { scores[A]+=3;
                     scores[B]+=3;  }   // they both get 3
                else         //  else B defects
                    scores[B]+=5;   // so A gets nothing, B gets 5
             else        //  else A defects
                if (!Bmove )  // and if B cooperates
                    scores[A]+=5;  //  so A gets 5 and B gets nothing!
                else          // else B defected
                    {  scores[A]+=1;   //  so they both get just 1.
                       scores[B]+=1;}

             prisoners[A].result(Bmove);  // Lastly, tell them each what
             prisoners[B].result(Amove);  //  the other one did!
           }







  //  Methods needed to implement the "WindowListener" interface

     public void windowOpened(WindowEvent win_ev)  {}
     public void windowClosing(WindowEvent win_ev)  // Catches click on
                 {                                  //  the window's "X"
                  dispose();
                  }
     public void windowClosed(WindowEvent win_ev)   {}
     public void windowActivated(WindowEvent win_ev)  {}
     public void windowDeactivated(WindowEvent win_ev)  {}
     public void windowIconified(WindowEvent win_ev)  {}
     public void windowDeiconified(WindowEvent win_ev)  {}

  //  Methods needed to implement the "MouseListener" interface

     public void mouseClicked(MouseEvent me)
                  {

                  	int x = me.getX();
           			int y = me.getY();
                    System.out.println("mouse click at " +
                                       x + ", " + y);

                    stopped = !stopped;   //  toggle the execution.

                    }
     public void mouseEntered(MouseEvent me)  {}
     public void mouseExited(MouseEvent me)  {}
     public void mousePressed(MouseEvent me)
          {

            arena.paint(g);    // Re-paint everything.
          }
     public void mouseReleased(MouseEvent me)  {}

   }