Thread: Always player #1's turn?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Always player #1's turn?

    After reviewing the below code for the past hour, I cannot seem to figure out this problem.

    This is a basic WIP for a tic-tac-toe game (I'm new to C and thought this would be a decent little challenge). I've got most of everything done, however - after you enter in Player #1's name and Player #2's name, the program will ALWAYS give the turn to whoever was assigned as Player #1 (who is assigned as player #1 is supposed to be random).

    So, to summarize:

    >enter in player #1's name [josh]
    >enter in player #2's name [justin]
    >program randomly assigns josh as player #1
    >[turn #1] josh, make your move
    >enter in coordinates
    >[turn #2] josh, make your move

    It never gives a turn to player two. Any ideas why this is? Thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "player.h"
    
    
    int board[3][3];
    int maxInput = 3; /* this represents the maximum int they can specify when choosing a position to play */
    int a = 0; //represent the turn currently being played
    int done = 0;
    
    
    struct players player[2];
    
    
    //prototypes
    int isEven(int x);
    void markPosition(int x, int y, char type);
    void checkForWinner();
    
    
    int main()
    {
        int first; //the player the goes first
    
    
        int x;
        int y;
    
    
        printf("Player one, enter your name: ");
        fgets(player[0].name, 256, stdin);
    
    
        printf("Player two, enter your name: ");
        fgets(player[1].name, 256, stdin);
    
    
        //remove \n at the end of each name
        player[0].name[strlen(player[0].name)-1] = '\0';
        player[1].name[strlen(player[1].name)-1] = '\0';
    
    
        //generate who will go first randomly
        first = (isEven(rand())) ? 1 : 0;
    
    
        //set who is x, who is y
        if(first == 1){
            player[0].type = 'O';
            player[1].type = 'X';
        }else{
            player[0].type = 'X';
            player[1].type = 'O';
        }
    
    
        printf("Player %s, you are X.\n", player[first].name);
        printf("=========================\n");
    
    
        while(done == 0){
            a++;
            printf("\n\n[TURN %d] %s, make your move.", a, (isEven(a) && first == 1) ? player[1].name : player[0].name);
    
    
            printf("\nInput X-Coord: ");
            scanf("%d", &x);
    
    
            printf("\nInput Y-Coord: ");
            scanf("%d", &y);
    
    
            markPosition(x,y, (isEven(a) && first == 1) ? player[1].type : player[0].type);
            checkForWinner();
        }
    
    
        FILE *file;
        file = fopen("C:\\Users\\Justin\\Desktop\\tictactoe_results.txt", "w+");
    
    
        //print results of the board/game into a text file
        int o = 0;
        int z = 0;
        int b;
        for(b = 0; b < 9; b++){
            //if the position isn't played, set it to the char -, else write who played that pos
            fputc((board[o][z] == NULL) ? '-' : board[o][z], file);
    
    
            if(o == 2) fputc('\n', file);
    
    
            if(o >= 2){
                o = 0;
                z++;
            }else{
                o++;
            }
        }
    
    
        fclose(file);
        getchar();
    }
    
    
    int isEven(int x){
        return !(x%2);
    }
    
    
    void checkForWinner(){
        if(a == 9){
            done = 1;
        }
    }
    
    
    void markPosition(int x, int y, char type){
        if(board[x][y] != NULL){
            printf("[ERROR] You cant select position already played.\n");
            if(a > 0) a--;
        }else if(x > maxInput || y > maxInput){
            printf("[ERROR] Your coordinates are off the board! \n");
            if(a > 0) a--;
        }else{
            board[x][y] = type;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("\n\n[TURN %d] %s, make your move.", a, (isEven(a) && first == 1) ? player[1].name : player[0].name);
    Well if first is 0, then first == 1 will always be false.
    Which makes the && expression itself always false, the isEven(a) plays no further part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could give each player a number, say 0 or 1

    then, right before you loop back to the top of the game loop:
    Code:
    player = !player;
    An example:
    Code:
    #include <stdio.h>
    
    int main(void) {
       volatile int i, player;
       i=player=0;
       while(i<5) {
          printf("Player is: %d \n",player);
          player=!player;
          ++i;
       }
       return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    There was no need to make that volatile Adak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    There was no need to make that volatile Adak.
    Oh yes there was!

    AVG free edition's latest upgrade, prevents it from running, if the volatile keyword is removed. Declaring it a virus!

    I tried making subtle changes to the code itself, like using an if() statement, etc., but that didn't work.

    Adding volatile was the only thing that let it get past AVG.

    I'm hoping the next update in a few days, will have that false positive problem, fixed.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Why would anyone else here care about your virus scanner issues - or your inability to configure it?

    how to exclude directory in avg - Google Search
    Add an exception for your code development directory, and you won't have to keep second-guessing whether your AV is going to throw a wobbler over your latest creation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    Why would anyone else here care about your virus scanner issues - or your inability to configure it?
    You asked, so I answered. Why do you think I can't configure it? There never was a reason to exclude a directory with the previous versions of AVG.

    Why would anyone else here, care about anything? Because we're all human. Some more pleasant than others.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Quote Originally Posted by Salem View Post
    > printf("\n\n[TURN %d] %s, make your move.", a, (isEven(a) && first == 1) ? player[1].name : player[0].name);
    Quote Originally Posted by Salem View Post
    Well if first is 0, then first == 1 will always be false.
    Which makes the && expression itself always false, the isEven(a) plays no further part.


    Ah, I guess that slipped away from me and I never really caught that.

    Thanks for the reply.

    Quote Originally Posted by Adak View Post
    You could give each player a number, say 0 or 1

    then, right before you loop back to the top of the game loop:
    Code:
    player = !player;
    An example:
    Code:
    #include <stdio.h>
    
    int main(void) {
       volatile int i, player;
       i=player=0;
       while(i<5) {
          printf("Player is: %d \n",player);
          player=!player;
          ++i;
       }
       return 0;
    }
    Thank you for the provided concept!

    Worked perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't turn on my pc
    By Niara in forum Tech Board
    Replies: 25
    Last Post: 10-27-2009, 06:29 AM
  2. Is it a watch? Is it an MP3 player? Is it a video player?
    By twomers in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-15-2006, 10:54 PM
  3. when I turn my pc on...
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 03-22-2002, 03:14 PM
  4. wave player or mp3 player using C
    By lliero in forum C Programming
    Replies: 5
    Last Post: 02-27-2002, 11:33 AM