Alternating turns in C code for 2 player game

This is a discussion on Alternating turns in C code for 2 player game within the C Programming forums, part of the General Programming Boards category; Hi I am interested in ways to code taking turns for 2 player board games & came across this code ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    34

    Alternating turns in C code for 2 player game

    Hi
    I am interested in ways to code taking turns for 2 player board games & came across this code using loops on the site here:For, While and Do While Loops in C - Cprogramming.com

    I am trying to code an example & have this so far (but it is very limited & probably unsound).

    Code:
    #include <stdio.h>
    
    #define TRUE 1
    
    int someone_has_won(){
        
        return 0;
        
    }
    
    int someone_wants_to_quit(){
        
        return 0;
        
        }
    
    int take_turn(){
        
        return 0;
    }
    
    int main(int argc, char **argv)
    {
        int player1;
        int player2;
        int true;
        
        while(true)
    {
        if (someone_has_won() || someone_wants_to_quit() == TRUE)
        {break;}
        take_turn(player1);
        if (someone_has_won() || someone_wants_to_quit() == TRUE)
        {break;}
        take_turn(player2);
    }
        return 0;
    }
    which compiles but with these warnings:
    gcc -Wall -c "turns.c" ()
    turns.c: In function 'main':
    turns.c:59:14: warning: 'player1' may be used uninitialized in this function [-Wuninitialized]
    turns.c:62:14: warning: 'player2' may be used uninitialized in this function [-Wuninitialized]
    turns.c:55:7: warning: 'true' is used uninitialized in this function [-Wuninitialized]
    Compilation finished successfully.
    Can someone please help me expand it further into code that would work properly? I'm very grateful for helpful replies-thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You haven't initialized you variables.
    You should give a value to every variable before you use it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    34
    Ok-thanks for your helpful reply. Giving values to my variables means the code compiles with no warnings eg:
    [CODE]
    int player1=0;
    int player2=1;
    int true=1;
    [CODE]
    gcc -Wall -o "turns" "turns.c" ()
    Compilation finished successfully.
    But how can I/do I use this code to toggle between player 1 & player 2? Can anyone add a bit more code here to show how this actually can work for me?
    (also int true=0; is that correct here? and/ or should TRUE be a #define ?
    Once again I am grateful for helpful replies-thanks

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If something is zero, then it results to false.
    Else, it results to true.

    It's a matter of taste, so you can do it as you desire. If you want to used the defined ones, you can find them in stdbool.h.

    As for the toggling, think of how thinks should work.
    • Round zero (even number) - 1st player
    • Round 1(odd number) - 2nd player
    • Round 2(even number) - 1st player
    • Round 3(odd number) - 2nd player
    • ...



    If I want to toggle between two players, I would do it like this
    Code:
    while(..)
    {
    if number of loop is even, then player1 plays
    else, player2 plays
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Richard, my suggestion is to first, slowly and deliberately, play a game of Tic-Tac-Toe, by yourself. Note what the pattern is - write it down.

    As to switching player on the move:

    int player = 0; //start with this. player=0 means player is White, in Chess, X's in Tic-Tac-Toe, first player on the dealers left in cards, etc.

    Then, in the main game loop use this:

    player=!player;

    Code:
       for(i=0;i<15;i++) {      //show it works
          player=!player;
          printf("player: %d\n",player);
       }

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    34
    Adak
    Thanks for your help.
    I now have this-which I think is getting near to what I intended!
    Code:
    #include <stdio.h>
    
    int twoplayergamecode(){
        
        return 0;
    }
    
    int main(int argc, char **argv)
    {
        twoplayergamecode();
        int player=0;
        int turn;
        int noturns;
        
        for (noturns=0;noturns<15;noturns++)
    {
        for(turn=0;turn<15;turn++)
         {      //show it works
       player=!player;
       printf("Turn no%d.player: %d\n",noturns,player);
    }
    
    }
    return 0;
    }
    This outputs:
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no0.player: 0
    Turn no0.player: 1
    Turn no1.player: 0
    Turn no1.player: 1
    etc etc
    to turn14
    I am looking for
    Turn no 0.player0
    Turn no 0.player1
    Turn no 1.player0
    Turn no 1.player1
    etc

    If I play around somemore with it I may get the output I want!
    The main code I want is to get a solid 2 player game turn function sorted!
    Again thanks Adak for your help.
    Last edited by richardpd; 01-20-2013 at 03:26 PM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You want in every turn both players to play?
    Then you can do it like this
    Code:
     for (noturns=0;noturns<15;noturns++)
    {
        for(turn=0;turn<2;turn++)
         {      //show it works
       player=!player;
       printf("Turn no%d.player: %d\n",noturns,player);
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    34
    Thanks std10093-just the job!
    (or in my pidgeon Greek- efharisto para poli para kalo!
    well-C coding is nearly all Greek to me or is that all Geek?! LOL!).
    Now to tackle some of those easy 2 player game algorithms like alphabeta minimax etc...I live in hope! :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 10-19-2012, 03:48 PM
  2. Trying to implement a sound class for a player in a game
    By SimplyEvert in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2012, 05:38 PM
  3. C++ Video Game Player Program
    By zonen in forum C++ Programming
    Replies: 33
    Last Post: 10-01-2011, 06:12 PM
  4. Basic structure for 2 player game
    By pants in forum C Programming
    Replies: 2
    Last Post: 05-10-2009, 05:16 PM
  5. alternating sum
    By alyeska in forum C++ Programming
    Replies: 20
    Last Post: 01-04-2008, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21