// Board.cpp: implementation of the CBoard class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Chess3D.h" #include "Board.h" #include "iPeshka.h" #include "iTura.h" #include "iSlon.h" #include "iFerz.h" #include "iKorol.h" #include "iKon.h" #include "iPeshka_Shadow.h" #include "Scene.h" #include #include #include // Letters #include "iA.h" #include "iB.h" #include "iC.h" #include "iD.h" #include "iE.h" #include "iF.h" #include "iG.h" #include "iH.h" #include "iL1.h" #include "iL2.h" #include "iL3.h" #include "iL4.h" #include "iL5.h" #include "iL6.h" #include "iL7.h" #include "iL8.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CBoard::CBoard() { m_pScene = NULL; m_bProcessAnimation = FALSE; } CBoard::~CBoard() { } BOOL CBoard::Init(matrix *pmtMatr, CScene *pScene) { m_pcMoveTable = &pScene->m_cMoveTable; m_pScene = pScene; m_pmtMatr = pmtMatr; bClicked = FALSE; ResetFigures(); m_arrBoardPoints.Set(WS_SIZE + 1, WS_SIZE + 1); int ix, iy; for(iy = 0; iy < WS_SIZE + 1; iy ++){ for(ix = 0; ix < WS_SIZE + 1; ix ++){ point3d ptPos; ptPos.x = WS_SIZE_SQUARE * (ix - WS_SIZE / 2); ptPos.y = WS_SIZE_SQUARE * (iy - WS_SIZE / 2); ptPos.z = 0.f; m_arrBoardPoints.e(ix, iy) = ptPos; } } for(ix = 0; ix < 6; ix++){ CMesh *pMesh = parrMeshes.AppendNew(); switch(ix + 1){ case WS_PESHKA: pMesh->m_arrVerts.Set(Peshka_vert_num); pMesh->m_arrFaces.Set(Peshka_face_num); memcpy(pMesh->m_arrVerts.Memory(), Peshka_vert_arr, pMesh->m_arrVerts.MemorySize()); memcpy(pMesh->m_arrFaces.Memory(), Peshka_face_arr, pMesh->m_arrFaces.MemorySize()); break; case WS_KON: pMesh->m_arrVerts.Set(Kon_vert_num); pMesh->m_arrFaces.Set(Kon_face_num); memcpy(pMesh->m_arrVerts.Memory(), Kon_vert_arr, pMesh->m_arrVerts.MemorySize()); memcpy(pMesh->m_arrFaces.Memory(), Kon_face_arr, pMesh->m_arrFaces.MemorySize()); break; case WS_SLON: pMesh->m_arrVerts.Set(Slon_vert_num); pMesh->m_arrFaces.Set(Slon_face_num); memcpy(pMesh->m_arrVerts.Memory(), Slon_vert_arr, pMesh->m_arrVerts.MemorySize()); memcpy(pMesh->m_arrFaces.Memory(), Slon_face_arr, pMesh->m_arrFaces.MemorySize()); break; case WS_TURA: pMesh->m_arrVerts.Set(Tura_vert_num); pMesh->m_arrFaces.Set(Tura_face_num); memcpy(pMesh->m_arrVerts.Memory(), Tura_vert_arr, pMesh->m_arrVerts.MemorySize()); memcpy(pMesh->m_arrFaces.Memory(), Tura_face_arr, pMesh->m_arrFaces.MemorySize()); break; case WS_FERZ: pMesh->m_arrVerts.Set(Ferz_vert_num); pMesh->m_arrFaces.Set(Ferz_face_num); memcpy(pMesh->m_arrVerts.Memory(), Ferz_vert_arr, pMesh->m_arrVerts.MemorySize()); memcpy(pMesh->m_arrFaces.Memory(), Ferz_face_arr, pMesh->m_arrFaces.MemorySize()); break; case WS_KOROL: pMesh->m_arrVerts.Set(Korol_vert_num); pMesh->m_arrFaces.Set(Korol_face_num); memcpy(pMesh->m_arrVerts.Memory(), Korol_vert_arr, pMesh->m_arrVerts.MemorySize()); memcpy(pMesh->m_arrFaces.Memory(), Korol_face_arr, pMesh->m_arrFaces.MemorySize()); break; } CalcNorms(pMesh->m_arrFaces, pMesh->m_arrVerts, pMesh->m_arrNorms); } return TRUE; } void CBoard::Draw() { glDisable(GL_TEXTURE_2D); glDisable(GL_ALPHA_TEST); glDisable(GL_BLEND); glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Draw Border... glEnable(GL_CULL_FACE); glDisable(GL_LIGHTING); point3d ptPos1, ptPos2, ptPos3, ptPos4, ptPos11, ptPos22; ptPos1 = m_arrBoardPoints.e(0, 0); ptPos2 = m_arrBoardPoints.e(0, WS_SIZE); ptPos3 = m_arrBoardPoints.e(WS_SIZE, WS_SIZE); ptPos4 = m_arrBoardPoints.e(WS_SIZE, 0); ptPos11 = ptPos1; ptPos22 = ptPos2; ptPos11.z -= 1.2f; ptPos22.z -= 1.2f; glBegin(GL_QUADS); glColor3f(0.6f, 0.f, 0.f); glVertex3fv(&ptPos1.x); glColor3f(0.6f, 0.6f, 0.f); glVertex3fv(&ptPos2.x); glColor3f(0.f, 0.6f, 0.6f); glVertex3fv(&ptPos22.x); glColor3f(0.6f, 0.f, 0.6f); glVertex3fv(&ptPos11.x); glEnd(); ptPos11 = ptPos2; ptPos22 = ptPos3; ptPos11.z -= 1.2f; ptPos22.z -= 1.2f; glBegin(GL_QUADS); glColor3f(0.6f, 0.f, 0.f); glVertex3fv(&ptPos2.x); glColor3f(0.6f, 0.6f, 0.f); glVertex3fv(&ptPos3.x); glColor3f(0.f, 0.6f, 0.6f); glVertex3fv(&ptPos22.x); glColor3f(0.6f, 0.f, 0.6f); glVertex3fv(&ptPos11.x); glEnd(); ptPos11 = ptPos3; ptPos22 = ptPos4; ptPos11.z -= 1.2f; ptPos22.z -= 1.2f; glBegin(GL_QUADS); glColor3f(0.6f, 0.f, 0.f); glVertex3fv(&ptPos3.x); glColor3f(0.6f, 0.6f, 0.f); glVertex3fv(&ptPos4.x); glColor3f(0.f, 0.6f, 0.6f); glVertex3fv(&ptPos22.x); glColor3f(0.6f, 0.f, 0.6f); glVertex3fv(&ptPos11.x); glEnd(); ptPos11 = ptPos4; ptPos22 = ptPos1; ptPos11.z -= 1.2f; ptPos22.z -= 1.2f; glBegin(GL_QUADS); glColor3f(0.6f, 0.f, 0.f); glVertex3fv(&ptPos4.x); glColor3f(0.6f, 0.6f, 0.f); glVertex3fv(&ptPos1.x); glColor3f(0.f, 0.6f, 0.6f); glVertex3fv(&ptPos22.x); glColor3f(0.6f, 0.f, 0.6f); glVertex3fv(&ptPos11.x); glEnd(); // Draw Figures... glDisable(GL_CULL_FACE); glEnable(GL_LIGHTING); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, m_nTexture); if(m_pScene->m_bTexturedFigs){ glEnable(GL_TEXTURE_2D); } glEnableClientState(GL_TEXTURE_COORD_ARRAY); for(int ix = 0; ix < m_arrFigures.Size(); ix++){ BOOL bDelFigure = FALSE; CFigure *pFigure = m_arrFigures.e(ix); CMesh *pMesh = parrMeshes[pFigure->m_nType - 1]; glPushMatrix(); if(m_bProcessAnimation){ if(pFigure == pCurSelFigure){ glTranslatef(ptPosAnimation.x, ptPosAnimation.y, ptPosAnimation.z); }else if(pFigure == pCurDelFigure){ glTranslatef(ptPosDelFig.x, ptPosDelFig.y, ptPosDelFig.z); glPushMatrix(); glMultMatrixf(&mtDelMatr.m[0][0]); bDelFigure = TRUE; }else{ glTranslatef(WS_SIZE_SQUARE * (pFigure->m_nX - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, WS_SIZE_SQUARE * (pFigure->m_nY - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, 0.f); } }else{ glTranslatef(WS_SIZE_SQUARE * (pFigure->m_nX - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, WS_SIZE_SQUARE * (pFigure->m_nY - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, 0.f); } if(pFigure->m_nColor == WS_WHITE){ glColor3f(1.f, 1.f, 1.f); }else{ glColor3f(0.5f, 0.5f, 0.5f); } glNormalPointer(GL_FLOAT, 0, pMesh->m_arrNorms.Memory()); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].x); glTexCoordPointer(2, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].tx); glDrawElements(GL_TRIANGLES, pMesh->m_arrFaces.Size() * 3, GL_UNSIGNED_INT, pMesh->m_arrFaces.Memory()); if(bDelFigure){ glPopMatrix(); } if(m_pScene->m_bMirror){ if(m_bProcessAnimation && ((pFigure == pCurSelFigure))){ glTranslatef(0.f, 0.f, -2.f * ptPosAnimation.z); } if(bDelFigure){ glTranslatef(0.f, 0.f, -2.f * ptPosDelFig.z); } float fDelta = -PI_LB; // if(pFigure->m_nColor == WS_BLACK){ // fDelta = -PI_LB; // } matrix mt; ident_matrix(&mt); rotate_matrix(&mt, PI_LB, 0.f, PI_LB + PI_LB / 2.f + fDelta); glPushMatrix(); glMultMatrixf(&mt.m[0][0]); glNormalPointer(GL_FLOAT, 0, pMesh->m_arrNorms.Memory()); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].x); glTexCoordPointer(2, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].tx); glDrawElements(GL_TRIANGLES, pMesh->m_arrFaces.Size() * 3, GL_UNSIGNED_INT, pMesh->m_arrFaces.Memory()); glPopMatrix(); } glPopMatrix(); } if(m_pScene->m_bDelFigures){ glEnable(GL_LIGHTING); int nPeshka = 0; int nOthers = 0; for(ix = 0; ix < parrWhiteDelFigs.Size(); ix++){ CFigure *pFigure = parrWhiteDelFigs.e(ix); CMesh *pMesh = parrMeshes[pFigure->m_nType - 1]; int nIndex = 0, nInd = 0; if(pFigure->m_nType != WS_PESHKA){ nIndex = 1; nInd = nPeshka; nPeshka ++; }else{ nInd = nOthers; nOthers++; } glColor3f(0.5f, 0.5f, 0.5f); glPushMatrix(); glTranslatef(WS_SIZE_SQUARE * (8.f + nIndex * 0.4f - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, WS_SIZE_SQUARE * (nInd - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, 0.f); glNormalPointer(GL_FLOAT, 0, pMesh->m_arrNorms.Memory()); glTexCoordPointer(2, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].tx); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].x); glDrawElements(GL_TRIANGLES, pMesh->m_arrFaces.Size() * 3, GL_UNSIGNED_INT, pMesh->m_arrFaces.Memory()); glPopMatrix(); } nPeshka = 0; nOthers = 0; for(ix = 0; ix < parrBlackDelFigs.Size(); ix++){ CFigure *pFigure = parrBlackDelFigs.e(ix); CMesh *pMesh = parrMeshes[pFigure->m_nType - 1]; int nIndex = 0, nInd = 0; if(pFigure->m_nType != WS_PESHKA){ nIndex = 0; nInd = nPeshka; nPeshka ++; }else{ nIndex = -1; nInd = nOthers; nOthers++; } glColor3f(1.f, 1.0f, 1.0f); glPushMatrix(); glTranslatef(WS_SIZE_SQUARE * (-1.5f - nIndex * 0.4f - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, WS_SIZE_SQUARE * (7 - nInd - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, 0.f); glNormalPointer(GL_FLOAT, 0, pMesh->m_arrNorms.Memory()); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].x); glTexCoordPointer(2, GL_FLOAT, sizeof(vert_t), &pMesh->m_arrVerts[0].tx); glDrawElements(GL_TRIANGLES, pMesh->m_arrFaces.Size() * 3, GL_UNSIGNED_INT, pMesh->m_arrFaces.Memory()); glPopMatrix(); } } glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Draw Board... glDisable(GL_LIGHTING); glLineWidth(2.f); if(m_pScene->m_bMirror){ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glAlphaFunc(GL_GREATER, .0001f); glEnable(GL_ALPHA_TEST); }else{ glDisable(GL_ALPHA_TEST); glDisable(GL_BLEND); } glDisable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, m_nTexture_Board); for(int iy = 0; iy < WS_SIZE; iy ++){ for(int ix = 0; ix < WS_SIZE; ix ++){ BOOL bWhite; if((ix + iy * (WS_SIZE + 1)) % 2){ bWhite = TRUE; glDisable(GL_TEXTURE_2D); glColor4f(214.f / 255.f, 171.f / 255.f, 123.f / 255.f, 0.75f); }else{ glColor4f(140.f / 255.f, 123.f / 255.f, 129.f / 255.f, 0.75f); bWhite = FALSE; if(m_pScene->m_bTexturedBoard){ glEnable(GL_TEXTURE_2D); // glColor4f(0.75f, 0.75f, 0.f, 0.75f); } } if(bClicked && (ix == m_nX) && (iy == m_nY)){ glColor4f(1.0f, 1.0f, 0.0f, 0.75f); } if(bClicked){ for(int kx = 0; kx < m_arrAvaibleMoves.Size(); kx++){ if((ix == m_arrAvaibleMoves.e(kx).x) && (iy == m_arrAvaibleMoves.e(kx).y)){ glColor4f(1.0f, 1.0f, 1.0f, 0.75f); } } } glBegin(GL_QUADS); glVertex3fv(&m_arrBoardPoints.e(ix, iy).x); glTexCoord2f(0.f, 0.f); glVertex3fv(&m_arrBoardPoints.e(ix + 1, iy).x); glTexCoord2f(0.f, 1.f); glVertex3fv(&m_arrBoardPoints.e(ix + 1, iy + 1).x); glTexCoord2f(1.f, 1.f); glVertex3fv(&m_arrBoardPoints.e(ix, iy + 1).x); glTexCoord2f(1.f, 0.f); glEnd(); glColor4f(0.15f, 0.75f, 0.15f, 0.75f); glBegin(GL_LINE_STRIP); glVertex3fv(&m_arrBoardPoints.e(ix, iy).x); glVertex3fv(&m_arrBoardPoints.e(ix + 1, iy).x); glVertex3fv(&m_arrBoardPoints.e(ix + 1, iy + 1).x); glVertex3fv(&m_arrBoardPoints.e(ix, iy + 1).x); glVertex3fv(&m_arrBoardPoints.e(ix, iy).x); glEnd(); } } glDisable(GL_TEXTURE_2D); // Selected Quads... for(iy = 0; iy < WS_SIZE; iy ++){ for(int ix = 0; ix < WS_SIZE; ix ++){ if(((nX_From == ix) && (nY_From == iy)) ||((nX_To == ix) && (nY_To == iy))){ glLineWidth(3.f); glColor4f(0.75f, 0.15f, 0.15f, 0.75f); glPushMatrix(); glTranslatef(0.f, 0.f, 0.05f); glBegin(GL_LINE_STRIP); glVertex3fv(&m_arrBoardPoints.e(ix, iy).x); glVertex3fv(&m_arrBoardPoints.e(ix + 1, iy).x); glVertex3fv(&m_arrBoardPoints.e(ix + 1, iy + 1).x); glVertex3fv(&m_arrBoardPoints.e(ix, iy + 1).x); glVertex3fv(&m_arrBoardPoints.e(ix, iy).x); glEnd(); glPopMatrix(); } } } // Shadows... if(m_pScene->m_bShadows){ for(ix = 0; ix < m_arrFigures.Size(); ix++){ CFigure *pFigure = m_arrFigures.e(ix); glPushMatrix(); if(m_bProcessAnimation && (pFigure == pCurSelFigure)){ glTranslatef(ptPosAnimation.x, ptPosAnimation.y, ptPosAnimation.z); }else{ glTranslatef(WS_SIZE_SQUARE * (pFigure->m_nX - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, WS_SIZE_SQUARE * (pFigure->m_nY - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, 0.f); } glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glAlphaFunc(GL_GREATER, .0001f); glEnable(GL_ALPHA_TEST); glTranslatef(0.f, 0.f, 0.01f); if(m_bProcessAnimation && (pFigure == pCurSelFigure)){ glTranslatef(0.f, 0.f, -ptPosAnimation.z); } glColor4f(0.f, 0.f, 0.f, 0.15f); glDisableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), &Peshka_Shadow_vert_arr[0].x); glDrawElements(GL_TRIANGLES, Peshka_Shadow_face_num * 3, GL_UNSIGNED_INT, &Peshka_Shadow_face_arr[0].v[0]); glEnableClientState(GL_NORMAL_ARRAY); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); glPopMatrix(); } } // Letters... if(m_pScene->m_bLetters){ glColor4f(1.f, 0.1f, 1.f, 1.f); glDisableClientState(GL_NORMAL_ARRAY); for(ix = 0; ix < 8; ix++){ float *pVert; unsigned int *pFace; int nFaces; switch(ix){ case 0: pVert = &A_vert_arr[0].x; pFace = &A_face_arr[0].v[0]; nFaces = A_face_num; break; case 1: pVert = &B_vert_arr[0].x; pFace = &B_face_arr[0].v[0]; nFaces = B_face_num; break; case 2: pVert = &C_vert_arr[0].x; pFace = &C_face_arr[0].v[0]; nFaces = C_face_num; break; case 3: pVert = &D_vert_arr[0].x; pFace = &D_face_arr[0].v[0]; nFaces = D_face_num; break; case 4: pVert = &E_vert_arr[0].x; pFace = &E_face_arr[0].v[0]; nFaces = E_face_num; break; case 5: pVert = &F_vert_arr[0].x; pFace = &F_face_arr[0].v[0]; nFaces = F_face_num; break; case 6: pVert = &G_vert_arr[0].x; pFace = &G_face_arr[0].v[0]; nFaces = G_face_num; break; case 7: pVert = &H_vert_arr[0].x; pFace = &H_face_arr[0].v[0]; nFaces = H_face_num; break; } glPushMatrix(); glTranslatef(WS_SIZE_SQUARE * (ix - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, WS_SIZE_SQUARE * (0 - WS_SIZE / 2), 0.f); glTranslatef(0.f, 0.f, 0.01f); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), pVert); glDrawElements(GL_TRIANGLES, nFaces * 3, GL_UNSIGNED_INT, pFace); glPopMatrix(); } for(ix = 0; ix < 8; ix++){ float *pVert; unsigned int *pFace; int nFaces; switch(ix){ case 0: pVert = &L1_vert_arr[0].x; pFace = &L1_face_arr[0].v[0]; nFaces = L1_face_num; break; case 1: pVert = &L2_vert_arr[0].x; pFace = &L2_face_arr[0].v[0]; nFaces = L2_face_num; break; case 2: pVert = &L3_vert_arr[0].x; pFace = &L3_face_arr[0].v[0]; nFaces = L3_face_num; break; case 3: pVert = &L4_vert_arr[0].x; pFace = &L4_face_arr[0].v[0]; nFaces = L4_face_num; break; case 4: pVert = &L5_vert_arr[0].x; pFace = &L5_face_arr[0].v[0]; nFaces = L5_face_num; break; case 5: pVert = &L6_vert_arr[0].x; pFace = &L6_face_arr[0].v[0]; nFaces = L6_face_num; break; case 6: pVert = &L7_vert_arr[0].x; pFace = &L7_face_arr[0].v[0]; nFaces = L7_face_num; break; case 7: pVert = &L8_vert_arr[0].x; pFace = &L8_face_arr[0].v[0]; nFaces = L8_face_num; break; } glPushMatrix(); glTranslatef(WS_SIZE_SQUARE * (0 - WS_SIZE / 2), WS_SIZE_SQUARE * (ix - WS_SIZE / 2) + WS_SIZE_SQUARE / 2, 0.f); glTranslatef(0.1f, 0.f, 0.01f); glVertexPointer(3, GL_FLOAT, sizeof(vert_t), pVert); glDrawElements(GL_TRIANGLES, nFaces * 3, GL_UNSIGNED_INT, pFace); glPopMatrix(); } glEnableClientState(GL_NORMAL_ARRAY); } glEnableClientState(GL_TEXTURE_COORD_ARRAY); } void CBoard::ResetFigures() { nCurrentTable = 0; m_pcMoveTable->m_parrStrMoves.FreeData(); m_pcMoveTable->nHeadOfList = 0; nX_From = nY_From = nX_To = nY_To = -1; // Clear situation. m_bTheEnd = FALSE; sGameStatus.bBeatField[0] = sGameStatus.bBeatField[1] = FALSE; sGameStatus.bRokirovka[0] = sGameStatus.bRokirovka[1] = FALSE; sGameStatus.bMoveTura[0] = sGameStatus.bMoveTura[1] = sGameStatus.bMoveTura[2] = sGameStatus.bMoveTura[3] = FALSE; m_nMove = -1; parrUndoStates.FreeData(); m_bThinking = FALSE; // Clean All Data. FreeData(); m_arrAvaibleMoves.FreeData(); bClicked = FALSE; // WHITE int ix; CFigure *pFigure; for(ix = 0; ix < 8; ix++){ pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_PESHKA; } pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_KON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_KON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_SLON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_SLON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_TURA; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_TURA; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_FERZ; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_WHITE; pFigure->m_nType = WS_KOROL; // BLACK for(ix = 0; ix < 8; ix++){ pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_PESHKA; } pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_KON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_KON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_SLON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_SLON; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_TURA; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_TURA; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_FERZ; pFigure = m_arrFigures.AppendNew(); pFigure->m_nColor = WS_BLACK; pFigure->m_nType = WS_KOROL; for(ix = 0; ix < 32; ix++){ pFigure = m_arrFigures.e(ix); switch(ix){ case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: pFigure->m_nX = ix; pFigure->m_nY = 1; break; // Peshki. case 8: pFigure->m_nX = 1; pFigure->m_nY = 0; break; // KON'. case 9: pFigure->m_nX = 6; pFigure->m_nY = 0; break; // KON'. case 10: pFigure->m_nX = 2; pFigure->m_nY = 0; break; // SLON. case 11: pFigure->m_nX = 5; pFigure->m_nY = 0; break; // SLON. case 12: pFigure->m_nX = 0; pFigure->m_nY = 0; break; // TURA. case 13: pFigure->m_nX = 7; pFigure->m_nY = 0; break; // TURA. case 14: pFigure->m_nX = 3; pFigure->m_nY = 0; break; // FERZ'. case 15: pFigure->m_nX = 4; pFigure->m_nY = 0; break; // KOROL'. case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: pFigure->m_nX = ix - 16; pFigure->m_nY = 6; break; // Peshki. case 24: pFigure->m_nX = 1; pFigure->m_nY = 7; break; // KON'. case 25: pFigure->m_nX = 6; pFigure->m_nY = 7; break; // KON'. case 26: pFigure->m_nX = 2; pFigure->m_nY = 7; break; // SLON. case 27: pFigure->m_nX = 5; pFigure->m_nY = 7; break; // SLON. case 28: pFigure->m_nX = 0; pFigure->m_nY = 7; break; // TURA. case 29: pFigure->m_nX = 7; pFigure->m_nY = 7; break; // TURA. case 30: pFigure->m_nX = 3; pFigure->m_nY = 7; break; // FERZ'. case 31: pFigure->m_nX = 4; pFigure->m_nY = 7; break; // KOROL'. } } if(m_pScene->IsOnePlayer() && (m_pScene->m_nPlayerColor == WS_BLACK)){ // Nothing! }else{ CreateUndo(); } } BOOL bPlayMode = FALSE; void CBoard::Test(point3d ptPos, point3d ptCamPos) { if(m_bProcessAnimation || bPlayMode){ return; } if(m_pScene->bThinkInetOponent){ return; } if(!m_pScene->m_bStartGame && m_pScene->m_nPlayerColor == WS_BLACK){ return; } if(m_bTheEnd || m_bThinking){ return; } point3d vtVect; sub_vectors(&vtVect.x, &ptCamPos.x, &ptPos.x); plane plPlane; point3d vtVectZ = { 0.f, 0.f, 1.f }, ptZero = { 0.f, 0.f, 0.f }; comp_plane_eqn(&plPlane.a, &ptZero.x, &vtVectZ.x); point3d ptRes; cross_line_plane(vtVect, &plPlane.a, ptCamPos, ptRes); for(int iy = 0; iy < 8; iy++){ for(int ix = 0; ix < 8; ix++){ point3d ptP1 = m_arrBoardPoints.e(ix, iy); point3d ptP2 = m_arrBoardPoints.e(ix + 1, iy); point3d ptP3 = m_arrBoardPoints.e(ix + 1, iy + 1); point3d ptP4 = m_arrBoardPoints.e(ix, iy + 1); transform_back_point(&ptP1, &ptP1, m_pmtMatr); transform_back_point(&ptP2, &ptP2, m_pmtMatr); transform_back_point(&ptP3, &ptP3, m_pmtMatr); transform_back_point(&ptP4, &ptP4, m_pmtMatr); if(point_in_triangle(ptP1, ptP2, ptP3, ptRes) || point_in_triangle(ptP3, ptP4, ptP1, ptRes)){ CFigure *pFigure = GetFigureByPosition(ix, iy); if(!bClicked){ if(pFigure){ if(pFigure->m_nColor == m_pScene->m_nCurrentMoveColor/*WS_WHITE*/){ m_nX = ix; m_nY = iy; bClicked = TRUE; pCurSelFigure = pFigure; GetAllMovesForSelectedFigure(); nX_From = m_nX; nY_From = m_nY; } } return; }else{ if(pFigure == NULL){ if(IsAvaibleMove(pCurSelFigure, ix, iy)){ bClicked = FALSE; m_pScene->m_bStartGame = TRUE; // TURA... if(pCurSelFigure->m_nType == WS_TURA){ if(!sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 0] && (pCurSelFigure->m_nX == 7)){ sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 0] = TRUE; } if(!sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 1] && (pCurSelFigure->m_nX == 0)){ sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 1] = TRUE; } } CheckProhodPeshka(pCurSelFigure, pCurSelFigure->m_nX, pCurSelFigure->m_nY, ix, iy); // Kill Prohod peshka... CFigure *pKillPeshka = NULL; if(pCurSelFigure->m_nType == WS_PESHKA && sGameStatus.bBeatField[m_pScene->m_nCurrentMoveColor]){ CPoint ptPos = sGameStatus.arrPosBeatPeshka[m_pScene->m_nCurrentMoveColor]; int nSign = 1; if(m_pScene->m_nCurrentMoveColor == WS_BLACK){ nSign = -1; } if(ix == ptPos.x && iy == ptPos.y){ pKillPeshka = GetFigureByPosition(ix, iy - nSign); } } nX_To = ix; nY_To = iy; int nSavedX = pCurSelFigure->m_nX; int nSavedY = pCurSelFigure->m_nY; int nSavedType = pCurSelFigure->m_nType; sGameStatus.bPeshkaChangeGeometry = FALSE; if(pCurSelFigure->m_nType == WS_PESHKA){ if(((m_pScene->m_nCurrentMoveColor == WS_WHITE) && (iy == 7)) || ((m_pScene->m_nCurrentMoveColor == WS_BLACK) && (iy == 0))){ m_pScene->m_cSelPeshka.DoModal(); pCurSelFigure->m_nType = m_pScene->m_nPeshkaSel; sGameStatus.bPeshkaChangeGeometry = TRUE; sGameStatus.nTypePeshkaNow = pCurSelFigure->m_nType; } } if(m_pScene->m_bAnimation){ AnimationFigure(pCurSelFigure, pKillPeshka, ix, iy); } if(pKillPeshka){ RemoveFigure(pKillPeshka); } // Rokirovka... if(pCurSelFigure->m_nType == WS_KOROL && fAbs(pCurSelFigure->m_nX - ix) == 2){ int nPlaceY = 0; if(pCurSelFigure->m_nColor == WS_BLACK){ nPlaceY = 7; } if((ix - pCurSelFigure->m_nX) > 0){ sGameStatus.bMoveTura[pCurSelFigure->m_nColor * 2 + 0] = TRUE; CFigure *pFig = GetFigureByPosition(7, nPlaceY); // Tura... if(pFig){ if(m_pScene->m_bAnimation){ pCurSelFigure->m_nX = ix; pCurSelFigure->m_nY = iy; AnimationFigure(pFig, NULL, ix - 1, iy); } pFig->m_nX = ix - 1; sGameStatus.bRokirovka[m_pScene->m_nCurrentMoveColor] = TRUE; } }else{ sGameStatus.bMoveTura[pCurSelFigure->m_nColor * 2 + 1] = TRUE; CFigure *pFig = GetFigureByPosition(0, nPlaceY); // Tura... if(pFig){ if(m_pScene->m_bAnimation){ pCurSelFigure->m_nX = ix; pCurSelFigure->m_nY = iy; AnimationFigure(pFig, NULL, ix + 1, iy); } pFig->m_nX = ix + 1; sGameStatus.bRokirovka[pCurSelFigure->m_nColor] = TRUE; } } } pCurSelFigure->m_nX = ix; pCurSelFigure->m_nY = iy; int nState; m_pScene->lpfnGetState(&m_arrFigures, m_pScene->m_nCurrentMoveColor, sGameStatus, nState); if(nState == WS_CHECK){ // Can't move figure because check! AfxMessageBox("You have Check!"); sGameStatus.bPeshkaChangeGeometry = FALSE; pCurSelFigure->m_nX = nSavedX; pCurSelFigure->m_nY = nSavedY; if(nSavedType != pCurSelFigure->m_nType){ pCurSelFigure->m_nType = nSavedType; } bClicked = FALSE; return; }else{ // Korol' MOVED! if(pCurSelFigure->m_nType == WS_KOROL){ sGameStatus.bRokirovka[m_pScene->m_nCurrentMoveColor] = TRUE; } MoveDone(); m_pScene->Draw(); int nState; m_pScene->lpfnGetState(&m_arrFigures, !m_pScene->m_nCurrentMoveColor, sGameStatus, nState); switch(nState){ case WS_CHECK: m_pScene->PlaySound(WS_CHECK); break; case WS_MATE: SendMessage(); m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("Mate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; case WS_PAT: SendMessage(); m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("StaleMate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; default: m_pScene->PlaySound(WS_MOVE); break; } } if(m_pScene->IsTwoPlayers()){ if(!m_pScene->m_bConnected){ m_pScene->m_nCurrentMoveColor = !m_pScene->m_nCurrentMoveColor; if(m_pScene->m_nCurrentMoveColor == WS_WHITE){ m_pScene->RotateForWhite(); }else{ m_pScene->RotateForBlack(); } }else{ // Game via Inet! SendMessage(); } }else{ CFigure *pDeleteFigure = NULL; CFigure *pMoveFigure = NULL; int nMoveX, nMoveY; // Start Computer Here!!! Our Move done! m_bThinking = TRUE; m_pScene->lpfnGetMove(&m_arrFigures, m_pScene->m_nPlayerColor, m_pScene->m_nGameLevel, sGameStatus, pDeleteFigure, pMoveFigure, nMoveX, nMoveY, &m_pScene->m_bStopThinking); if(m_pScene->m_bFinish){ return; } m_bThinking = m_pScene->m_bStopThinking = FALSE; nX_From = pMoveFigure->m_nX; nY_From = pMoveFigure->m_nY; nX_To = nMoveX; nY_To = nMoveY; if(m_pScene->m_bAnimation){ AnimationFigure(pMoveFigure, pDeleteFigure, nMoveX, nMoveY); } if(pDeleteFigure){ RemoveFigure(pDeleteFigure); } pMoveFigure->m_nX = nMoveX; pMoveFigure->m_nY = nMoveY; MoveDone(); int nState; m_pScene->lpfnGetState(&m_arrFigures, m_pScene->m_nCurrentMoveColor, sGameStatus, nState); switch(nState){ case WS_CHECK: m_pScene->PlaySound(WS_CHECK); break; case WS_MATE: m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("Mate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; case WS_PAT: m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("StaleMate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; default: m_pScene->PlaySound(WS_MOVE); break; } } m_pScene->Draw(); CreateUndo(); } }else if(pFigure != pCurSelFigure){ if(pFigure->m_nColor == m_pScene->m_nCurrentMoveColor){ m_nX = ix; m_nY = iy; nX_From = m_nX; nY_From = m_nY; pCurSelFigure = pFigure; GetAllMovesForSelectedFigure(); }else{ if(IsAvaibleMove(pCurSelFigure, ix, iy)){ // TURA... if(pCurSelFigure->m_nType == WS_TURA){ if(!sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 0] && (pCurSelFigure->m_nX == 7)){ sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 0] = TRUE; } if(!sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 1] && (pCurSelFigure->m_nX == 0)){ sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 1] = TRUE; } } nX_To = ix; nY_To = iy; int nSavedX = pCurSelFigure->m_nX; int nSavedY = pCurSelFigure->m_nY; int nSavedType = pCurSelFigure->m_nType; sGameStatus.bPeshkaChangeGeometry = FALSE; if(pCurSelFigure->m_nType == WS_PESHKA){ if(((m_pScene->m_nCurrentMoveColor == WS_WHITE) && (iy == 7)) || ((m_pScene->m_nCurrentMoveColor == WS_BLACK) && (iy == 0))){ m_pScene->m_cSelPeshka.DoModal(); pCurSelFigure->m_nType = m_pScene->m_nPeshkaSel; sGameStatus.bPeshkaChangeGeometry = TRUE; sGameStatus.nTypePeshkaNow = pCurSelFigure->m_nType; } } if(m_pScene->m_bAnimation){ AnimationFigure(pCurSelFigure, pFigure, ix, iy); } pCurSelFigure->m_nX = ix; pCurSelFigure->m_nY = iy; CFigure *pSavedFigure = new CFigure(); *pSavedFigure = *pFigure; RemoveFigure(pFigure); // Kill this Figure. int nState; m_pScene->lpfnGetState(&m_arrFigures, m_pScene->m_nCurrentMoveColor, sGameStatus, nState); if(nState == WS_CHECK){ // Can't move figure because check! AfxMessageBox("You have Check!"); sGameStatus.bPeshkaChangeGeometry = FALSE; pCurSelFigure->m_nX = nSavedX; pCurSelFigure->m_nY = nSavedY; CFigure *pFig = m_arrFigures.AppendNew(); *pFig = *pSavedFigure; RemoveFromDeleteFigures(pFig); delete(pSavedFigure); if(nSavedType != pCurSelFigure->m_nType){ pCurSelFigure->m_nType = nSavedType; } bClicked = FALSE; return; }else{ delete(pSavedFigure); MoveDone(); m_pScene->Draw(); bClicked = FALSE; m_pScene->m_bStartGame = TRUE; int nState; m_pScene->lpfnGetState(&m_arrFigures, !m_pScene->m_nCurrentMoveColor, sGameStatus, nState); switch(nState){ case WS_CHECK: m_pScene->PlaySound(WS_CHECK); break; case WS_MATE: SendMessage(); m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("Mate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; case WS_PAT: SendMessage(); m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("StaleMate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; default: m_pScene->PlaySound(WS_MOVE); break; } } if(m_pScene->IsTwoPlayers()){ if(!m_pScene->m_bConnected){ m_pScene->m_nCurrentMoveColor = !m_pScene->m_nCurrentMoveColor; if(m_pScene->m_nCurrentMoveColor == WS_WHITE){ m_pScene->RotateForWhite(); }else{ m_pScene->RotateForBlack(); } }else{ // Game via Inet! SendMessage(); } m_pScene->Draw(); }else{ m_pScene->Draw(); CFigure *pDeleteFigure = NULL; CFigure *pMoveFigure = NULL; int nMoveX, nMoveY; // Start Computer Here!!! Our Move done! m_bThinking = TRUE; m_pScene->lpfnGetMove(&m_arrFigures, m_pScene->m_nPlayerColor, m_pScene->m_nGameLevel, sGameStatus, pDeleteFigure, pMoveFigure, nMoveX, nMoveY, &m_pScene->m_bStopThinking); if(m_pScene->m_bFinish){ return; } m_bThinking = m_pScene->m_bStopThinking = FALSE; nX_From = pMoveFigure->m_nX; nY_From = pMoveFigure->m_nY; nX_To = nMoveX; nY_To = nMoveY; MoveDone(); if(m_pScene->m_bAnimation){ AnimationFigure(pMoveFigure, pDeleteFigure, nMoveX, nMoveY); } if(pDeleteFigure){ RemoveFigure(pDeleteFigure); } pMoveFigure->m_nX = nMoveX; pMoveFigure->m_nY = nMoveY; int nState; m_pScene->lpfnGetState(&m_arrFigures, m_pScene->m_nCurrentMoveColor, sGameStatus, nState); switch(nState){ case WS_CHECK: m_pScene->PlaySound(WS_CHECK); break; case WS_MATE: m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("Mate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; case WS_PAT: m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("StaleMate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; default: m_pScene->PlaySound(WS_MOVE); break; } m_bThinking = FALSE; } CreateUndo(); } } } } return; } } } } BOOL CBoard::IsAvaibleMove(CFigure *pFigure, int x, int y) { if(pFigure){ int ix, nSz = m_arrAvaibleMoves.Size(); for(ix = 0; ix < nSz; ix++){ if(m_arrAvaibleMoves.e(ix).x == x && m_arrAvaibleMoves.e(ix).y == y){ return TRUE; } } } return FALSE; } void CBoard::GetAllMovesForSelectedFigure() { m_arrAvaibleMoves.FreeData(); int nX = pCurSelFigure->m_nX; int nY = pCurSelFigure->m_nY; CFigure *pFigure = NULL; switch(pCurSelFigure->m_nType){ case WS_PESHKA:{ int nSign = 1; if(m_pScene->m_nCurrentMoveColor == WS_BLACK){ nSign = -1; } pFigure = GetFigureByPosition(nX - 1, nY + 1 * nSign); if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY + 1 * nSign)); } } pFigure = GetFigureByPosition(nX + 1, nY + 1 * nSign); if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY + 1 * nSign)); } } pFigure = GetFigureByPosition(nX, nY + 1 * nSign); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX, nY + 1 * nSign)); } if((((nY == 1) && (m_pScene->m_nCurrentMoveColor == WS_WHITE) || ((nY == 6) && m_pScene->m_nCurrentMoveColor == WS_BLACK))) && !GetFigureByPosition(nX, nY + 1 * nSign)/*empty field*/){ pFigure = GetFigureByPosition(nX, nY + 2 * nSign); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX, nY + 2 * nSign)); } } if(sGameStatus.bBeatField[m_pScene->m_nCurrentMoveColor]){ CPoint ptPos = sGameStatus.arrPosBeatPeshka[m_pScene->m_nCurrentMoveColor]; if(((ptPos.x == nX - 1) && (ptPos.y == nY + 1 * nSign)) || ((ptPos.x == nX + 1) && (ptPos.y == nY + 1 * nSign))){ m_arrAvaibleMoves.Append(ptPos); } } break; } case WS_KON:{ pFigure = GetFigureByPosition(nX + 1, nY + 2); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY + 2)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY + 2)); } } pFigure = GetFigureByPosition(nX - 1, nY + 2); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY + 2)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY + 2)); } } pFigure = GetFigureByPosition(nX - 1, nY - 2); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY - 2)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY - 2)); } } pFigure = GetFigureByPosition(nX + 1, nY - 2); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY - 2)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY - 2)); } } pFigure = GetFigureByPosition(nX + 2, nY + 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 2, nY + 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 2, nY + 1)); } } pFigure = GetFigureByPosition(nX + 2, nY - 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 2, nY - 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 2, nY - 1)); } } pFigure = GetFigureByPosition(nX - 2, nY - 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 2, nY - 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 2, nY - 1)); } } pFigure = GetFigureByPosition(nX - 2, nY + 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 2, nY + 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 2, nY + 1)); } } break; } case WS_SLON:{ int nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } break; } case WS_TURA:{ int nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } break; } case WS_FERZ:{ int nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nXX, nYY)); } } break; } case WS_KOROL:{ pFigure = GetFigureByPosition(nX + 1, nY + 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY + 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY + 1)); } } pFigure = GetFigureByPosition(nX - 1, nY + 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY + 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY + 1)); } } pFigure = GetFigureByPosition(nX - 1, nY - 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY - 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY - 1)); } } pFigure = GetFigureByPosition(nX + 1, nY - 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY - 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY - 1)); } } pFigure = GetFigureByPosition(nX, nY + 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX, nY + 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX, nY + 1)); } } pFigure = GetFigureByPosition(nX, nY - 1); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX, nY - 1)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX, nY - 1)); } } pFigure = GetFigureByPosition(nX - 1, nY); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX - 1, nY)); } } pFigure = GetFigureByPosition(nX + 1, nY); if(!pFigure){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY)); }else{ if(pFigure->m_nColor == !m_pScene->m_nCurrentMoveColor){ m_arrAvaibleMoves.Append(CPoint(nX + 1, nY)); } } // Rokirovka... if(!sGameStatus.bRokirovka[m_pScene->m_nCurrentMoveColor]){ // 0-0 if(!sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 0]){ if(IsEmptyField(nX + 1, nY) && IsEmptyField(nX + 2, nY)){ if(IsRokirovkaAvailable(TRUE, nX, nY)){ m_arrAvaibleMoves.Append(CPoint(nX + 2, nY)); } } } // 0-0-0 if(!sGameStatus.bMoveTura[m_pScene->m_nCurrentMoveColor * 2 + 1]){ if(IsEmptyField(nX - 1, nY) && IsEmptyField(nX - 2, nY) && IsEmptyField(nX - 3, nY)){ if(IsRokirovkaAvailable(FALSE, nX, nY)){ m_arrAvaibleMoves.Append(CPoint(nX - 2, nY)); } } } } break; } } // Remove KOROLYA. int ix; for(ix = 0; ix < m_arrAvaibleMoves.Size(); ix++){ CPoint ptPos = m_arrAvaibleMoves.e(ix); CFigure *pFig = GetFigureByPosition(ptPos.x, ptPos.y); if(pFig){ if(pFig->m_nType == WS_KOROL){ m_arrAvaibleMoves.ShiftRemove(ix); ix--; continue; } } } } CFigure * CBoard::GetFigureByPosition(int x, int y) { CFigure *pFigure = NULL; int ix, nSz = m_arrFigures.Size(); for(ix = 0; ix < nSz; ix++){ pFigure = m_arrFigures.e(ix); if(pFigure->m_nX == x && pFigure->m_nY == y){ return pFigure; } } return NULL; } BOOL CBoard::IsPosInBoard(int x, int y) { if(x >= 0 && x <= 7 && y >=0 && y <= 7){ return TRUE; } else{ return FALSE; } } void CBoard::RemoveFigure(CFigure *pFigure) { if(pFigure->m_nColor == WS_WHITE){ CFigure *pFig = parrBlackDelFigs.AppendNew(); *pFig = *pFigure; }else{ CFigure *pFig = parrWhiteDelFigs.AppendNew(); *pFig = *pFigure; } int ix, nSz = m_arrFigures.Size(); for(ix = 0; ix < nSz; ix++){ if(m_arrFigures.e(ix) == pFigure){ m_arrFigures.ShiftRemove(ix); return; } } } void CBoard::Save(FILE *pStream) { int ix, nSz = m_arrFigures.Size(); fprintf(pStream, "%d ", nSz); for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = m_arrFigures.e(ix); fprintf(pStream, "%d %d %d %d ", pFigure->m_nType, pFigure->m_nColor, pFigure->m_nX, pFigure->m_nY); } // Game Status... fprintf(pStream, "%d %d %d %d %d %d %d %d %d %d %d %d ", \ sGameStatus.bRokirovka[0], \ sGameStatus.bRokirovka[1], \ sGameStatus.bBeatField[0], \ sGameStatus.bBeatField[1], \ sGameStatus.arrPosBeatPeshka[0].x, \ sGameStatus.arrPosBeatPeshka[0].y, \ sGameStatus.arrPosBeatPeshka[1].x, \ sGameStatus.arrPosBeatPeshka[1].y, \ sGameStatus.bMoveTura[0], \ sGameStatus.bMoveTura[1], \ sGameStatus.bMoveTura[2], \ sGameStatus.bMoveTura[3]); // Deleted Figures. nSz = parrWhiteDelFigs.Size(); fprintf(pStream, "%d ", nSz); for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = parrWhiteDelFigs.e(ix); fprintf(pStream, "%d %d %d %d ", pFigure->m_nType, pFigure->m_nColor, pFigure->m_nX, pFigure->m_nY); } nSz = parrBlackDelFigs.Size(); fprintf(pStream, "%d ", nSz); for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = parrBlackDelFigs.e(ix); fprintf(pStream, "%d %d %d %d ", pFigure->m_nType, pFigure->m_nColor, pFigure->m_nX, pFigure->m_nY); } // Save Undo. nSz = parrUndoStates.Size(); fprintf(pStream, "%d ", nSz); int jx; for(ix = 0; ix < nSz; ix++){ sBoardState *pBoardState = parrUndoStates.e(ix); pBoardState->sGameStatus.Save(pStream); fprintf(pStream, "%d %d %d %d ", nX_From, nY_From, nX_To, nY_To); fprintf(pStream, "%d ", pBoardState->arrStates.Size()); for(jx = 0; jx < pBoardState->arrStates.Size(); jx++){ sState *pState = &pBoardState->arrStates.e(jx); pState->Save(pStream); } fprintf(pStream, "%d ", pBoardState->arrStatesDelBlack.Size()); for(jx = 0; jx < pBoardState->arrStatesDelBlack.Size(); jx++){ sState *pState = &pBoardState->arrStatesDelBlack.e(jx); pState->Save(pStream); } fprintf(pStream, "%d ", pBoardState->arrStatesDelWhite.Size()); for(jx = 0; jx < pBoardState->arrStatesDelWhite.Size(); jx++){ sState *pState = &pBoardState->arrStatesDelWhite.e(jx); pState->Save(pStream); } } m_pcMoveTable->Save(pStream); fprintf(pStream, "\n"); } void CBoard::Load(char *pStr) { int kx = 0, ix, nSz; FreeData(); nSz = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = m_arrFigures.AppendNew(); pFigure->m_nType = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nColor = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nX = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nY = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; } // Game Status... sGameStatus.bRokirovka[0] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bRokirovka[1] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bBeatField[0] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bBeatField[1] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.arrPosBeatPeshka[0].x = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.arrPosBeatPeshka[0].y = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.arrPosBeatPeshka[1].x = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.arrPosBeatPeshka[1].y = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bMoveTura[0] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bMoveTura[1] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bMoveTura[2] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; sGameStatus.bMoveTura[3] = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; // Deleted Figures. nSz = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = parrWhiteDelFigs.AppendNew(); pFigure->m_nType = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nColor = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nX = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nY = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; } nSz = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = parrBlackDelFigs.AppendNew(); pFigure->m_nType = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nColor = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nX = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pFigure->m_nY = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; } // Load Undo. nSz = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; parrUndoStates.FreeData(); m_nMove = nSz - 1; if(!nSz){ CreateUndo(); } int jx; for(ix = 0; ix < nSz; ix++){ sBoardState *pBoardState = parrUndoStates.AppendNew(); pBoardState->sGameStatus.Load(pStr, kx); pBoardState->nX_From = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pBoardState->nY_From = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pBoardState->nX_To = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pBoardState->nY_To = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; int nSzStates = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pBoardState->arrStates.Set(nSzStates); for(jx = 0; jx < nSzStates; jx++){ sState *pState = &pBoardState->arrStates.e(jx); pState->Load(pStr, kx); } nSzStates = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pBoardState->arrStatesDelBlack.Set(nSzStates); for(jx = 0; jx < nSzStates; jx++){ sState *pState = &pBoardState->arrStatesDelBlack.e(jx); pState->Load(pStr, kx); } nSzStates = atoi(&pStr[kx]); while(pStr[kx] != ' '){ kx++; } kx++; pBoardState->arrStatesDelWhite.Set(nSzStates); for(jx = 0; jx < nSzStates; jx++){ sState *pState = &pBoardState->arrStatesDelWhite.e(jx); pState->Load(pStr, kx); } } m_pcMoveTable->Load(pStr, kx); nCurrentTable = m_pcMoveTable->m_parrStrMoves.Size(); } #define WS_STEPS_ANIMATION 20 #define WS_DELAY 10 void CBoard::AnimationFigure(CFigure *pFigure, CFigure *pFigDel, int nToX, int nToY) { if(pFigure == NULL){ return; } CFigure *pSavedFigure = pCurSelFigure; pCurSelFigure = pFigure; pCurDelFigure = pFigDel; m_bProcessAnimation = TRUE; ptPosAnimation.x = WS_SIZE_SQUARE * (pFigure->m_nX - WS_SIZE / 2) + WS_SIZE_SQUARE / 2; ptPosAnimation.y = WS_SIZE_SQUARE * (pFigure->m_nY - WS_SIZE / 2) + WS_SIZE_SQUARE / 2; ptPosAnimation.z = 0.f; point3d ptPos2; ptPos2.x = WS_SIZE_SQUARE * (nToX - WS_SIZE / 2) + WS_SIZE_SQUARE / 2; ptPos2.y = WS_SIZE_SQUARE * (nToY - WS_SIZE / 2) + WS_SIZE_SQUARE / 2; ptPos2.z = 0.f; point3d vtVect; sub_vectors(&vtVect.x, &ptPos2.x, &ptPosAnimation.x); scale_vect(&vtVect.x, 1.f / float(WS_STEPS_ANIMATION)); if(pFigDel){ ptPosDelFig.x = WS_SIZE_SQUARE * (pFigDel->m_nX - WS_SIZE / 2) + WS_SIZE_SQUARE / 2; ptPosDelFig.y = WS_SIZE_SQUARE * (pFigDel->m_nY - WS_SIZE / 2) + WS_SIZE_SQUARE / 2; ptPosDelFig.z = 0.f; ident_matrix(&mtDelMatr); rotate_matrix(&mtDelMatr, HALFPI / 2.f, 0.f, 0.f); } matrix m, mres; for(int ix = 0; ix < WS_STEPS_ANIMATION; ix++){ add_vectors(&ptPosAnimation.x, &ptPosAnimation.x, &vtVect.x); if(ix < WS_STEPS_ANIMATION / 2){ ptPosAnimation.z += ix / 20.f; } else{ ptPosAnimation.z -= (ix - WS_STEPS_ANIMATION / 2) / 20.f; } ident_matrix(&m); rotate_matrix(&m, 0.f, 0.f, HALFPI / 20.f * float(ix)); mult_matrix(&mres, &mtDelMatr, &m); mtDelMatr = mres; ptPosDelFig.z += ix / 40.f; m_pScene->Draw(); // _sleep(WS_DELAY); } m_bProcessAnimation = FALSE; pCurSelFigure = pSavedFigure; } void CBoard::StartWhite() { m_pScene->m_bStartGame = TRUE; bClicked = FALSE; CFigure *pDeleteFigure = NULL; CFigure *pMoveFigure = NULL; int nMoveX, nMoveY; m_bThinking = TRUE; m_pScene->lpfnGetMove(&m_arrFigures, m_pScene->m_nPlayerColor, m_pScene->m_nGameLevel, sGameStatus, pDeleteFigure, pMoveFigure, nMoveX, nMoveY, &m_pScene->m_bStopThinking); if(m_pScene->m_bFinish){ return; } m_pScene->m_bStopThinking = FALSE; nX_To = nMoveX; nY_To = nMoveY; nX_From = pMoveFigure->m_nX; nY_From = pMoveFigure->m_nY; if(m_pScene->m_bAnimation){ AnimationFigure(pMoveFigure, pDeleteFigure, nMoveX, nMoveY); } if(pDeleteFigure){ RemoveFigure(pDeleteFigure); } pMoveFigure->m_nX = nMoveX; pMoveFigure->m_nY = nMoveY; m_pScene->PlaySound(WS_MOVE); m_pScene->Draw(); m_bThinking = FALSE; MoveDone(); CreateUndo(); } void CBoard::CheckProhodPeshka(CFigure *pFigure, int nOldX, int nOldY, int nNewX, int nNewY) { int nOpositeType = !m_pScene->m_nCurrentMoveColor; if(pFigure->m_nType == WS_PESHKA && fAbs(nNewY - nOldY) == 2){ int nSign = 1; if(m_pScene->m_nCurrentMoveColor == WS_BLACK){ nSign = -1; } for(int ix = 0; ix < m_arrFigures.Size(); ix++){ CFigure *pFig = m_arrFigures.e(ix); if(pFig->m_nType == WS_PESHKA && pFig->m_nColor == nOpositeType){ if(((pFig->m_nX == nNewX - 1) && (pFig->m_nY == nNewY))|| ((pFig->m_nX == nNewX + 1) && (pFig->m_nY == nNewY))){ sGameStatus.bBeatField[nOpositeType] = TRUE; sGameStatus.arrPosBeatPeshka[nOpositeType] = CPoint(nNewX, nOldY + nSign); return; } } } } sGameStatus.bBeatField[nOpositeType] = FALSE; } BOOL CBoard::IsEmptyField(int x, int y) { CFigure *pFig = GetFigureByPosition(x, y); if(pFig){ return FALSE; }else{ return TRUE; } } void CBoard::CreateUndo() { if(m_pScene->m_bConnected){ return; } m_nMove++; sBoardState *pState; if(m_nMove >= parrUndoStates.Size()){ pState = parrUndoStates.AppendNew(); }else{ pState = parrUndoStates.e(m_nMove); // Kill tail! for(int jx = m_nMove + 1; jx < parrUndoStates.Size(); jx++){ parrUndoStates.ShiftRemove(jx); jx--; } } int ix, nSize = m_arrFigures.Size(); pState->arrStates.Set(nSize); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = m_arrFigures.e(ix); pState->arrStates.e(ix).m_nType = pFig->m_nType; pState->arrStates.e(ix).m_nColor = pFig->m_nColor; pState->arrStates.e(ix).m_nX = pFig->m_nX; pState->arrStates.e(ix).m_nY = pFig->m_nY; } pState->sGameStatus = sGameStatus; pState->nX_From = nX_From; pState->nY_From = nY_From; pState->nX_To = nX_To; pState->nY_To = nY_To; nSize = parrWhiteDelFigs.Size(); pState->arrStatesDelWhite.Set(nSize); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.e(ix); pState->arrStatesDelWhite.e(ix).m_nX = pState->arrStatesDelWhite.e(ix).m_nY = 0; pState->arrStatesDelWhite.e(ix).m_nType = pFig->m_nType; pState->arrStatesDelWhite.e(ix).m_nColor = pFig->m_nColor; } nSize = parrBlackDelFigs.Size(); pState->arrStatesDelBlack.Set(nSize); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.e(ix); pState->arrStatesDelBlack.e(ix).m_nX = pState->arrStatesDelBlack.e(ix).m_nY = 0; pState->arrStatesDelBlack.e(ix).m_nType = pFig->m_nType; pState->arrStatesDelBlack.e(ix).m_nColor = pFig->m_nColor; } } BOOL bWasEnd = FALSE; void CBoard::Undo() { bWasEnd = m_bTheEnd; m_bTheEnd = FALSE; m_nMove--; nCurrentTable --; if(m_pScene->IsOnePlayer()){ nCurrentTable --; } sBoardState *pState = parrUndoStates.e(m_nMove); int ix, nSize = pState->arrStates.Size(); nX_From = pState->nX_From; nY_From = pState->nY_From; nX_To = pState->nX_To; nY_To = pState->nY_To; FreeData(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = m_arrFigures.AppendNew(); pFig->m_nType = pState->arrStates.e(ix).m_nType; pFig->m_nColor = pState->arrStates.e(ix).m_nColor; pFig->m_nX = pState->arrStates.e(ix).m_nX; pFig->m_nY = pState->arrStates.e(ix).m_nY; } sGameStatus = pState->sGameStatus; nSize = pState->arrStatesDelWhite.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelWhite.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelWhite.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } nSize = pState->arrStatesDelBlack.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelBlack.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelBlack.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } if(m_pScene->IsTwoPlayers()){ bClicked = FALSE; m_pScene->m_nCurrentMoveColor = !m_pScene->m_nCurrentMoveColor; } } void CBoard::Redo() { nCurrentTable ++; if(m_pScene->IsOnePlayer()){ nCurrentTable ++; } m_nMove++; sBoardState *pState = parrUndoStates.e(m_nMove); if((m_nMove == parrUndoStates.Size() - 1) && bWasEnd){ m_bTheEnd = TRUE; bWasEnd = FALSE; } int ix, nSize = pState->arrStates.Size(); nX_From = pState->nX_From; nY_From = pState->nY_From; nX_To = pState->nX_To; nY_To = pState->nY_To; FreeData(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = m_arrFigures.AppendNew(); pFig->m_nType = pState->arrStates.e(ix).m_nType; pFig->m_nColor = pState->arrStates.e(ix).m_nColor; pFig->m_nX = pState->arrStates.e(ix).m_nX; pFig->m_nY = pState->arrStates.e(ix).m_nY; } sGameStatus = pState->sGameStatus; nSize = pState->arrStatesDelWhite.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelWhite.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelWhite.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } nSize = pState->arrStatesDelBlack.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelBlack.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelBlack.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } if(m_pScene->IsTwoPlayers()){ bClicked = FALSE; m_pScene->m_nCurrentMoveColor = !m_pScene->m_nCurrentMoveColor; } } void CBoard::FreeData() { /* for(int ix = 0; ix < m_arrFigures.Size(); ix++){ m_arrFigures.e(ix)->m_arrVerts.FreeData(); m_arrFigures.e(ix)->m_arrFaces.FreeData(); m_arrFigures.e(ix)->m_arrNorms.FreeData(); } */ m_arrFigures.FreeData(); parrWhiteDelFigs.FreeData(); parrBlackDelFigs.FreeData(); } void CBoard::RemoveFromDeleteFigures(CFigure *pFigure) { int ix, nSize = parrWhiteDelFigs.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.e(ix); if((pFig->m_nType == pFigure->m_nType) && (pFig->m_nColor == pFigure->m_nColor) && (pFig->m_nX == pFigure->m_nX) && (pFig->m_nY == pFigure->m_nY)){ parrWhiteDelFigs.ShiftRemove(ix); return; } } nSize = parrBlackDelFigs.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.e(ix); if((pFig->m_nType == pFigure->m_nType) && (pFig->m_nColor == pFigure->m_nColor) && (pFig->m_nX == pFigure->m_nX) && (pFig->m_nY == pFigure->m_nY)){ parrBlackDelFigs.ShiftRemove(ix); return; } } } BOOL CBoard::IsRokirovkaAvailable(BOOL bOO, int nX, int nY) { Array m_arrAvaibleMove; GetAllMovesForAllFigures(m_arrAvaibleMove, !m_pScene->m_nCurrentMoveColor); if(bOO){ for(int ix = nX; ix < nX + 4; ix++){ for(int jx = 0; jx < m_arrAvaibleMove.Size(); jx++){ CPoint ptPos = m_arrAvaibleMove.e(jx); if((ix == ptPos.x) && (nY == ptPos.y)){ return FALSE; } } } return TRUE; }else{ for(int ix = nX; ix > nX - 5; ix--){ for(int jx = 0; jx < m_arrAvaibleMove.Size(); jx++){ CPoint ptPos = m_arrAvaibleMove.e(jx); if((ix == ptPos.x) && (nY == ptPos.y)){ return FALSE; } } } return TRUE; } } void CBoard::GetAllMovesForAllFigures(Array &m_arrAvaibleMove, BOOL bColor) { m_arrAvaibleMove.FreeData(); for(int ix = 0; ix < m_arrFigures.Size(); ix++){ CFigure *pFig = m_arrFigures.e(ix); int nX = pFig->m_nX; int nY = pFig->m_nY; if(pFig->m_nColor != bColor){ continue; } CFigure *pFigure = NULL; switch(pFig->m_nType){ case WS_PESHKA:{ int nSign = 1; if(bColor == WS_BLACK){ nSign = -1; } pFigure = GetFigureByPosition(nX - 1, nY + 1 * nSign); if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY + 1 * nSign)); } } pFigure = GetFigureByPosition(nX + 1, nY + 1 * nSign); if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY + 1 * nSign)); } } pFigure = GetFigureByPosition(nX, nY + 1 * nSign); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX, nY + 1 * nSign)); } if((((nY == 1) && (bColor == WS_WHITE) || ((nY == 6) && bColor == WS_BLACK))) && !GetFigureByPosition(nX, nY + 1 * nSign)/*empty field*/){ pFigure = GetFigureByPosition(nX, nY + 2 * nSign); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX, nY + 2 * nSign)); } } if(sGameStatus.bBeatField[bColor]){ CPoint ptPos = sGameStatus.arrPosBeatPeshka[bColor]; if(((ptPos.x == nX - 1) && (ptPos.y == nY + 1 * nSign)) || ((ptPos.x == nX + 1) && (ptPos.y == nY + 1 * nSign))){ m_arrAvaibleMove.Append(ptPos); } } break; } case WS_KON:{ pFigure = GetFigureByPosition(nX + 1, nY + 2); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY + 2)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY + 2)); } } pFigure = GetFigureByPosition(nX - 1, nY + 2); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY + 2)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY + 2)); } } pFigure = GetFigureByPosition(nX - 1, nY - 2); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY - 2)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY - 2)); } } pFigure = GetFigureByPosition(nX + 1, nY - 2); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY - 2)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY - 2)); } } pFigure = GetFigureByPosition(nX + 2, nY + 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 2, nY + 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 2, nY + 1)); } } pFigure = GetFigureByPosition(nX + 2, nY - 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 2, nY - 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 2, nY - 1)); } } pFigure = GetFigureByPosition(nX - 2, nY - 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 2, nY - 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 2, nY - 1)); } } pFigure = GetFigureByPosition(nX - 2, nY + 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 2, nY + 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 2, nY + 1)); } } break; } case WS_SLON:{ int nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } break; } case WS_TURA:{ int nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } break; } case WS_FERZ:{ int nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(++nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, --nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(--nXX, nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } nXX = nX, nYY = nY; while((pFigure = GetFigureByPosition(nXX, ++nYY)) == NULL){ if(!IsPosInBoard(nXX, nYY)){ break; } m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } if(pFigure){ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nXX, nYY)); } } break; } case WS_KOROL:{ pFigure = GetFigureByPosition(nX + 1, nY + 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY + 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY + 1)); } } pFigure = GetFigureByPosition(nX - 1, nY + 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY + 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY + 1)); } } pFigure = GetFigureByPosition(nX - 1, nY - 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY - 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY - 1)); } } pFigure = GetFigureByPosition(nX + 1, nY - 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY - 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY - 1)); } } pFigure = GetFigureByPosition(nX, nY + 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX, nY + 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX, nY + 1)); } } pFigure = GetFigureByPosition(nX, nY - 1); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX, nY - 1)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX, nY - 1)); } } pFigure = GetFigureByPosition(nX - 1, nY); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX - 1, nY)); } } pFigure = GetFigureByPosition(nX + 1, nY); if(!pFigure){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY)); }else{ if(pFigure->m_nColor == !bColor){ m_arrAvaibleMove.Append(CPoint(nX + 1, nY)); } } break; } } } } #include "bmp_file.h" void CBoard::LoadTexture() { BMP_File FileBMP; CString sWoodFile; sWoodFile.Format("%s\\Textures\\wood.bmp", m_pScene->sWorkDirectory); if(!FileBMP.Load(sWoodFile.GetBuffer(MAX_PATH))){ MessageBox(0, "Can't load image", "Board", 0); return; } glPixelStorei(GL_PACK_ALIGNMENT, 1); glGenTextures(1, &m_nTexture); glBindTexture(GL_TEXTURE_2D, m_nTexture); glEnable(GL_TEXTURE_2D); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, FileBMP.SizeX(), FileBMP.SizeX(), GL_RGB, GL_UNSIGNED_BYTE, &FileBMP.im.e(0,0)); sWoodFile.Format("%s\\Textures\\board.bmp", m_pScene->sWorkDirectory); if(!FileBMP.Load(sWoodFile.GetBuffer(MAX_PATH))){ MessageBox(0, "Can't load image", "Board", 0); return; } glPixelStorei(GL_PACK_ALIGNMENT, 1); glGenTextures(1, &m_nTexture_Board); glBindTexture(GL_TEXTURE_2D, m_nTexture_Board); glEnable(GL_TEXTURE_2D); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, FileBMP.SizeX(), FileBMP.SizeX(), GL_RGB, GL_UNSIGNED_BYTE, &FileBMP.im.e(0,0)); // 2D View Mode. Load all Bitmap Figures. int ix, nSz = m_arrFigures.Size(); for(ix = 0; ix < nSz; ix++){ CFigure *pFigure = m_arrFigures.e(ix); char buff[100]; if(pFigure->m_nColor == WS_WHITE){ switch(pFigure->m_nType){ case WS_PESHKA: strcpy(buff, ""); break; } } } } void CBoard::MoveInetOponent(char nXFrom, char nYFrom, char nXTo, char nYTo) { nX_From = nXFrom; nY_From = nYFrom; nX_To = nXTo; nY_To = nYTo; CFigure *pFigure = GetFigureByPosition(nXFrom, nYFrom); if(pFigure){ // Peshka changed Geometry! if(sGameStatus.bPeshkaChangeGeometry){ pFigure->m_nType = sGameStatus.nTypePeshkaNow; } // Rokirovka... if(pFigure->m_nType == WS_KOROL && fAbs(nXTo - nXFrom) == 2){ // 0-0 if(nXTo == 6){ CFigure *pFigTura = GetFigureByPosition(nXTo + 1, nYTo); if(m_pScene->m_bAnimation){ AnimationFigure(pFigTura, NULL, nXTo - 1, nYTo); } pFigTura->m_nX = nXTo - 1; pFigTura->m_nY = nYTo; } // 0-0-0 if(nXTo == 2){ CFigure *pFigTura = GetFigureByPosition(0, nYTo); if(m_pScene->m_bAnimation){ AnimationFigure(pFigTura, NULL, nXTo + 1, nYTo); } pFigTura->m_nX = nXTo + 1; pFigTura->m_nY = nYTo; } } CFigure *pFigure2 = GetFigureByPosition(nXTo, nYTo); // Prohodnaya Peshka. if((pFigure->m_nType == WS_PESHKA) && (pFigure2 == NULL) && (nXFrom != nXTo)){ int nCoeff = -1; if(pFigure->m_nColor == WS_BLACK){ nCoeff = 1; } pFigure2 = GetFigureByPosition(nXTo, nYTo + nCoeff); // Here Should be Another Peshka! } if(pFigure2){ if(m_pScene->m_bAnimation){ AnimationFigure(pFigure, pFigure2, nXTo, nYTo); } RemoveFigure(pFigure2); }else{ if(m_pScene->m_bAnimation){ AnimationFigure(pFigure, NULL, nXTo, nYTo); } } pFigure->m_nX = nXTo; pFigure->m_nY = nYTo; } int nState; m_pScene->lpfnGetState(&m_arrFigures, m_pScene->m_nCurrentMoveColor, sGameStatus, nState); switch(nState){ case WS_CHECK: m_pScene->PlaySound(WS_CHECK); break; case WS_MATE: m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("Mate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; case WS_PAT: m_pScene->PlaySound(WS_MATE); m_pScene->m_bStartGame = FALSE; AfxMessageBox("StaleMate!"); m_bTheEnd = TRUE; CreateUndo(); return; break; default: m_pScene->PlaySound(WS_MOVE); break; } MoveDone(); } void CBoard::SendMessage() { if(!m_pScene->m_bConnected){ return; } if(m_pScene->m_bServer){ m_pScene->SendMessageFromServer(WS_CLIENT_MOVE, nX_From, nY_From, nX_To, nY_To); }else{ m_pScene->SendMessageFromClient(WS_CLIENT_MOVE, nX_From, nY_From, nX_To, nY_To); } } void CBoard::MoveDone() { if(nCurrentTable < m_pcMoveTable->m_parrStrMoves.Size()){ for(int ix = nCurrentTable; ix < m_pcMoveTable->m_parrStrMoves.Size(); ix++){ m_pcMoveTable->m_parrStrMoves.ShiftRemove(ix); ix--; } } CString s; s.Format("%s - %s", GetStrByPos(nX_From, nY_From), GetStrByPos(nX_To, nY_To)); sMove *pMove = m_pcMoveTable->m_parrStrMoves.AppendNew(); pMove->sStr = s; pMove->nXFrom = nX_From; pMove->nYFrom = nY_From; pMove->nXTo = nX_To; pMove->nYTo = nY_To; nCurrentTable ++; } CString CBoard::GetStrByPos(int x, int y) { CString sRet; char cXOff = 'A'; char cYOff = '1'; sRet.Format("%c%c", cXOff + x, cYOff + y); return sRet; } void CBoard::StartUndo() { if(m_bThinking){ return; } bWasEnd = m_bTheEnd; m_bTheEnd = FALSE; m_pcMoveTable->nHeadOfList = 0; nCurrentTable = m_nMove = 0; sBoardState *pState = parrUndoStates.e(m_nMove); int ix, nSize = pState->arrStates.Size(); FreeData(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = m_arrFigures.AppendNew(); pFig->m_nType = pState->arrStates.e(ix).m_nType; pFig->m_nColor = pState->arrStates.e(ix).m_nColor; pFig->m_nX = pState->arrStates.e(ix).m_nX; pFig->m_nY = pState->arrStates.e(ix).m_nY; } sGameStatus = pState->sGameStatus; nX_From = pState->nX_From; nY_From = pState->nY_From; nX_To = pState->nX_To; nY_To = pState->nY_To; nSize = pState->arrStatesDelWhite.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelWhite.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelWhite.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } nSize = pState->arrStatesDelBlack.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelBlack.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelBlack.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } m_pScene->m_nCurrentMoveColor = WS_WHITE; if(m_pScene->IsOnePlayer() && (m_pScene->m_nPlayerColor == WS_BLACK)){ m_pScene->m_nCurrentMoveColor = WS_BLACK; nCurrentTable = 1; } } void CBoard::EndUndo() { if(m_bThinking){ return; } if(bWasEnd){ m_bTheEnd = TRUE; bWasEnd = FALSE; } m_pcMoveTable->nHeadOfList = 0; m_nMove = parrUndoStates.Size() - 1; nCurrentTable = m_pcMoveTable->m_parrStrMoves.Size(); sBoardState *pState = parrUndoStates.e(m_nMove); int ix, nSize = pState->arrStates.Size(); FreeData(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = m_arrFigures.AppendNew(); pFig->m_nType = pState->arrStates.e(ix).m_nType; pFig->m_nColor = pState->arrStates.e(ix).m_nColor; pFig->m_nX = pState->arrStates.e(ix).m_nX; pFig->m_nY = pState->arrStates.e(ix).m_nY; } sGameStatus = pState->sGameStatus; nX_From = pState->nX_From; nY_From = pState->nY_From; nX_To = pState->nX_To; nY_To = pState->nY_To; nSize = pState->arrStatesDelWhite.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelWhite.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelWhite.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } nSize = pState->arrStatesDelBlack.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelBlack.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelBlack.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } if(m_pScene->IsTwoPlayers()){ bClicked = FALSE; if(m_nMove % 2){ m_pScene->m_nCurrentMoveColor = WS_BLACK; }else{ m_pScene->m_nCurrentMoveColor = WS_WHITE; } } } void CBoard::Play(void) { ShowCursor(FALSE); if(bWasEnd){ m_bTheEnd = TRUE; bWasEnd = FALSE; } m_pcMoveTable->nHeadOfList = 0; m_nMove = nCurrentTable = 0; for(int kx = 0; kx < parrUndoStates.Size(); kx++){ m_nMove = kx; if(m_pScene->m_bAnimation){ if(kx){ if(m_pScene->IsOnePlayer()){ sMove *pMove = m_pcMoveTable->m_parrStrMoves.e(nCurrentTable - 2); CFigure *pFigMove = GetFigureByPosition(pMove->nXFrom, pMove->nYFrom); CFigure *pFigDel = GetFigureByPosition(pMove->nXTo, pMove->nYTo); AnimationFigure(pFigMove, pFigDel, pMove->nXTo, pMove->nYTo); pFigMove->m_nX = pMove->nXTo; pFigMove->m_nY = pMove->nYTo; if(pFigDel){ RemoveFigure(pFigDel); } pMove = m_pcMoveTable->m_parrStrMoves.e(nCurrentTable - 1); if(pMove){ pFigMove = GetFigureByPosition(pMove->nXFrom, pMove->nYFrom); pFigDel = GetFigureByPosition(pMove->nXTo, pMove->nYTo); AnimationFigure(pFigMove, pFigDel, pMove->nXTo, pMove->nYTo); } }else{ sMove *pMove = m_pcMoveTable->m_parrStrMoves.e(nCurrentTable - 1); CFigure *pFigMove = GetFigureByPosition(pMove->nXFrom, pMove->nYFrom); CFigure *pFigDel = GetFigureByPosition(pMove->nXTo, pMove->nYTo); AnimationFigure(pFigMove, pFigDel, pMove->nXTo, pMove->nYTo); } } } sBoardState *pState = parrUndoStates.e(m_nMove); int ix, nSize = pState->arrStates.Size(); FreeData(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = m_arrFigures.AppendNew(); pFig->m_nType = pState->arrStates.e(ix).m_nType; pFig->m_nColor = pState->arrStates.e(ix).m_nColor; pFig->m_nX = pState->arrStates.e(ix).m_nX; pFig->m_nY = pState->arrStates.e(ix).m_nY; } sGameStatus = pState->sGameStatus; nX_From = pState->nX_From; nY_From = pState->nY_From; nX_To = pState->nX_To; nY_To = pState->nY_To; nSize = pState->arrStatesDelWhite.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrWhiteDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelWhite.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelWhite.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } nSize = pState->arrStatesDelBlack.Size(); for(ix = 0; ix < nSize; ix++){ CFigure *pFig = parrBlackDelFigs.AppendNew(); pFig->m_nType = pState->arrStatesDelBlack.e(ix).m_nType; pFig->m_nColor = pState->arrStatesDelBlack.e(ix).m_nColor; pFig->m_nX = pFig->m_nY = 0; } if(m_pScene->IsTwoPlayers()){ bClicked = FALSE; if(m_nMove % 2){ m_pScene->m_nCurrentMoveColor = WS_BLACK; }else{ m_pScene->m_nCurrentMoveColor = WS_WHITE; } } m_pScene->Draw(); _sleep(300); nCurrentTable++; if(m_pScene->IsOnePlayer()){ nCurrentTable ++; } } ShowCursor(TRUE); }