vector<Tile*> Pawn::getLegalMoves() { int teamDirection; if (player->getSide() == WHITE) { teamDirection = 1; } else { teamDirection = -1; } vector<Coordinate> moves; if (board->getTile(Coordinate(x, y + (1 * teamDirection)))->piece == nullptr) { moves.push_back(Coordinate(x, y + (1 * teamDirection))); Coordinate twoAhead(x, y + (2 * teamDirection)); if (!hasMoved && player->board->getTile(twoAhead)->piece == nullptr) { moves.push_back(twoAhead); } } vector<Tile*> legalMoves; for (int i = 0; i < moves.size(); i++) { Coordinate move = moves[i]; if (moveIsPossible(move)) { legalMoves.push_back(board->getTile(move)); } } Tile* diagonalLeft = board->getTile(Coordinate(x - 1, y + teamDirection)); if (moveIsPossible(diagonalLeft)) { // if the tile exists if (diagonalLeft->piece != nullptr) { // if the tile contains a piece if (diagonalLeft->piece->getSide() != this->side) { // if it's an enemy legalMoves.push_back(diagonalLeft); } } } Tile* diagonalRight = board->getTile(Coordinate(x + 1, y + teamDirection)); if (moveIsPossible(diagonalRight)) { if (diagonalRight->piece != nullptr) { if (diagonalRight->piece->getSide() != this->side) { legalMoves.push_back(diagonalRight); } } } return legalMoves; }
void grid::Grid::move( Direction direction ) { m_InMove = true; m_Direction = direction; switch( direction ) { case Direction::UP: for( int i = 0; i < m_Grid.size(); i++ ) { for( int j = 0; j < m_Grid[0].size(); j++ ) { if( m_Grid[i][j].get() != 0 ) { bool move; move = false; egn::FloatRect position; ref ref_tmp; for( int k = i-1; k >= 0; k-- ) { if( m_Grid[k][j].get() == 0 || m_Grid[i][j].get() == m_Grid[k][j].get() ) { position = m_Grid[k][j].getPosition(); move = true; ref_tmp.i = k; ref_tmp.j = j; } else { k = 0; } } if( move == true ) { grid::Number number = grid::Number( m_Grid[i][j].get(), m_Grid[i][j].getPosition() ); number.setNextPosition( position ); m_Grid[ref_tmp.i][ref_tmp.j].setDraw( false ); if( m_Grid[ref_tmp.i][ref_tmp.j].get() == 0 ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() ); } else if( m_Grid[ref_tmp.i][ref_tmp.j].get() == m_Grid[i][j].get() ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() * 2 ); m_PlayerInfo.setScore(m_PlayerInfo.getScore() + m_Grid[i][j].get()*2); } m_Grid[i][j].set( 0 ); number.move( Direction::UP, ref_tmp ); m_MoveNumber.push_back( number ); } } } } break; case Direction::DOWN: for( int i = m_Grid.size() - 1; i >= 0; i-- ) { for( int j = m_Grid[0].size() - 1; j >= 0; j-- ) { if( m_Grid[i][j].get() != 0 ) { bool move; move = false; egn::FloatRect position; ref ref_tmp; for( int k = i+1; k < m_Grid.size(); k++ ) { if( m_Grid[k][j].get() == 0 || m_Grid[i][j].get() == m_Grid[k][j].get() ) { position = m_Grid[k][j].getPosition(); move = true; ref_tmp.i = k; ref_tmp.j = j; } else { k = m_Grid.size(); } } if( move == true ) { grid::Number number = grid::Number( m_Grid[i][j].get(), m_Grid[i][j].getPosition() ); number.setNextPosition( position ); m_Grid[ref_tmp.i][ref_tmp.j].setDraw( false ); if( m_Grid[ref_tmp.i][ref_tmp.j].get() == 0 ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() ); } else if( m_Grid[ref_tmp.i][ref_tmp.j].get() == m_Grid[i][j].get() ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() * 2 ); m_PlayerInfo.setScore(m_PlayerInfo.getScore() + m_Grid[i][j].get()*2); } m_Grid[i][j].set( 0 ); number.move( Direction::DOWN, ref_tmp ); m_MoveNumber.push_back( number ); } } } } break; case Direction::LEFT: for( int i = 0; i < m_Grid.size(); i++ ) { for( int j = 0; j < m_Grid[0].size(); j++ ) { if( m_Grid[i][j].get() != 0 ) { bool move; move = false; egn::FloatRect position; ref ref_tmp; for( int k = j-1; k >= 0; k--) { if( m_Grid[i][k].get() == 0 || m_Grid[i][j].get() == m_Grid[i][k].get() ) { position = m_Grid[i][k].getPosition(); move = true; ref_tmp.i = i; ref_tmp.j = k; } else { k = 0; } } if( move == true ) { grid::Number number = grid::Number( m_Grid[i][j].get(), m_Grid[i][j].getPosition() ); number.setNextPosition( position ); m_Grid[ref_tmp.i][ref_tmp.j].setDraw( false ); if( m_Grid[ref_tmp.i][ref_tmp.j].get() == 0 ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() ); } else if( m_Grid[ref_tmp.i][ref_tmp.j].get() == m_Grid[i][j].get() ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() * 2 ); m_PlayerInfo.setScore(m_PlayerInfo.getScore() + m_Grid[i][j].get()*2); } m_Grid[i][j].set( 0 ); number.move( Direction::LEFT, ref_tmp ); m_MoveNumber.push_back( number ); } } } } break; case Direction::RIGHT: for( int i = 0; i < m_Grid.size(); i++ ) { for( int j = m_Grid[0].size() - 1; j >= 0; j-- ) { if( m_Grid[i][j].get() != 0 ) { bool move; move = false; egn::FloatRect position; ref ref_tmp; for( int k = j+1; k < m_Grid[0].size(); k++ ) { if( m_Grid[i][k].get() == 0 || m_Grid[i][j].get() == m_Grid[i][k].get() ) { position = m_Grid[i][k].getPosition(); move = true; ref_tmp.i = i; ref_tmp.j = k; } else { k = m_Grid[0].size(); } } if( move == true ) { grid::Number number = grid::Number( m_Grid[i][j].get(), m_Grid[i][j].getPosition() ); number.setNextPosition( position ); m_Grid[ref_tmp.i][ref_tmp.j].setDraw( false ); if( m_Grid[ref_tmp.i][ref_tmp.j].get() == 0 ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() ); } else if( m_Grid[ref_tmp.i][ref_tmp.j].get() == m_Grid[i][j].get() ) { m_Grid[ref_tmp.i][ref_tmp.j].set( m_Grid[i][j].get() * 2 ); m_PlayerInfo.setScore(m_PlayerInfo.getScore() + m_Grid[i][j].get()*2); } m_Grid[i][j].set( 0 ); number.move( Direction::RIGHT, ref_tmp ); m_MoveNumber.push_back( number ); } } } } break; } if( isFull() && !moveIsPossible() ) { if( m_Continue && !m_Challenge ) { GameState::set( Gamestate::MENU_WIN ); } else { if( !m_Challenge ) GameState::set( Gamestate::MENU_LOSE ); else loseChallenge(); } } }
void grid::Grid::update() { if( egn::Keyboard::isActive( "return" ) ) { GameState::set( Gamestate::MENU_PAUSE ); egn::Keyboard::setActive("return",false); } if( egn::Keyboard::isActive( "space" ) ) { cheat( 1024 ); egn::Keyboard::setActive( "space", false ); } if( m_InMove == false ) { if( egn::Keyboard::isActive("up") ) { move(Direction::UP); egn::Keyboard::setActive("up",false); egn::AudioManager::get()->playSound("data/audio/theme1.ogg"); } else if( egn::Keyboard::isActive("down") ) { move(Direction::DOWN); egn::Keyboard::setActive("down",false); egn::AudioManager::get()->playSound("data/audio/theme1.ogg"); } else if( egn::Keyboard::isActive("left") ) { move(Direction::LEFT); egn::Keyboard::setActive("left",false); egn::AudioManager::get()->playSound("data/audio/theme1.ogg"); } else if( egn::Keyboard::isActive("right") ) { move(Direction::RIGHT); egn::Keyboard::setActive("right",false); egn::AudioManager::get()->playSound("data/audio/theme1.ogg"); } } for( int i = 0; i < m_MoveNumber.size(); i++ ) { m_MoveNumber[i].update(); if( m_MoveNumber[i].inMove() == false ) { ref ref_tmp = m_MoveNumber[i].getRef(); m_Grid[ref_tmp.i][ref_tmp.j].setDraw( true ); if( m_Grid[ref_tmp.i][ref_tmp.j].get() == 2048 && m_Continue == false && !m_Challenge ) { m_Continue = true; GameState::set(Gamestate::MENU_CONTINUE); m_InMove = false; } } } for( int i = 0; i < m_MoveNumber.size(); i++ ) { if( m_MoveNumber[i].inMove() == false ) { m_MoveNumber.erase( m_MoveNumber.begin() + i ); } } if( m_MoveNumber.size() <= 0 && m_InMove == true ) { if( !moveIsPossible( m_Direction ) ) { m_InMove = false; if( !isFull() ) { popNumber( 1 ); } } else { move( m_Direction ); } } m_PlayerInfo.update(); }