Avatar of larry84
larry84
 asked on

Visual C++, MFC

I am programming a game on a SDI. The display consists of three rectangles, a small rectangle in a larger rectangle within a larger rectangle. These rectangles are connected by 4 lines, 2 vertical, 2 horizintal, similar to a cross being placed on the rectangles except the cross doesn't show inside the inner rectangle. On each rectangle I can place either an x or an o at 8 different positions, on all the corners and half way between the corners. The object of the game is that there are two players, each places a piece on the display in alternate goes. Each player gets 6 pieces, if a player gets 3 of his pieces in a row he can remove one of the components pieces.

The problem I have is that I can display all 12 pieces but after all 12 pieces are placed I need to move pieces already placed.
// E4gameView.cpp : implementation of the CE4gameView class
//
 
#include "stdafx.h"
#include "E4game.h"
 
#include "E4gameDoc.h"
#include "E4gameView.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// CE4gameView
 
IMPLEMENT_DYNCREATE(CE4gameView, CView)
 
BEGIN_MESSAGE_MAP(CE4gameView, CView)
	//{{AFX_MSG_MAP(CE4gameView)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
 
/////////////////////////////////////////////////////////////////////////////
// CE4gameView construction/destruction
 
CE4gameView::CE4gameView()
{
	// TODO: add construction code here
 
	
    for (int i=0; i<7; i++) {
	for (int j=0; j<7; j++) {
	int x=(i*70)+10;
	int y=-(j*70)-10;
	m_rect[i][j].SetRect(x,y,x +60, y - 60);
	m_rect[i][j].NormalizeRect();
	}
	}
}
 
CE4gameView::~CE4gameView()
{
}
 
BOOL CE4gameView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
 
	return CView::PreCreateWindow(cs);
}
 
/////////////////////////////////////////////////////////////////////////////
// CE4gameView drawing
 
void CE4gameView::OnDraw(CDC* pDC)
{
	CE4gameDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDC->SetMapMode (MM_LOENGLISH);
	//
	CRect rect;
	//
	// TODO: add draw code for native data here
	//
    CPen pen (PS_SOLID,5,RGB(0,0,255));
	CPen* pOldPen = pDC->SelectObject (&pen);
 
	pDC->MoveTo (30,-30);
	pDC->LineTo (30,-460);
    pDC->LineTo (460,-460);     //Big rect
    pDC->LineTo (460,-30);
    pDC->LineTo (30,-30);
 
    pDC->MoveTo (110,-110);
	pDC->LineTo (110,-390);
    pDC->LineTo (390,-390);    //Middle rect
    pDC->LineTo (390,-110);
    pDC->LineTo (110,-110);
 
    pDC->MoveTo (180,-180);
	pDC->LineTo (180,-320);
    pDC->LineTo (320,-320);     //Small rect
    pDC->LineTo (320,-180);
    pDC->LineTo (180,-180);
 
    pDC->MoveTo (30,-250);
	pDC->LineTo (180,-250);
 
    pDC->MoveTo (250,-30);
	pDC->LineTo (250,-180);
 
    pDC->MoveTo (320,-250);
	pDC->LineTo (460,-250);
 
    pDC->MoveTo (250,-320);
	pDC->LineTo (250,-460);
 
 
 
 
 
	pDC->SelectObject (pOldPen);
}
 
/////////////////////////////////////////////////////////////////////////////
// CE4gameView printing
 
BOOL CE4gameView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}
 
void CE4gameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}
 
void CE4gameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}
 
/////////////////////////////////////////////////////////////////////////////
// CE4gameView diagnostics
 
#ifdef _DEBUG
void CE4gameView::AssertValid() const
{
	CView::AssertValid();
}
 
void CE4gameView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
 
CE4gameDoc* CE4gameView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CE4gameDoc)));
	return (CE4gameDoc*)m_pDocument;
}
#endif //_DEBUG
 
/////////////////////////////////////////////////////////////////////////////
// CE4gameView message handlers
 
void CE4gameView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    //
    //
	CE4gameDoc*pDoc=GetDocument();
	//
	CClientDC dc (this);
	dc.SetMapMode (MM_LOENGLISH);
	dc.DPtoLP (&point);
	//
	//
	BOOL bQuit = FALSE;
	for (int i=0; i<7 && !bQuit; i++){
	for (int j=0; j<7 && !bQuit; j++){
	if (m_rect[i][j].PtInRect (point)){
    if(pDoc->GetSquare(i,j) ==0) {
    if (pDoc->Isitp1Turn()) {
						pDoc->Addpiece1 (i,j);
						Drawp1 (&dc, &m_rect[i][j]);
					}
					else{
						pDoc->Addpiece2 (i,j);
						Drawp2 (&dc, &m_rect[i][j]);
				}
				}
				bQuit = TRUE;
				
			}
		}
	}
 
	
	CView::OnLButtonDown(nFlags, point);
}
 
void CE4gameView::Drawp1(CDC *pDC, CRect *pRect)
{
    CE4gameDoc* pDoc = GetDocument();
    //
	//make a local copy of the rectangle and shrink it.
	//
	CRect rect;
	rect.CopyRect (pRect);
	rect.DeflateRect (1,1);
	//
	//create a red pen and use it to draw an x.
	//
	CPen pen (PS_SOLID,4,RGB(255,0,0));
	CPen* pOldPen = pDC->SelectObject (&pen);
	pDC->MoveTo (rect.left, rect.top);
	pDC->LineTo (rect.right, rect.bottom);
	pDC->MoveTo (rect.left, rect.bottom);
	pDC->LineTo (rect.right, rect.top);
	pDC->SelectObject (pOldPen);
 
	
 
    
}
 
void CE4gameView::Drawp2(CDC *pDC, CRect *pRect)
{
    CE4gameDoc* pDoc = GetDocument();
    //
	//make a local copy of the rectangle and shrink it
	//
    CRect rect;
	rect.CopyRect (pRect);
	rect.DeflateRect (1,1);
	//
	//Create a blue pen and use it to draw an O.
	//
	CPen pen (PS_SOLID, 4,RGB (0,255,0));
	CPen* pOldPen = pDC->SelectObject (&pen);
	pDC->SelectStockObject (NULL_BRUSH);
	pDC->Ellipse (rect);
	pDC->SelectObject (pOldPen);
 
    int count= pDoc->Getcount();
 
    if (count >=12){
    AfxMessageBox ("Move Piece");
    pDoc->OnNewDocument();
	
	}
 
}

Open in new window

System ProgrammingVisual C++.NETVisual Basic.NET

Avatar of undefined
Last Comment
AndyAinscow

8/22/2022 - Mon
AndyAinscow

So what is the question?
larry84

ASKER
Sorry, my question is once the 12 pieces are placed how would I move one of the pieces to an empty space?
AndyAinscow

I'd suggest a 'drag and drop' approach.  When one piece is clicked and the mouse button held down the user can drag it to a vacant square.  That may well be the obvious way a user would attempt (expect) to move a piece.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
larry84

ASKER
Ok thanks, What way would I go about this and what sort of code would be needed for the drag and drop approach?
AndyAinscow

For a drag/drop.
You would need to handle the left mouse button down and up events (and the mousemove if you wanted to show a piece being dragged).
When the button is down - is it over a valid piece to select ?
When the button is released - was it dragging a piece? Can it be dropped here?

You need to list all the possible things a user might do and what you would want in that instance.  Eg. Dragging onto the same square as it started on, moving off the playing field...

You would also need to decide what visual clues you want to give the user.
ASKER CERTIFIED SOLUTION
alb66

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AndyAinscow

And my input ?
Suggestion of drag and drop. Events to handle. Situations to consider.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.