//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "audio.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
#include "Unit5.h"
#include "Unit6.h"
#include "Video.h"
#include "compress.h"

#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>

#include "winsock.h"

#define PORT 6000
#define WSA_ASYNC WM_USER+1
#define MSG_LEN 1024
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
HWND hWnd=Application->Handle;
WSADATA wsadata;
SOCKET sock;
int iRet;
struct sockaddr_in FAR stSock, stAddr;
char* audio;
Video video;
Compressor compressor;
char image[300000];

#define buffsize 2000
WAVEFORMATEX wformat;
WAVEHDR whdr,whdo;
HWAVEIN hwi;
HWAVEOUT hwo;
DWORD volume;
char audiobuff[4][buffsize],recvbuff[4][buffsize];
int current=0, currentplay=0;
MMRESULT result;
WAVEINCAPS m_WaveInDevCaps;
bool callback=false;
int buffpos;
int refuse = 0;
void CALLBACK record_callback(HWAVEIN, UINT, DWORD, DWORD, DWORD);
void play();
AnsiString ComBuf, temp, temp2, temp3, nickName;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
   ServerSocket1->Open();
   useSer = 0;
   counter = 0;
           num = 0;
        cnum = 1;
        max = 0 ;
        listen = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	//當有連線訊息時，執行ApplicationOnMessage並將Message送過去
   Application->OnMessage = ApplicationOnMessage;

   wformat.wFormatTag=WAVE_FORMAT_PCM;
   wformat.nChannels=2;
   wformat.nSamplesPerSec=30000;
   wformat.nAvgBytesPerSec=120000;
   wformat.nBlockAlign=4;
   wformat.wBitsPerSample=16;
   result=waveOutOpen(&hwo, WAVE_MAPPER, &wformat, NULL, 0L, CALLBACK_NULL);
   /*Form1->Memo1->Lines->Add("waveOutOpen");
   Form1->Memo1->Lines->Add(AnsiString(result));*/

   video.init(Form1->Handle);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)//get connection
{
   ClientSocket1->Host = Edit1->Text;
   ClientSocket1->Port = PORT;
   ClientSocket1->Open();
   NMUDP3->RemoteHost=Edit1->Text;
   NMUDP4->RemoteHost=Edit1->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationOnMessage(tagMSG &Msg, bool &Handled)
{
/*	switch(Msg.message)
	{
		case WSA_ASYNC:
        		switch(WSAGETSELECTEVENT(Msg.lParam)){
                		case FD_READ:
                                	int addrSize=sizeof(stAddr);
                                	iRet=recvfrom(Msg.wParam, audio, MSG_LEN, 0, (LPSOCKADDR) &stAddr, &addrSize);
                                        if(iRet==SOCKET_ERROR){
                                        	Memo1->Lines->Add("ERROR: 無法收資料！\n");
                                                //socketClose(Msg.wParam);
                                        	return;
                                        }
                                break;
                        }
                break;
        }           */
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   if ( useSer == 1)
      NMUDP1->RemoteHost = ServerSocket1->Socket->Connections[0]->RemoteHost;
   else
      NMUDP1->RemoteHost = Edit1->Text;
   Memo1->Lines->Add("開始傳送語音訊息");
        //recording
      whdr.lpData=audiobuff[0];
      whdr.dwBufferLength=buffsize;
      whdr.dwFlags=0;

      result=waveInGetNumDevs();
      //Memo1->Lines->Add("waveInGetNumDevs");
      //Memo1->Lines->Add(AnsiString(result));
      result=waveInGetDevCaps (0, &m_WaveInDevCaps, sizeof(WAVEINCAPS));
      //Memo1->Lines->Add("waveInGetDevCaps");
      //Memo1->Lines->Add(AnsiString(result));

      result=waveInOpen(&hwi, WAVE_MAPPER, &wformat,(DWORD) record_callback, NULL, CALLBACK_FUNCTION);
        //result=waveInOpen(&hwi, WAVE_MAPPER, &wformat, 0, NULL, CALLBACK_NULL);

      //Memo1->Lines->Add("waveInOpen");
      //Memo1->Lines->Add(AnsiString(result));

      result=waveInPrepareHeader(hwi, &whdr, sizeof(WAVEHDR));
      //Memo1->Lines->Add("waveInPrepareHeader");
      //Memo1->Lines->Add(AnsiString(result));
      result=waveInAddBuffer(hwi,&whdr,sizeof(WAVEHDR));
      //Memo1->Lines->Add("waveInAddBuffer");
      //Memo1->Lines->Add(AnsiString(result));
      result=waveInStart(hwi);
      //Memo1->Lines->Add("waveInStart");
      //Memo1->Lines->Add(AnsiString(result));
      current=0;
      callback=true;
}
//---------------------------------------------------------------------------
void CALLBACK record_callback(HWAVEIN hwi, UINT uMsg, DWORD dwInstance,
    DWORD dwParam1, DWORD dwParam2)
{
    int temp;
    if(callback){

        waveInUnprepareHeader(hwi, &whdr, sizeof(WAVEHDR));
        temp = current;
        if(current==0) current=1;
        else if(current==1) current=2;
        else if(current==2) current=3;
        else if(current==3) current=0;
        whdr.lpData=audiobuff[current];
        whdr.dwBufferLength=buffsize;
        whdr.dwFlags = 0;
        result=waveInPrepareHeader(hwi, &whdr, sizeof(WAVEHDR));
        result=waveInAddBuffer(hwi,&whdr,sizeof(WAVEHDR));
        Form1->NMUDP1->SendBuffer(audiobuff[temp],buffsize,buffsize);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
   waveInStop(hwi);
   waveInClose(hwi);
   waveOutReset(hwo);
   waveOutUnprepareHeader( hwo, &whdr, sizeof( WAVEHDR));
   waveOutClose(hwo);
   Memo1->Lines->Add("關閉語音傳送");
}
//---------------------------------------------------------------------------
void play()
{
	result=waveOutOpen(&hwo, WAVE_MAPPER, &wformat, NULL, NULL, CALLBACK_NULL);
        //Form1->Memo1->Lines->Add("waveOutOpen");
        //Form1->Memo1->Lines->Add(AnsiString(result));
        result=waveOutPrepareHeader(hwo, &whdr, sizeof(WAVEHDR));
        //Form1->Memo1->Lines->Add("waveOutPrepareHeader");
        //Form1->Memo1->Lines->Add(AnsiString(result));
        result=waveOutWrite(hwo, &whdr, sizeof(WAVEHDR));
        //Form1->Memo1->Lines->Add("waveOutWrite");
        //Form1->Memo1->Lines->Add(AnsiString(result));

}
//---------------------------------------------------------------------------


void __fastcall TForm1::NMUDP1DataSend(TObject *Sender)
{
    //Form1->Memo1->Lines->Add("Send success");
}
//---------------------------------------------------------------------------


void __fastcall TForm1::NMUDP2DataReceived(TComponent *Sender,
      int NumberBytes, AnsiString FromIP, int Port)
{
   waveOutReset(hwo);
   NMUDP2->ReadBuffer(recvbuff[currentplay], NumberBytes, NumberBytes);
   //Form1->Memo1->Lines->Add("Receive success");
   //Form1->Memo1->Lines->Add(recvbuff[currentplay]);
   //Form1->Memo1->Lines->Add(FromIP);
   result=waveOutUnprepareHeader(hwo, &whdo, sizeof(WAVEHDR));
   //Form1->Memo1->Lines->Add("waveOutUnprepareHeader");
   //Form1->Memo1->Lines->Add(AnsiString(result));
   whdo.lpData=recvbuff[currentplay];
   whdo.dwBufferLength=buffsize;
   whdo.dwFlags=0;

   result=waveOutPrepareHeader(hwo, &whdo, sizeof(WAVEHDR));
   //Form1->Memo1->Lines->Add("waveOutPrepareHeader");
   //Form1->Memo1->Lines->Add(AnsiString(result));
   (DWORD) volume = MAKEWPARAM(TrackBar1->Position, TrackBar1->Position);
   waveOutSetVolume(hwo, volume);
   result=waveOutWrite(hwo, &whdo, sizeof(WAVEHDR));
   //Form1->Memo1->Lines->Add("waveOutWrite");
   //Form1->Memo1->Lines->Add(AnsiString(result));


   //Form1->Memo1->Lines->Add(AnsiString(currentplay));
   if ( currentplay < 3 )
      currentplay++;
   else
      currentplay = 0;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
   (DWORD) volume = MAKEWPARAM(TrackBar1->Position, TrackBar1->Position);
   waveOutSetVolume(hwo, volume);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
  Form3->Show();
  if ( useSer == 1)
  {
     ServerSocket1->Socket->Connections[0]->SendText("Board");
     Form3->NMUDP1->RemoteHost = ServerSocket1->Socket->Connections[0]->RemoteHost;
     Form3->NMUDP1->LocalPort = 6100;
     Form3->NMUDP1->RemotePort = 6110;
  }
  else
  {
     ClientSocket1->Socket->SendText("Board");
     Form3->NMUDP1->RemoteHost = Edit1->Text;
     Form3->NMUDP1->LocalPort = 6110;
     Form3->NMUDP1->RemotePort = 6100;

  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,
      TCustomWinSocket *Socket)
{

   if ( useSer == 0)
   {
      if ( refuse == 0)
      {
         Form2->Label1->Caption = Form2->Label1->Caption + Socket->RemoteAddress + "的連線嗎？";
         Form2->ShowModal();
         if ( Form2->accept1 == true)
         {
            Socket->SendText("accept");
            Memo1->Lines->Add("連線建立成功");
            Button1->Enabled = false;
            Edit1->Enabled = false;
            SpeedButton2->Enabled = true;
            SpeedButton1->Enabled = true;
            SpeedButton3->Enabled = true;
            Button2->Enabled = true;
            Button3->Enabled = true;
            Edit2->Enabled = true;
            useSer = 1;
            refuse = 1;
            Form6->ShowModal();
            nickName = Form6->Edit1->Text;
                if(video.initflag==true  && video.startcap==true)
                {
                    Button5->Enabled=true;
                }
            onconnectflag=true;    
         }
         else
         {
            Memo1->Lines->Add("我拒絕連線");
            SpeedButton2->Enabled = false;
            SpeedButton1->Enabled = false;
            SpeedButton3->Enabled = false;
            SpeedButton4->Enabled = false;
            Button2->Enabled = false;
            Button3->Enabled = false;
            Edit2->Enabled = false;
            useSer = 0;
            refuse = 0;
            Socket->Close();
         }
      }
      else
         Socket->Close();
   }
   Form2->Label1->Caption = "您要接受來自";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
   Memo1->Lines->Add("對方拒絕連線");
   Button1->Enabled = true;
   Edit1->Enabled = true;
   SpeedButton2->Enabled = false;
   SpeedButton1->Enabled = false;
   SpeedButton3->Enabled = false;
   SpeedButton4->Enabled = false;
   Button2->Enabled = false;
   Button3->Enabled = false;
   Edit2->Enabled = false;
   refuse = 0;
   onconnectflag=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{
   Button1->Enabled = false;
   Edit1->Enabled = false;
   SpeedButton2->Enabled = true;
   SpeedButton1->Enabled = true;
   SpeedButton3->Enabled = true;
   Button2->Enabled = true;
   Button3->Enabled = true;
   Edit2->Enabled = true;
   refuse = 1;

   if(video.initflag==true  && video.startcap==true)
   {
       Button5->Enabled=true;
   }
   onconnectflag=true;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
   if ( useSer == 1)
   {
      Button1->Enabled = true;
      Edit1->Enabled = true;
      SpeedButton2->Enabled = false;
      SpeedButton1->Enabled = false;
      SpeedButton3->Enabled = false;
      SpeedButton4->Enabled = false;
      Button2->Enabled = false;
      Button3->Enabled = false;
      Edit2->Enabled = false;
      Socket->Close();
      ServerSocket1->Close();
      useSer = 0;
      refuse = 0;
      ServerSocket1->Open();
   }
   onconnectflag=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
   waveInStop(hwi);
   waveInClose(hwi);
   waveOutReset(hwo);
   waveOutUnprepareHeader( hwo, &whdr, sizeof( WAVEHDR));
   waveOutClose(hwo);
   if ( useSer == 1)
   {
      ServerSocket1->Close();
      useSer = 0;
      ServerSocket1->Open();
   }
   else
   {
      ClientSocket1->Close();
   }
   Button1->Enabled = true;
   Edit1->Enabled = true;
   SpeedButton2->Enabled = false;
   SpeedButton1->Enabled = false;
   SpeedButton3->Enabled = false;
   SpeedButton4->Enabled = false;
   Button2->Enabled = false;
   Button3->Enabled = false;
   Edit2->Enabled = false;
   refuse = 0;
   video.StopSend();
   Button6->Enabled=false;
   Button5->Enabled=false;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
   if ( Key == 0x0d && Edit1->Text != "")
      Form1->Button1Click( Sender);        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
   Memo1->Lines->Add( "無法進行連線");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   ServerSocket1->Close();
   ClientSocket1->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
   if ( useSer == 1)
   {
      ServerSocket1->Socket->Connections[0]->SendText("OpenGame");
   }
   else
   {
      ClientSocket1->Socket->SendText("OpenGame");
   }
   Form4->Show();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ServerSocket1ClientWrite(TObject *Sender,
      TCustomWinSocket *Socket)
{
   ComBuf = Socket->ReceiveText();
   temp = ComBuf.SubString( 1, 4);
   temp2 = ComBuf.SubString( 2, 4);
   temp3 = ComBuf.SubString( 3, 4);
if ( useSer == 1)
{
   if ( ComBuf.AnsiCompare("YourTurn") == 0)
      Form4->Label1->Caption = "換你囉!!";
   if ( temp.AnsiCompare("talk") == 0)
      Memo1->Lines->Add(ComBuf.SubString( 5, ComBuf.Length()-4));
   else if ( ComBuf.AnsiCompare("OpenGame") == 0 )//Socket->ReceiveText() == "OpenGame")
      Form4->Show();
   else if ( ComBuf.AnsiCompare("CloseGame") == 0)
      Memo1->Lines->Add("對方已關閉遊戲!!");
   else if ( ComBuf.AnsiCompare("Board") == 0)
      Form3->Show();
   else if ( ComBuf.AnsiCompare("CloseBoard") == 0)
      Memo1->Lines->Add("對方已關閉電子白板");
   else if ( ComBuf.AnsiCompare("IWin") == 0 || temp2.AnsiCompare("IWin") == 0 || temp3.AnsiCompare("IWin") == 0)
   {  
      Form5->Label1->Caption = "You Lose!!";
      Form5->ShowModal();
   }
   else if ( ComBuf.AnsiCompare("OK1") == 0)
   {
      Form4->OK1 = 1;
      if ( Form4->OK1 == 1 && Form4->OK2 == 1)
         Form4->playGame();
   }
   else if ( ComBuf.AnsiCompare("OK2") == 0)
   {
      Form4->OK2 = 1;
      if ( Form4->OK1 == 1 && Form4->OK2 == 1)
         Form4->playGame();
   }
   else if ( ComBuf.AnsiCompare("1") == 0)
   {
      map[1].binGo->Down = true;
      map[1].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("2") == 0)
   {
      map[2].binGo->Down = true;
      map[2].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("3") == 0)
   {
      map[3].binGo->Down = true;
      map[3].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("4") == 0)
   {
      map[4].binGo->Down = true;
      map[4].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("5") == 0)
   {
      map[5].binGo->Down = true;
      map[5].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("6") == 0)
   {
      map[6].binGo->Down = true;
      map[6].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("7") == 0)
   {
      map[7].binGo->Down = true;
      map[7].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("8") == 0)
   {
      map[8].binGo->Down = true;
      map[8].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("9") == 0)
   {
      map[9].binGo->Down = true;
      map[9].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("10") == 0)
   {
      map[10].binGo->Down = true;
      map[10].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("11") == 0)
   {
      map[11].binGo->Down = true;
      map[11].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("12") == 0)
   {
      map[12].binGo->Down = true;
      map[12].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("13") == 0)
   {
      map[13].binGo->Down = true;
      map[13].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("14") == 0)
   {
      map[14].binGo->Down = true;
      map[14].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("15") == 0)
   {
      map[15].binGo->Down = true;
      map[15].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("16") == 0)
   {
      map[16].binGo->Down = true;
      map[16].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("17") == 0)
   {
      map[17].binGo->Down = true;
      map[17].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("18") == 0)
   {
      map[18].binGo->Down = true;
      map[18].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("19") == 0)
   {
      map[19].binGo->Down = true;
      map[19].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("20") == 0)
   {
      map[20].binGo->Down = true;
      map[20].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("21") == 0)
   {
      map[21].binGo->Down = true;
      map[21].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("22") == 0)
   {
      map[22].binGo->Down = true;
      map[22].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("23") == 0)
   {
      map[23].binGo->Down = true;
      map[23].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("24") == 0)
   {
      map[24].binGo->Down = true;
      map[24].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if (ComBuf.AnsiCompare("25") == 0)
   {
      map[25].binGo->Down = true;
      map[25].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   if ( Form4->SpeedButton1->Down == true && Form4->SpeedButton2->Down == true && Form4->SpeedButton3->Down == true &&
        Form4->SpeedButton4->Down == true && Form4->SpeedButton5->Down == true)
   {
      Form4->SpeedButton1->Font->Color = clGreen;
      Form4->SpeedButton2->Font->Color = clGreen;
      Form4->SpeedButton3->Font->Color = clGreen;
      Form4->SpeedButton4->Font->Color = clGreen;
      Form4->SpeedButton5->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton6->Down == true && Form4->SpeedButton7->Down == true && Form4->SpeedButton8->Down == true &&
        Form4->SpeedButton9->Down == true && Form4->SpeedButton10->Down == true)
   {
      Form4->SpeedButton6->Font->Color = clGreen;
      Form4->SpeedButton7->Font->Color = clGreen;
      Form4->SpeedButton8->Font->Color = clGreen;
      Form4->SpeedButton9->Font->Color = clGreen;
      Form4->SpeedButton10->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton11->Down == true && Form4->SpeedButton12->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton14->Down == true && Form4->SpeedButton15->Down == true)
   {
      Form4->SpeedButton11->Font->Color = clGreen;
      Form4->SpeedButton12->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton14->Font->Color = clGreen;
      Form4->SpeedButton15->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton16->Down == true && Form4->SpeedButton17->Down == true && Form4->SpeedButton18->Down == true &&
        Form4->SpeedButton19->Down == true && Form4->SpeedButton20->Down == true)
   {
      Form4->SpeedButton16->Font->Color = clGreen;
      Form4->SpeedButton17->Font->Color = clGreen;
      Form4->SpeedButton18->Font->Color = clGreen;
      Form4->SpeedButton19->Font->Color = clGreen;
      Form4->SpeedButton20->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton21->Down == true && Form4->SpeedButton22->Down == true && Form4->SpeedButton23->Down == true &&
        Form4->SpeedButton24->Down == true && Form4->SpeedButton25->Down == true)
   {
      Form4->SpeedButton21->Font->Color = clGreen;
      Form4->SpeedButton22->Font->Color = clGreen;
      Form4->SpeedButton23->Font->Color = clGreen;
      Form4->SpeedButton24->Font->Color = clGreen;
      Form4->SpeedButton25->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton1->Down == true && Form4->SpeedButton6->Down == true && Form4->SpeedButton11->Down == true &&
        Form4->SpeedButton16->Down == true && Form4->SpeedButton21->Down == true)
   {
      Form4->SpeedButton1->Font->Color = clGreen;
      Form4->SpeedButton6->Font->Color = clGreen;
      Form4->SpeedButton11->Font->Color = clGreen;
      Form4->SpeedButton16->Font->Color = clGreen;
      Form4->SpeedButton21->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton2->Down == true && Form4->SpeedButton7->Down == true && Form4->SpeedButton12->Down == true &&
        Form4->SpeedButton17->Down == true && Form4->SpeedButton22->Down == true)
   {
      Form4->SpeedButton2->Font->Color = clGreen;
      Form4->SpeedButton7->Font->Color = clGreen;
      Form4->SpeedButton12->Font->Color = clGreen;
      Form4->SpeedButton17->Font->Color = clGreen;
      Form4->SpeedButton22->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton3->Down == true && Form4->SpeedButton8->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton18->Down == true && Form4->SpeedButton23->Down == true)
   {
      Form4->SpeedButton3->Font->Color = clGreen;
      Form4->SpeedButton8->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton18->Font->Color = clGreen;
      Form4->SpeedButton23->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton4->Down == true && Form4->SpeedButton9->Down == true && Form4->SpeedButton14->Down == true &&
        Form4->SpeedButton19->Down == true && Form4->SpeedButton24->Down == true)
   {
      Form4->SpeedButton4->Font->Color = clGreen;
      Form4->SpeedButton9->Font->Color = clGreen;
      Form4->SpeedButton14->Font->Color = clGreen;
      Form4->SpeedButton19->Font->Color = clGreen;
      Form4->SpeedButton24->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton5->Down == true && Form4->SpeedButton10->Down == true && Form4->SpeedButton15->Down == true &&
        Form4->SpeedButton20->Down == true && Form4->SpeedButton25->Down == true)
   {
      Form4->SpeedButton5->Font->Color = clGreen;
      Form4->SpeedButton10->Font->Color = clGreen;
      Form4->SpeedButton15->Font->Color = clGreen;
      Form4->SpeedButton20->Font->Color = clGreen;
      Form4->SpeedButton25->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton1->Down == true && Form4->SpeedButton7->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton19->Down == true && Form4->SpeedButton25->Down == true)
   {
      Form4->SpeedButton1->Font->Color = clGreen;
      Form4->SpeedButton7->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton19->Font->Color = clGreen;
      Form4->SpeedButton25->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton5->Down == true && Form4->SpeedButton9->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton17->Down == true && Form4->SpeedButton21->Down == true)
   {
      Form4->SpeedButton5->Font->Color = clGreen;
      Form4->SpeedButton9->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton17->Font->Color = clGreen;
      Form4->SpeedButton21->Font->Color = clGreen;
      counter++;
   }
   if ( counter == 5)
   {
      Socket->SendText("IWin");
      Form5->Label1->Caption = "You Win!!";
      Form5->ShowModal();
   }
   counter = 0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
ComBuf = Socket->ReceiveText();
temp = ComBuf.SubString( 1, 4);
temp2 = ComBuf.SubString( 2, 4);
temp3 = ComBuf.SubString( 3, 4);
if ( useSer == 0)
{
   if ( ComBuf.AnsiCompare("YourTurn") == 0)
      Form4->Label1->Caption = "換你囉!!";
   if ( temp.AnsiCompare("talk") == 0)
      Memo1->Lines->Add(ComBuf.SubString( 5, ComBuf.Length()-4));
   else if ( ComBuf.AnsiCompare("accept") == 0)
   {
      Form6->ShowModal();
      nickName = Form6->Edit1->Text;
   }
   else if ( ComBuf.AnsiCompare("OpenGame") == 0 )//Socket->ReceiveText() == "OpenGame")
      Form4->Show();
   else if ( ComBuf.AnsiCompare("CloseGame") == 0)
      Memo1->Lines->Add("對方已關閉遊戲!!");
   else if ( ComBuf.AnsiCompare("Board") == 0)
      Form3->Show();
   else if ( ComBuf.AnsiCompare("CloseBoard") == 0)
      Memo1->Lines->Add("對方已關閉電子白板");
   else if ( ComBuf.AnsiCompare("IWin") == 0 || temp2.AnsiCompare("IWin") == 0 || temp3.AnsiCompare("IWin") == 0)
   {
      Form5->Label1->Caption = "You Lose!!";
      Form5->ShowModal();
   }
   else if ( ComBuf.AnsiCompare("OK1") == 0)
   {
      Form4->OK1 = 1;
      if ( Form4->OK1 == 1 && Form4->OK2 == 1)
         Form4->playGame();
   }
   else if ( ComBuf.AnsiCompare("OK2") == 0)
   {
      Form4->OK2 = 1;
      if ( Form4->OK1 == 1 && Form4->OK2 == 1)
         Form4->playGame();
   }
   if ( ComBuf.AnsiCompare("1") == 0)
   {
      map[1].binGo->Down = true;
      map[1].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("2") == 0)
   {
      map[2].binGo->Down = true;
      map[2].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("3") == 0)
   {
      map[3].binGo->Down = true;
      map[3].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("4") == 0)
   {
      map[4].binGo->Down = true;
      map[4].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("5") == 0)
   {
      map[5].binGo->Down = true;
      map[5].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("6") == 0)
   {
      map[6].binGo->Down = true;
      map[6].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("7") == 0)
   {
      map[7].binGo->Down = true;
      map[7].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("8") == 0)
   {
      map[8].binGo->Down = true;
      map[8].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("9") == 0)
   {
      map[9].binGo->Down = true;
      map[9].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("10") == 0)
   {
      map[10].binGo->Down = true;
      map[10].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("11") == 0)
   {
      map[11].binGo->Down = true;
      map[11].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("12") == 0)
   {
      map[12].binGo->Down = true;
      map[12].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("13") == 0)
   {
      map[13].binGo->Down = true;
      map[13].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("14") == 0)
   {
      map[14].binGo->Down = true;
      map[14].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("15") == 0)
   {
      map[15].binGo->Down = true;
      map[15].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("16") == 0)
   {
      map[16].binGo->Down = true;
      map[16].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("17") == 0)
   {
      map[17].binGo->Down = true;
      map[17].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("18") == 0)
   {
      map[18].binGo->Down = true;
      map[18].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("19") == 0)
   {
      map[19].binGo->Down = true;
      map[19].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("20") == 0)
   {
      map[20].binGo->Down = true;
      map[20].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("21") == 0)
   {
      map[21].binGo->Down = true;
      map[21].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("22") == 0)
   {
      map[22].binGo->Down = true;
      map[22].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("23") == 0)
   {
      map[23].binGo->Down = true;
      map[23].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if ( ComBuf.AnsiCompare("24") == 0)
   {
      map[24].binGo->Down = true;
      map[24].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   else if (ComBuf.AnsiCompare("25") == 0)
   {
      map[25].binGo->Down = true;
      map[25].binGo->Font->Color = clRed;
      Form4->count = Form4->count + 1;
   }
   if ( Form4->SpeedButton1->Down == true && Form4->SpeedButton2->Down == true && Form4->SpeedButton3->Down == true &&
        Form4->SpeedButton4->Down == true && Form4->SpeedButton5->Down == true)
   {
      Form4->SpeedButton1->Font->Color = clGreen;
      Form4->SpeedButton2->Font->Color = clGreen;
      Form4->SpeedButton3->Font->Color = clGreen;
      Form4->SpeedButton4->Font->Color = clGreen;
      Form4->SpeedButton5->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton6->Down == true && Form4->SpeedButton7->Down == true && Form4->SpeedButton8->Down == true &&
        Form4->SpeedButton9->Down == true && Form4->SpeedButton10->Down == true)
   {
      Form4->SpeedButton6->Font->Color = clGreen;
      Form4->SpeedButton7->Font->Color = clGreen;
      Form4->SpeedButton8->Font->Color = clGreen;
      Form4->SpeedButton9->Font->Color = clGreen;
      Form4->SpeedButton10->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton11->Down == true && Form4->SpeedButton12->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton14->Down == true && Form4->SpeedButton15->Down == true)
   {
      Form4->SpeedButton11->Font->Color = clGreen;
      Form4->SpeedButton12->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton14->Font->Color = clGreen;
      Form4->SpeedButton15->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton16->Down == true && Form4->SpeedButton17->Down == true && Form4->SpeedButton18->Down == true &&
        Form4->SpeedButton19->Down == true && Form4->SpeedButton20->Down == true)
   {
      Form4->SpeedButton16->Font->Color = clGreen;
      Form4->SpeedButton17->Font->Color = clGreen;
      Form4->SpeedButton18->Font->Color = clGreen;
      Form4->SpeedButton19->Font->Color = clGreen;
      Form4->SpeedButton20->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton21->Down == true && Form4->SpeedButton22->Down == true && Form4->SpeedButton23->Down == true &&
        Form4->SpeedButton24->Down == true && Form4->SpeedButton25->Down == true)
   {
      Form4->SpeedButton21->Font->Color = clGreen;
      Form4->SpeedButton22->Font->Color = clGreen;
      Form4->SpeedButton23->Font->Color = clGreen;
      Form4->SpeedButton24->Font->Color = clGreen;
      Form4->SpeedButton25->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton1->Down == true && Form4->SpeedButton6->Down == true && Form4->SpeedButton11->Down == true &&
        Form4->SpeedButton16->Down == true && Form4->SpeedButton21->Down == true)
   {
      Form4->SpeedButton1->Font->Color = clGreen;
      Form4->SpeedButton6->Font->Color = clGreen;
      Form4->SpeedButton11->Font->Color = clGreen;
      Form4->SpeedButton16->Font->Color = clGreen;
      Form4->SpeedButton21->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton2->Down == true && Form4->SpeedButton7->Down == true && Form4->SpeedButton12->Down == true &&
        Form4->SpeedButton17->Down == true && Form4->SpeedButton22->Down == true)
   {
      Form4->SpeedButton2->Font->Color = clGreen;
      Form4->SpeedButton7->Font->Color = clGreen;
      Form4->SpeedButton12->Font->Color = clGreen;
      Form4->SpeedButton17->Font->Color = clGreen;
      Form4->SpeedButton22->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton3->Down == true && Form4->SpeedButton8->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton18->Down == true && Form4->SpeedButton23->Down == true)
   {
      Form4->SpeedButton3->Font->Color = clGreen;
      Form4->SpeedButton8->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton18->Font->Color = clGreen;
      Form4->SpeedButton23->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton4->Down == true && Form4->SpeedButton9->Down == true && Form4->SpeedButton14->Down == true &&
        Form4->SpeedButton19->Down == true && Form4->SpeedButton24->Down == true)
   {
      Form4->SpeedButton4->Font->Color = clGreen;
      Form4->SpeedButton9->Font->Color = clGreen;
      Form4->SpeedButton14->Font->Color = clGreen;
      Form4->SpeedButton19->Font->Color = clGreen;
      Form4->SpeedButton24->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton5->Down == true && Form4->SpeedButton10->Down == true && Form4->SpeedButton15->Down == true &&
        Form4->SpeedButton20->Down == true && Form4->SpeedButton25->Down == true)
   {
      Form4->SpeedButton5->Font->Color = clGreen;
      Form4->SpeedButton10->Font->Color = clGreen;
      Form4->SpeedButton15->Font->Color = clGreen;
      Form4->SpeedButton20->Font->Color = clGreen;
      Form4->SpeedButton25->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton1->Down == true && Form4->SpeedButton7->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton19->Down == true && Form4->SpeedButton25->Down == true)
   {
      Form4->SpeedButton1->Font->Color = clGreen;
      Form4->SpeedButton7->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton19->Font->Color = clGreen;
      Form4->SpeedButton25->Font->Color = clGreen;
      counter++;
   }
   if ( Form4->SpeedButton5->Down == true && Form4->SpeedButton9->Down == true && Form4->SpeedButton13->Down == true &&
        Form4->SpeedButton17->Down == true && Form4->SpeedButton21->Down == true)
   {
      Form4->SpeedButton5->Font->Color = clGreen;
      Form4->SpeedButton9->Font->Color = clGreen;
      Form4->SpeedButton13->Font->Color = clGreen;
      Form4->SpeedButton17->Font->Color = clGreen;
      Form4->SpeedButton21->Font->Color = clGreen;
      counter++;
   }
   if ( counter == 5)
   {
      Socket->SendText("IWin");
      Form5->Label1->Caption = "You Win!!";
      Form5->ShowModal();
   }
   counter = 0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit2Change(TObject *Sender)
{
   if ( Edit2->Text != "")
      SpeedButton4->Enabled = true;
   else
      SpeedButton4->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit2KeyPress(TObject *Sender, char &Key)
{
   if ( Key == 0x0d && Edit2->Text != "")
   {
      temp = "talk" + nickName + "說：" + Edit2->Text;
      if ( useSer == 1)
         ServerSocket1->Socket->Connections[0]->SendText(temp);
      else
         ClientSocket1->Socket->SendText(temp);
      Memo1->Lines->Add(temp.SubString( 5, temp.Length()-4));
      Edit2->Clear();
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
   Edit2KeyPress( Sender, 0x0d);
   video.Display();
   video.StopSend();
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Button4Click(TObject *Sender)
{
    video.Display();
    //Button5->Enabled=true;
    Button9->Enabled=true;
    if(video.initflag==true  && video.startcap==true)
    {
        Button4->Enabled=false;
    }
    if (onconnectflag==true)
    {
       Button5->Enabled=true;
    }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::NMUDP4DataReceived(TComponent *Sender,
      int NumberBytes, AnsiString FromIP, int Port)
{
       	DWORD size = ICCompressGetSize(compressor.compvar.hic, compressor.compvar.lpbiIn, compressor.compvar.lpbiOut);
	long shift = 2000 * num;
	char hold[2000];
	for (int j=0; j<2002; j++)
		hold[j]='\0';
	NMUDP4->ReadBuffer( hold,2002,2002);
	//if (flag ==1)
	//{
		if (hold[0]=='0' && hold[1]=='0')
		{
			for(int k=0; k<2000; k++)
				image[shift+k]=hold[2+k];
			num++;
		}
		if(hold[0]=='0' && hold[1]=='1')
		{
			char temp[4];
			int hictemp, intemp, outtemp;
			if (cnum == 0)
			{
				for(int i=0; i<4; i++)
					temp[i]=hold[2+i];
				hictemp = StrToInt(temp);
				for(int i=0; i<4; i++)
					temp[i]=hold[6+i];
				intemp = StrToInt(temp);
				for(int i=0; i<4; i++)
					temp[i]=hold[10+i];
				outtemp = StrToInt(temp);
				compressor.compvar.hic = (HIC) hictemp;
				compressor.compvar.lpbiIn = (LPBITMAPINFO) &intemp;
				compressor.compvar.lpbiOut = (LPBITMAPINFO) &outtemp;
				cnum =1;
			}
			else
			{
			    num=0;
			    LPVOID compresseddata = &image[0];
                            LPVOID decompresseddata ;
                             decompresseddata = compressor.decompress(compresseddata);
                            if(!DrawDibBegin(video.hdd,video.hdc,-1,-1,&video.bmpinfo.bmiHeader,video.CAPWIDTH,video.CAPHEIGHT,NULL))
                            {
                                 Application->MessageBox("initDisplay DrawDibBegin Error!","Error",MB_OK);
                            }
                            if(!DrawDibDraw(video.hdd,video.hdc,350,video.CAPHEIGHT+60,video.CAPWIDTH,video.CAPHEIGHT,&video.bmpinfo.bmiHeader, decompresseddata
                               ,0,0,video.CAPWIDTH,video.CAPHEIGHT,DDF_SAME_DRAW | DDF_SAME_HDC))
                            {
                                 Application->MessageBox("DrawDibDraw ERROR","Error",MB_OK);
                            }
	         	}

	          }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
    video.StartSend();
    Button5->Enabled=false;
    Button6->Enabled=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{
    video.StopSend();
    Button6->Enabled=false;
    Button5->Enabled=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)
{
       listen=false;
       NMUDP4->LocalPort=0;
       Button7->Enabled=false;
       Button8->Enabled=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button8Click(TObject *Sender)
{
       listen=true;
       NMUDP4->LocalPort= 3367;;
       Button8->Enabled=false;
       Button7->Enabled=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button9Click(TObject *Sender)
{
    video.StopCap();
    Button9->Enabled=false;
    Button4->Enabled=true;
    Button6->Enabled=false;
}
//---------------------------------------------------------------------------


