Скачиваний:
30
Добавлен:
01.05.2014
Размер:
1.56 Кб
Скачать
#include "StdAfx.h"
#include "KardanoSquare.h"
#include <stdlib.h>

KardanoSquare::KardanoSquare(void)
{
   size = 4;
   squareData = new char[size*size];
}

KardanoSquare::~KardanoSquare(void)
{
   delete[] squareData;
}

int KardanoSquare::SetSize(const int NewSize)
{
   if (NewSize >=4 && NewSize <= 64)
   {
      size = NewSize;
      delete[] squareData;
      squareData = new char[size*size];
      Generate();
      return 1;
   }
   else return 0;
}

int KardanoSquare::GetSize() const
{
   return size;
}

void KardanoSquare::Generate()
{
   for (int k=0; k<size*size; k++) squareData[k] = 0;
   int i = rand()%size, j = rand()%size;
   int c;
   for (int k=0; k<size*size/4; k++)
   {
      c = 0;
      while (c<5 && squareData[i*size+j]!=0)
      {
         i = rand()%size;
         j = rand()%size;
         c++;
      }
      if (squareData[i*size+j]!=0)
      {
         c = 0;
         while (squareData[c]!=0) c++;
         i = c/size;
         j = c%size;
      }
      squareData[i*size+j] = 0x01;
      squareData[(size-1-j)*size+i] = 0x02;
      squareData[(size-1-i)*size+(size-1-j)] = 0x03;
      squareData[j*size+(size-1-i)]= 0x04;
   }
}

void KardanoSquare::Rotate()
{
   for (int i=0; i<size*size; i++)
      squareData[i] = (squareData[i]==4) ? 1 : squareData[i]+1;
}

int KardanoSquare::GetValue(const int Row, const int Col) const
{
   if (Row >= 0 && Row <= size && Col >= 0 && Col <= size)
      return squareData[Row*size+Col];
   else return 0;
}
Соседние файлы в папке Kardano