#Tic Tac Toe problem with win condition

21 messages · Page 1 of 1 (latest)

halcyon grove
#
#include <stdio.h>

int main(){
        int j;
        int k;
        char pos[10] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
        int i;
        printf(" %c | %c | %c \n", pos[1], pos[2], pos[3]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n", pos[4], pos[5], pos[6]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n", pos[7], pos[8], pos[9]);
        for(int i = 0; i < 9;  i++){
                scanf("%d",&k);
                while (pos[k] != ' ' || k > 9 || k < 0){
                        printf("You gave the wrong input, try again:");
                        scanf("%d", &k);
                }
                if(i % 2 == 0){
                        pos[k] = 'X';
                        printf("Player X: %d\n", k);
                }
                else{
                        pos[k] = '0';
                        printf("Player 0:%d\n", k);
                }
                printf(" %c | %c | %c \n", pos[1], pos[2], pos[3]);
                printf("---+---+---\n");
                printf(" %c | %c | %c \n", pos[4], pos[5], pos[6]);
                printf("---+---+---\n");
                printf(" %c | %c | %c \n", pos[7], pos[8], pos[9]);
                for (int j = 0; j < 2; j++){
                        if(pos[j*3 + 1] == pos[j*3 + 2] && pos[j*3 +2] == pos[j*3 + 3] && pos[j*3 + 1] != ' '){
                                        printf("%c Wins!", pos[j*3 + 1]);
                                        return 0;
                        }
                }
                for(int j = 1; j <= 3; j += 2){
                        if(pos[j] == pos[j + (5-i)] && pos[j] == pos[j + 2*(5-i)] && pos[j] != ' '){
                                printf("%c Wins!", pos [j]);
                                return 0;
                        }
                }
        }
                printf("It's a draw!");
                return 0;

}

For some reason it detects/prints when a player has won delayed

unique badgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

warped bison
#

what do you mean delayed. Can you give the actual output vs what you expect?

halcyon grove
#
---+---+---
   |   |
---+---+---
   |   |
1
Player X: 1
 X |   |
---+---+---
   |   |
---+---+---
   |   |
2
Player 0:2
 X | 0 |
---+---+---
   |   |
---+---+---
   |   |
5
Player X: 5
 X | 0 |
---+---+---
   | X |
---+---+---
   |   |
3
Player 0:3
 X | 0 | 0
---+---+---
   | X |
---+---+---
   |   |
9
Player X: 9
 X | 0 | 0
---+---+---
   | X |
---+---+---
   |   | X
4
Player 0:4
 X | 0 | 0
---+---+---
 0 | X |
---+---+---
   |   | X
X Wins!```
halcyon grove
#

The order of the commands in my code doesn't seem wrongfully alligned, it probably is I just can't tell how

warped bison
#

honestly your final for loops are kinda wacky

#

for(int j = 1; j <= 3; j += 2){

#

this is weird

#

from 1 to 3 in steps of 2?

#

and you index based on i which shouldn't have anything to do with this code: pos[j + (5-i)]

#

i is the number of turns so why is it being used to check a position for a win?

#

Also you really don't have enough checks. There are 3 vertical, 3 horizontal and 2 diagonal possible wins. But you do 4 checks, which clearly isn't enough to cover the 8 situations

#

also why are you indexing from 1? We usually index base 0 since it makes the math easier. It also means you're wasting a slot in your array and were forced to make it size 10 instead of 9 for no reason

#

Basically at the very least your win checks need a do-over (maybe you can adapt them but I think its probably best just to write them from scratch its only 6 lines and you'll have a better time if you do some pen and paper math to figure out what checks you need to do)

halcyon grove
#

the vertical is from 1 to 3 with steps of 2 because there are 2 diagonal wins, and this code covers both those instances

halcyon grove
#

and I haven't added a vertical check, that's another mistake

#

Ok now it works, thanks for your help!

unique badgeBOT
#

@halcyon grove Has your question been resolved? If so, type !solved :)

halcyon grove
#

!solved