diff --git a/CMakeLists.txt b/CMakeLists.txt index 79aa1eb..2c7ce5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) -set(CMAKE_C_FLAGS "-std=c99 -g -Wall") +set(CMAKE_C_FLAGS "-std=c99 -lncurses -g -Wall") set(CMAKE_INSTALL_PREFIX ".") -#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) include_directories(include) +#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) project(utictactoe) -add_executable(${PROJECT_NAME} src/utictactoe.c src/model.c) +add_executable(${PROJECT_NAME} src/utictactoe.c src/model.c src/view.c) diff --git a/include/common.h b/include/common.h index 371b0f6..97101b6 100644 --- a/include/common.h +++ b/include/common.h @@ -24,4 +24,9 @@ typedef enum { ALMOST, } e_status; +typedef enum { + HUMAN, + RANDOM, + CLEVER, +} e_tactic; #endif /*COMMON*/ diff --git a/include/ia.h b/include/ia.h new file mode 100644 index 0000000..f61ce40 --- /dev/null +++ b/include/ia.h @@ -0,0 +1,17 @@ +#ifndef IA_H +#define IA_H +#include "common.h" +#include "model.h" +/*! + * This function modify the move in parameter in order to get a valid move (1) randomly if tactic is 0 or + * (2) in a clever way if tactic is 1 in the corresponding utictactoe u. + * + * \param p_uttt a pointer on a s_utictactoe. + * \param p_move a pointer on a s_move to be modified. + * \param tactic an e_tactic set to 0 for random strategy and 1 for clever one. + */ +void best_move(s_utictactoe* p_uttt, s_move* p_move, e_tactic tactic); + +s_tictactoe * clone_ttt( s_tictactoe * p_ttt ); +e_location get_best_random_position( s_tictactoe * p_uttt, e_player player); +#endif diff --git a/include/model.h b/include/model.h index 21be7e1..8b465a6 100644 --- a/include/model.h +++ b/include/model.h @@ -192,4 +192,5 @@ e_status play_move(s_utictactoe *p_uttt, s_move *p_move); /*Given usefull functions*/ void draw_utictactoe(s_utictactoe *p_uttt); void draw_utictactoe_history(s_utictactoe *p_uttt); -#endif /* MODEL_H */ \ No newline at end of file +void save_a_utictactoe_to_file(FILE *p_f, s_utictactoe *p_uttt); +#endif /* MODEL_H */ diff --git a/include/view.h b/include/view.h new file mode 100644 index 0000000..a5d5e5d --- /dev/null +++ b/include/view.h @@ -0,0 +1,39 @@ +#ifndef VIEW_H +#define VIEW_H +#include +#include "common.h" +#include "model.h" + +typedef struct view * p_view; + +/*! + * This function allocates dynamically a struct view in order to handle the corresponding u_tictactoe. + * + * \param p_uttt a pointer on a s_utictactoe. + * \return a reference to the s_utictactoe memory space allocated, NULL in case + * of allocation problem. + */ +p_view create_view(s_utictactoe * p_uttt); + +void draw_ttt(WINDOW * w, s_tictactoe * t); +//e_location display_ttt( WINDOW * w, s_tictactoe * ttt); +e_location coord_to_elocation( int x, int y); +int elocation_to_coord(e_location l); +/*! + * This function retrieves a valid move proposal from the user using the corresponding view. + * + * \param p_move a pointer on a s_move to be modified. + * \param v a pointer on the view to use. + */ +void set_next_player_move(s_move * p_move, p_view v, e_tactic tactic); + +void display_winner(p_view v); +/*! + * This function free all the memory used by a given view which + * reference is given. + * + * \param v a pointer on a view to be freed. + */ +void free_view(p_view v); + +#endif /* VIEW_H */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8722958..996dcc8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,6 @@ cmake_minimum_required(VERSION 3.5) -set(CMAKE_C_FLAGS "-std=c99 -g -Wall") +set(CMAKE_C_FLAGS "-std=c99 -lncurses -g -Wall") set(CMAKE_INSTALL_PREFIX ".") include_directories(../include) - project(utictactoe) -add_executable(${PROJECT_NAME} utictactoe.c model.c) +add_executable(${PROJECT_NAME} utictactoe.c model.c view.c ia.c) diff --git a/src/ia.c b/src/ia.c new file mode 100644 index 0000000..64a73ff --- /dev/null +++ b/src/ia.c @@ -0,0 +1,76 @@ +#include "common.h" +#include "model.h" +#include "view.h" +#include "ia.h" + +void best_move (s_utictactoe * p_uttt, s_move * m ,e_tactic tactic) +{ + //do{ + if ( p_uttt->inception_level == 1 ) { + m->outer_position = get_best_random_position( + p_uttt->outer_tictactoe, + m->player + ); + } + else { + m->outer_position = get_next_outer_position(p_uttt); + if ( m->outer_position == FREE ) { + do { + m->outer_position = (e_location)(TOPLEFT + rand() % TICTACTOE_SIZE); + } while(p_uttt->inner_tictactoes[m->outer_position]->winner != NOBODY); + } + + m->inner_position = get_best_random_position( + p_uttt->inner_tictactoes[m->outer_position], + m->player + ); + } + //} while (is_move_valid(p_uttt,m) != YES); + return; +} + +s_tictactoe * clone_ttt( s_tictactoe * p_ttt ) { + s_tictactoe * t = create_empty_tictactoe(); + for (int i=0; icontent[i] = p_ttt->content[i]; + } + return t; +} + +e_location get_best_random_position (s_tictactoe * p_ttt, e_player player) { + mvwprintw(stdscr, 13, 0, " "); + s_tictactoe * c_ttt = clone_ttt(p_ttt); + e_player opponent = ( player == PLAYER_O )?PLAYER_X:PLAYER_O; + + // Check if IA player can win + for(int i=0; icontent[i] == NOBODY) { + c_ttt->content[i] = player; + set_tictactoe_winner(c_ttt); + if ( c_ttt->winner != NOBODY ) { + free_tictactoe(c_ttt); + mvwprintw(stdscr, 13, 0, "Je vais gagner "); + return i; + } + c_ttt->content[i] = NOBODY; + } + } + + // avoid loosing + for(int i=0; icontent[i] == NOBODY) { + c_ttt->content[i] = opponent; + set_tictactoe_winner(c_ttt); + if ( c_ttt->winner != NOBODY ) { + free_tictactoe(c_ttt); + mvwprintw(stdscr, 13, 0, "Je ne peux pas te laisser gagner!"); + return i; + } + c_ttt->content[i] = NOBODY; + } + } + + // We have not a winnner move... + free_tictactoe(c_ttt); + return (e_location)(rand() % TICTACTOE_SIZE); +} diff --git a/src/model.c b/src/model.c index f7b6499..14d5d93 100644 --- a/src/model.c +++ b/src/model.c @@ -1,5 +1,50 @@ #include "model.h" +// Define shorts positions +char * get_short_position(e_location pos){ + switch(pos) { + case TOPLEFT: + return "TL"; + case TOPCENTER: + return "TC"; + case TOPRIGHT: + return "TR"; + case MIDLEFT: + return "ML"; + case MIDCENTER: + return "MC"; + case MIDRIGHT: + return "MR"; + case BOTTOMLEFT: + return "BL"; + case BOTTOMCENTER: + return "BC"; + case BOTTOMRIGHT: + return "BR"; + case FREE: + return "FR"; + case NONE: + return "NO"; + default: + return "NO"; + } +} + +char get_short_player(e_player player){ + switch(player){ + case PLAYER_O: + return 'O'; + case PLAYER_X: + return 'X'; + case NOBODY: + return '.'; + case BOTH: + return '#'; + default: + return NO; + } +} + /* * Create empty move @@ -170,113 +215,154 @@ e_status is_move_valid(s_utictactoe *p_uttt, s_move *p_move) { } void set_tictactoe_winner(s_tictactoe *p_ttt){ - bool find; - int next; - if ( p_ttt->winner == NOBODY ) { - for(int i = 0; i < TICTACTOE_WIDTH; i++){ - - /* - * Test for first diagonale - */ - if ( p_ttt->content[i] != NOBODY ) { - // if case 0 we need to test diagonale - if ( i == 0 ){ - int next; - find = true; - for (int l = 1; l < TICTACTOE_WIDTH; l++) { - // Maybe it is overkill to test diagonal but... - // ... no need to rewrite if we update TICTACTOE_WIDTH - next = l * TICTACTOE_WIDTH + l; - if ( p_ttt->content[i] != p_ttt ->content[next]) { - find = false; - break; - } - } - if (find == true) { - p_ttt->winner = p_ttt->content[i]; - return; - } - } - } - - /* - * second diagonal, from the end of line - */ - if ( i == TICTACTOE_WIDTH - 1 ) { - find = true; - for ( int l = 1; l < TICTACTOE_WIDTH; l++){ - next = (TICTACTOE_WIDTH - 1) * l; - if ( p_ttt->content[i] == p_ttt->content[next] ) { - find = false; - break; - } - } - if ( find == true ){ - p_ttt->winner = p_ttt->content[i]; - return; - } - } - - /* - * column - */ - find = true; - for (int c = 1; c < TICTACTOE_WIDTH; c++ ) { - next = TICTACTOE_WIDTH * c + i; - if ( p_ttt->content[i] != p_ttt->content[next]) { - find = false; - break; - } - if ( find == true ){ - p_ttt->winner = p_ttt->content[i]; - return; - } - } - - /* - * line - */ - int line = i * TICTACTOE_WIDTH; - if (p_ttt->content[line] != NOBODY){ - find = true; - for ( int l = 1; l < TICTACTOE_WIDTH; l++ ) { - next = line + l; - if ( p_ttt->content[line] != p_ttt->content[next] ) { - find = false; - break; - } - } - if ( find == true ) { - p_ttt->winner = p_ttt->content[line]; - return; - } - } + // columns + for ( int i=0; i < TICTACTOE_WIDTH; i++ ) { + if (p_ttt->content[i] != NOBODY + &&p_ttt->content[i] == p_ttt->content[i+3] + && p_ttt->content[i] == p_ttt->content[i+6]) { + p_ttt->winner = p_ttt->content[i]; + return; } + } - // check if a move could be done, if not retuen BOTH (no winner) - find = false; - for (int i = 0; i < TICTACTOE_SIZE; i++){ - if ( p_ttt->content[i] == NOBODY ) { - find = true; - break; - } + //lines + for ( int i=0; i < TICTACTOE_SIZE; i=i+3 ) { + if (p_ttt->content[i] != NOBODY + &&p_ttt->content[i] == p_ttt->content[i+1] + && p_ttt->content[i] == p_ttt->content[i+2]) { + p_ttt->winner = p_ttt->content[i]; + return; } - if ( find == false ) { - p_ttt->winner = BOTH; + } + + //diagonale + if ( p_ttt->content[0] != NOBODY + && p_ttt->content[0] == p_ttt->content[4] + && p_ttt->content[0] == p_ttt->content[8] ) { + p_ttt->winner = p_ttt->content[0]; + return; + } + //diagonale + if ( p_ttt->content[2] != NOBODY + && p_ttt->content[2] == p_ttt->content[4] + && p_ttt->content[2] == p_ttt->content[6] ) { + p_ttt->winner = p_ttt->content[2]; + return; + } + + // check if a move could be done, if not retuen BOTH (no winner) + for (int i = 0; i < TICTACTOE_SIZE; i++){ + if ( p_ttt->content[i] == NOBODY ) { + return; } - } + } + p_ttt->winner = BOTH; } e_status play_move(s_utictactoe *p_uttt, s_move *p_move) { - if (is_move_valid(p_uttt, p_move) == YES) { - // we can process things - if ( p_uttt->inception_level == 1 ) { - p_uttt->outer_tictactoe->content[p_move->outer_position] = p_move->player; - } - else { - - } + if (is_move_valid(p_uttt, p_move) == NO) { + return NO; } + list_element_s_move *value = (list_element_s_move*) + malloc(sizeof(list_element_s_move)); + + // we need to create a s_move in memory... + // and affect p_move + value->last_move = create_empty_move(); + + value->last_move->inner_position = p_move->inner_position; + value->last_move->outer_position = p_move->outer_position; + value->last_move->player = p_move->player; + if ( p_uttt->history == NULL ) { + value->next = NULL; + } + else { + value->next = p_uttt->history; + } + p_uttt->history = value; + + if ( p_uttt->inception_level != 1 ) { + p_uttt->inner_tictactoes[p_move->outer_position]->content[p_move->inner_position] = p_move->player; + set_tictactoe_winner(p_uttt->inner_tictactoes[p_move->outer_position]); + + //check if we have a winner in inner_ttt then set player to outer + p_uttt->outer_tictactoe->content[p_move->outer_position] = p_uttt->inner_tictactoes[p_move->outer_position]->winner; + } + else { + p_uttt->outer_tictactoe->content[p_move->outer_position] = p_move->player; + } + set_tictactoe_winner(p_uttt->outer_tictactoe); return YES; } + +void save_a_utictactoe_to_file(FILE *p_f, s_utictactoe *p_uttt){ + assert(p_f); + assert(p_uttt); + fprintf(p_f, "%d\n", p_uttt->inception_level); + if(p_uttt->history){ + list_element_s_move * c_hist = p_uttt->history; + while (c_hist != NULL){ + if(c_hist->last_move){ + fprintf(p_f, "%s %s %c\n", + get_short_position(c_hist->last_move->inner_position), + get_short_position(c_hist->last_move->outer_position), + get_short_player(c_hist->last_move->player)); + } + c_hist = c_hist->next; + } + } +} +void draw_ith_line_of_ttt(s_tictactoe *p_ttt, uint line) { + assert(p_ttt); + printf("%c%c%c", p_ttt->content[line * TICTACTOE_WIDTH], + p_ttt->content[line * TICTACTOE_WIDTH + 1], + p_ttt->content[line * TICTACTOE_WIDTH + 2]); +} + +void draw_tictactoe(s_tictactoe *p_ttt) { + assert(p_ttt); + for (uint line = 0; line < TICTACTOE_WIDTH; line++) { + draw_ith_line_of_ttt(p_ttt, line); + printf("\n"); + } +} + +void draw_utictactoe_history(s_utictactoe *p_uttt) { + assert(p_uttt); + char *e_location_name[] = {"TL", "TC", "TR", "ML", "MC", "MR", + "BL", "BC", "BR", "FREE", "NONE"}; + list_element_s_move *tmp = p_uttt->history; + while (tmp != NULL) { + if (tmp->last_move) { + printf(" - %s %s %c \n", e_location_name[tmp->last_move->inner_position], + e_location_name[tmp->last_move->outer_position], + tmp->last_move->player); + } + tmp = tmp->next; + } +} + +void draw_utictactoe(s_utictactoe *p_uttt) { + assert(p_uttt); + if (p_uttt->inception_level == 1) { + draw_tictactoe(p_uttt->outer_tictactoe); + } else { + for (uint id_ttt = 0; id_ttt < TICTACTOE_SIZE; + id_ttt = id_ttt + TICTACTOE_WIDTH) { + for (uint line = 0; line < TICTACTOE_WIDTH; line++) { + draw_ith_line_of_ttt(p_uttt->inner_tictactoes[id_ttt], line); + printf("|"); + draw_ith_line_of_ttt(p_uttt->inner_tictactoes[id_ttt + 1], line); + printf("|"); + draw_ith_line_of_ttt(p_uttt->inner_tictactoes[id_ttt + 2], line); + printf("\n"); + } + if (id_ttt + TICTACTOE_WIDTH < TICTACTOE_SIZE) { + printf("-----------"); + printf("\n"); + } + } + } + printf("\n####\n"); +} diff --git a/src/utictactoe.c b/src/utictactoe.c index b23d427..e3d957b 100644 --- a/src/utictactoe.c +++ b/src/utictactoe.c @@ -8,6 +8,8 @@ #include "utictactoe.h" #include "common.h" #include "model.h" +#include "view.h" +#include "ia.h" #define OPTIONAL_ARGUMENT_IS_PRESENT \ ((optarg == NULL && optind < argc && argv[optind][0] != '-') \ @@ -45,15 +47,15 @@ int check_int_value ( char *value, int min, int max){ int main(int argc, char* argv[]) { int optc; - int cross_ai, round_ai = 0; - int inception_level = 0; + e_tactic tactic_cross = HUMAN, tactic_round = HUMAN; + int inception_level = 2; bool verbose, contest_mode = false; char * contest_file; static struct option long_opts[] = { {"inception-level", optional_argument, 0, 'i'}, - {"cross-ai", required_argument, 0, 'x'}, - {"round-ai", required_argument, 0, 'o'}, + {"tactic_cross", required_argument, 0, 'x'}, + {"tactic_round", required_argument, 0, 'o'}, {"contest", required_argument, 0, 'c'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, @@ -79,8 +81,8 @@ int main(int argc, char* argv[]) { case 'x': printf ("cross ai\n"); if ( optarg ) { - cross_ai = check_int_value(optarg, 0, 1); - if ( cross_ai == -1 ) { + tactic_cross = check_int_value(optarg, 0, 2); + if ( tactic_cross == -1 ) { fprintf(stderr, "Error: incorrect cross ai value\n"); exit (EXIT_FAILURE); } @@ -95,8 +97,8 @@ int main(int argc, char* argv[]) { case 'o': printf ("round ai\n"); if ( optarg ) { - round_ai = check_int_value(optarg, 0, 1); - if ( round_ai == -1 ) { + tactic_round = check_int_value(optarg, 0, 2); + if ( tactic_round == -1 ) { fprintf(stderr, "Error: incorrect round ai value\n"); exit (EXIT_FAILURE); } @@ -140,15 +142,23 @@ int main(int argc, char* argv[]) { exit (EXIT_FAILURE); } } - s_move *test_move = create_empty_move(); - free_move(test_move); - s_tictactoe *test_ttt = create_empty_tictactoe(); - free_tictactoe(test_ttt); - s_utictactoe *test_uttt = create_empty_utictactoe(1); - free_utictactoe(test_uttt); - test_uttt = create_empty_utictactoe(2); - free_utictactoe(test_uttt); - test_uttt = create_empty_utictactoe(3); - free_utictactoe(test_uttt); - return EXIT_SUCCESS; + srand(time(NULL)); + s_utictactoe *s = create_empty_utictactoe(inception_level); + s_move *m = create_empty_move(); + p_view v = create_view(s); + while (s->outer_tictactoe->winner == NOBODY) { + m->player = get_next_player_to_play(s); + m->outer_position = get_next_outer_position(s); + if (m->player == PLAYER_X) { + set_next_player_move(m, v, tactic_cross); + } else { + set_next_player_move(m, v, tactic_round); + } + play_move(s, m); + } + display_winner(v); + getchar();//just to pause before quitting the program + free_view(v); + free_move(m); + free_utictactoe(s); } diff --git a/src/view.c b/src/view.c new file mode 100644 index 0000000..45a1ea6 --- /dev/null +++ b/src/view.c @@ -0,0 +1,276 @@ +#include "common.h" +#include "model.h" +#include "view.h" +#include "ia.h" + +struct view{ + s_utictactoe * p_uttt; + WINDOW* w_uttt; + WINDOW* w_ttt; +}; + + +p_view create_view(s_utictactoe * u) +{ + p_view v = (p_view) malloc(sizeof(p_view)); + v->p_uttt = u; + + int p_left = 4; + int p_top = 2; + + initscr(); + raw(); + noecho(); + start_color(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_BLACK,COLOR_WHITE); + init_pair(2, COLOR_BLACK,COLOR_RED); + curs_set( 0 ); + + mvwprintw(stdscr,0,0,"Inception level %d", u->inception_level); + // Create the outer win + WINDOW * w_outer = subwin(stdscr, 9, 11, p_top, p_left); + box(w_outer, ACS_VLINE, ACS_HLINE); + draw_ttt(w_outer, u->outer_tictactoe); + v->w_uttt = w_outer; + + // If needed, create the inner ttt + if ( u->inception_level == 2 ){ + int r,c; + getmaxyx(w_outer, r, c); + WINDOW * w_inner = subwin(stdscr, 9, 11, p_top, p_left + c); + box(w_inner, ACS_VLINE, ACS_HLINE); + //draw_ttt(w_innera); + //wrefresh(w_inner); + v->w_ttt = w_inner; + } + wrefresh(w_outer); + wrefresh(stdscr); + + return v; +} + +void draw_ttt(WINDOW * w, s_tictactoe * u){ + int x,y; + int i = 0; + for( y = 2; y < 7; y++){ + if ( y % 2 == 0){ + for(x = 3; x < 8 ;x++){ + if (x % 2 != 0) { + mvwprintw(w,y,x,"%c", (u->content[i] == NOBODY)?'.':u->content[i]); + i++; + } + else { mvwprintw(w,y,x,"|"); } + } + } + else { mvwprintw(w,y,3,"-----"); } + } +} + +void set_next_player_move(s_move * m, p_view v, e_tactic tactic ) +{ + + // computer player + if(tactic != HUMAN) { + best_move(v->p_uttt, m, tactic); + return ; + } + + char * s="Player to play:%c"; + int in_x = 3, out_x = 3; + int out_y = 2, in_y = 2; + bool outer_selected, played = false; + int ch=-1; + int p_x, p_y, p_yx = 0; + if ( v->p_uttt->inception_level == 2 && m->outer_position != FREE ) { + p_yx = elocation_to_coord(m->outer_position); + p_y = p_yx/10; + p_x = p_yx - ( p_y * 10); + } + while(played == false){ + // debug print + // mvwprintw(stdscr, 20,0, " position out: y:%d x:%d, in: y:%d x:%d\n", out_y, out_x, in_y, in_x); + + mvwprintw(stdscr,1,0,s, m->player); + draw_ttt(v->w_uttt, v->p_uttt->outer_tictactoe); + + // Need to draw the second tictac toe if we have an inecption level + if ( v->p_uttt->inception_level == 2 ) { + draw_ttt(v->w_ttt, v->p_uttt->inner_tictactoes[coord_to_elocation(out_y, out_x)]); + + // and mark the current active inner ttt with red cursor + if ( m->outer_position != FREE && v->p_uttt->inception_level == 2) { + wattron(v->w_uttt, COLOR_PAIR(2)); + mvwaddch(v->w_uttt ,p_y, p_x, + v->p_uttt->outer_tictactoe->content[m->outer_position] + ); + wattroff(v->w_uttt, COLOR_PAIR(2)); + } + } + + // inner_cursor cursor + if ( outer_selected == true ) { + wattron(v->w_ttt, COLOR_PAIR(1)); + mvwaddch(v->w_ttt ,in_y, in_x, + v->p_uttt->inner_tictactoes[coord_to_elocation(out_y, out_x)]->content[coord_to_elocation(in_y, in_x)] + ); + wattroff(v->w_ttt, COLOR_PAIR(1)); + } + + // outer_cursor cursor + wattron(v->w_uttt, COLOR_PAIR(1)); + mvwaddch(v->w_uttt ,out_y, out_x, + v->p_uttt->outer_tictactoe->content[coord_to_elocation(out_y, out_x)] + ); + wattroff(v->w_uttt, COLOR_PAIR(1)); + + // refresh screen + wrefresh(v->w_uttt); + if (v->p_uttt->inception_level == 2) wrefresh(v->w_ttt); + wrefresh(stdscr); + + ch = wgetch(stdscr); + + switch(ch) + { + case KEY_BACKSPACE: + case 127: + case '\b': + // if we are in inner TTT window, go back to outer + if ( outer_selected == true && v->p_uttt->inception_level == 2 ) { + outer_selected = false; + } + else { + // If not Exit programm, but we need to do it properly. + free_utictactoe(v->p_uttt); + free_move(m); + free_view(v); + exit(1); + } + break; + case KEY_RIGHT: + if ( outer_selected ) { + if(in_x < 7) { in_x+=2; } + } + else { + if(out_x < 7) { out_x+=2; } + } + break; + case KEY_LEFT: + if ( outer_selected ) { + if( in_x > 3 ) { in_x-=2; } + } + else { + if ( out_x > 3 ) { out_x-=2; } + } + break; + case KEY_UP: + if ( outer_selected ) { + if ( in_y > 2 ) { in_y-=2; } + } + else { + if ( out_y > 2 ) { out_y-=2; } + } + break; + case KEY_DOWN: + if ( outer_selected ) { + if ( in_y < 6 ) { in_y+=2; } + } + else { + if ( out_y < 6 ) { out_y+=2; } + } + break; + case '\n': + if ( v->p_uttt->inception_level == 1 ) { + m->outer_position = coord_to_elocation( out_y, out_x); + return; + } + else { + if ( outer_selected == true ) { + m->outer_position = coord_to_elocation( out_y, out_x); + m->inner_position = coord_to_elocation( in_y, in_x); + return; + } + else { + if ( coord_to_elocation( out_y, out_x ) == m->outer_position || m->outer_position == FREE){ + outer_selected = true; + } + } + } + break; + } + } + +} + +e_location coord_to_elocation( int y, int x) { + if ( y == 2 && x == 3 ){ return TOPLEFT; } + if ( y == 2 && x == 5 ){ return TOPCENTER; } + if ( y == 2 && x == 7 ){ return TOPRIGHT; } + if ( y == 4 && x == 3 ){ return MIDLEFT; } + if ( y == 4 && x == 5 ){ return MIDCENTER; } + if ( y == 4 && x == 7 ){ return MIDRIGHT; } + if ( y == 6 && x == 3 ){ return BOTTOMLEFT; } + if ( y == 6 && x == 5 ){ return BOTTOMCENTER; } + if ( y == 6 && x == 7 ){ return BOTTOMRIGHT; } + return NONE; +} + +int elocation_to_coord(e_location l){ + switch(l){ + case TOPLEFT: + return 23; + break; + case TOPCENTER: + return 25; + break; + case TOPRIGHT: + return 27; + break; + case MIDLEFT: + return 43; + break; + case MIDCENTER: + return 45; + break; + case MIDRIGHT: + return 47; + break; + case BOTTOMLEFT: + return 63; + break; + case BOTTOMCENTER: + return 65; + break; + case BOTTOMRIGHT: + return 67; + break; + default: + return NONE; + } +} + +void display_winner(p_view v){ + int ch = ' '; + draw_ttt(v->w_uttt, v->p_uttt->outer_tictactoe); + wrefresh(v->w_uttt); + if (v->p_uttt->inception_level == 2){ + draw_ttt(v->w_ttt, v->p_uttt->inner_tictactoes[TOPLEFT]); + } + wrefresh(stdscr); + do { + mvwprintw(stdscr, 1, 0, "le gagnant est : %c, apputez sur 'entree' pour quitter", + v->p_uttt->outer_tictactoe->winner); + ch = wgetch(stdscr); + } while (ch != '\n'); + +} +void free_view(p_view v){ + delwin(v->w_uttt); + v->w_uttt = NULL; + delwin(v->w_ttt); + v->w_ttt = NULL; + v->w_uttt = NULL; + endwin(); +} +