Skip to content

Instantly share code, notes, and snippets.

@agrison
Created May 16, 2018 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrison/39d5d5c46bb240d29ff172833f5050f4 to your computer and use it in GitHub Desktop.
Save agrison/39d5d5c46bb240d29ff172833f5050f4 to your computer and use it in GitHub Desktop.
Tic tac toe
#ifndef TICTACTOE_GRID_H
#define TICTACTOE_GRID_H
#define COLOR1 "\x1B[32m"
#define COLOR2 "\x1B[35m"
#define NORMAL "\x1B[0m"
#define GRID "\t\t+-------+-------+-------+\n" \
"\t\t| | | |\n" \
"\t\t| %s | %s | %s |\n" \
"\t\t| 1| 2| 3|\n" \
"\t\t+-------+-------+-------+\n" \
"\t\t| | | |\n" \
"\t\t| %s | %s | %s |\n" \
"\t\t| 4| 5| 6|\n" \
"\t\t+-------+-------+-------+\n" \
"\t\t| | | |\n" \
"\t\t| %s | %s | %s |\n" \
"\t\t| 7| 8| 9|\n" \
"\t\t+-------+-------+-------+\n\n"
#endif
#include <stdlib.h>
#include <stdio.h>
#include "tictactoe.h"
square_state_t STATE[] = {EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY};
int main(int argc, char **argv)
{
draw_game(STATE);
return 0;
}
#include <stdio.h>
#include <stdlib.h> // pour avoir system()
#include "tictactoe.h"
#include "grid.h"
char *square_state(square_state_t state)
{
if (state == PLAYER1)
return COLOR1 "X" NORMAL;
else if (state == PLAYER2)
return COLOR2 "O" NORMAL;
else // EMPTY
return " "; // espace
}
void draw_game(square_state_t *game_state)
{
system("clear"); // system("cls") sur windows.
printf(GRID,
square_state(game_state[0]),
square_state(game_state[1]),
square_state(game_state[2]),
square_state(game_state[3]),
square_state(game_state[4]),
square_state(game_state[5]),
square_state(game_state[6]),
square_state(game_state[7]),
square_state(game_state[8]));
}
#ifndef TICTACTOE_H
#define TICTACTOE_H
// type custom permettant de représenter
// l'état d'une case du morpion
typedef enum
{
EMPTY = 0,
PLAYER1,
PLAYER2
} square_state_t;
void draw_game(square_state_t *game_state);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment