Microchip® Advanced Software Framework

tictactoe.c File Reference

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)
#define STRING_LENGTH   20

Referenced by setup_board().

#define STRING_X   (SQUARE2_X + SQUARE_SIZE*2)

Referenced by main(), and setup_board().

enum button
Enumerator
BUTTON_1 
BUTTON_2 
BUTTON_3 
BUTTON_NONE 
enum player
Enumerator
NONE 
USER 
COMPUTER 

static void draw_circle ( uint8_t  square_num)
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 void draw_cross ( uint8_t  square_num)
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 enum button get_button ( void  )
static
static void highlight_square ( uint8_t  square_num)
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 void init_display ( void  )
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, LCD_WIDTH_PIXELS, SQUARE3_Y, SQUARE6_Y, and sysfont.

Referenced by main().

static void opponent_turn ( void  )
static

Selects a random, unoccupied square.

References COMPUTER, draw_cross(), and occupied_squares.

Referenced by main().

static void setup_board ( void  )
static
static void user_turn ( void  )
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 uint8_t we_have_a_winner ( void  )
static

Checks for winner.

Returns
Enum to say if there is a winner or not.
Return values
NONENo winner
COMPUTEROpponent won
USERUser 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]
const uint8_t square_coord[9][2]
Initial value:
= {
}
#define SQUARE3_X
Definition: tictactoe.c:105
#define SQUARE5_Y
Definition: tictactoe.c:110
#define SQUARE0_X
Definition: tictactoe.c:99
#define SQUARE1_Y
Definition: tictactoe.c:102
#define SQUARE8_X
Definition: tictactoe.c:115
#define SQUARE0_Y
Definition: tictactoe.c:100
#define SQUARE6_Y
Definition: tictactoe.c:112
#define SQUARE2_Y
Definition: tictactoe.c:104
#define SQUARE3_Y
Definition: tictactoe.c:106
#define SQUARE7_Y
Definition: tictactoe.c:114
#define SQUARE1_X
Definition: tictactoe.c:101
#define SQUARE4_X
Definition: tictactoe.c:107
#define SQUARE4_Y
Definition: tictactoe.c:108
#define SQUARE6_X
Definition: tictactoe.c:111
#define SQUARE8_Y
Definition: tictactoe.c:116
#define SQUARE7_X
Definition: tictactoe.c:113
#define SQUARE2_X
Definition: tictactoe.c:103
#define SQUARE5_X
Definition: tictactoe.c:109

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().