//frontier for hegemony int frontier (char * b, char color) { int f = 0; int in_frontier = 0; int i; int j; for (i = 0; i<BOARD_SIZE; i++) { for (j = 0; j<BOARD_SIZE; j++) { in_frontier = 0; if (get_cell(i,j,b) != color) { if (in_board(i,j+1)) { if (get_cell(i,j+1,b)==color) { in_frontier = 1; }} if (in_board(i,j-1)) { if (get_cell(i,j-1,b)==color) { in_frontier = 1; }} if (in_board(i+1,j)) { if (get_cell(i+1,j,b)==color) { in_frontier = 1; }} if (in_board(i-1,j)) { if (get_cell(i-1,j,b)==color) { in_frontier = 1; }} f += in_frontier; } } } return f; }
//available space for starve strategy int available(char* b, char color) { int matrix[BOARD_SIZE * BOARD_SIZE] = {0}; int av = 0; int otherPlayer = other(color); char change = 1; int i = 0; int j = 0; for (i = 0; i<BOARD_SIZE; i++) { for (j = 0; j<BOARD_SIZE; j++) { if (get_cell(i,j,b) == color) { matrix[j*BOARD_SIZE + i] = 1; av += 1; } } } while (change) { change = 0; for (i = 0; i < BOARD_SIZE; i++) { for (j = 0; j < BOARD_SIZE; j++) { if (get_cell(i,j,b) != otherPlayer && !matrix[j*BOARD_SIZE + i]){ if (in_board(i+1, j)) { if (matrix[j*BOARD_SIZE + i+1]){ matrix[j*BOARD_SIZE + i] = 1; change = 1; } } if (in_board(i-1, j)) { if (matrix[j*BOARD_SIZE + i-1]){ matrix[j*BOARD_SIZE + i] = 1; change = 1; } } if (in_board(i, j+1)) { if (matrix[(j+1)*BOARD_SIZE + i]) { matrix[j*BOARD_SIZE + i] = 1; change = 1; } } if (in_board(i, j-1)) { if (matrix[(j-1)*BOARD_SIZE + i]) { matrix[j*BOARD_SIZE + i] = 1; change = 1; } } av += matrix[j*BOARD_SIZE + i]; } } } } return av; }
//update, given player, choice and board void update_board(char player, char color, char * b) { int i,j; int change = 0; for (i=0; i<BOARD_SIZE; i++) { for (j=0; j<BOARD_SIZE; j++) { if (get_cell(i,j,b) == color) { if (in_board(i-1,j)) {if (get_cell(i-1,j,b) == player) {set_cell(i,j,player,b); change = 1;}} if (in_board(i+1,j)) {if (get_cell(i+1,j,b) == player) {set_cell(i,j,player,b); change = 1;}} if (in_board(i,j-1)) {if (get_cell(i,j-1,b) == player) {set_cell(i,j,player,b); change = 1;}} if (in_board(i,j+1)) {if (get_cell(i,j+1,b) == player) {set_cell(i,j,player,b); change = 1;}} } } } if (change) {update_board(player, color, b );} }
void game::on_event(const SDL_Event& event) { if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) { if (in_board()) { if (m_active_special) { const int digit = m_board.get_digit(m_selx, m_sely); // Attempt a special move if (m_active_special->m_type == special_move::nuke && digit == -1) { clear_3x3(m_selx, m_sely); m_active_special->m_type = special_move::none; } if (m_active_special->m_type == special_move::clear_digit && digit != -1) { for (int y = 0; y != 9; ++y) for (int x = 0; x != 9; ++x) if (m_board.get_digit(x, y) == digit) m_board.set_digit(x, y, -1); m_active_special->m_type = special_move::none; } m_active_special = 0; } else { do_digit(); } if (m_queue.digits_left() == 0 || m_board.get_filled() == 0) end_game(); } for (int i = 0; i != 3; ++i) if (m_specials[i].m_button.click(event.button) && m_specials[i].m_type != special_move::none) do_special(m_specials[i]); if (m_quit.click(event.button)) m_sm.set_next_state("title"); } if (event.type == SDL_MOUSEMOTION) { // Check if the cursor is inside the board int mx = event.motion.x; int my = event.motion.y; if (mx >= board_xpos && mx < (board_xpos + 32 * 9) && my >= board_ypos && my < (board_ypos + 32 * 9)) { m_selx = (mx - board_xpos) / 32; m_sely = (my - board_ypos) / 32; } else { m_selx = -1; m_sely = -1; } m_quit.motion(event.motion); for (int i = 0; i != 3; ++i) if (m_specials[i].m_type != special_move::none) m_specials[i].m_button.motion(event.motion); } }
void game::do_digit() { if (in_board() && m_board.get_digit(m_selx, m_sely) == -1) { int board_digit = m_board.get_3x3_sum(m_selx, m_sely) % 10; int queue_digit = m_queue.take(); if (board_digit == queue_digit) { // Ding, clear the 3x3 square int cleared = clear_3x3(m_selx, m_sely); if (cleared > 3) { m_bonus += cleared * 100; m_bonusanims.push_back(bonus_anim(m_bonus_gfx, cleared, board_xpos + (m_selx * 32) - 29, board_ypos + (m_sely * 32) - 8)); } } else { // Oh noes, place the queue digit m_board.set_digit(m_selx, m_sely, queue_digit); if (m_board.get_cleared() == 0) end_game(); } } }
void game::on_draw(sdl_surface& screen) { SDL_BlitSurface(m_bkg, 0, screen, 0); // TODO: Do this once to a surface at game instantiation SDL_Rect board_bkg_dest = {34, 100}; SDL_BlitSurface(m_board_bkg, 0, screen, &board_bkg_dest); SDL_Rect queue_bkg_dest = {354, 100}; SDL_BlitSurface(m_queue_bkg, 0, screen, &queue_bkg_dest); m_board.draw(screen, board_xpos, board_ypos); m_queue.draw(screen, queue_xpos, queue_ypos); for (std::list<tile_anim>::iterator ai = m_anims.begin(); ai != m_anims.end();) { ai->draw(screen); if (ai->tick()) ai = m_anims.erase(ai); else ++ai; } if (in_board() && m_board.get_digit(m_selx, m_sely) == -1) { // Draw the selection int lx = std::max(0, m_selx - 1); int hx = std::min(9, m_selx + 2); int ly = std::max(0, m_sely - 1); int hy = std::min(9, m_sely + 2); // Draw corners SDL_Rect dest, src = {0, 0, 8, 8}; // Top left dest.x = board_xpos + lx * 32 - 8; dest.y = board_ypos + ly * 32 - 8; SDL_BlitSurface(m_selection, &src, screen, &dest); src.x = 104; // Top right dest.x = board_xpos + hx * 32; dest.y = board_ypos + ly * 32 - 8; SDL_BlitSurface(m_selection, &src, screen, &dest); src.y = 104; // Bottom right dest.x = board_xpos + hx * 32; dest.y = board_ypos + hy * 32; SDL_BlitSurface(m_selection, &src, screen, &dest); src.x = 0; // Bottom left dest.x = board_xpos + lx * 32 - 8; dest.y = board_ypos + hy * 32; SDL_BlitSurface(m_selection, &src, screen, &dest); // Draw top & bottom src.x = 8; src.w = 32; for (int x = lx; x != hx; ++x) // Top & bottom { src.y = 0; dest.x = board_xpos + x * 32; dest.y = board_ypos + ly * 32 - 8; SDL_BlitSurface(m_selection, &src, screen, &dest); src.y = 104; dest.y = board_ypos + hy * 32; SDL_BlitSurface(m_selection, &src, screen, &dest); } src.y = 8; src.w = 8; src.h = 32; for (int y = ly; y != hy; ++y) // Left & right { src.x = 0; dest.x = board_xpos + lx * 32 - 8; dest.y = board_ypos + y * 32; SDL_BlitSurface(m_selection, &src, screen, &dest); src.x = 104; dest.x = board_xpos + hx * 32; SDL_BlitSurface(m_selection, &src, screen, &dest); } int queue_digit = m_queue.top(); if (queue_digit != -1) { // Fade the queue digit in a pleasant manner int digit_alpha = 96 + static_cast<int>(std::sin(SDL_GetTicks() / 300.0) * 48); SDL_Rect src = {Sint16(queue_digit * 32), 0, 32, 32}; SDL_Rect dest = {Sint16(board_xpos + m_selx * 32), Sint16(board_ypos + m_sely * 32)}; SDL_SetAlpha(m_digits, SDL_SRCALPHA, digit_alpha); SDL_BlitSurface(m_digits, &src, screen, &dest); SDL_SetAlpha(m_digits, 0, 255); } } m_quit.draw(screen); for (int i = 0; i != 3; ++i) { if (m_specials[i].m_type != special_move::none) { m_specials[i].m_button.draw(screen); } } for (auto ai = m_bonusanims.begin(); ai != m_bonusanims.end();) { ai->draw(screen); if (ai->tick()) { ai = m_bonusanims.erase(ai); } else { ++ai; } } }