Rewrite set_tictactoe_winner()

This commit is contained in:
Yorick Barbanneau 2021-10-08 16:52:28 +02:00
parent c1a0446402
commit ed98fa9d32

View file

@ -170,104 +170,50 @@ e_status is_move_valid(s_utictactoe *p_uttt, s_move *p_move) {
} }
void set_tictactoe_winner(s_tictactoe *p_ttt){ void set_tictactoe_winner(s_tictactoe *p_ttt){
bool find;
int next;
if ( p_ttt->winner == NOBODY ) { // columns
for ( int i=0; i < TICTACTOE_WIDTH; i++ ) { for ( int i=0; i < TICTACTOE_WIDTH; i++ ) {
if (p_ttt->content[i] != NOBODY
/* &&p_ttt->content[i] == p_ttt->content[i+1]
* Test for first diagonale && p_ttt->content[i] == p_ttt->content[i+6]) {
*/
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]; p_ttt->winner = p_ttt->content[i];
return; return;
} }
} }
//lines
/* for ( int i=0; i < TICTACTOE_SIZE; i=i+3 ) {
* second diagonal, from the end of line if (p_ttt->content[i] != NOBODY
*/ &&p_ttt->content[i] == p_ttt->content[i+1]
if ( i == TICTACTOE_WIDTH - 1 ) { && p_ttt->content[i] == p_ttt->content[i+2]) {
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]; p_ttt->winner = p_ttt->content[i];
return; return;
} }
} }
/* //diagonale
* column if ( p_ttt->content[0] != NOBODY
*/ && p_ttt->content[0] == p_ttt->content[4]
find = true; && p_ttt->content[0] == p_ttt->content[8] ) {
for (int c = 1; c < TICTACTOE_WIDTH; c++ ) { p_ttt->winner = p_ttt->content[0];
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; return;
} }
} //diagonale
} if ( p_ttt->content[2] != NOBODY
&& p_ttt->content[2] == p_ttt->content[4]
/* && p_ttt->content[2] == p_ttt->content[6] ) {
* line p_ttt->winner = p_ttt->content[0];
*/
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; return;
} }
}
}
// check if a move could be done, if not retuen BOTH (no winner) // check if a move could be done, if not retuen BOTH (no winner)
find = false;
for (int i = 0; i < TICTACTOE_SIZE; i++){ for (int i = 0; i < TICTACTOE_SIZE; i++){
if ( p_ttt->content[i] == NOBODY ) { if ( p_ttt->content[i] == NOBODY ) {
find = true; return;
break;
} }
} }
if ( find == false ) {
p_ttt->winner = BOTH; p_ttt->winner = BOTH;
} }
}
}
e_status play_move(s_utictactoe *p_uttt, s_move *p_move) { e_status play_move(s_utictactoe *p_uttt, s_move *p_move) {
if (is_move_valid(p_uttt, p_move) == YES) { if (is_move_valid(p_uttt, p_move) == YES) {