Доброго времени суток. На летней практике мне дали задание написать игру похожую Lines. Смысл игры:
Есть поле шариков 11 на 14. они 6ти цветов. При нажатии мышкой на шарик происходит проверка соседних шариков на тот же цвет, и если соседний шарик этого же цвета, то удалить эти шарики. Естественно это дело должно осуществляться по средствам рекурсии.
Проблема: Рекурсия поднятия флага удаления не работает ( отчаиваюсь уже неделю ) Запись истории какая точка уже проверялась рекурсией вроде идет правильно.
Я вырезал из проекта только эти функции и сейчас работаю с матрицами структур шариков. Прошу помощи найти ошибку. Или посоветовать другой алгоритм поднятия флага удаления. Вот листинг файла DeleteSphere.cpp
Код:
#include "stdafx.h"
#include "iostream"
#include "locale.h"
#include "stdlib.h"
#include "stdio.h"
#include "ctime"
using namespace std;
struct object
{
int nNumb;
int nColor;
bool FlagDelete;
};
void DeleteSphere( int x, int y, object **MatrixPoint );
bool CheckWasSphere( object localSphere );
int delCount = 0;
int NumberDeletedSpheres[154];
void main()
{
int iCount, jCount;
object **Point;
int X,Y;
int NUMB = 0;
Point = new object*[ 11 ];
for ( iCount = 0; iCount < 11; iCount++)
Point[ iCount ] = new object[ 14 ];
srand( time(NULL));
// НУМЕРАЦИЯ СТРОК И СТОЛБЦОВ НАЧИНАЕТСЯ С 0
// iCount - строки
// jCount - столбцы
// x - строка, y - столбец
// забиваем матрицу структур значениями
for ( iCount = 0; iCount < 11; iCount++ )
{
for ( jCount = 0; jCount < 14; jCount++ )
{
Point[ iCount ][ jCount ].nColor = (int) rand()%3;
Point[ iCount ][ jCount ].nNumb = NUMB;
Point[ iCount ][ jCount ].FlagDelete = false;
NUMB++;
cout << Point[ iCount ][ jCount ].nColor << " ";
}
cout << endl;
}
cout << endl << endl;
// выводим матрицу флагов
for ( iCount = 0; iCount < 11; iCount++ )
{
for ( jCount = 0; jCount < 14; jCount++ )
cout << Point[ iCount ][ jCount ].FlagDelete <<" " ;
cout << endl;
}
cin >> X >> Y;
cout << endl << Point[ X ][ Y ].nNumb << " " << X << " " << Y << " " << Point[ X ][ Y ].nColor << endl ;
DeleteSphere( X, Y, Point ); // грёбаная рекурския
// заново выводим матрицу флагов
for ( iCount = 0; iCount < 11; iCount++ )
{
for ( jCount = 0; jCount < 14; jCount++ )
cout << Point[ iCount ][ jCount ].FlagDelete <<" " ;
cout << endl;
}
cout << endl;
cout << endl;
// заново выводим матрицу цветов
for ( iCount = 0; iCount < 11; iCount++ )
{
for ( jCount = 0; jCount < 14; jCount++ )
{
if ( Point[ iCount ][ jCount ].FlagDelete == true )
{
Point[ iCount ][ jCount ].nColor = 8; //
cout << " ";
}
else cout << Point[ iCount ][ jCount ].nColor <<" " ;
}
cout << endl;
}
}
void DeleteSphere( int x, int y, object **MatrixPoint )
{
if ( x-1 >= 0 )
if ( CheckWasSphere( MatrixPoint[ x-1 ][ y ] ) )
if( MatrixPoint[ x ][ y ].nColor == MatrixPoint[ x-1 ][ y ].nColor )
{
cout << endl << endl << x - 1 << " "<< y << endl;
MatrixPoint[x][y].FlagDelete = true;
MatrixPoint[x-1][y].FlagDelete = true;
DeleteSphere ( x-1 , y, MatrixPoint );
}
if ( x+1 < 11 )
if ( CheckWasSphere ( MatrixPoint[ x+1 ][ y ] ) )
if( MatrixPoint[ x ][ y ].nColor == MatrixPoint[ x+1 ][ y ].nColor )
{
cout << endl << endl << x + 1 << " "<< y << endl;
MatrixPoint[x][y].FlagDelete = true;
MatrixPoint[x+1][y].FlagDelete = true;
DeleteSphere ( x-1 , y, MatrixPoint );
}
if ( y-1 >= 0 )
if ( CheckWasSphere ( MatrixPoint[ x ][ y - 1 ] ) )
if( MatrixPoint[ x ][ y ].nColor == MatrixPoint[ x ][ y-1 ].nColor )
{
cout << endl << endl << x << " "<< y - 1 << endl;
MatrixPoint[x][y].FlagDelete = true;
MatrixPoint[x][y-1].FlagDelete = true;
DeleteSphere ( x-1 , y, MatrixPoint );
}
if ( y+1 < 14 )
if ( CheckWasSphere ( MatrixPoint[ x ][ y + 1 ] ) )
if( MatrixPoint[ x ][ y ].nColor == MatrixPoint[ x ][ y+1 ].nColor )
{
cout << endl << endl << x << " "<< y + 1 << endl;
MatrixPoint[x][y].FlagDelete = true;
MatrixPoint[x][y+1].FlagDelete = true;
DeleteSphere ( x-1 , y, MatrixPoint );
}
return;
}
bool CheckWasSphere( object localSphere )
{
int i, delCountBuf = delCount;
for ( i = 0; i < delCountBuf ; i++)
{
if ( NumberDeletedSpheres[ i ] == localSphere.nNumb )
return false;// был
else
{
delCount++;
NumberDeletedSpheres[ delCount - 1 ] = localSphere.nNumb;
}
}
return true;
}
}