void draw_game_running(SDL_Renderer* ren) { int bW, bH; SDL_QueryTexture(background, NULL, NULL, &bW, &bH); renderTexture(background, ren, 0, 0); //draw the fixed peices int i, j, x, y; for(i=0; i<10; i++) { for(j=0; j<16; j++) { x = GRID_START_X + BLOCK_SIZE * i; y = GRID_START_Y + BLOCK_SIZE * j; int color = grid[i][j]; if(color>0) renderTextureClip(blocks, ren, x, y, &blockClips[color-1]); } } DROP *d = (DROP*)dropData+useClip+1; int r = rotation%(d->rotations); PIECE *p = &(d->peice[r]); x = drop_x; y = drop_y; drawPiece(ren, p, x, y, useClip); //Draw the next piece to the right DROP *dN = (DROP*)dropData+nextPiece+1; PIECE *pN = &(dN->peice[0]); //rotation 0 x = drop_x; y = drop_y; drawPiece(ren, pN, NEXT_PIECE_X, NEXT_PIECE_Y, nextPiece); }
void drawPuzzle(void) { bool isPlaying = (state == STATE_FREE || state == STATE_PICKED); if (toDrawAll) { arduboy.clear(); drawBoard(0); drawPieces(); toDrawAll = false; } else if (isPlaying && focusPieceIdx >= 0) { drawPiece(focusPieceIdx); } if (state == STATE_FREE) { drawCursor(); } if (state == STATE_CLEAR) { drawClearEffect(); } if (isHelpVisible && isPlaying) { HELP_T idx; if (state == STATE_FREE) { idx = HELP_FREE; } else { idx = (arduboy.buttonPressed(A_BUTTON)) ? HELP_HOLD : HELP_PICK; } drawHelp(idx, helpX, helpY); } }
void GraphicsHandler::drawBoard(const board::Board *b) { drawBackground(); // Valid moves are drawn for the piece being hovered over // Or the piece that is currently selected board::Piece* pCurrent = b->getCurrent(); board::Piece* pSelect = b->getSelected(); if (pSelect) for (auto &i: pSelect->getTrajectory()) { if(i.isValid()) drawValidMove(i.getX(), i.getY()); } else if (pCurrent) for (auto &i: pCurrent->getTrajectory()) { if(i.isValid()) drawValidMove(i.getX(), i.getY()); } // Draw the non-selected pieces for(auto &i: b->pieces) { if(i && i != b->getSelected()) drawPiece(i); } // Draw the selected piece if (b->getSelected()) drawPieceAt(b->getSelected(), sf::Mouse::getPosition(*display)); }
void JKTetrisPiece::drawBoard(QPainter *painter, QSize tilesize, bool withPiece) const { painter->save(); QPen p(QColor("black")); p.setWidthF(2.5); painter->setPen(p); painter->drawLine(0,boardHeight*tilesize.height(), boardWidth*tilesize.width(), boardHeight*tilesize.height()); painter->restore(); int i=0; for (int yy=0; yy<boardHeight; yy++) { for (int xx=0; xx<boardWidth; xx++) { if (board[i]>=0){ QColor col=JKTetrisPiece::PieceColor[board[i]]; QColor borderCol=col.darker(); painter->save(); QRect r(xx*tilesize.width(), yy*tilesize.height(), tilesize.width()-1, tilesize.height()-1); painter->fillRect(r,col); painter->setPen(borderCol); painter->drawRect(r); painter->restore(); } i++; } } if (withPiece) { drawPiece(painter, tilesize, x*tilesize.width(), y*tilesize.height()); } }
Tetri *GameLogic::nextBlock(void) { Tetri *rpiece = NULL; rpiece = nextPiece; nnextPiece = drawPiece(); nextPiece = nnextPiece; return rpiece; }
void update() { int x, y, z; current_view = (current_view == left_view) ? right_view : left_view; // draw complete background rectfill(current_view, 0, 0, 639, 479, BACKGROUND_COLOR); draw_sprite(current_view, new_game, NEW_GAME_BUTTON_X1, BUTTON_YOFF); draw_sprite(current_view, undo, UNDO_BUTTON_X1, BUTTON_YOFF); draw_sprite(current_view, help, HELP_BUTTON_X1, BUTTON_YOFF); draw_sprite(current_view, quit, QUIT_BUTTON_X1, BUTTON_YOFF); if(editing) { textprintf(current_view, font, 200, 2, SELECTED, "Editing: '%s' (%s)", layout_title, get_filename(editing)); textprintf(current_view, font, 200, 12, SELECTED, "%3i pieces left", n_pieces_left); } for(z = 0; z < 3; z++) for(y = 0; y < 9; y++) for(x = 0; x < 16; x++) if(board[x][y][z].value < EMPTY || editing) drawPiece(current_view, &board[x][y][z]); scroll_screen((current_view == left_view) ? 0 : SCREEN_WIDTH, 0); show_mouse(current_view); }
void updatePiece(Piece *p) { BITMAP *work = (current_view == left_view) ? right_view : left_view; int i, j, k; int lo_i = (p->x > 0) ? p->x - 1 : 0; int hi_i = (p->x < 15) ? p->x + 1 : 15; int lo_j = (p->y > 0) ? p->y - 1 : 0; int hi_j = (p->y < 8) ? p->y + 1 : 8; int x = p->x * FACE_WIDTH - p->z * EDGE_WIDTH + BOARD_XOFF; int y = p->y * FACE_HEIGHT - p->z * EDGE_HEIGHT + BOARD_YOFF; rectfill(work, x, y, x + (SPRITE_WIDTH - 1), y + (SPRITE_HEIGHT - 1), BACKGROUND_COLOR); for(k = 0; k < 3; k++) for(j = lo_j; j <= hi_j; j++) for(i = lo_i; i <= hi_i; i++) if(board[i][j][k].value < EMPTY || editing) drawPiece(work, &board[i][j][k]); scare_mouse(); blit(work, current_view, x, y, x, y, SPRITE_WIDTH, SPRITE_HEIGHT); if(editing) textprintf(current_view, font, 200, 12, SELECTED, "%3i pieces left", n_pieces_left); unscare_mouse(); }
void GraphicsHandler::drawBoard(board::Board const &b) { drawBackground(); for(auto const &pp : b) { drawPiece(*pp); } }
GLvoid ChessGame::drawChessBoard() { #if !TEXTURED_BOARD glColor3f(1.0f,0.0f,0.0f); glDisable(GL_TEXTURE_2D); #else glColor3f(1.0f,1.0f,1.0f); glBindTexture(GL_TEXTURE_2D, texture->texID); #endif glBegin(GL_QUADS); glNormal3f(0,1,0); glTexCoord2f(1.0f, 1.0f); glVertex3f( SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM* SQUARE_SIZE, -SQUARE_NUM * SQUARE_SIZE); glTexCoord2f(0.0f, 1.0f); glVertex3f(-SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, -SQUARE_NUM * SQUARE_SIZE); glTexCoord2f(0.0f, 0.0f); glVertex3f(-SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE ); glTexCoord2f(1.0f, 0.0f); glVertex3f(SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE ); glEnd(); // Draw Rows glBindTexture(GL_TEXTURE_2D, texRow->texID); glBegin(GL_QUADS); glNormal3f(0,1,0); glTexCoord2f(1.0f, 1.0f); glVertex3f( SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM* SQUARE_SIZE, -SQUARE_NUM * SQUARE_SIZE); glTexCoord2f(0.0f, 1.0f); glVertex3f(SQUARE_NUM*SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE ); glTexCoord2f(0.0f, 0.0f); glVertex3f(1+SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE ); glTexCoord2f(1.0f, 0.0f); glVertex3f(1+SQUARE_NUM * SQUARE_SIZE , SQUARE_NUM * SQUARE_SIZE, -SQUARE_NUM * SQUARE_SIZE); glEnd(); //Draw Columns glBindTexture(GL_TEXTURE_2D, texCol->texID); glBegin(GL_QUADS); glNormal3f(0,1,0); glTexCoord2f(-1.0f, -1.0f); glVertex3f( SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM* SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE+1); glTexCoord2f(0.0f, -1.0f); glVertex3f(SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE ); glTexCoord2f(0.0f, 0.0f); glVertex3f(-SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE ); glTexCoord2f(-1.0f, 0.0f); glVertex3f(-SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE, SQUARE_NUM * SQUARE_SIZE+1); glEnd(); //glTranslatef(0.0f,8.001f,-12.0f); //glScalef(.2f,.2f,.2f); for(char i = 'a'; i < 'j'; i++) { for(int j = 1; j < 9; j++) { tempChessLoc.setChessLocX(i); tempChessLoc.setChessLocY(j); if(board->getPiece(tempChessLoc).getPieceName() != 'X') { // glTranslatef(GUI_LOCATION_X,GUI_LOCATION_Y,8.001); drawPiece(GUI_LOCATION_X, GUI_LOCATION_Y,PIECE_COLOR,PIECE_NAME); } } } }
QPixmap JKTetrisPiece::drawPiece(QSize tilesize, int xoffset, int yoffset) const { QPixmap pix(tilesize.width()*JKTetrisPiece::PieceSize, tilesize.height()*JKTetrisPiece::PieceSize); pix.fill(QColor("grey")); { QPainter painter(&pix); drawPiece(&painter, tilesize, xoffset, yoffset); painter.end(); } return pix; }
GameLogic::GameLogic(std::vector<Animator *> &animlist) : lockTimeOut(800), dropTimeout(440), lastDrop(0), score(0), lines_completed(0), lines_to_level(10), level(0), blockbag({ 1, 2, 3, 4, 5, 6, 7 }), alist(&animlist), nnextPiece(NULL), nextPiece(drawPiece()) {}
void GameLogic::reset(void) { lastDrop = 0; score = 0; lines_completed = 0; lines_to_level = 10; level = 0; lockTimeOut = 800; dropTimeout = 440; if (nnextPiece != NULL) { delete nnextPiece; } nnextPiece = NULL; nextPiece = drawPiece(); }
void drawScreen() { //Draw everything need for play the game clearScreen(); acquire_bitmap(score_buffer); drawInfo(score, lev_act, time_l, rows_del, myfont, speed-9); acquire_bitmap(map_buffer); drawBoard(); drawPiece(current, 0); acquire_bitmap(next_buffer); drawPiece(next, 1); blit (map_buffer, screen, 0, 0, 100, 40, 200, 400); blit (score_buffer, screen, 0, 0, 350, 40, 180, 160); blit (next_buffer, screen, 0, 0, 350, 210, 80, 80); release_bitmap(map_buffer); release_bitmap(score_buffer); release_bitmap(next_buffer); }
void GraphicsHandler::drawTrajectory(piece::Piece const &p, bool enemy) { { auto &sprite = (enemy? enemy_move : valid_move); for(auto const &it : p.board.pieceTrajectory(p)) { if(!p.board.occupied(it.second)) { if(std::find_if(p.board.pieceCapturables().begin(), p.board.pieceCapturables().end(), [&](board::Board::Movements_t::value_type const &m) { return m.second == it.second && (*m.first)->suit != p.suit; }) == p.board.pieceCapturables().end()) { drawSpriteAtCell(sprite, it.second.x, it.second.y); } } } } { auto &sprite = (enemy? enemy_capture : valid_capture); for(auto const &it : p.board.pieceCapturing(p)) { for(auto const &c : p.board.pieceCapturables()) { if(c.second == it.second && (*c.first)->suit != p.suit) { drawSpriteAtCell(sprite, it.second.x, it.second.y); auto jt = std::find_if(p.board.pieceTrajectories().begin(), p.board.pieceTrajectories().end(), [&](board::Board::Movements_t::value_type const &m) { return (*m.first)->pos == it.second; }); if(jt != p.board.pieceTrajectories().end()) { drawPiece(**(jt->first)); //redraw } break; } } } } }
void draw(GAME game) { int x,y; int hs,vs; //horizontal and vertical shift, maps game to gl coordinates hs=-game.width/2; vs=-game.height/2+1; glLoadIdentity(); for(x=0;x<game.width;++x){ for(y=0;y<game.height;++y){ drawAtom(game.field[game.height*x+y],x+hs,y+vs); } } if(game.piece!='\0'){ drawPiece(game.piece,game.x+hs,game.y+vs,game.rot); } drawFrame(game.width,game.height,hs,vs); }
void getHumanMove() { int x=3 ; int ch; while(TRUE) { gotoxy(x*6 +4, 2); ch = (int)getch(); switch(ch) { case 75:/*LEFT*/ if(x>0) x--; break; case 77:/*RIGHT*/ if(x<(X_BOARD-1)) x++; break; case 27:/*ESC*/ textcolor(7); textbackground(0); clrscr(); printf("Thank You For Playing 4 in line by Cheok Yan Cheng!\n"); exit(0); break; case 32:/*SPACE*/ if(humanMove(x)) { drawPiece(); return; } else { gotoxy(1,20); textcolor(4); cprintf("OOPs! Wrong Move! \n"); } } } }
void TetrisScreen::draw(StateManager* state_manager) { frr.startFrame(); SDL_FillRect(state_manager->screen, &state_manager->screen->clip_rect,0); Uint32 gray = SDL_MapRGB(state_manager->screen->format, 35,35,35); SDL_Rect rect; rect.x = BOARD_OFFSETX * blocksize; rect.y = 0; rect.w = board.getWidth() * blocksize; rect.h = (board.getHeight() - TetrisBoard::NUM_HIDDEN_ROWS) * blocksize; SDL_FillRect(state_manager->screen, &rect, gray); //draw the board std::vector<std::vector<TetrisPiece::PieceType> > board_blocks; board_blocks = board.getBoard(); int xblocks=0, yblocks=0; //blocks int xpix=0, ypix=0; //pixels //for each row for(unsigned int y = 0 ; y < board_blocks.size() ; y++) { //for each block in the row. for(unsigned int x = 0 ; x < board_blocks[y].size() ; x++) { if(getColor(board_blocks[y][x]) < 0) continue; else { yblocks = board.getHeight() - y - 1 - TetrisBoard::NUM_HIDDEN_ROWS; // in blocks ypix = yblocks * blocksize; xblocks = BOARD_OFFSETX + x; //in blocks xpix = xblocks * blocksize; applySurface(xpix, ypix, blocks[getColor(board_blocks[y][x])], state_manager->screen); } } } //draw the hold piece TetrisPiece temp = board.getHold(); std::vector<Point> pblocks; rect.x = blocksize; rect.y = blocksize; rect.w = TetrisPiece::BLOCKS_PER_PIECE * blocksize; rect.h = rect.w; SDL_FillRect(state_manager->screen, &rect, gray); if(temp.getPieceType() != TetrisPiece::EMPTY_PIECE) drawPiece(&temp, state_manager->screen, blocksize,blocksize*5); //draw the current piece temp = board.getActivePiece(); yblocks = board.getHeight() - temp.getLocation().y - TetrisBoard::NUM_HIDDEN_ROWS; xblocks = BOARD_OFFSETX + temp.getLocation().x; drawPiece(&temp, state_manager->screen, xblocks * blocksize, yblocks * blocksize); pblocks.clear(); pblocks = temp.getBlocks(); //draw the ghost piece temp = board.getGhost(); pblocks = temp.getBlocks(); SDL_FillRect(ghost_surface, &ghost_surface->clip_rect, ghost_mask); yblocks = board.getHeight() - temp.getLocation().y - 1 - 3 - TetrisBoard::NUM_HIDDEN_ROWS; xblocks = BOARD_OFFSETX + temp.getLocation().x; drawPiece(&temp, ghost_surface, 0, ghost_surface->h); applySurface(xblocks*blocksize,yblocks*blocksize,ghost_surface,state_manager->screen); //draw the next pieces rect.x = blocksize * (NEXT_OFFSETX + board.getWidth()); rect.y = blocksize * NEXT_OFFSETY; rect.w = blocksize * TetrisPiece::BLOCKS_PER_PIECE; rect.h = 3 * blocksize * (TetrisPiece::BLOCKS_PER_PIECE + 1); SDL_FillRect(state_manager->screen, &rect, gray); std::vector<TetrisPiece> bag = board.getBag(); xblocks = board.getWidth() + NEXT_OFFSETX; for(int i = 0 ; i < 3 ; i++) { yblocks = NEXT_OFFSETY + (4*(i+1)) + i; drawPiece(&bag.at(i),state_manager->screen,xblocks*blocksize,yblocks*blocksize); } //draw the score/lines/level std::stringstream ss; rect.x = blocksize; rect.y = 2*blocksize + 4*blocksize; ss << "Lines: " << board.getLines(); font->drawString(ss.str(), state_manager->screen, rect.x,rect.y); ss.str(""); rect.y += font->getLineSpacing() + font->getHeight(); ss << "Score: " << board.getScore(); font->drawString(ss.str(),state_manager->screen,rect.x,rect.y); ss.str(""); rect.y += font->getLineSpacing() + font->getHeight(); ss << "Level: " << board.getLevel(); font->drawString(ss.str(),state_manager->screen,rect.x,rect.y); //draw toasts if(my_toasts.size() > 0) { bool isdead; do{ isdead = false; for(unsigned int i = 0 ; i < my_toasts.size() ; i++) { if(my_toasts[i].isDone()) { isdead = true; std::vector<Toast>::iterator it = my_toasts.begin(); it += i; my_toasts.erase(it); break; } } } while(isdead); for(unsigned int i = 0 ; i < my_toasts.size() ; i++) my_toasts[i].drawToast(state_manager->screen); } SDL_Flip(state_manager->screen); frr.endFrame(); }
void BasicSet::draw(const ChessGameState& cgs) { Board b = cgs.getBoard(); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); // Move to center of bottom left square glPushMatrix(); glTranslated(0.5,0,-0.5); for(BoardPosition bp('a', 1); !bp.outN(); bp.moveN()) { for(bp.setFile('a'); !bp.outE(); bp.moveE()) { if(m_hovertimer[bp.x()][bp.y()].started()) { m_hovertimer[bp.x()][bp.y()]++; m_hoverheight[bp.x()][bp.y()] += m_hovertimer[bp.x()][bp.y()].change(); } if(m_hovertimer[bp.x()][bp.y()].done()) { m_hoverheight[bp.x()][bp.y()] = 0.01; m_hovertimer[bp.x()][bp.y()].resetDone(); } double offx = 0.0, offy = 0.0; if(m_movetimer[bp.x()][bp.y()].first.started()) { cout << bp.x() << " " << bp.y() << endl; m_movetimer[bp.x()][bp.y()].first++; m_movetimer[bp.x()][bp.y()].second++; offx = m_movetimer[bp.x()][bp.y()].first.value(); offy = m_movetimer[bp.x()][bp.y()].second.value(); } if(b.isOccupied(bp)) { Piece* p = b.getPiece(bp); bool alert = false; if(cgs.isCheck() && p->type() == Piece::KING && p->color() == cgs.getTurn()) alert = true; glTranslated(offx, m_hoverheight[bp.x()][bp.y()], -offy); drawPiece(p, 1.0, alert); glTranslated(-offx, -m_hoverheight[bp.x()][bp.y()], offy); } glTranslated(1,0,0); } // Move to the next rank glTranslated(-8,0,-1); } // Draw the pieces for pawn promotion selection if (m_drawPromotionSelector) { if (cgs.isWhiteTurn()) { // We're waiting for the white player to select which // piece to promote to, so draw the pieces at the black // end of the board. glTranslated(2,0,0); static Piece * q = new Piece(Piece::WHITE, Piece::QUEEN); drawPiece(q, 1.0, false); glTranslated(1,0,0); static Piece * k = new Piece(Piece::WHITE, Piece::KNIGHT); drawPiece(k, 1.0, false); glTranslated(1,0,0); static Piece * b = new Piece(Piece::WHITE, Piece::BISHOP); drawPiece(b, 1.0, false); glTranslated(1,0,0); static Piece * r = new Piece(Piece::WHITE, Piece::ROOK); drawPiece(r, 1.0, false); glTranslated(-5,0,0); } else { // We're waiting for the black player to select which // piece to promote to, so draw the pieces at the white // end of the board. glTranslated(5,0,9); static Piece * q = new Piece(Piece::BLACK, Piece::QUEEN); drawPiece(q, 1.0, false); glTranslated(-1,0,0); static Piece * k = new Piece(Piece::BLACK, Piece::KNIGHT); drawPiece(k, 1.0, false); glTranslated(-1,0,0); static Piece * b = new Piece(Piece::BLACK, Piece::BISHOP); drawPiece(b, 1.0, false); glTranslated(-1,0,0); static Piece * r = new Piece(Piece::BLACK, Piece::ROOK); drawPiece(r, 1.0, false); glTranslated(-2,0,-9); } } glPopMatrix(); if(m_selected.isValid() && !m_drawPromotionSelector) { Piece* selected = b.getPiece(m_selected); bool alert = false; if(cgs.isCheck() && selected->type() == Piece::KING && selected->color() == cgs.getTurn()) alert = true; glTranslated(m_mouseX, 0.2, m_mouseY); drawPiece(b.getPiece(m_selected), 0.5, alert); glTranslated(-m_mouseX, -0.2, -m_mouseY); } }
int main(void) { BOOLEAN myturn; myturn = TRUE; drawBoard(); srand(time(NULL)); rand(); do { switch (status()) { case WIN: case LOSE: case DRAW: init(); drawBoard(); if (myturn) { makeMove(COMPUTER,(2+rand()%4),square); } myturn = !myturn; drawPiece(); } textcolor(4); gotoxy(1,20); cprintf("Your Turn, Please.\n"); getHumanMove(); gotoxy(1,20); textcolor(4); switch (status()) { case WIN: cprintf(WIN_MESSAGE); getch(); break; case LOSE: cprintf(LOSE_MESSAGE); getch(); break; case DRAW: cprintf(DRAW_MESSAGE); getch(); break; default:/*OK*/ if(computerMove()) { gotoxy(1,20); drawPiece(); gotoxy(1,20); switch (status()) { case WIN: cprintf(WIN_MESSAGE); getch(); break; case LOSE: cprintf(LOSE_MESSAGE); getch(); break; case DRAW: cprintf(DRAW_MESSAGE); getch(); break; } } } } while(TRUE); }
void display() { /* This function is used to display the content on to the screen... This is not important for the participant... He can skip it */ int gridDup[6][7],moveDup=move,endGameFlagDup,rowNum; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); drawGrid(grid); if(animate) { drawPiece(result,displacement,move); if(stopAnimate(grid,result,displacement)) { animate=0; if(!errorFlag) { rowNum=updateGrid(move,grid,result); // update the grid implying the move has been made drawGrid(grid); if(checkWinOrLose(grid,result,rowNum)) { // check for win condition win=move; // win will contain which bot has won endGameFlag=1; } else if(checkDraw(grid)) { // check for draw condition win=0; // win=0 implies draw endGameFlag=1; } move*=-1; // toggle between plaper 1 and player 2 } } } if(errorFlag) // If error occurs text(displayStr,-1.5,-1.8,font3); if(endGameFlag) { // If game ends if(win>0) // player 1 wins strcpy(displayStr,"Bot 1 wins!!!"); else if(win<0) // player 2 wins strcpy(displayStr,"Bot 2 wins!!!"); else // draw strcpy(displayStr,"Game drawn!!!"); text(displayStr,-1.5,-1.8,font3); printf("\n\n\t%s\n\n\t\tgrid content : \n\n",displayStr); displayStatus(grid); } glFlush(); glutSwapBuffers(); if(animate) displacement+=0.005; if(!endGameFlag && !errorFlag && !animate) { copyGrid(grid,gridDup); moveDup=move; endGameFlagDup=endGameFlag; if(move>0) result=bot1(grid,move); else result=bot2(grid,move); if(!(equalGrid(grid,gridDup) && move==moveDup && endGameFlagDup==endGameFlag)) { // grid() and move variables should not be changed sprintf(displayStr,"Bot %d error : Grid or move is modified!!!",move); errorFlag=1; } checkForErrors(result,move,grid); // errors like result should be within the 0 to 6 range, etc... animate=1; displacement=0; } }
static void drawPieces(void) { for (int i = 0; i < PIECES; i++) { drawPiece(pieceOrder[i]); } }
void drawScene(Board *theBoard, Game *theGame){ drawBoard(theBoard, theGame); drawPiece(theGame->mXloc, theGame->mYloc, theGame->mPiece, theGame->mRot); drawPiece(theGame->nextX, theGame->nextY, theGame->nextP, theGame->nextRot); }
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; }
void ChessBoard::mousePressEvent(QMouseEvent *event) { if(isPVP()) { int row=static_cast<size_t>(event->y()/_gridsidelength),col=static_cast<size_t>(event->x()/_gridsidelength); if(!isLocatedPiece(row,col)) { drawPiece(row,col,_currentplayer); update(); } if(isFiveInARow(row,col)) { if(_currentplayer) { QMessageBox::information(this,tr("BLACK WIN!"),tr("BLACK WIN!"),QMessageBox::Ok); } else { QMessageBox::information(this,tr("WHITE WIN!"),tr("WHITE WIN!"),QMessageBox::Ok); } init(); return; } if(_total==rowgridnum*rowgridnum) { QMessageBox::information(this,tr("DRAWN GAME"),tr("DRAWN GAME"),QMessageBox::Ok); init(); return; } _currentplayer =!_currentplayer; } else { //PVE condition//悔棋功能也要分两类实现 //Player: int row=static_cast<size_t>(event->y()/_gridsidelength),col=static_cast<size_t>(event->x()/_gridsidelength); if(!isLocatedPiece(row,col)) { drawPiece(row,col,true);//默认玩家先手持黑 update(); } else{return;} if(isFiveInARow(row,col)) { QMessageBox::information(this,tr("BLACK WIN!"),tr("BLACK WIN!"),QMessageBox::Ok); init(); return; } if(_total==rowgridnum*rowgridnum) { QMessageBox::information(this,tr("DRAWN GAME"),tr("DRAWN GAME"),QMessageBox::Ok); init(); return; } //AI: Loc tmp=_AI.analyse(); qDebug()<<tmp.row<<""<<tmp.col;//test drawPiece(tmp.row,tmp.col,false);//默认AI后手持白 update(); if(isFiveInARow(tmp.row,tmp.col)) { QMessageBox::information(this,tr("WHITE WIN!"),tr("WHITE WIN!"),QMessageBox::Ok); init(); return; } if(_total==rowgridnum*rowgridnum) { QMessageBox::information(this,tr("DRAWN GAME"),tr("DRAWN GAME"),QMessageBox::Ok); init(); return; } } }