Example #1
0
// Called every frame
void ACSUEBomb::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	if (bombMesh)
		checkOverlap();

}
Example #2
0
int checkCont(SHPObject *a, SHPObject *b)
{
  int jLim;
  int hLim;
  int i,j,k,h;
  for(i=0;i<a->nParts;i++) //accounts for multiple polygons
    {
      if(i==a->nParts-1){
        jLim=a->nVertices-1;
      }
      else{
        jLim=a->panPartStart[i+1]-2;
      }
		
      for(j=a->panPartStart[i];j<jLim;j++)
        {
          for(k=0;k<b->nParts;k++){
            if(j==b->nParts-1){
              hLim=b->nVertices-1;
            }
            else{
              hLim=b->panPartStart[k+1]-2;
            }
            for(h=b->panPartStart[k];h<hLim;h++){
              if( checkOverlap(a->padfX[j], a->padfY[j], a->padfX[j+1],a->padfY[j+1],b->padfX[h], b->padfY[h], b->padfX[h+1],b->padfY[h+1])){
              //if(a->nShapeId == 20 || b->nShapeId == 20) {printf("\n 20 contig returning true for %d and %d \n", a->nShapeId, b->nShapeId); }
                return TRUE;
              }
            }
          }
        }
    }
     return FALSE;
}
int solution(int A[], int B[], int N) {
    // write your code in C99 (gcc 4.8.2)
    int count = 1;
    int i = 0;
    int currS, currE;
    
    if (N <= 1) return N;

    currS = A[N - 1];
    currE = B[N - 1];
    for (i = N - 2; i >= 0; i--) { 
       
        if (checkOverlap(currS, currE, A[i], B[i])) {
            if(currS < A[i]) {
                currS = A[i];
                currE = B[i];
            }
        }
        else {
            count++;
            currS = A[i];
            currE = B[i];
        }
    }
        
    return count;
}
Example #4
0
int Pinsert(double x, double y)
{
  int i;
  for (i=0; i<Nm; i++)
  {
    if (checkOverlap(i, x, y)) return 0;
  }
  return 1;
}
Example #5
0
void CS::collisionUpdate(){
	//qt.clear();
	//qt.updateBounds(&worldbounds);
	//std::cout << "CRASHED?!1" << std::endl;
	grid.updateBounds(&worldbounds);
	grid.clear();
	for(auto checking = collisionCS.begin(); checking != collisionCS.end(); checking++){
		//qt.insert(checking->first);
		checking->second->getGridIndex(grid);
	}
	Rect area;
	int n=0;
	//std::cout << "CRASHED?!2" << std::endl;

	for(auto checking = collisionCS.begin(); checking != collisionCS.end(); checking++){
		if(!CS::collisionCS[checking->first]->moveable)
			continue;
		std::vector<eId> entities;
		std::map<float, eId> overlapedMap;
		//if(!qt.getObject(entities, checking->first))
		//	continue;
		//std::map<eId, float> areas;
		float maxArea=0;
		eId maxAreaID;
		bool collided = false;
		entities = grid.getEntities(checking->second->gridIndex);
		for (auto it = entities.begin(); it != entities.end(); ++it){
			if(*it == checking->first)
				continue;
			if(!CS::collisionCS[*it]->moveable && !CS::collisionCS[checking->first]->moveable)
				continue;
			if(checkOverlap(checking->first, *it, &area)){
				checking->second->overlapingWith.push_back(*it);
				collisionCS[*it]->overlapingWith.push_back(checking->first);
				checking->second->overlaped = true;
				CS::collisionCS[*it]->overlaped = true;
				if(CS::collisionCS[*it]->solid && CS::collisionCS[checking->first]->solid){
					checking->second->collided = true;
					CS::collisionCS[*it]->collided = true;
					collided = true;
					overlapedMap[area.w*area.h] = *it;
				}
			}
		}
		for(auto it = overlapedMap.rbegin(); it != overlapedMap.rend(); ++it)
			collide(checking->first, it->second);
		overlapedMap.clear();
	}
	//qt.draw();
	//grid.draw();
	//Window::DrawRect(&CS::worldbounds, 255, 0, 0);
	if(nc != n)
	std::cout << n << std::endl;
	nc = n;
}
Example #6
0
void callOverlapTestCode()
{
  int test1, test2, test3, test4, test5, test6;	


  test1 = checkOverlap(3, 3.5, 2, 3, 1, 2.5, 4, 4);  //should return true

  test2 = checkOverlap(1, 2.5, 2, 3, 3, 3.5, 4, 4);  //should return false

  test3 = checkOverlap(1,2.5,3, 3.5, 2, 3, 4, 4);    //should return true

  test4 = checkOverlap(4, 0, 10, 0, 4, 0, 10, 0);    //should return true

  test5 = checkOverlap(0,4,0,10,0,5,0,11);	//should return true

  test6 = checkOverlap(1,1,-1,-1,-1,1,1,-1);	//should return false

  printf("test1 should be true: %i\n",test1);
  printf("test2 should be false: %i\n",test2);
  printf("test3 should be true: %i\n",test3);
  printf("test4 should be true: %i\n",test4);
  printf("test5 should be true: %i\n",test5);
  printf("test6 should be false: %i\n",test6);



}
void metaCollect(char *metaDb, char *reqViewsName, 
    char *expVarsName)
/* metaCollect - build collections using metadata. */
{
boolean validated;
struct mdbObj *mdbObjs = mdbObjsLoadFromFormattedFile(metaDb, &validated);
struct slName *expVars = slNameLoadReal(expVarsName);
struct slName *reqViews = slNameLoadReal(reqViewsName);

struct group *groups = groupByExpVar(mdbObjs, expVars);

checkOverlap(mdbObjs, groups);

checkForReqView(groups, reqViews);
}
Example #8
0
void CCLayerSorter::createGraphEdges()
{
#if !defined( NDEBUG )
    LOG(CCLayerSorter, "Edges:\n");
#endif
    // Fraction of the total zRange below which z differences
    // are not considered reliable.
    const float zThresholdFactor = 0.01;
    float zThreshold = m_zRange * zThresholdFactor;

    for (unsigned na = 0; na < m_nodes.size(); na++) {
        GraphNode& nodeA = m_nodes[na];
        if (!nodeA.layer->drawsContent() && !nodeA.layer->renderSurface())
            continue;
        for (unsigned nb = na + 1; nb < m_nodes.size(); nb++) {
            GraphNode& nodeB = m_nodes[nb];
            if (!nodeB.layer->drawsContent() && !nodeB.layer->renderSurface())
                continue;
            float weight = 0;
            ABCompareResult overlapResult = checkOverlap(&nodeA.shape, &nodeB.shape, zThreshold, weight);
            GraphNode* startNode = 0;
            GraphNode* endNode = 0;
            if (overlapResult == ABeforeB) {
                startNode = &nodeA;
                endNode = &nodeB;
            } else if (overlapResult == BBeforeA) {
                startNode = &nodeB;
                endNode = &nodeA;
            }

            if (startNode) {
#if !defined( NDEBUG )
                LOG(CCLayerSorter, "%d -> %d\n", startNode->layer->debugID(), endNode->layer->debugID());
#endif
                m_edges.append(GraphEdge(startNode, endNode, weight));
            }
        }
    }

    for (unsigned i = 0; i < m_edges.size(); i++) {
        GraphEdge& edge = m_edges[i];
        m_activeEdges.add(&edge, &edge);
        edge.from->outgoing.append(&edge);
        edge.to->incoming.append(&edge);
        edge.to->incomingEdgeWeight += edge.weight;
    }
}
Example #9
0
static struct cdsExon *loadExon(struct gene *gene, struct binKeeper *chrBins,
                                struct genePred *gp, int iExon, int start, int end,
                                int cdsOff)
/* load information about an exon into various structures */
{
struct cdsExon *exon;
checkOverlap(gene->genes, chrBins, gp, start, end);
lmAllocVar(gene->genes->memPool, exon);
exon->gene = gene;
exon->chromStart = start;
exon->chromEnd = end;
exon->frame = gp->exonFrames[iExon];
if (exon->gene->strand == '+')
    exon->exonNum = iExon;
else
    exon->exonNum = (gp->exonCount-1) - iExon;
exon->cdsOff = cdsOff;
binKeeperAdd(chrBins, start, end, exon);
slAddHead(&gene->exons, exon);
return exon;
}
Example #10
0
void CCLayerSorter::createGraphEdges()
{
#if !defined( NDEBUG )
    LOG(CCLayerSorter, "Edges:\n");
#endif
    for (unsigned na = 0; na < m_nodes.size(); na++) {
        GraphNode& nodeA = m_nodes[na];
        if (!nodeA.layer->drawsContent() && !nodeA.layer->renderSurface())
            continue;
        for (unsigned nb = na + 1; nb < m_nodes.size(); nb++) {
            GraphNode& nodeB = m_nodes[nb];
            if (!nodeB.layer->drawsContent() && !nodeB.layer->renderSurface())
                continue;
            ABCompareResult overlapResult = checkOverlap(&nodeA, &nodeB);
            GraphNode* startNode = 0;
            GraphNode* endNode = 0;
            if (overlapResult == ABeforeB) {
                startNode = &nodeA;
                endNode = &nodeB;
            } else if (overlapResult == BBeforeA) {
                startNode = &nodeB;
                endNode = &nodeA;
            }

            if (startNode) {
#if !defined( NDEBUG )
                LOG(CCLayerSorter, "%d -> %d\n", startNode->layer->debugID(), endNode->layer->debugID());
#endif
                m_edges.append(GraphEdge(startNode, endNode));
            }
        }
    }

    for (unsigned i = 0; i < m_edges.size(); i++) {
        GraphEdge& edge = m_edges[i];
        m_activeEdges.add(&edge, &edge);
        edge.from->outgoing.append(&edge);
        edge.to->incoming.append(&edge);
    }
}
Example #11
0
int		placeHeros(t_hero *heros, int nbHeros)
{
  t_hero	*tmp;
  int		offset;
  int		totalSize;
  int		loop;

  totalSize = calcSize(heros);
  if (totalSize > MEM_SIZE)
    return (write(2, "Not enough space to place heros\n", 32), 1);
  offset = MEM_SIZE / nbHeros;
  tmp = heros;
  loop = 0;
  while (tmp)
    {
      if (tmp->customAddress == false)
	tmp->loadAddress = offset * loop;
      tmp->loadAddress = tmp->loadAddress % MEM_SIZE;
      tmp = tmp->next;
      ++loop;
    }
  return (checkOverlap(heros));
}
void perturb()
{ 
  int i;
  int m;
  double deltax, deltay;
  double newx, newy;

  counter++;

  m = RND()*n;
  deltax =RND()*plength-plength/2;
  deltay =RND()*plength-plength/2;
  newx=X[m]+deltax;
  newy=Y[m]+deltay;

  if (newx>L) return;
  if (newy>L) return;
  if (newx<0) return;
  if (newy<0) return;

  for (i=0; i<n; i++)
  {
    if (i!=m)
    if (checkOverlap(i, newx, newy))
    {
      //printf("perturbation failed\n");
      return;
    }
  }

  // success:
  X[m] = newx;
  Y[m] = newy;
  //printf("!");
  //printf("%d:  success at %lf, %lf\n", n, newx, newy);
  return;
}
Example #13
0
void examRummy(const CardRecord* const record, const int numOfCard, RummyResult* const result) {
	int i, j;
	int p, val, atteptedRunNumber, temp, m, n, numOfResult = 0;
	
	int bJockerNum = record[15].count;
	int rJockerNum = record[16].count;
	
	int runCopy[18] = {0};

	int tempRecord[17];
	int tempRecordBackUp[17];


	for (i = 0; i < 17; i++) {
		tempRecord[i] = tempRecordBackUp[i] = record[i].count;
	}

	RummyResult results[1014] = { { 0, { 0 }, 0, { 0 }, 0, 0, 0 } };

	for (m = 14; m >= 2; m--) {

		if (rJockerNum > 0) {
			tempRecord[m + 1]--;
			tempRecordBackUp[m + 1]--;
			tempRecord[m]++;
			tempRecordBackUp[m]++;
		} else m = 2;

		for (n = 14; n >= 2; n--) {

			for (i = 0; i < 15; i++) {
				tempRecord[i] = tempRecordBackUp[i];
			}

			p = 0;
			results[numOfResult].isRummy = 0;
			results[numOfResult].numOfSet = 0;
			results[numOfResult].numOfRun = 0;
			results[numOfResult].bJocker = 0;
			results[numOfResult].rJocker = 0;

			if (rJockerNum > 0) {
				results[numOfResult].rJocker = m;
			}

			if (bJockerNum > 0) {
				if (m < 14 && n == 14) {
					tempRecord[2]--;
					tempRecordBackUp[2]--;
				}
				tempRecord[n + 1]--;
				tempRecordBackUp[n + 1]--;
				tempRecord[n]++;
				tempRecordBackUp[n]++;
				results[numOfResult].bJocker = n;
			} else n = 2;

			for (i = 14; i >= 2 && results[numOfResult].numOfSet != 3; i--) {
				if (tempRecord[i] >= 3) {
                    tempRecord[i] -=3;
                    tempRecordBackUp[i] -=3;
					results[numOfResult].set[p++] = i;
					results[numOfResult].numOfSet++;
				}
			}
			for (j = 0; j < 3; j++) {
                
                if (j > 0) {
                    results[numOfResult].isRummy = 0;
                    results[numOfResult].numOfSet = results[numOfResult - 1].numOfSet;
                    results[numOfResult].bJocker = results[numOfResult - 1].bJocker;
                    results[numOfResult].rJocker = results[numOfResult - 1].rJocker;
                    memcpy(results[numOfResult].set, results[numOfResult - 1].set, sizeof(results[numOfResult - 1].set));
                }
                
				p = 0;
				results[numOfResult].numOfRun = 0;
				for (i = 0; i < 15; i++) {
					tempRecord[i] = tempRecordBackUp[i];
				}
				for (i = 13 - j; i >= 2; i--) {
					if (tempRecord[i] >= 1 && tempRecord[i + 1] >= 1 && (i == 2 ? tempRecord[14] >= 1 : tempRecord[i - 1] >= 1)) {
						tempRecord[i]--;
						tempRecord[i + 1]--;
						tempRecord[i == 2 ? 14 : i - 1]--;
						results[numOfResult].run[p++] = i;
						results[numOfResult].numOfRun++;
						i++;
					}
				}

                memcpy(runCopy, results[numOfResult].run, sizeof(results[numOfResult].run));
                for (i = 0; i < 15; i++) {
                    tempRecord[i] = tempRecordBackUp[i];
                }

                if (results[numOfResult].numOfSet == 3) {
                    results[numOfResult].isRummy = 1;
                    results[numOfResult].numOfRun = 0;
                    numOfResult++;
                    continue;
                }
                else if (results[numOfResult].numOfSet == 2) {
                    val = checkOverlap(tempRecord, &results[numOfResult], 2);
                    if (val == 1) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 1;
                        numOfResult++;
                        continue;
                    }

                    memcpy(results[numOfResult].run, runCopy, sizeof(runCopy));
                    val = checkOverlap(tempRecord, &results[numOfResult], 1);
                    if (val == 2) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfSet = 1;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }

                    memcpy(results[numOfResult].run, runCopy, sizeof(runCopy));
                    temp = results[numOfResult].set[0];
                    results[numOfResult].set[0] = results[numOfResult].set[1];
                    val = checkOverlap(tempRecord, &results[numOfResult], 1);
                    if (val == 2) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfSet = 1;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }
                    if (results[numOfResult].numOfRun >= 3) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfSet = 0;
                        results[numOfResult].numOfRun = 3;
                        numOfResult++;
                        continue;
                    }
                    results[numOfResult].set[0] = temp;
                    results[numOfResult].isRummy = 0;
                    results[numOfResult].numOfSet = 2;
                    results[numOfResult].numOfRun = 0;
                    numOfResult++;
                    continue;
                }
                else if (results[numOfResult].numOfSet == 1) {
                    val = checkOverlap(tempRecord, &results[numOfResult], 1);
                    if (val == 2) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }
                    else if (results[numOfResult].numOfRun >= 3) {
                        results[numOfResult].numOfSet = 0;
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 3;
                        numOfResult++;
                        continue;
                    }
                    else if (val == 1) {					
                        results[numOfResult].numOfRun = 1;
                        numOfResult++;
                        continue;
                    }
                    else if (results[numOfResult].numOfRun == 2) {
                        memcpy(results[numOfResult].run, runCopy, sizeof(runCopy));
                        results[numOfResult].numOfSet = 0;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }
                    else {
                        results[numOfResult].numOfSet = 1;
                        results[numOfResult].numOfRun = 0;
                        numOfResult++;
                        continue;
                    }
                }
                else {
                    if (results[numOfResult].numOfRun >= 3) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 3;
                        numOfResult++;
                        continue;
                    }
                    else {
                        numOfResult++;
                        continue;
                    }
                }
            }
		}
	}
    
    for (i = 0; i < 17; i++) {
		tempRecord[i] = tempRecordBackUp[i] = record[i].count;
	}
    
    for (m = 14; m >= 2; m--) {

		if (rJockerNum > 0) {
			tempRecord[m + 1]--;
			tempRecordBackUp[m + 1]--;
			tempRecord[m]++;
			tempRecordBackUp[m]++;
		} else m = 2;

		for (n = 14; n >= 2; n--) {

			for (i = 0; i < 15; i++) {
				tempRecord[i] = tempRecordBackUp[i];
			}

			results[numOfResult].isRummy = 0;
			results[numOfResult].numOfSet = 0;
			results[numOfResult].numOfRun = 0;
			results[numOfResult].bJocker = 0;
			results[numOfResult].rJocker = 0;

			if (rJockerNum > 0) {
				results[numOfResult].rJocker = m;
			}

			if (bJockerNum > 0) {
				if (m < 14 && n == 14) {
					tempRecord[2]--;
					tempRecordBackUp[2]--;
				}
				tempRecord[n + 1]--;
				tempRecordBackUp[n + 1]--;
				tempRecord[n]++;
				tempRecordBackUp[n]++;
				results[numOfResult].bJocker = n;
			} else n = 2;

			for (j = 0; j < 3; j++) {
                
                if (j > 0) {
                    results[numOfResult].isRummy = 0;
                    results[numOfResult].bJocker = results[numOfResult - 1].bJocker;
                    results[numOfResult].rJocker = results[numOfResult - 1].rJocker;
                }
                
				p = 0;
                results[numOfResult].numOfSet = 0;
				results[numOfResult].numOfRun = 0;
				for (i = 0; i < 15; i++) {
					tempRecord[i] = tempRecordBackUp[i];
				}
				for (i = 13 - j; i >= 2; i--) {
					if (tempRecord[i] >= 1 && tempRecord[i + 1] >= 1 && (i == 2 ? tempRecord[14] >= 1 : tempRecord[i - 1] >= 1)) {
						tempRecord[i]--;
						tempRecord[i + 1]--;
						tempRecord[i == 2 ? 14 : i - 1]--;
						results[numOfResult].run[p++] = i;
						results[numOfResult].numOfRun++;
						i++;
					}
				}

                memcpy(runCopy, results[numOfResult].run, sizeof(results[numOfResult].run));
                
                p = 0;
                
                for (i = 14; i >= 2 && results[numOfResult].numOfSet != 3; i--) {
				    if (tempRecord[i] >= 3) {
					   results[numOfResult].set[p++] = i;
					   results[numOfResult].numOfSet++;
				    }   
			    }
                
                for (i = 0; i < 15; i++) {
                    tempRecord[i] = tempRecordBackUp[i];
                }
                            
                if (results[numOfResult].numOfSet == 3) {
                    results[numOfResult].isRummy = 1;
                    results[numOfResult].numOfRun = 0;
                    numOfResult++;
                    continue;
                }
                else if (results[numOfResult].numOfSet == 2) {
                    val = checkOverlap(tempRecord, &results[numOfResult], 2);
                    if (val == 1) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 1;
                        numOfResult++;
                        continue;
                    }

                    memcpy(results[numOfResult].run, runCopy, sizeof(runCopy));
                    val = checkOverlap(tempRecord, &results[numOfResult], 1);
                    if (val == 2) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfSet = 1;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }

                    memcpy(results[numOfResult].run, runCopy, sizeof(runCopy));
                    temp = results[numOfResult].set[0];
                    results[numOfResult].set[0] = results[numOfResult].set[1];
                    val = checkOverlap(tempRecord, &results[numOfResult], 1);
                    if (val == 2) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfSet = 1;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }
                    if (results[numOfResult].numOfRun >= 3) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfSet = 0;
                        results[numOfResult].numOfRun = 3;
                        numOfResult++;
                        continue;
                    }
                    results[numOfResult].set[0] = temp;
                    results[numOfResult].isRummy = 0;
                    results[numOfResult].numOfSet = 2;
                    results[numOfResult].numOfRun = 0;
                    numOfResult++;
                    continue;
                }
                else if (results[numOfResult].numOfSet == 1) {
                    val = checkOverlap(tempRecord, &results[numOfResult], 1);
                    if (val == 2) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }
                    else if (results[numOfResult].numOfRun >= 3) {
                        results[numOfResult].numOfSet = 0;
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 3;
                        numOfResult++;
                        continue;
                    }
                    else if (val == 1) {					
                        results[numOfResult].numOfRun = 1;
                        numOfResult++;
                        continue;
                    }
                    else if (results[numOfResult].numOfRun == 2) {
                        memcpy(results[numOfResult].run, runCopy, sizeof(runCopy));
                        results[numOfResult].numOfSet = 0;
                        results[numOfResult].numOfRun = 2;
                        numOfResult++;
                        continue;
                    }
                    else {
                        results[numOfResult].numOfSet = 1;
                        results[numOfResult].numOfRun = 0;
                        numOfResult++;
                        continue;
                    }
                }
                else {
                    if (results[numOfResult].numOfRun >= 3) {
                        results[numOfResult].isRummy = 1;
                        results[numOfResult].numOfRun = 3;
                        numOfResult++;
                        continue;
                    }
                    else {
                        numOfResult++;
                        continue;
                    }
                }
            }
		}
	}

    
    
    
	int max = findMax(results, numOfResult);

	result->isRummy = results[max].isRummy;
	memcpy(result->set, results[max].set, sizeof(results[max].set));
	result->numOfSet = results[max].numOfSet;
	memcpy(result->run, results[max].run, sizeof(results[max].run));
	result->numOfRun = results[max].numOfRun;
	result->rJocker = results[max].rJocker;
	result->bJocker = results[max].bJocker;
}
Example #14
0
void tetrisSinglePlayer(bool** ledArray) {
  /*Setting values of the global variables for Tetris:*/
  TOP_MARGIN = 0.0;
  BOT_MARGIN = 0.0;
  LEFT_MARGIN = 26.0;
  RIGHT_MARGIN = 26.0;
  BOT_END = ARRAY_HEIGHT - BOT_MARGIN - 1.0;
  RIGHT_END = ARRAY_WIDTH - RIGHT_MARGIN - 1.0;

  /*Tetris-specific constants:*/
  const int PIECE_WIDTH = 8; /*Must be evenly divisible by 4*/
  const int SQUARE_WIDTH = PIECE_WIDTH / 4;
  const int LEFT_BORDER = 2; /*Should be a multiple of SQUARE_WIDTH*/
  const int RIGHT_BORDER = 2;
  const int TOP_BORDER = 0;
  const int BOT_BORDER = 2;
  const int INIT_X = LEFT_MARGIN + (RIGHT_END - LEFT_MARGIN - PIECE_WIDTH) / 2 + 1;
  const int INIT_Y = TOP_BORDER;
  srand(time(NULL));

  /*"Bag" of upcoming piece types: ("double" indicating two sets)*/
  int* doubleBag = make1DArray(14);
  refillBag(doubleBag, true);

  /*Tetris-specific variables:*/
  float curX = INIT_X;
  float curY = INIT_Y;
  float projX = curX;
  float projY = curY;
  float shadY = curY;
  int curType = pluckBag(doubleBag);
  int pieceOrien = 1; /*1-4 corresponds to north, east, south, west*/
  int score = 0;
  int input = 0;
  int timer = 1;
  int dropTime = 15; /*Should be > 1; may be decreased for difficulty, but decreasing it might also reduce responsiveness*/
  int garbageLines = 0;

  /*Solid borders:*/
  drawCheckerboard(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_END - TOP_MARGIN + 1, LEFT_BORDER);
  drawCheckerboard(ledArray, TOP_MARGIN, RIGHT_END + 1 - RIGHT_BORDER, BOT_END - TOP_MARGIN + 1, RIGHT_BORDER);
  drawCheckerboard(ledArray, TOP_MARGIN, LEFT_MARGIN, TOP_BORDER, RIGHT_END - LEFT_MARGIN + 1);
  drawCheckerboard(ledArray, BOT_END + 1 - BOT_BORDER, LEFT_MARGIN, BOT_BORDER, RIGHT_END - LEFT_MARGIN + 1);

  /*Current piece state and its next projected state:*/
  bool** curPiece = make2DArray(PIECE_WIDTH, PIECE_WIDTH);
  importPiece(curPiece, curType, pieceOrien, PIECE_WIDTH);
  bool** projPiece = make2DArray(PIECE_WIDTH, PIECE_WIDTH);
  copyPiece(projPiece, curPiece, PIECE_WIDTH);
  while(checkOverlap(ledArray, projPiece, curPiece, shadY + SQUARE_WIDTH, curX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) {
    shadY += SQUARE_WIDTH;
  }

  while(1) {
    drawPiece(ledArray, curPiece, curType, false, curY, curX, PIECE_WIDTH);
    drawShadow(ledArray, curPiece, curType, false, shadY, curX, PIECE_WIDTH);
    drawPiece(ledArray, projPiece, curType, true, projY, projX, PIECE_WIDTH);
    copyPiece(curPiece, projPiece, PIECE_WIDTH);
    curX = projX;
    curY = projY;
    shadY = curY;
    while(checkOverlap(ledArray, projPiece, curPiece, shadY + SQUARE_WIDTH, curX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) {
      shadY += SQUARE_WIDTH;
    }
    drawShadow(ledArray, curPiece, curType, true, shadY, curX, PIECE_WIDTH);
    frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_MARGIN, RIGHT_MARGIN );
    input = getLeftInput();
    if (input == 1) { /*Hard drop*/
      while(checkOverlap(ledArray, projPiece, curPiece, projY + SQUARE_WIDTH, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) {
        projY += SQUARE_WIDTH;
        drawPiece(ledArray, curPiece, curType, false, curY, curX, PIECE_WIDTH);
        copyPiece(curPiece, projPiece, PIECE_WIDTH);
        curX = projX;
        curY = projY;
        drawPiece(ledArray, curPiece, curType, true, curY, curX, PIECE_WIDTH);
        frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_MARGIN, RIGHT_MARGIN );
      }
      score = checkLines(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER - garbageLines * SQUARE_WIDTH, TOP_MARGIN + TOP_BORDER, score, curY, PIECE_WIDTH, SQUARE_WIDTH);
        /*
        if(some measure of time has passed) {
          if (addGarbage(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER, TOP_MARGIN + TOP_BORDER, SQUARE_WIDTH)) {
            break; //Game loss
          }
          else {
            garbageLines++;
          }
        }
        */
      projX = INIT_X;
      projY = INIT_Y;
      curX = projX;
      curY = projY;
      shadY = curY;
      curType = pluckBag(doubleBag);
      pieceOrien = 1;
      timer = 1;
      importPiece(curPiece, curType, pieceOrien, PIECE_WIDTH);
      copyPiece(projPiece, curPiece, PIECE_WIDTH);
      if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, true)) {
        break; /*Game loss*/
      }
    }
    else if(input == 5 || timer++ % dropTime == 0) { /*Soft drop*/
      if(checkOverlap(ledArray, projPiece, curPiece, projY + SQUARE_WIDTH, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
        score = checkLines(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER - garbageLines * SQUARE_WIDTH, TOP_MARGIN + TOP_BORDER, score, curY, PIECE_WIDTH, SQUARE_WIDTH);
        /*
        if(some measure of time has passed) {
          if (addGarbage(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER, TOP_MARGIN + TOP_BORDER, SQUARE_WIDTH)) {
            break; //Game loss
          }
          else {
            garbageLines++;
          }
        }
        */
        projX = INIT_X;
        projY = INIT_Y;
        curX = projX;
        curY = projY;
        shadY = curY;
        curType = pluckBag(doubleBag);
        pieceOrien = 1;
        timer = 1;
        importPiece(curPiece, curType, pieceOrien, PIECE_WIDTH);
        copyPiece(projPiece, curPiece, PIECE_WIDTH);
        if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, true)) {
          break; /*Game loss*/
        }
      }
      else {
        projY += SQUARE_WIDTH;
        timer = 1;
      }
    }
    else if(input == 9) { /*Spin clockwise*/
      pieceOrien++;
      if(pieceOrien > 4) {
        pieceOrien -= 4;
      }
      importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH);
      if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
        if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX  + SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX += SQUARE_WIDTH;
        }
        else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX + 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX += 2 * SQUARE_WIDTH;
        }
        else if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX - SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX -= SQUARE_WIDTH;
        }
        else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX - 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX -= 2 * SQUARE_WIDTH;
        }
        else {
          pieceOrien--;
          if(pieceOrien < 1) {
            pieceOrien += 4;
          }
          importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH);
        }
      }
    }
    else if(input == 10) { /*Spin counterclockwise*/
      pieceOrien--;
      if(pieceOrien < 1) {
        pieceOrien += 4;
      }
      importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH);
      if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
        if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX  + SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX += SQUARE_WIDTH;
        }
        else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX + 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX += 2 * SQUARE_WIDTH;
        }
        else if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX - SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX -= SQUARE_WIDTH;
        }
        else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX - 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) {
          projX -= 2 * SQUARE_WIDTH;
        }
        else {
          pieceOrien++;
          if(pieceOrien > 4) {
            pieceOrien -= 4;
          }
          importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH);
        }
      }
    }
    else if(input == 3) { /*Move right*/
      if(checkOverlap(ledArray, projPiece, curPiece, projY, projX + SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) {
        projX += SQUARE_WIDTH;
      }
    }
    else if(input == 7) { /*Move left*/
      if(checkOverlap(ledArray, projPiece, curPiece, projY, projX - SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) {
        projX -= SQUARE_WIDTH;
      }
    }
  }
  drawPiece(ledArray, curPiece, curType, true, curY, curX, PIECE_WIDTH);
  frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_MARGIN, RIGHT_MARGIN );
  free(doubleBag);
  free2DArray(curPiece, PIECE_WIDTH);
  free2DArray(projPiece, PIECE_WIDTH);
  return;
}