/********************************************************
* Filename              : Game.java
* Version               : Beta V1.0
* Author                : Roger Webster, Ph.D.
* Modified by Mary Garland & Kristi Garner
**********************************************************/
import java.awt.*;
import java.net.*;
import java.applet.*;
        
class Game extends Frame implements Runnable
{ 
        private clientwindow cw;
        private Thread thread;
        private String nextMove = "test";
        private String opponent_move;
        private String move;
        private boolean you_go_first;
        private boolean move_made = false;
        private boolean sent_my_move = false;
        private URL base_url;
        //private int framewidth = 400;
        //private int frameheight = 300;
        private int framewidth = 850;
        private int frameheight = 925;
        Applet myapplet;
        AudioClip mysound;
        Image Gameimage;
        private TextField textField;
	button_panel b1;
	button_panel b2;
	button_panel b3;

  Image image[] = new Image[6];                 // holds images 
  Image letter_images[] = new Image[26];        // holds tile images    
  Image emptytile;
  board_panel board_pan;                        // game board
  tiles_panel tiles;
  boolean my_turn;

  TextArea info, wordlist;
  TextField score1, score2;
  MessageBox mb1, mb2;
  Image m;
  Image scores, logo, instruct;
  ImageCanvas points, clogo, cinstruct;  
  Color lavendar = new Color (230,230,250);

// constructor
Game (Applet myapplet, URL base_url, clientwindow parent, boolean you_go_first, Image gameimage) 
{
        super("Scrabble Game Window");      
        this.base_url = base_url;
        this.cw = parent;
        this.you_go_first = you_go_first;
        my_turn = you_go_first;
        this.Gameimage = gameimage;
        this.myapplet = myapplet;
        mysound = myapplet.getAudioClip(base_url, "BEEPBEEP.au");
        load_images();
        mb1 = new MessageBox("Game Information", m, 700,600);
        mb1.setVisible (false);
        mb2 = new MessageBox ("About Information",m, 360, 325);
        mb2.setVisible (false);

        this.start();


}

// gets the thread going
public void start() 
{
        System.out.println("starting thread");
        thread = new Thread(this,"Game");
        thread.setPriority(Thread.MIN_PRIORITY); 
        thread.start();
}

public void run() 
{
        //mysound.play();
        System.out.println("RUNNING thread");
        if (!you_go_first)
        {
                sent_my_move = true;
        }

        makewindow();
        while (true) 
        {
                try{thread.sleep(20);}  
                catch(InterruptedException e){}

                // wait for opponent's move     
                if (sent_my_move)
                {
                        move = this.get_opponent_move();
                        //info.setText(move);
                        board_pan.getOpponentMove(move); 
                        // process the move
                }       

        } // loop until the game is over
}


// called by clientwindow Don't change this!!!
public synchronized void set_opponent_move(String nextMove)
{
        move_made = true;
        System.out.println("setting opponent's next move is " + nextMove);
        opponent_move = nextMove;
        notifyAll();
}

// Don't change this!!!
public synchronized String get_opponent_move()
{
        if (!move_made)
        {
                try
                {
                        wait();
                }
                catch (Exception e)
                {
                        System.out.println("Exception in wait " + e.toString());        
                }
        }
        System.out.println("get opponent's next move " + opponent_move);
        move_made = false;
        sent_my_move = false;
        return (opponent_move);

}
          
public void send_move (String nextMove)
{
                
        if (!sent_my_move)
        {
                System.out.println("waiting for send my move its false\n");
        }
        cw.sendMove(nextMove); // sends move to other player
        sent_my_move = true;
        System.out.println("sent move " + nextMove);

}

// You re-write all this...
public void makewindow()
{
          this.setBackground(Color.cyan);
          /* this.setForeground(Color.black);*/
          this.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
	  scores = myapplet.getImage(base_url,"scores.gif");
	  logo = myapplet.getImage(base_url, "logo.jpg");
	  instruct = myapplet.getImage(base_url, "instruct2.jpg");
	  clogo = new ImageCanvas(logo, 3);
	  cinstruct = new ImageCanvas(instruct, 2);
	  points = new ImageCanvas(scores, 1);
          score1 = new TextField ("Player 1 score = 0");
          score1.setBackground(lavendar);
	  score2 = new TextField ("Player 2 score = 0");
	  score2.setBackground(lavendar);
          wordlist = new TextArea (10,28);
          info = new TextArea (10,35);
	  wordlist.setBackground(lavendar);
	  info.setBackground(lavendar);
          info.setEditable(false);
          wordlist.setEditable (false);
          score1.setEditable (false);
          score2.setEditable (false);
       
       	b1 = new button_panel("help","about","quit");
	b2 = new button_panel( "done","undo");
	b3 = new button_panel("challenge", "accept");

	this.add(wordlist);
	this.add(clogo);
	this.add(info);
        this.add(cinstruct);
	cinstruct.setVisible(true);
	clogo.setVisible(true);
	wordlist.append("No words created yet\n");
	    // Instantiate game board

        board_pan = new board_panel(15,15,my_turn,this,info,wordlist,
                    score1,score2,b2,b3);
	this.add(board_pan); 
  
	this.add(points);
	this.add(b1);
	this.add(b2);


        // Instantiate player tiles
        // 
        tiles = new tiles_panel(emptytile, 1, letter_images, board_pan);
	this.add(score1); 
        this.add(tiles);
	this.add(score2);
	this.add(b3);

        place_board_squares();

        this.setSize(framewidth, frameheight);
        this.setResizable(false);
        this.show(); 
}

public boolean action(Event evt, Object arg) 
{
      if (evt.target instanceof Button) {
        String label = (String)arg;

        if (label.equals("Quit")) {
          cw.quitWindow();
          mb1.quitWindow();
          mb2.quitWindow();
          this.dispose();
          info.append("Your opponent quit.  YOU WIN!!\n");
          return true;
        } else if(label.equals("Done")) {
          board_pan.doneTurn();
        } else if(label.equals("Undo")) {
          board_pan.undoTurn();
        } else if(label.equals("Accept")) {
          board_pan.acceptTurn();
        } else if(label.equals("Challenge")) {
          board_pan.challengeTurn();
        } else if(label.equals("Help")) {
          mb1.setVisible(true);
        }
        else if (label.equals ("About")) {
          mb2.setVisible (true);

        }
      }
        if (evt.target == textField) 
        {
                nextMove = textField.getText();
                send_move(nextMove);
                return true;
        
        }
    return false;
}


public void load_images() {

    int i;
    String image_name;
    char ch;

    // load images
    myapplet.showStatus("Loading images");
    image[0] = myapplet.getImage(base_url, "emptytile.jpg");
    image[1] = myapplet.getImage(base_url, "doublet.jpg");
    image[2] = myapplet.getImage(base_url, "triplet.jpg");
    image[3] = myapplet.getImage(base_url, "doubword.jpg");
    image[4] = myapplet.getImage(base_url, "tripword.jpg");
    image[5] = myapplet.getImage(base_url, "swirl.jpg");
    emptytile = myapplet.getImage(base_url, "smile.jpg");

    ch = (char)97;
    for(i = 0; i < 26; i++) {
      image_name = "tile_" + ch + ".jpg";
      letter_images[i] = myapplet.getImage(base_url, image_name);
      ch++;
    }

    myapplet.showStatus("Done loading images");

}

public void place_board_squares() {

    int i, j;
    int lscore, wscore, x;
    int [][] gboard = new int[15][15];

    // empty squares
    for(i = 0; i < 15; i++) {
      for(j = 0; j < 15; j++) {
       gboard[i][j] = 0;
      }
    }
    // board squares that will be double letter = 1
    gboard[3][3] = 1; gboard[3][11] = 1; gboard[5][7] = 1; gboard[7][5] = 1; gboard[7][9] = 1; 
    gboard[9][7] = 1; gboard[11][3] = 1; gboard[11][11] = 1;

    // board squares that will be triple letter = 2
    gboard[2][2] = 2; gboard[2][7] = 2; gboard[2][12] = 2; gboard[3][5] = 2; gboard[3][9] = 2; 
    gboard[4][6] = 2; gboard[4][8] = 2; gboard[5][3] = 2; gboard[5][11] = 2; gboard[6][4] = 2; 
    gboard[6][10] = 2; gboard[7][2] = 2; gboard[7][12] = 2;gboard[8][4] = 2; 
    gboard[8][10] = 2; gboard[9][3] = 2; gboard[9][11] = 2; gboard[10][6] = 2; gboard[10][8] = 2; 
    gboard[11][5] = 2; gboard[11][9] = 2; gboard[12][2] = 2; gboard[12][7] = 2; gboard[12][12] = 2;
    
    // board squares that will be double word = 3
    gboard[1][1] = 3; gboard[1][6] = 3; gboard[1][8] = 3; gboard[1][13] = 3; gboard[6][1] = 3; 
    gboard[6][13] = 3; gboard[8][1] = 3; gboard[8][13] = 3; gboard[13][1] = 3; gboard[13][6] = 3; 
    gboard[13][8] = 3; gboard[13][13] = 3; 

    // board squares that will be triple word = 4
    gboard[0][0] = 4; gboard[0][5] = 4; gboard[0][9] = 4; gboard[0][14] = 4; gboard[5][0] = 4; 
    gboard[5][14] = 4; gboard[9][0] = 4; gboard[9][14] = 4; gboard[14][0] = 4; gboard[14][5] = 4;
    gboard[14][9] = 4; gboard[14][14] = 4; 

    // swirl
     gboard[7][7] = 5;

    // Add game board panels
    // .....add_a_panel(image to be displayed; row; col; single, double, or triple letter score;
    //                   single, double, or triple word score; tiles) 
    for(i = 0; i < 15; i++) {
      for(j = 0; j < 15; j++) {
        x = gboard[i][j];
        lscore = 1;
        wscore = 1;
        if(x == 1) {
          lscore = 2;
        } else if(x == 2) {
          lscore = 3;
        } else if(x == 3) {
          wscore = 2;
        } else if(x == 4) {
          wscore = 3;
        }
        board_pan.add_a_panel(image[x], i, j, lscore, wscore, tiles);
      }
    }


}

} // end Game class


