示例#1
0
/******************************************************************************
keyDown
Called when Tetris gets the keyDown message in the loop method.
Passes down the appropriete keys. Checks for line completion & game over.

Input Parameters:
	none

Output:
	None
******************************************************************************/
void Tetris::keyDown(keyPress k) {
	if(!game_over) {
		last_action = k;
		if(k == DOWN) {
			score.addDown();
			timer = 1;
			if(!current_tetromino->down()) {
				checkLines();
				new_tetromino();
			}
		} else if (k == ROTATE) {
			current_tetromino->rotate();
		} else if (k == LEFT) {
			current_tetromino->left();
		} else if (k == RIGHT) {
			current_tetromino->right();
		} else if (k == DROP) {
			timer = 1;
			score.addDown(current_tetromino->drop());
			checkLines();
			new_tetromino();
		}
	} else {
		if (k == DROP) {
			game_over = false;
			setupBlocks();
			new_tetromino();
			score.reset();
		}
	}
}
示例#2
0
/*!
*/
void GridItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (!m_ballSelected) {
        event->ignore();
        return;
    }

    GridPos pt;
    fromViewToGridCoordinate(event->pos(), pt);

    if (m_beginPos == pt) {
        m_ballSelected = false;
        selectBall(pt, false);

        // clear the path tracker and repaint the grid
        m_pathTracker.clear();
        update();
        //
    } else if (isValidPosition(pt) && isFreePos(pt)) {
        QVector<GridPos>& path = m_pathTracker.path();
        if (!path.isEmpty()) {
            // have we moved onto a square occupied by a hint ball ? if we have then a new set of the hint balls
            // needs to be generated.
            BallItem *ball = ballAt(path.back());
            bool enforceHintBalls = (ball && ball->isHint());
            //

            moveBall(ballAt(m_beginPos), path);

            // unselect the moving ball
            ball = ballAt(path.back());
            ball->select(false);
            m_ballSelected = false;

            // searches for the lines of the same ball items that have the same colours.
            checkLines(ball);

            // do we have more available positions ? if we do then proceed with a new set of ball items.
            if (!BallItemsProvider::instance()->availableIndexes().isEmpty()) {
                // get the next set of balls
                const QList<BallItem*> &currentBalls = BallItemsProvider::instance()->nextBalls(enforceHintBalls);
                // searches for the lines of the same ball items that have the same colours.
                checkLines(currentBalls);
            } 
            
            // even more available positions ? if not then quit or reset the game.
            if (BallItemsProvider::instance()->availableIndexes().isEmpty()){
                if (promptForGameEnd()) {
                    QCoreApplication::quit();
                } else {
                    MainWidget::instance()->boardView()->reset();
                } 
            }
        }
    }
}
示例#3
0
CheckFileHashWidget::CheckFileHashWidget(int Type, QFileInfoList list, QWidget *parent) :
    QWidget(parent), algorithmType(Type)
{
    QLabel *labelHash = new QLabel("File's hash:");
    QLabel *labelInput = new QLabel("Input hash for check:");
    labelIcon = new QLabel();
    compareWithFile = new QPushButton("Compare file");
    hashLine = new QLineEdit();
    inputLine = new QLineEdit();
    hashLine->setReadOnly(true);
    QVBoxLayout *vbox = new QVBoxLayout();
    QHBoxLayout *hbox = new QHBoxLayout();
    hbox->addWidget(inputLine);
    hbox->addWidget(labelIcon);
    connect(this, SIGNAL(statusChanged(QString)), this->parent(), SLOT(changeStatus(QString)));
    hashLine->setText(getHash(list.at(0).absoluteFilePath()));
    vbox->addWidget(labelHash);
    vbox->addWidget(hashLine);
    vbox->addWidget(labelInput);
    vbox->addLayout(hbox);
    vbox->addWidget(compareWithFile);
    setLayout(vbox);
    connect(inputLine, SIGNAL(textChanged(QString)), SLOT(checkLines(QString)));
    connect(compareWithFile, SIGNAL(clicked()), SLOT(compareFileAndLine()));
}
示例#4
0
文件: tetris.cpp 项目: tomrigo/ecs50
int play(int *score, Tetromino *tetromino)
{
  int col;
  Tetromino tempTetromino;

  tempTetromino = *tetromino;
  tempTetromino.location += 38;
  printTetromino(tetromino, ' ');

  if(checkMove(&tempTetromino)) // can move
  {
    *tetromino = tempTetromino;  
    printTetromino(tetromino, 'X');
  }  // if can move
  else // cannot move
  {
    printTetromino(tetromino, 'X');
    checkLines(score);

    for(col = 1; col <= 10; col++)
      if(crt[1][col] != ' ')
        return 0;  // X on the second row  so game over.

    startTetromino(tetromino); 
  }  // else cannot move

  return 1;
}  // play()
示例#5
0
/*!
*/
void GridItem::checkLines(BallItem *ball)
{
    QList<BallItem*> *list = new QList<BallItem*>;
    list->push_back(ball);

    checkLines(*list);

    delete list;
}
示例#6
0
void main()
{
	int i;
	int a = 0;
	int b=0;
	int w=0;
	int N, M;
	int S=0;
	int F=0;
	int k;
	int **edges;
	int *lenMin;  //lenMin[i] - длина кратчайшего пути от вершины s в i
	int *h;  //h[i] - вершина, предшествующая i-й вершине на кратчайшем пути
	N=checkAmount();
	if (N == EOF)
		return;
	if (checkInVertex(N,&S,&F) == EOF)
		return;
	M = checkLines(N);
	if (M == EOF)
		return;
	S = S - 1;
	F = F - 1;
	edges = (int**)malloc(N*sizeof(int*));
	for (i = 0; i < N; i++)
	{
		edges[i] = (int*)malloc(N*sizeof(int));
	}
	nullEgdes(N, edges);
	lenMin = (int*)malloc(N*sizeof(int));
	h = (int*)malloc(N*sizeof(int));
	for (i = 0; i < M; i++)
	{
		if (checkEdges(N, &a, &b, &w) == EOF)
			return;
		edges[a - 1][b - 1] = w;
		edges[b - 1][a - 1] = w;
	}
	for (i = 0; i < N; i++)
	{
		lenMin[i] = Infinity; //Сначала все кратчайшие пути из s в i равны бесконечности        
	}
	k = 0;	//счетчик кратчайших путей равен нулю
	Deykstra(N, &k, edges, &lenMin, &h, S, F);
	printOutput(N, S, F, k, h, lenMin);
	getchar();
	getchar();
}
示例#7
0
/******************************************************************************
update
The loop function:
- When the time is up, automatically move down the Tetromino. (So it won't be
stuck in air and not moving at all.)
- Each dead block is rendered.

Input Parameters:
	none

Output:
	None
******************************************************************************/
void Tetris::update() {

	timer = (timer + 1) % UINT_MAX;

	if(!game_over) {
		if((last_action != DOWN) && ((timer % score.getSpeed()) == 0)) {
			if(!current_tetromino->down()) {
				checkLines();
				new_tetromino();
			}
		}
		last_action = EMPTY;
	}


	// Render
	Screen->Clear (0, 0, 0, 255);
	Screen->StartFrame();
	
	// background render
	POINT kPosition = {0, 0};
	misc_surfaces[0].Render (&kPosition);

	// render current blocks
	for(int i = 0; i < height; i++) {
		for(int j = 0; j < width; j++) {
			if(blocks[i][j].getColor() != CLEAR) {
				if(blocks[i][j].getY() >= hidden_lines) {
					block_surfaces[i][j].Create (&images[blocks[i][j].getColor()]);
					POINT kPosition = {container_x + blocks[i][j].getX() * Block::width, container_y + (blocks[i][j].getY() - hidden_lines) * Block::height};
					block_surfaces[i][j].Render (&kPosition);
				}
			}
		}
	}
	Tetromino::renderNext();
	score.updateScreen();

	if(!game_over) {
		current_tetromino->update();
	} else {
		POINT kPosition = {gameover_x, gameover_y};
		misc_surfaces[1].Render(&kPosition);
	}

	Screen->EndFrame ();
}
示例#8
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;
}
示例#9
0
文件: main.cpp 项目: nem0301/Tetris
int main(int argc, char* argv[]) {

	int width = SIZE * WIDTH;
	int height = SIZE * HEIGHT;
	int size = SIZE;

	Mat img(height, width, CV_8UC3, Scalar(0));
	namedWindow("Display Image", WINDOW_AUTOSIZE);

	initialPlate(plate);
	while (true) {

		LBlock b(Point2f(WIDTH / 2, 3), plate);

		Block& block = b;

		Point2f point(0, 0);

		time_t tick, tock;
		time(&tick);
		while (true) {
			//image initialize by black
			img = Mat::zeros(height, width, CV_8UC3);

			//draw whole plate
			for (int y = 0; y < HEIGHT; y++) {
				for (int x = 0; x < WIDTH; x++) {
					int temp = y * WIDTH + x;
					if (plate[temp] != 0) {
						Point2f tempPoint1(x * size, y * size);
						Point2f tempPoint2(x * size + size - 1,
								y * size + size - 1);
						Rect rect(tempPoint1, tempPoint2);
						rectangle(img, rect, Scalar(255, 255, 255), -1);
					}
				}
			}

			//get input
			int input = cvWaitKey(1);
			if (input == 65361) {		//left
				block.move(-1);
			} else if (input == 65362) {	//up
				block.rotate();
			} else if (input == 65363) {	//right
				block.move(1);
			} else if (input == 65364) {	//down
				block.move(0);
			} else if (input == 32) {		//space
				block.moveFullDown();
				checkLines(plate);
				break;
			} else if (input == 27)		//esc
				return 0;;

			time(&tock);
			if (tock - tick > 0) {
				int deadOrNot = block.move(0);
				tick = tock;
				if (deadOrNot == -1) {
					break;
				}
			}

			imshow("Display Image", img);

		}
	}

	destroyAllWindows();
	cvWaitKey();
	return 0;
}