// Eg11_1View.cpp : implementation of the CEg11_1View class
//

#include "stdafx.h"
#include "Eg11_1.h"


#include "Eg11_1Doc.h"
#include "Eg11_1View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEg11_1View

IMPLEMENT_DYNCREATE(CEg11_1View, CFormView)

BEGIN_MESSAGE_MAP(CEg11_1View, CFormView)
	//{{AFX_MSG_MAP(CEg11_1View)
	ON_COMMAND(ID_FILE_BEGIN, OnFileBegin)
	ON_BN_CLICKED(IDC_DISCARD, OnDiscard)
	ON_COMMAND(ID_FILE_END, OnFileEnd)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEg11_1View construction/destruction

CEg11_1View::CEg11_1View()
	: CFormView(CEg11_1View::IDD) ,
	m_computerready(TRUE),
    m_playerready(FALSE)
{
	
	//{{AFX_DATA_INIT(CEg11_1View)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

 m_playing=false;
 m_cardnum=0;
 m_computernum=24;
 m_playernum=24;
	
}

CEg11_1View::~CEg11_1View()
{
}

void CEg11_1View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEg11_1View)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CEg11_1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CEg11_1View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
    
	GetDlgItem(IDC_DISCARD)->EnableWindow(FALSE);
	SetDlgItemText(IDC_STATIC_SCORE1,"24");
	SetDlgItemText(IDC_STATIC_SCORE2,"24");


}

/////////////////////////////////////////////////////////////////////////////
// CEg11_1View printing

BOOL CEg11_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEg11_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEg11_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CEg11_1View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CEg11_1View diagnostics

#ifdef _DEBUG
void CEg11_1View::AssertValid() const
{
	CFormView::AssertValid();
}

void CEg11_1View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CEg11_1Doc* CEg11_1View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEg11_1Doc)));
	return (CEg11_1Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEg11_1View message handlers

void CEg11_1View::OnFileBegin() 
{
	// TODO: Add your command handler code here
    m_playing=true;
	//启动计算机出牌线程和游戏者线程
	AfxBeginThread(CompThread,(void*)this);
	AfxBeginThread(PlayerThread,(void*)this);	
}
void CEg11_1View::OnFileEnd() 
{
	// TODO: Add your command handler code here
	exit(0);
	
}


void CEg11_1View::play()
{
	while(m_playing)
	{
	m_computerready.Lock();
	GetDlgItem(IDC_DISCARD)->EnableWindow(TRUE);
	SetDlgItemText(IDC_STATIC_SHOW2,"该你出牌了");
	}
}

void CEg11_1View::OnDiscard() 
{
	// TODO: Add your control notification handler code here
    GetDlgItem(IDC_DISCARD)->EnableWindow(FALSE);
	TCHAR buffer[10];
    BOOL bWinning=FALSE;
	//产生随机数
    srand((unsigned)time(NULL)*10);
    int card=rand()%12+1;
	//出牌以后改变成绩和牌局
	  m_card[m_cardnum]=card;
      m_cardnum++;
	  m_playernum--;
	  showcard();	  
	  SetDlgItemText(IDC_STATIC_SCORE1,ltoa(m_playernum,buffer,10));
      Sleep(1000);  
   //判断是不是赢牌了并修改成绩
    for(int i=0;i<m_cardnum-1;i++)
	{
	    if(card==m_card[i])
		{
		  m_playernum+=m_cardnum-i;
		 // m_pMainWnd::AfxGetMainWnd(),
	      CString str1="你赢了";		  
		  CString str2="张";
		  ::AfxGetMainWnd()->MessageBox(str1+itoa((m_cardnum-i),buffer,10)+str2,"排火车",MB_ICONINFORMATION);
		  m_cardnum=i;
          bWinning=TRUE;         
		  break;
		}
	}
	SetDlgItemText(IDC_STATIC_SCORE1,ltoa(m_playernum,buffer,10));
    //如果牌的张数为零则机器赢  	  
      if(m_playernum==0)
	  {
	     m_playing=FALSE;
	     AfxMessageBox("对不起,你输了");
		 exit(0);
	  }

 
    //显示修改以后的成绩
    showcard();
    Sleep(1000);
	//游戏者出牌完毕等待计算机出牌
    m_playerready.SetEvent();
    m_computerready.Unlock();
}

void CEg11_1View::showcard()
{
	  CString str;
	  TCHAR buffer[10];
	  for(int i=0;i<m_cardnum;i++)
	  {
	  itoa(m_card[i],buffer,10);
	  str+="   ";
	  str+=buffer;
	  }
	  SetDlgItemText(IDC_STATIC_SHOW1,str);	
	  
}


UINT   PlayerThread(LPVOID lpParameter)
{
	CEg11_1View* pView=(CEg11_1View*)lpParameter;
	pView->play();
	return 1;
}
UINT   CompThread(LPVOID lpParameter)
{
	CEg11_1View* pView=(CEg11_1View*)lpParameter;
	TCHAR buffer[10];
    BOOL Winning=FALSE;
    while(pView->m_playing)
	{  
	 Winning=FALSE;
	 pView->m_playerready.Lock();	 
	 pView->SetDlgItemText(IDC_STATIC_SHOW2,"机器正在出牌......");      	  
     
	  srand((unsigned)time(NULL)*10);
      int   card=rand()%12+1;   
	  pView->m_card[pView->m_cardnum]=card;
      pView->m_cardnum++;
	  pView->m_computernum--;
	  pView->showcard();	  
	  pView->SetDlgItemText(IDC_STATIC_SCORE2,ltoa(pView->m_computernum,buffer,10));
      Sleep(1000);    
	  
	  for(int i=0;i<pView->m_cardnum-1;i++)
	  {
	     if(card==pView->m_card[i])
		 {
		  pView->m_computernum+=(pView->m_cardnum-i);
		  CString str1="机器赢了";		  
		  CString str2="张";
		  
	::AfxGetMainWnd()->MessageBox(str1+itoa((pView->m_cardnum-i),buffer,10)+str2,"排火车",MB_ICONINFORMATION);			
		  pView->m_cardnum=i;
          Winning=TRUE;  
		  break;		  
		 }
	  }
    pView->SetDlgItemText(IDC_STATIC_SCORE2,ltoa(pView->m_computernum,buffer,10));

      if(pView->m_computernum==0)
	  {
	     pView->m_playing=FALSE;
	     AfxMessageBox("恭喜你,你赢了");
		 exit(0);
	  }

   pView->showcard();
   Sleep(1000); 

   pView->m_computerready.SetEvent();
   pView->m_playerready.Unlock();
 }
  return 1;
	
}


