From ed98fa9d32149ac50d599ca1f289fce4e8ae737f Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 8 Oct 2021 16:52:28 +0200 Subject: [PATCH] Rewrite set_tictactoe_winner() --- src/model.c | 130 +++++++++++++++------------------------------------- 1 file changed, 38 insertions(+), 92 deletions(-) diff --git a/src/model.c b/src/model.c index af633ef..84856d4 100644 --- a/src/model.c +++ b/src/model.c @@ -170,103 +170,49 @@ 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+1] + && 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[0]; + 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) {