Skip to content

Commit

Permalink
refactor: use camel_case names for naming functions
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Agarwal <[email protected]>
  • Loading branch information
Aditya-A-garwal committed May 23, 2024
1 parent 77997f6 commit 4443b78
Showing 1 changed file with 50 additions and 55 deletions.
105 changes: 50 additions & 55 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ int16_t grid[3][3] = {

// ----- begin helper math functions -----

bool inRange(uint16_t value, uint16_t lo, uint16_t hi) {
bool in_range(uint16_t value, uint16_t lo, uint16_t hi) {
return lo <= value && value <= hi;
}

// ----- begin functions to handle touch input -----

void toDisplayMode() {
void to_display_mode() {

pinMode(XM, OUTPUT);
pinMode(XP, OUTPUT);
pinMode(YM, OUTPUT);
pinMode(YP, OUTPUT);
}

void convertTouchCoors(uint16_t tx, uint16_t ty, uint16_t *xptr, uint16_t *yptr) {
void convert_touch_coors(uint16_t tx, uint16_t ty, uint16_t *xptr, uint16_t *yptr) {

tx = constrain(tx, XBEGIN, XEND);
ty = constrain(ty, YBEGIN, YEND);
Expand All @@ -48,28 +48,28 @@ void convertTouchCoors(uint16_t tx, uint16_t ty, uint16_t *xptr, uint16_t *yptr)
*yptr = ty;
}

void getTouchCoors(uint16_t *xptr, uint16_t *yptr) {
void get_touch_coors(uint16_t *xptr, uint16_t *yptr) {

TSPoint p;

for (;;) {

p = ts.getPoint();
if (inRange(p.z, PRESSURE_LEFT, PRESSURE_RIGHT)) {
if (in_range(p.z, PRESSURE_LEFT, PRESSURE_RIGHT)) {
break;
}
}
convertTouchCoors(p.x, p.y, xptr, yptr);
toDisplayMode();
convert_touch_coors(p.x, p.y, xptr, yptr);
to_display_mode();
}

bool getGridTouchCoors(uint16_t x, uint16_t y, uint16_t *grid_x, uint16_t *grid_y) {
bool get_grid_touch_coors(uint16_t x, uint16_t y, uint16_t *grid_x, uint16_t *grid_y) {

for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {

if (inRange(x - GRID_X - i*CELL_W, 0, CELL_W)
&& inRange(y - GRID_Y - j*CELL_W, 0, CELL_W)) {
if (in_range(x - GRID_X - i*CELL_W, 0, CELL_W)
&& in_range(y - GRID_Y - j*CELL_W, 0, CELL_W)) {

if (grid[i][j] == NONE) {

Expand All @@ -86,23 +86,23 @@ bool getGridTouchCoors(uint16_t x, uint16_t y, uint16_t *grid_x, uint16_t *grid_

// ----- begin functions to draw complex shapes -----

void drawThickHLine(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {
void draw_thick_hline(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {

const int LINE_WIDTH = 7;
tft.fillRect(x, y - LINE_WIDTH/2, d, LINE_WIDTH, clr);
tft.fillCircle(x, y, LINE_WIDTH/2, clr);
tft.fillCircle(x + d, y, LINE_WIDTH/2, clr);
}

void drawThickVLine(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {
void draw_thick_vline(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {

const int LINE_WIDTH = 7;
tft.fillRect(x - LINE_WIDTH/2, y, LINE_WIDTH, d, clr);
tft.fillCircle(x, y, LINE_WIDTH/2, clr);
tft.fillCircle(x, y + d, LINE_WIDTH/2, clr);
}

void drawThickDownDiagonal(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {
void draw_thick_down_diagonal(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {

const int LINE_WIDTH = 5;
for (uint16_t i = 0; i < LINE_WIDTH/2; ++i) {
Expand All @@ -116,7 +116,7 @@ void drawThickDownDiagonal(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {
tft.fillCircle(x + d, y + d, LINE_WIDTH/2, clr);
}

void drawThickUpDiagonal(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {
void draw_thick_up_diagonal(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {

const int LINE_WIDTH = 5;
for (uint16_t i = 0; i < LINE_WIDTH/2; ++i) {
Expand All @@ -130,13 +130,13 @@ void drawThickUpDiagonal(uint16_t x, uint16_t y, uint16_t d, uint16_t clr) {
tft.fillCircle(x + d, y - d, LINE_WIDTH/2, clr);
}

void drawNaught(uint16_t x, uint16_t y, uint16_t clr) {
void draw_naught(uint16_t x, uint16_t y, uint16_t clr) {

tft.fillCircle(x, y, MARK_W/2, clr);
tft.fillCircle(x, y, (MARK_W - MARK_THICKNESS)/2, BLACK);
}

void drawCross(uint16_t x, uint16_t y, uint16_t clr) {
void draw_cross(uint16_t x, uint16_t y, uint16_t clr) {

tft.fillRoundRect(x - MARK_W/2, y - MARK_W/2, MARK_W, MARK_W, MARK_THICKNESS/2, clr);

Expand Down Expand Up @@ -175,7 +175,7 @@ void drawCross(uint16_t x, uint16_t y, uint16_t clr) {

// ----- begin functions to draw widgets -----

void drawMenuItem(uint16_t offset, uint16_t clr, const char msg[]) {
void draw_menu_item(uint16_t offset, uint16_t clr, const char msg[]) {

tft.drawRect(ITEM_X, MENU_Y + offset, ITEM_W, ITEM_H, clr);

Expand All @@ -185,7 +185,7 @@ void drawMenuItem(uint16_t offset, uint16_t clr, const char msg[]) {
tft.println(msg);
}

void drawMenu() {
void draw_menu() {

// draw the boundary and show the title
tft.drawRect(MENU_X, MENU_Y, MENU_W, MENU_HEIGHT, YELLOW);
Expand All @@ -195,20 +195,20 @@ void drawMenu() {
tft.print("TIC-TAC-TOE");

// show the starting options
drawMenuItem(ITEM_H*1, RED, "PLAY AS X");
drawMenuItem(ITEM_H*3, BLUE, "PLAY AS O");
draw_menu_item(ITEM_H*1, RED, "PLAY AS X");
draw_menu_item(ITEM_H*3, BLUE, "PLAY AS O");
}

void drawGrid() {
void draw_grid() {

drawThickHLine(GRID_X, GRID_Y + CELL_W, GRID_W, WHITE);
drawThickHLine(GRID_X, GRID_Y + 2*CELL_W, GRID_W, WHITE);
draw_thick_hline(GRID_X, GRID_Y + CELL_W, GRID_W, WHITE);
draw_thick_hline(GRID_X, GRID_Y + 2*CELL_W, GRID_W, WHITE);

drawThickVLine(GRID_X + CELL_W, GRID_Y, GRID_W, WHITE);
drawThickVLine(GRID_X + 2*CELL_W, GRID_Y, GRID_W, WHITE);
draw_thick_vline(GRID_X + CELL_W, GRID_Y, GRID_W, WHITE);
draw_thick_vline(GRID_X + 2*CELL_W, GRID_Y, GRID_W, WHITE);
}

void drawWinner(int16_t winner) {
void draw_winner(int16_t winner) {

tft.setTextSize(3);
tft.setTextColor((winner == CROSS) ? RED : BLUE);
Expand All @@ -222,7 +222,7 @@ void drawWinner(int16_t winner) {
tft.print("GAME OVER");
}

void drawTie() {
void draw_tie() {

tft.setTextSize(3);
tft.setTextColor(GRAY);
Expand All @@ -236,43 +236,45 @@ void drawTie() {

// ----- begin functions to update widgets -----

void updateMenu() {
void choose_piece() {

uint16_t x, y;

draw_menu();

for (;;) {

// wait till a valid touch is detected
getTouchCoors(&x, &y);
get_touch_coors(&x, &y);

// Touch not within menu
if (!inRange(x, ITEM_X, ITEM_X + ITEM_W)) {
if (!in_range(x, ITEM_X, ITEM_X + ITEM_W)) {
continue;
}

// player has chosen cross
if (inRange(y, MENU_Y + ITEM_H*1, MENU_Y + ITEM_H*2)) {
if (in_range(y, MENU_Y + ITEM_H*1, MENU_Y + ITEM_H*2)) {
player = CROSS;
break;
}

// player has chosen naught
if (inRange(y, MENU_Y + ITEM_H*3, MENU_Y + ITEM_H*4)) {
if (in_range(y, MENU_Y + ITEM_H*3, MENU_Y + ITEM_H*4)) {
player = NAUGHT;
break;
}
}
}


uint16_t getWinner() {
uint16_t get_winner() {

// check for vertical groups
for (uint16_t i = 0; i < 3; ++i) {

if (grid[i][0] != NONE && grid[i][0] == grid[i][1] && grid[i][1] == grid[i][2]) {

drawThickVLine(GRID_X + CELL_W/2 + CELL_W * i, GRID_Y + CELL_W/2, CELL_W * 2, GREEN);
draw_thick_vline(GRID_X + CELL_W/2 + CELL_W * i, GRID_Y + CELL_W/2, CELL_W * 2, GREEN);
return grid[i][0];
}
}
Expand All @@ -282,52 +284,45 @@ uint16_t getWinner() {

if (grid[0][i] != NONE && grid[0][i] == grid[1][i] && grid[1][i] == grid[2][i]) {

drawThickHLine(GRID_X + CELL_W/2, GRID_Y + CELL_W/2 + CELL_W * i, CELL_W * 2, GREEN);
draw_thick_hline(GRID_X + CELL_W/2, GRID_Y + CELL_W/2 + CELL_W * i, CELL_W * 2, GREEN);
return grid[0][i];
}
}

// check diagonals
if (grid[0][0] != NONE && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]) {

drawThickDownDiagonal(GRID_X + CELL_W/2, GRID_Y + CELL_W/2, CELL_W * 2, GREEN);
draw_thick_down_diagonal(GRID_X + CELL_W/2, GRID_Y + CELL_W/2, CELL_W * 2, GREEN);
return grid[0][0];
}

if (grid[2][0] != NONE && grid[2][0] == grid[1][1] && grid[1][1] == grid[0][2]) {

drawThickUpDiagonal(GRID_X + CELL_W/2, GRID_Y + CELL_W/2 + CELL_W * 2, CELL_W * 2, GREEN);
draw_thick_up_diagonal(GRID_X + CELL_W/2, GRID_Y + CELL_W/2 + CELL_W * 2, CELL_W * 2, GREEN);
return grid[2][0];
}

return NONE;
}

bool getTie() {
bool get_tie() {

return ++moves == 9;
}

void setup() {

// Use Serial Monitor for debugging
Serial.begin(115200);
tft.begin(0x9486);

// Fill the screen black for a fresh start
tft.fillScreen(BLACK);

// draw the menu and wait for a selection
drawMenu();
updateMenu();
choose_piece();

// fill the screen black to clear the menu
tft.fillScreen(BLACK);

delay(300); // eases transition from menu

// draw the grid
drawGrid();
draw_grid();
}

void loop() {
Expand All @@ -354,9 +349,9 @@ void loop() {

for (;;) {

getTouchCoors(&x, &y);
get_touch_coors(&x, &y);

if (getGridTouchCoors(x, y, &grid_x, &grid_y)) {
if (get_grid_touch_coors(x, y, &grid_x, &grid_y)) {
break;
}
}
Expand All @@ -365,16 +360,16 @@ void loop() {
// update the grid and display the move
grid[grid_x][grid_y] = turn;
(turn == CROSS)
? drawCross(GRID_X + CELL_W/2 + grid_x*CELL_W, GRID_Y + CELL_W/2 + grid_y*CELL_W, RED)
: drawNaught(GRID_X + CELL_W/2 + grid_x*CELL_W, GRID_Y + CELL_W/2 + grid_y*CELL_W, BLUE);
? draw_cross(GRID_X + CELL_W/2 + grid_x*CELL_W, GRID_Y + CELL_W/2 + grid_y*CELL_W, RED)
: draw_naught(GRID_X + CELL_W/2 + grid_x*CELL_W, GRID_Y + CELL_W/2 + grid_y*CELL_W, BLUE);

if ((winner = getWinner()) != NONE) {
drawWinner(winner);
if ((winner = get_winner()) != NONE) {
draw_winner(winner);
for (;;);
}

if (getTie()) {
drawTie();
if (get_tie()) {
draw_tie();
for (;;);
}

Expand Down

0 comments on commit 4443b78

Please sign in to comment.