Like Tree1Likes
  • 1 Post By Salem

Tic Tac Toe without array! Need help and advice please.

This is a discussion on Tic Tac Toe without array! Need help and advice please. within the C Programming forums, part of the General Programming Boards category; I have to right a tic tac toe board that randomly generates with a seed, I don't know if I ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Elkton, Florida, United States
    Posts
    5

    Tic Tac Toe without array! Need help and advice please.

    I have to right a tic tac toe board that randomly generates with a seed, I don't know if I have it right. As well as that I need to find out how to make it stop after my bool get seed if it is incorrect data. But instead it goes on to the gen board and I am not aloud to use a break or exit in the program I can only use if else and bool functions. Please help.


    insert
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    //input parameters
    bool getSeed(int* pSeed);
    int genBoard(int seed, int* pTopL, int* pTopC , int* pTopR, int* pCenL, 
                 int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pbotR);
    void prtBoard(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR);
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR);
    bool detWinner(int topL, int topC, int topR, int cenL, int cenC, int 
                   cenR, int botL, int botC, int botR);
    void prtWinner(int topL, int topC, int topR, int cenL, int cenC, int 
                   cenR, int botL, int botC, int botR);
    
    
    int main(void)
    {
       int seed;
       int topL, topC, topR;
       int cenL, cenC, cenR;
       int botL, botC, botR;  
       
       
       getSeed(&seed);      
       genBoard(seed, &topL, &topC, &topR, &cenL, &cenC, &cenR, &botL, 
                &botC, &botR);
       prtBoard(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
       ifValid(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
       detWinner(topL, topC, topR, cenL, cenC, cenR, botL, botC, 
                 botR);
       prtWinner(topL, topC, topR, cenL, cenC, cenR, botL, botC, 
                 botR);
                  
       return 0;
    }
    
    
    bool getSeed(int* pSeed)
    {
       bool success = false; 
       int scanRes;
    
    
       printf("\nName: Clayton Cushway");
       printf("\nPlease enter a nonnegative integer seed: ");
       scanRes = scanf("%d", pSeed);
       
       if (scanRes == 1 && "%d" > 0) 
         success = true;
         
         
       else if (scanRes == 0)
         printf("\nNot an integer.");
       else
         printf("\nRan out of data.");
    
    
       if (!success);
         printf("\n\n");     
          
       return success;
    }
    
    
    int genBoard(int seed, int* pTopL, int* pTopC , int* pTopR, int* pCenL,       
                    int* pCenC, int* pCenR, int* pBotL, int* pBotC, 
                    int* pBotR)
    {
    
    
       int topL = *pTopL;
       int topC = *pTopC;
       int topR = *pTopR;
       int cenL = *pCenL;
       int cenC = *pCenC;
       int cenR = *pCenR;
       int botL = *pBotL;
       int botC = *pBotC;
       int botR = *pBotR;
    
    
       *pTopL = (rand() % 2) ? 'X' : 'O';
       *pTopC = (rand() % 2) ? 'X' : 'O';
       *pTopR = (rand() % 2) ? 'X' : 'O';
       *pCenL = (rand() % 2) ? 'X' : 'O';
       *pCenC = (rand() % 2) ? 'X' : 'O';
       *pCenR = (rand() % 2) ? 'X' : 'O';
       *pBotL = (rand() % 2) ? 'X' : 'O';
       *pBotC = (rand() % 2) ? 'X' : 'O';
       *pBotR = (rand() % 2) ? 'X' : 'O';
       
    }
    
    
    void prtBoard(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR)
    {
       printf("\n%c|%c|%c", topL, topC, topR); 
       printf("\n%c|%c|%c", cenL, cenC, cenR); 
       printf("\n%c|%c|%c", botL, botC, botR); 
    
    
    }
    
    
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR)
    {
       bool success = false;  
    
    
       int total = 0;
       
       if (topL == 'X')
         total++;
       if (topC =='X')
         total++;
       if (topR =='X')
         total++;
       if (cenL == 'X')
         total++;
       if (cenC == 'X')
        total++;
       if (cenR == 'X')
        total++;
       if (botL == 'X')
        total++;
       if (botC == 'X')
        total++;
       if (botR == 'X')
        total++;
    
    
       if (total > 5)
         printf("\nInvalid Board. There are too many X's");
       if ( total < 4)
         printf("\nInvalid Board. There are too many O's");   
       else
         success = true;
       (!success);
         printf("\n\n"); 
       return success;
    
    
    }  
    
    
    bool detWinner(int topL, int topC, int topR, int cenL, int cenC, int 
                   cenR, int botL, int botC, int botR)
    {
       int top, cen, bot, colL, colC, colR, diagL, diagR;
    
    
       if (topL == 'X')
         topL = 1;
       else
          topL = 0; 
       if (topC == 'X')
         topC = 1;
       else
          topC = 0; 
       if (topR == 'X')
         topR = 1;
       else
          topR = 0; 
       if (cenL == 'X')
         cenL = 1;
       else
          cenL = 0; 
       if (cenC == 'X')
         cenC = 1;
       else
          cenC = 0; 
       if (cenR == 'X')
         cenR = 1;
       else
          cenR = 0; 
       if (botL == 'X')
         botL = 1;
       else
          botL = 0; 
       if (botC == 'X')
         botC = 1;
       else
          botC = 0; 
       if (botR == 'X')
         botR = 1;
       else
          botR = 0; 
      
       top = topL + topC + topR;
       cen = cenL + cenC + cenR;
       bot = botL + botC + botR;
       colL = topL + cenL + botL;
       colC = topC + cenC + botC;
       colR = topR + cenR + botR;
       diagL = topL + cenC + botR;
       diagR = topR + cenC + botL;  
      
       if(top == 3 || cen == 3 || bot == 3 || colL == 3 || colC == 3 || 
          colR == 3 || diagL == 3 || diagR == 3)
       {  
          printf("\nX's are the winner");
       }        
       if(top == 0 || cen == 0 || bot == 0 || colL == 0 || colC == 0 || 
          colR == 0 || diagL == 0 || diagR == 0)
       {  
          printf("\nO's are the winner");
       }        
    }
    
    
    void prtWinner(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                   int botL, int botC, int botR)
    {
            
    }
    Last edited by Clayfer; 03-05-2012 at 10:54 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    <vader>I sense a presence in the source, a feeling I've seen this before</vader>
    manasij7479 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Elkton, Florida, United States
    Posts
    5
    I don't know why you would have seen this before this is my first post on this site? Do other people have the same problem. And it's for a class, I just can't figure it out for the life of me, and I really need to do well I'm even hopping for a C.. and its a way for me and a buddy to work on it together.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    Elkton, Florida, United States
    Posts
    5
    insert
    Code:
     //Name: Clayton Cushway//Course: COP 2220 000
    //Description: Project 3
    //Due Date/Time: Friday March 9 at 8:00A.M.
    
    
    #include <stdio.h>
    #include <stdbool.h>
    
    
    //input parameters
    bool getSeed(int* pSeed);
    int genBoard(int seed, int* pTopL, int* pTopC , int* pTopR, int* pCenL, 
                 int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pbotR);
    void prtBoard(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR);
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR);
    bool detWinner(int topL, int topC, int topR, int cenL, int cenC, int 
                   cenR, int botL, int botC, int botR);
    void prtWinner(int topL, int topC, int topR, int cenL, int cenC, int 
                   cenR, int botL, int botC, int botR);
    
    
    int main(void)
    {
       int seed;
       int topL, topC, topR;
       int cenL, cenC, cenR;
       int botL, botC, botR;  
       
       getSeed(&seed);      
       genBoard(seed, &topL, &topC, &topR, &cenL, &cenC, &cenR, &botL, &botC,
                &botR);
       prtBoard(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
       ifValid(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
       detWinner(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
       prtWinner(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
                  
       return 0;
    }
    
    
    bool getSeed(int* pSeed)
    { 
        int scanRes;
    
    
        printf("\nName: Clayton Cushway");
        printf("\n");
        printf("\nPlease enter a nonnegative integer seed: ");
        scanRes = scanf("%d", pSeed);
    
    
        bool success = false;
      
        if (scanRes == 1)
        {  
           int seed = *pSeed;
             if (seed < 0)
                printf("\nThis is not a nonnegative integer");
             else 
                success = true;
                srand(seed);
        }  
        else if (scanRes == 0)
         printf("\nNot an integer.");
        else 
         printf("\nRan out of data.");
      
        if(!success);
           printf("\n");     
          
       return success;
    }
    
    
    int genBoard(int seed, int* pTopL, int* pTopC , int* pTopR, int* pCenL,       
                    int* pCenC, int* pCenR, int* pBotL, int* pBotC, 
                    int* pBotR)
    {
       
       int topL = *pTopL;
       int topC = *pTopC;
       int topR = *pTopR;
       int cenL = *pCenL;
       int cenC = *pCenC;
       int cenR = *pCenR;
       int botL = *pBotL;
       int botC = *pBotC;
       int botR = *pBotR;
    
    
      
       *pTopL = (rand() % 2) ? 'X' : 'O';
       *pTopC = (rand() % 2) ? 'X' : 'O';
       *pTopR = (rand() % 2) ? 'X' : 'O';
       *pCenL = (rand() % 2) ? 'X' : 'O';
       *pCenC = (rand() % 2) ? 'X' : 'O';
       *pCenR = (rand() % 2) ? 'X' : 'O';
       *pBotL = (rand() % 2) ? 'X' : 'O';
       *pBotC = (rand() % 2) ? 'X' : 'O';
       *pBotR = (rand() % 2) ? 'X' : 'O';
       
    }
    
    
    void prtBoard(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR)
    {
       printf("\n%c|%c|%c", topL, topC, topR); 
       printf("\n%c|%c|%c", cenL, cenC, cenR); 
       printf("\n%c|%c|%c", botL, botC, botR); 
    
    
    }
    
    
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                 int botL, int botC, int botR)
    {
       bool success = false;  
    
    
       int total = 0;
       
       if (topL == 'X')
         total++;
       if (topC =='X')
         total++;
       if (topR =='X')
         total++;
       if (cenL == 'X')
         total++;
       if (cenC == 'X')
        total++;
       if (cenR == 'X')
        total++;
       if (botL == 'X')
        total++;
       if (botC == 'X')
        total++;
       if (botR == 'X')
        total++;
    
    
       if (total > 5 || total < 4)
       {   
          if (total > 5)
             printf("\nInvalid Board. There are too many X's");
          else if ( total < 4)
            printf("\nInvalid Board. There are too many O's");
       }    
       else
         success = true;
       if (!success);
         printf("\n\n"); 
       return success;
    
    
    }  
    
    
    bool detWinner(int topL, int topC, int topR, int cenL, int cenC, int 
                   cenR, int botL, int botC, int botR)
    {
       int top, cen, bot, colL, colC, colR, diagL, diagR;
    
    
       if (topL == 'X')
         topL = 1;
       else
          topL = 0; 
       if (topC == 'X')
         topC = 1;
       else
          topC = 0; 
       if (topR == 'X')
         topR = 1;
       else
          topR = 0; 
       if (cenL == 'X')
         cenL = 1;
       else
          cenL = 0; 
       if (cenC == 'X')
         cenC = 1;
       else
          cenC = 0; 
       if (cenR == 'X')
         cenR = 1;
       else
          cenR = 0; 
       if (botL == 'X')
         botL = 1;
       else
          botL = 0; 
       if (botC == 'X')
         botC = 1;
       else
          botC = 0; 
       if (botR == 'X')
         botR = 1;
       else
          botR = 0; 
      
       top = topL + topC + topR;
       cen = cenL + cenC + cenR;
       bot = botL + botC + botR;
       colL = topL + cenL + botL;
       colC = topC + cenC + botC;
       colR = topR + cenR + botR;
       diagL = topL + cenC + botR;
       diagR = topR + cenC + botL;  
      
       if(top == 3 || cen == 3 || bot == 3 || colL == 3 || colC == 3 || 
          colR == 3 || diagL == 3 || diagR == 3) 
       {
          printf("\nX's are the winner");
       }
       else if (top == 0 || cen == 0 || bot == 0 || colL == 0 || colC == 0 || 
          colR == 0 || diagL == 0 || diagR == 0)  
       {  
           printf("\nO's are the winner");        
       }
    }
    
    
    void prtWinner(int topL, int topC, int topR, int cenL, int cenC, int cenR, 
                   int botL, int botC, int botR)
    {
            
    }
    that is what I have done so far and revised but still can't get it to stop the function if I get the boolean back as a false...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2012, 09:14 PM
  2. Need Advice
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2006, 12:21 PM
  3. Advice?
    By Mokkan in forum Game Programming
    Replies: 5
    Last Post: 12-24-2003, 05:26 PM
  4. More array advice!
    By CrackerJack in forum C Programming
    Replies: 8
    Last Post: 11-10-2003, 11:29 AM
  5. Need advice
    By trapped in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2002, 07:07 PM

Tags for this Thread


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