2021-10-13 20:08:35 +00:00
|
|
|
#include "displayapp/screens/Twos.h"
|
2021-01-02 20:08:12 +00:00
|
|
|
#include <array>
|
2021-10-29 10:10:34 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <lvgl/lvgl.h>
|
2021-01-02 20:08:12 +00:00
|
|
|
#include <utility>
|
2021-10-29 10:10:34 +00:00
|
|
|
#include <vector>
|
2021-01-02 20:08:12 +00:00
|
|
|
|
|
|
|
using namespace Pinetime::Applications::Screens;
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
|
2021-01-02 20:08:12 +00:00
|
|
|
|
|
|
|
// create styles to apply to different valued tiles
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_style_init(&style_cell1);
|
|
|
|
lv_style_init(&style_cell2);
|
|
|
|
lv_style_init(&style_cell3);
|
|
|
|
lv_style_init(&style_cell4);
|
|
|
|
lv_style_init(&style_cell5);
|
|
|
|
|
|
|
|
lv_style_set_border_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
|
|
|
|
lv_style_set_border_width(&style_cell1, LV_STATE_DEFAULT, 3);
|
|
|
|
lv_style_set_bg_opa(&style_cell1, LV_STATE_DEFAULT, LV_OPA_COVER);
|
|
|
|
lv_style_set_bg_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xcdc0b4));
|
2021-04-15 23:18:32 +00:00
|
|
|
lv_style_set_pad_top(&style_cell1, LV_STATE_DEFAULT, 25);
|
|
|
|
lv_style_set_text_color(&style_cell1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
|
2021-01-02 20:08:12 +00:00
|
|
|
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_style_set_border_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
|
|
|
|
lv_style_set_border_width(&style_cell2, LV_STATE_DEFAULT, 3);
|
|
|
|
lv_style_set_bg_opa(&style_cell2, LV_STATE_DEFAULT, LV_OPA_COVER);
|
|
|
|
lv_style_set_bg_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xefdfc6));
|
2021-04-15 23:18:32 +00:00
|
|
|
lv_style_set_pad_top(&style_cell2, LV_STATE_DEFAULT, 25);
|
|
|
|
lv_style_set_text_color(&style_cell2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
|
2021-01-02 20:08:12 +00:00
|
|
|
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_style_set_border_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
|
|
|
|
lv_style_set_border_width(&style_cell3, LV_STATE_DEFAULT, 3);
|
|
|
|
lv_style_set_bg_opa(&style_cell3, LV_STATE_DEFAULT, LV_OPA_COVER);
|
|
|
|
lv_style_set_bg_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xef9263));
|
2021-04-15 23:18:32 +00:00
|
|
|
lv_style_set_pad_top(&style_cell3, LV_STATE_DEFAULT, 25);
|
2021-01-02 20:08:12 +00:00
|
|
|
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_style_set_border_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
|
|
|
|
lv_style_set_border_width(&style_cell4, LV_STATE_DEFAULT, 3);
|
|
|
|
lv_style_set_bg_opa(&style_cell4, LV_STATE_DEFAULT, LV_OPA_COVER);
|
|
|
|
lv_style_set_bg_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xf76142));
|
2021-04-15 23:18:32 +00:00
|
|
|
lv_style_set_pad_top(&style_cell4, LV_STATE_DEFAULT, 25);
|
2021-01-28 17:13:28 +00:00
|
|
|
|
|
|
|
lv_style_set_border_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
|
|
|
|
lv_style_set_border_width(&style_cell5, LV_STATE_DEFAULT, 3);
|
|
|
|
lv_style_set_bg_opa(&style_cell5, LV_STATE_DEFAULT, LV_OPA_COVER);
|
|
|
|
lv_style_set_bg_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0x007dc5));
|
2021-04-15 23:18:32 +00:00
|
|
|
lv_style_set_pad_top(&style_cell5, LV_STATE_DEFAULT, 25);
|
2021-01-02 20:08:12 +00:00
|
|
|
|
|
|
|
// format grid display
|
2021-04-18 17:28:14 +00:00
|
|
|
|
|
|
|
gridDisplay = lv_table_create(lv_scr_act(), nullptr);
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1, &style_cell1);
|
|
|
|
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL2, &style_cell2);
|
|
|
|
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL3, &style_cell3);
|
|
|
|
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4, &style_cell4);
|
|
|
|
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4 + 1, &style_cell5);
|
2021-01-02 20:08:12 +00:00
|
|
|
lv_table_set_col_cnt(gridDisplay, 4);
|
|
|
|
lv_table_set_row_cnt(gridDisplay, 4);
|
2021-04-18 17:28:14 +00:00
|
|
|
lv_table_set_col_width(gridDisplay, 0, LV_HOR_RES / 4);
|
|
|
|
lv_table_set_col_width(gridDisplay, 1, LV_HOR_RES / 4);
|
|
|
|
lv_table_set_col_width(gridDisplay, 2, LV_HOR_RES / 4);
|
|
|
|
lv_table_set_col_width(gridDisplay, 3, LV_HOR_RES / 4);
|
2021-01-02 20:08:12 +00:00
|
|
|
lv_obj_align(gridDisplay, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
|
|
|
|
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_obj_clean_style_list(gridDisplay, LV_TABLE_PART_BG);
|
|
|
|
|
2021-01-02 20:08:12 +00:00
|
|
|
// initialize grid
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int col = 0; col < 4; col++) {
|
2021-01-02 20:08:12 +00:00
|
|
|
grid[row][col].value = 0;
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_table_set_cell_type(gridDisplay, row, col, 1);
|
2021-01-02 20:08:12 +00:00
|
|
|
lv_table_set_cell_align(gridDisplay, row, col, LV_LABEL_ALIGN_CENTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
placeNewTile();
|
|
|
|
placeNewTile();
|
|
|
|
|
|
|
|
// format score text
|
|
|
|
scoreText = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_width(scoreText, LV_HOR_RES);
|
|
|
|
lv_label_set_align(scoreText, LV_ALIGN_IN_LEFT_MID);
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_obj_align(scoreText, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 10);
|
|
|
|
lv_label_set_recolor(scoreText, true);
|
|
|
|
lv_label_set_text_fmt(scoreText, "Score #FFFF00 %i#", score);
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
lv_obj_t* backgroundLabel = lv_label_create(lv_scr_act(), nullptr);
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP);
|
|
|
|
lv_obj_set_size(backgroundLabel, 240, 240);
|
|
|
|
lv_obj_set_pos(backgroundLabel, 0, 0);
|
2022-03-20 14:47:25 +00:00
|
|
|
lv_label_set_text_static(backgroundLabel, "");
|
2021-01-02 20:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Twos::~Twos() {
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_style_reset(&style_cell1);
|
|
|
|
lv_style_reset(&style_cell2);
|
|
|
|
lv_style_reset(&style_cell3);
|
|
|
|
lv_style_reset(&style_cell4);
|
|
|
|
lv_style_reset(&style_cell5);
|
2021-01-02 20:08:12 +00:00
|
|
|
lv_obj_clean(lv_scr_act());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Twos::placeNewTile() {
|
2021-04-18 17:28:14 +00:00
|
|
|
std::vector<std::pair<int, int>> availableCells;
|
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int col = 0; col < 4; col++) {
|
|
|
|
if (!grid[row][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
availableCells.push_back(std::make_pair(row, col));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (availableCells.size() == 0) {
|
|
|
|
return false; // game lost
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2021-01-02 20:08:12 +00:00
|
|
|
auto it = availableCells.cbegin();
|
|
|
|
int random = rand() % availableCells.size();
|
|
|
|
std::advance(it, random);
|
2021-04-18 17:28:14 +00:00
|
|
|
std::pair<int, int> newCell = *it;
|
2021-01-02 20:08:12 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if ((rand() % 100) < 90)
|
|
|
|
grid[newCell.first][newCell.second].value = 2;
|
|
|
|
else
|
|
|
|
grid[newCell.first][newCell.second].value = 4;
|
2021-01-02 20:08:12 +00:00
|
|
|
updateGridDisplay(grid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-05 20:22:06 +00:00
|
|
|
bool Twos::tryMerge(TwosTile grid[][4], int& newRow, int& newCol, int oldRow, int oldCol) {
|
2022-03-13 21:03:29 +00:00
|
|
|
if (grid[newRow][newCol].value == grid[oldRow][oldCol].value) {
|
2021-04-18 17:28:14 +00:00
|
|
|
if ((newCol != oldCol) || (newRow != oldRow)) {
|
|
|
|
if (!grid[newRow][newCol].merged) {
|
2021-01-02 20:08:12 +00:00
|
|
|
unsigned int newVal = grid[oldRow][oldCol].value *= 2;
|
|
|
|
grid[newRow][newCol].value = newVal;
|
|
|
|
score += newVal;
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_label_set_text_fmt(scoreText, "Score #FFFF00 %i#", score);
|
2021-01-02 20:08:12 +00:00
|
|
|
grid[oldRow][oldCol].value = 0;
|
|
|
|
grid[newRow][newCol].merged = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-05 20:22:06 +00:00
|
|
|
bool Twos::tryMove(TwosTile grid[][4], int newRow, int newCol, int oldRow, int oldCol) {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (((newCol >= 0) && (newCol != oldCol)) || ((newRow >= 0) && (newRow != oldRow))) {
|
2021-01-02 20:08:12 +00:00
|
|
|
grid[newRow][newCol].value = grid[oldRow][oldCol].value;
|
|
|
|
grid[oldRow][oldCol].value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Twos::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
|
2021-04-08 15:15:57 +00:00
|
|
|
bool validMove = false;
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int col = 0; col < 4; col++) {
|
2021-01-02 20:08:12 +00:00
|
|
|
grid[row][col].merged = false; // reinitialize merge state
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
switch (event) {
|
2021-01-02 20:08:12 +00:00
|
|
|
case TouchEvents::SwipeLeft:
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int col = 1; col < 4; col++) { // ignore tiles already on far left
|
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
if (grid[row][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
int newCol = -1;
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int potentialNewCol = col - 1; potentialNewCol >= 0; potentialNewCol--) {
|
|
|
|
if (!grid[row][potentialNewCol].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
newCol = potentialNewCol;
|
2021-04-18 17:28:14 +00:00
|
|
|
} else { // blocked by another tile
|
|
|
|
if (tryMerge(grid, row, potentialNewCol, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
if (tryMove(grid, row, newCol, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (validMove) {
|
|
|
|
placeNewTile();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case TouchEvents::SwipeRight:
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int col = 2; col >= 0; col--) { // ignore tiles already on far right
|
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
if (grid[row][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
int newCol = -1;
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int potentialNewCol = col + 1; potentialNewCol < 4; potentialNewCol++) {
|
|
|
|
if (!grid[row][potentialNewCol].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
newCol = potentialNewCol;
|
2021-04-18 17:28:14 +00:00
|
|
|
} else { // blocked by another tile
|
|
|
|
if (tryMerge(grid, row, potentialNewCol, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
if (tryMove(grid, row, newCol, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (validMove) {
|
|
|
|
placeNewTile();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case TouchEvents::SwipeUp:
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int row = 1; row < 4; row++) { // ignore tiles already on top
|
|
|
|
for (int col = 0; col < 4; col++) {
|
|
|
|
if (grid[row][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
int newRow = -1;
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int potentialNewRow = row - 1; potentialNewRow >= 0; potentialNewRow--) {
|
|
|
|
if (!grid[potentialNewRow][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
newRow = potentialNewRow;
|
2021-04-18 17:28:14 +00:00
|
|
|
} else { // blocked by another tile
|
|
|
|
if (tryMerge(grid, potentialNewRow, col, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
if (tryMove(grid, newRow, col, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (validMove) {
|
|
|
|
placeNewTile();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case TouchEvents::SwipeDown:
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int row = 2; row >= 0; row--) { // ignore tiles already on bottom
|
|
|
|
for (int col = 0; col < 4; col++) {
|
|
|
|
if (grid[row][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
int newRow = -1;
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int potentialNewRow = row + 1; potentialNewRow < 4; potentialNewRow++) {
|
|
|
|
if (!grid[potentialNewRow][col].value) {
|
2021-01-02 20:08:12 +00:00
|
|
|
newRow = potentialNewRow;
|
2021-04-18 17:28:14 +00:00
|
|
|
} else { // blocked by another tile
|
|
|
|
if (tryMerge(grid, potentialNewRow, col, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
if (tryMove(grid, newRow, col, row, col))
|
|
|
|
validMove = true;
|
2021-01-02 20:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (validMove) {
|
|
|
|
placeNewTile();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-05 20:22:06 +00:00
|
|
|
void Twos::updateGridDisplay(TwosTile grid[][4]) {
|
2021-04-18 17:28:14 +00:00
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int col = 0; col < 4; col++) {
|
2021-01-02 20:08:12 +00:00
|
|
|
if (grid[row][col].value) {
|
2021-10-29 10:10:34 +00:00
|
|
|
char buffer[7];
|
|
|
|
sprintf(buffer, "%d", grid[row][col].value);
|
|
|
|
lv_table_set_cell_value(gridDisplay, row, col, buffer);
|
2021-04-18 17:28:14 +00:00
|
|
|
} else {
|
2021-01-02 20:08:12 +00:00
|
|
|
lv_table_set_cell_value(gridDisplay, row, col, "");
|
|
|
|
}
|
|
|
|
switch (grid[row][col].value) {
|
|
|
|
case 0:
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_table_set_cell_type(gridDisplay, row, col, 1);
|
|
|
|
break;
|
2021-01-02 20:08:12 +00:00
|
|
|
case 2:
|
|
|
|
case 4:
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_table_set_cell_type(gridDisplay, row, col, 2);
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
case 16:
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_table_set_cell_type(gridDisplay, row, col, 3);
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
case 64:
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_table_set_cell_type(gridDisplay, row, col, 4);
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-28 17:13:28 +00:00
|
|
|
lv_table_set_cell_type(gridDisplay, row, col, 5);
|
2021-01-02 20:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 13:26:12 +00:00
|
|
|
}
|