Tic Tac Toe application for SAM Xplained Pro with OLED1 Xplained Pro.
Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.
#include <asf.h>
#include <stdlib.h>
#include <stdio.h>
Macros | |
#define | CIRCLE_SIZE (SQUARE_SIZE / 4) |
#define | CROSS_SIZE (SQUARE_SIZE / 3) |
#define | NUMBER_OF_SQUARES 9 |
#define | SQUARE0_X 0 |
#define | SQUARE0_Y 0 |
#define | SQUARE1_X (SQUARE0_X + SQUARE_SIZE) |
#define | SQUARE1_Y SQUARE0_Y |
#define | SQUARE2_X (SQUARE1_X + SQUARE_SIZE) |
#define | SQUARE2_Y SQUARE0_Y |
#define | SQUARE3_X SQUARE0_X |
#define | SQUARE3_Y (SQUARE0_Y + SQUARE_SIZE) |
#define | SQUARE4_X SQUARE1_X |
#define | SQUARE4_Y SQUARE3_Y |
#define | SQUARE5_X SQUARE2_X |
#define | SQUARE5_Y SQUARE3_Y |
#define | SQUARE6_X SQUARE0_X |
#define | SQUARE6_Y (SQUARE3_Y + SQUARE_SIZE) |
#define | SQUARE7_X SQUARE1_X |
#define | SQUARE7_Y SQUARE6_Y |
#define | SQUARE8_X SQUARE2_X |
#define | SQUARE8_Y SQUARE6_Y |
#define | SQUARE_SIZE (LCD_HEIGHT_PIXELS / 3) |
#define | STRING_LENGTH 20 |
#define | STRING_X (SQUARE2_X + SQUARE_SIZE*2) |
Enumerations | |
enum | button { BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_NONE } |
enum | player { NONE, USER, COMPUTER } |
Functions | |
static void | draw_circle (uint8_t square_num) |
Draws a circle at selected point. More... | |
static void | draw_cross (uint8_t square_num) |
Draws a cross in selected square. More... | |
static enum button | get_button (void) |
Gets button push. More... | |
static void | highlight_square (uint8_t square_num) |
Highlights a square. More... | |
static void | init_buttons (void) |
Configures buttons. More... | |
static void | init_display (void) |
Initializes the display with explanatory text for the buttons. More... | |
int | main (void) |
Main entry of example application. More... | |
static void | opponent_turn (void) |
Selects a random, unoccupied square. More... | |
static void | setup_board (void) |
Draws the Tic-tac-toe board on the display. More... | |
static void | user_turn (void) |
User turn Waits for the user to push buttons and select a new square. More... | |
static uint8_t | we_have_a_winner (void) |
Checks for winner. More... | |
Variables | |
uint16_t | games = 0 |
enum player | occupied_squares [3][3] |
const uint8_t | square_coord [9][2] |
char | win_string [STRING_LENGTH] |
uint16_t | wins = 0 |
#define CIRCLE_SIZE (SQUARE_SIZE / 4) |
Referenced by draw_circle(), and init_display().
#define CROSS_SIZE (SQUARE_SIZE / 3) |
Referenced by draw_cross().
#define NUMBER_OF_SQUARES 9 |
#define SQUARE0_X 0 |
#define SQUARE0_Y 0 |
Referenced by setup_board().
#define SQUARE1_X (SQUARE0_X + SQUARE_SIZE) |
#define SQUARE1_Y SQUARE0_Y |
#define SQUARE2_X (SQUARE1_X + SQUARE_SIZE) |
#define SQUARE2_Y SQUARE0_Y |
#define SQUARE3_X SQUARE0_X |
#define SQUARE3_Y (SQUARE0_Y + SQUARE_SIZE) |
Referenced by init_display(), main(), and setup_board().
#define SQUARE4_X SQUARE1_X |
#define SQUARE4_Y SQUARE3_Y |
#define SQUARE5_X SQUARE2_X |
#define SQUARE5_Y SQUARE3_Y |
#define SQUARE6_X SQUARE0_X |
#define SQUARE6_Y (SQUARE3_Y + SQUARE_SIZE) |
Referenced by init_display().
#define SQUARE7_X SQUARE1_X |
#define SQUARE7_Y SQUARE6_Y |
#define SQUARE8_X SQUARE2_X |
#define SQUARE8_Y SQUARE6_Y |
#define SQUARE_SIZE (LCD_HEIGHT_PIXELS / 3) |
Referenced by draw_circle(), draw_cross(), highlight_square(), and setup_board().
#define STRING_LENGTH 20 |
Referenced by setup_board().
#define STRING_X (SQUARE2_X + SQUARE_SIZE*2) |
Referenced by main(), and setup_board().
enum button |
enum player |
|
static |
Draws a circle at selected point.
References CIRCLE_SIZE, gfx_mono_draw_circle, GFX_PIXEL_SET, GFX_WHOLE, square_coord, and SQUARE_SIZE.
Referenced by user_turn().
|
static |
Draws a cross in selected square.
References CROSS_SIZE, gfx_mono_draw_line, GFX_PIXEL_SET, square_coord, and SQUARE_SIZE.
Referenced by opponent_turn().
|
static |
Gets button push.
References BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_NONE, and port_pin_get_input_level().
Referenced by main(), and user_turn().
|
static |
Highlights a square.
References gfx_mono_draw_rect, GFX_PIXEL_CLR, GFX_PIXEL_SET, square_coord, and SQUARE_SIZE.
Referenced by user_turn().
|
static |
Configures buttons.
References port_config::direction, port_config::input_pull, port_get_config_defaults(), PORT_PIN_DIR_INPUT, PORT_PIN_PULL_UP, and port_pin_set_config().
Referenced by main().
|
static |
Initializes the display with explanatory text for the buttons.
References CIRCLE_SIZE, gfx_mono_draw_circle, gfx_mono_draw_string(), GFX_PIXEL_SET, GFX_WHOLE, SQUARE3_Y, SQUARE6_Y, and sysfont.
Referenced by main().
int main | ( | void | ) |
Main entry of example application.
References BUTTON_NONE, delay_init(), delay_ms, games, get_button(), gfx_mono_draw_string(), gfx_mono_init, i, init_buttons(), init_display(), opponent_turn(), setup_board(), SQUARE3_Y, STRING_X, sysfont, system_init(), user_turn(), we_have_a_winner(), and wins.
|
static |
Selects a random, unoccupied square.
References COMPUTER, draw_cross(), and occupied_squares.
Referenced by main().
|
static |
Draws the Tic-tac-toe board on the display.
References games, gfx_mono_draw_filled_rect, gfx_mono_draw_line, gfx_mono_draw_string(), GFX_PIXEL_CLR, GFX_PIXEL_SET, i, j, NONE, occupied_squares, SQUARE0_Y, SQUARE3_Y, SQUARE_SIZE, STRING_LENGTH, STRING_X, sysfont, win_string, and wins.
Referenced by main().
|
static |
User turn Waits for the user to push buttons and select a new square.
References BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_NONE, draw_circle(), get_button(), highlight_square(), NONE, occupied_squares, and USER.
Referenced by main().
|
static |
Checks for winner.
NONE | No winner |
COMPUTER | Opponent won |
USER | User won |
References NONE, and occupied_squares.
Referenced by main().
uint16_t games = 0 |
Referenced by main(), and setup_board().
enum player occupied_squares[3][3] |
Referenced by opponent_turn(), setup_board(), user_turn(), and we_have_a_winner().
const uint8_t square_coord[9][2] |
Referenced by draw_circle(), draw_cross(), and highlight_square().
char win_string[STRING_LENGTH] |
Referenced by setup_board().
uint16_t wins = 0 |
Referenced by main(), and setup_board().