Esempio n. 1
0
int movePieace(state_t st, point_t p){
  direction_t d;
  char buffer[30];
  while(1){
    choosePiece(st, p);
    printf("you chose (%d, %d): choose direction\n", p->x, p->y);
    printf("press 'L' or 'R'\n");
    fgets(buffer, 30, stdin);
    int c = tolower(buffer[0]);
    switch(c){
    case 'r':
      d = R;
      break;
    case 'l':
      d = L;
      break;
    default:
      d = other;
      break;
    }
    if(move(st, p, d) == 1){
      printf("you can't choose that direction\n");//動かすのに失敗したらやりなおし
      continue;
    }
    break;
  }

  return 0;
}
Esempio n. 2
0
/**
 * @brief Player::move plays given piece and choose another piece for opponent
 * @param piece to be placed
 * @return Choosen piece for opponent
 */
Piece *Player::move(Piece *piece)
{
    board->putPiece(chooseField(piece), piece);
    piece = choosePiece();
    board->deletePieceFromStock(piece);    
    return piece;
}
Esempio n. 3
0
int movePieace(state_t st, point_t p){
  direction_t d;
  while(1){
    choosePiece(st, p);
    printf("you chose (%d, %d): choose direction\n", p->x, p->y);
    printf("press 'L' or 'R'\n");
    
    int c = tolower(getchar());
    getchar();
    switch(c){
    case 'r':
      d = R;
      break;
    case 'l':
      d = L;
      break;
    default:
      d = other;
      break;
    }
    if(move(st, p, d) == 1){
      printf("moving failed");//動かすのに失敗したらやりなおし
      continue;
    }
    break;
  }

  return 0;
}
Esempio n. 4
0
void Game::newGame() {
    for(int i = 0; i < gridWidth * gridHeight; ++i)
        this->grid[i] = 0;

    score = 0;
    ticks = defTicks;

    choosePiece();
}
Esempio n. 5
0
void Game::update() {
    running = running && this->window.getKey(GLFW_KEY_ESCAPE) == GLFW_RELEASE;

    tick--;
    if(tick == 0 || (this->window.getKey(GLFW_KEY_DOWN) != GLFW_RELEASE && !prevDown)) {
        tick = ticks;

        bool canmove = true;

        const int *p = this->piece.getArray();

        posy++;

        for(int y = 0; y < 4 && canmove; ++y)
            for(int x = 0; x < 4 && canmove; ++x) {
                if(p[y * 4 + x] != 0 && posy + y >= 0)
                    canmove = canmove && (posy + y < gridHeight) && (this->grid[(posy + y) * gridWidth + posx + x] == 0);
            }

        if(canmove)
            passGridBuffer();
        else {
            posy--;
            if(!moved) {
                for(int y = 3; y >= 0; --y) {
                    for(int x = 0; x < 4; ++x) {
                        if(p[y * 4 + x] != 0 && posy + y < 0) {
                            losses++;
                            newGame();
                            prevLeft = this->window.getKey(GLFW_KEY_LEFT) != GLFW_RELEASE;
                            prevRight = this->window.getKey(GLFW_KEY_RIGHT) != GLFW_RELEASE;
                            prevUp = this->window.getKey(GLFW_KEY_UP) != GLFW_RELEASE;
                            prevZ = this->window.getKey(GLFW_KEY_SEMICOLON) != GLFW_RELEASE;
                            prevDown = this->window.getKey(GLFW_KEY_DOWN) != GLFW_RELEASE;
                            prevSpace = this->window.getKey(GLFW_KEY_SPACE) != GLFW_RELEASE;
                            prevShift = this->window.getKey(GLFW_KEY_LEFT_SHIFT) != GLFW_RELEASE;
                            return;
                        }
                        if(p[y * 4 + x] != 0)
                            this->grid[(posy + y) * gridWidth + posx + x] = p[y * 4 + x];
                    }
                }
                for(int y = posy + 3; y >= 1; --y) {
                    bool line = true;
                    for(int x = 0; x < gridWidth && line; ++x)
                        line = line && (this->grid[y * gridWidth + x] != 0);
                    if(line) {
                        for(int ly = y - 1; ly >= 0; --ly)
                            for(int x = 0; x < gridWidth; ++x)
                                this->grid[(ly + 1) * gridWidth + x] = this->grid[ly * gridWidth + x];
                        score++;
                        ticks-=2;
                        if(ticks < 1)
                            ticks = 1;
                        y++;
                    }
                }
                held = false;
                choosePiece();
            }
            moved = false;
        }
    }
    if(this->window.getKey(GLFW_KEY_SPACE) != GLFW_RELEASE && !prevSpace) {
        bool canmove = true;

        const int *p = this->piece.getArray();

        while(canmove) {

            posy++;

            for(int y = 3; y >= 0 && canmove; --y)
                for(int x = 0; x < 4 && canmove; ++x) {
                    if(p[y * 4 + x] != 0 && posy + y >= 0)
                        canmove = canmove && (posy + y < gridHeight) && (this->grid[(posy + y) * gridWidth + posx + x] == 0);
                }
        }

        posy--;
        for(int y = 0; y < 4; ++y) {
            for(int x = 0; x < 4; ++x) {
                if(p[y * 4 + x] != 0 && posy + y < 0) {
                    losses++;
                    newGame();
                    prevLeft = this->window.getKey(GLFW_KEY_LEFT) != GLFW_RELEASE;
                    prevRight = this->window.getKey(GLFW_KEY_RIGHT) != GLFW_RELEASE;
                    prevUp = this->window.getKey(GLFW_KEY_UP) != GLFW_RELEASE;
                    prevZ = this->window.getKey(GLFW_KEY_SEMICOLON) != GLFW_RELEASE;
                    prevDown = this->window.getKey(GLFW_KEY_DOWN) != GLFW_RELEASE;
                    prevSpace = this->window.getKey(GLFW_KEY_SPACE) != GLFW_RELEASE;
                    prevShift = this->window.getKey(GLFW_KEY_LEFT_SHIFT) != GLFW_RELEASE;
                    return;
                }
                if(p[y * 4 + x] != 0)
                    this->grid[(posy + y) * gridWidth + posx + x] = p[y * 4 + x];
            }
        }
        for(int y = posy + 3; y >= 1; --y) {
            bool line = true;
            for(int x = 0; x < gridWidth && line; ++x)
                line = line && (this->grid[y * gridWidth + x] != 0);
            if(line) {
                for(int ly = y - 1; ly >= 0; --ly) {
                    for(int x = 0; x < gridWidth; ++x)
                        this->grid[(ly + 1) * gridWidth + x] = this->grid[ly * gridWidth + x];
                }
                y++;
                ticks-=2;
                if(ticks < 1)
                    ticks = 1;
                score++;
            }
        }
        held = false;
        choosePiece();
    }

    if(this->window.getKey(GLFW_KEY_LEFT) != GLFW_RELEASE && !prevLeft) {
        bool canmove = true;

        const int *p = this->piece.getArray();

        posx--;

        for(int y = 0; y < 4 && canmove; ++y)
            for(int x = 0; x < 4 && canmove; ++x) {
                if(p[y * 4 + x] != 0 && posy + y >= 0)
                    canmove = canmove && (posx + x >= 0) && (posy + y < 0 || this->grid[(posy + y) * gridWidth + posx + x] == 0);
            }

        moved = canmove;

        if(canmove)
            passGridBuffer();
        else
            posx++;
    }
    else if(this->window.getKey(GLFW_KEY_RIGHT) != GLFW_RELEASE && !prevRight) {
        bool canmove = true;

        const int *p = this->piece.getArray();

        posx++;

        for(int y = 0; y < 4 && canmove; ++y)
            for(int x = 0; x < 4 && canmove; ++x) {
                if(p[y * 4 + x] != 0)
                    canmove = canmove && (posx + x < gridWidth) && (posy + y < 0 || this->grid[(posy + y) * gridWidth + posx + x] == 0);
            }

        moved = canmove;

        if(canmove)
            passGridBuffer();
        else
            posx--;
    }
    else if(this->window.getKey(GLFW_KEY_UP) != GLFW_RELEASE && !prevUp) {
        Mino newMino(this->piece);

        newMino.rotClock();

        const int *p = newMino.getArray();

        bool canrotate = true;

        for(int y = 0; y < 4 && canrotate; ++y)
            for(int x = 0; x < 4 && canrotate; ++x) {
                if(p[y * 4 + x] != 0)
                    canrotate = canrotate && (posx + x < gridWidth) && (posx + x >= 0) && (posy + y < 0 || this->grid[(posy + y) * gridWidth + posx + x] == 0);
            }

        moved = canrotate;

        if(canrotate) {
            this->piece = newMino;
            passGridBuffer();
        }
    }
    else if(this->window.getKey(GLFW_KEY_SEMICOLON) != GLFW_RELEASE && !prevZ) {
        Mino newMino(this->piece);

        newMino.rotCounterClock();

        const int *p = newMino.getArray();

        bool canrotate = true;

        for(int y = 0; y < 4 && canrotate; ++y)
            for(int x = 0; x < 4 && canrotate; ++x) {
                if(p[y * 4 + x] != 0)
                    canrotate = canrotate && (posx + x < gridWidth) && (posx + x >= 0) && (this->grid[(posy + y) * gridWidth + posx + x] == 0);
            }

        moved = canrotate;

        if(canrotate) {
            this->piece = newMino;
            passGridBuffer();
        }
    }

    if(this->window.getKey(GLFW_KEY_LEFT_SHIFT) != GLFW_RELEASE && !prevShift && !held) {
        held = true;

        Mino newMino(this->piece);
        this->piece = this->hold;
        this->hold = newMino;

        this->posx = 3;
        this->posy = this->piece.startOffset();

        passGridBuffer();
    }

    prevLeft = this->window.getKey(GLFW_KEY_LEFT) != GLFW_RELEASE;
    prevRight = this->window.getKey(GLFW_KEY_RIGHT) != GLFW_RELEASE;
    prevUp = this->window.getKey(GLFW_KEY_UP) != GLFW_RELEASE;
    prevZ = this->window.getKey(GLFW_KEY_SEMICOLON) != GLFW_RELEASE;
    prevDown = this->window.getKey(GLFW_KEY_DOWN) != GLFW_RELEASE;
    prevSpace = this->window.getKey(GLFW_KEY_SPACE) != GLFW_RELEASE;
    prevShift = this->window.getKey(GLFW_KEY_LEFT_SHIFT) != GLFW_RELEASE;
}