Like Tree1Likes
  • 1 Post By Salem

What's wrong with my code ??

This is a discussion on What's wrong with my code ?? within the Game Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define MAX_STRING_LEN 80 void gameBoard(); int checkWinner(); void computerPlayer(); int size, ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    What's wrong with my code ??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define MAX_STRING_LEN 80
    
    
    void gameBoard();
    int checkWinner();
    void computerPlayer();
    
    
    int size, test, row, col, turn, option2 = 0;
    
    
    char board[15][15];
    char alpha[] = {"abcdefghijklmnopqrstuvwxyz"};
    
    
    int main()
    {
     int size, num, test, row, col, turn, option2 = 0;
     char move;
         move = num;
         move = alpha;
    
    
     while(size < 2 || size > 9)
         {
    printf("Qual o tamanho da tabela (2...9) : ");
     scanf("%d", &size);
     if( size < 2 || size > 9)
    printf("Tente de novo");
    else
    break;
    
     }
    
    
     while(option2 = -1)
           {      
     if(turn%2 == 0)
             {
     int condition = 1;
     while(condition == 1) 
             {      
    
     for(int rows = 0; rows < size; rows++)
             {
     for(int cols = 0; cols < size; cols++)
             {
             board[rows][cols] = '+';
     scanf("%d %d", &row, &col);
             }        
     printf("/n");
             }  
    //Program crashses here because I do not know how to input the move correctly
    printf("Escolha os pontos para conectar (a1-b1 ou 1a-b1): ");
     scanf("%c", &move);
    
     if(board[row][col] == ' ')
             {   
             board[row][col] = '-';
             turn++;
             condition = 0;
             }    
     else
    printf("Jogada invalida/n");
             }    
             }    
    else//AI must do something
             {
             computerPlayer();          
             turn++;
             }
    
     if(checkWinner() == 1) //X wins
             {
    printf("Parabens vc ganhou/n/n");
             option2 = -1;
             }          
     else if(checkWinner() == 2) //X wins
             {
    printf("Voce perdeu/n/n");
             option2 = -1;
             }          
             gameBoard();
           }  
    
    system("PAUSE");
    return0;
    }
    //****************************************************Functions
    //print the board to the screen 
    void gameBoard()
    {
     printf("/n");
     for(int i = 0; i < 3; i++)
        {
     for(int j = 0; j < 3; j++)
            {        
     printf("+", board[i][j], "+");
            }        
     printf("/n");
        }        
    }     
    //**************************************************************
    int checkWinner()
    {
    //check to see if '-' 
    if(board[0][0] == '-' && board[0][1] == '-' && board[0][2] == '-')
     return 1;
    if(board[1][0] == '-' && board[1][1] == '-' && board[1][2] == '-')
     return 1;     
    if(board[2][0] == '-' && board[2][1] == '-' && board[2][2] == '-')
     return 1;  
    
    if(board[0][0] == '-' && board[1][0] == '-' && board[2][0] == '-')
     return 1;  
    if(board[0][1] == '-' && board[1][1] == '-' && board[2][1] == '-')
     return 1; 
    if(board[0][2] == '-' && board[1][2] == '-' && board[2][2] == '-')
     return 1;          
    
    
    if(board[0][0] == '-' && board[1][1] == '-' && board[2][2] == '-')
     return 1;   
    if(board[2][0] == '-' && board[1][1] == '-' && board[0][2] == '-')
     return 1;  
    //check to see '|'
    if(board[0][0] == '|' && board[0][1] == '|' && board[0][2] == '|')
     return 0;
    if(board[1][0] == '|' && board[1][1] == '|' && board[1][2] == '|')
     return 0;     
    if(board[2][0] == '|' && board[2][1] == '|' && board[2][2] == '|')
     return 0;  
    
    if(board[0][0] == '|' && board[1][0] == '|' && board[2][0] == '|')
     return 0;  
    if(board[0][1] == '|' && board[1][1] == '|' && board[2][1] == '|')
     return 0; 
    if(board[0][2] == '|' && board[1][2] == '|' && board[2][2] == '|')
     return 0;          
    
    
    if(board[0][0] == '|' && board[1][1] == '|' && board[2][2] == '|')
     return 0;   
    if(board[2][0] == '|' && board[1][1] == '|' && board[0][2] == '|')
     return 0;          
    }        
    //*************************************************************
    //This function generates a random move and see if it's available. if it is, then make it
    //if it isn't then generate another random move
    void computerPlayer()
    {
        col = -1;
        row = -1;
     while(col == -1 || row == -1)
        {
     time_t seconds;
     time(&seconds);
     srand((unsigned int) seconds);  
           col = rand()%3;
           row = rand()%3;
     if(board[row][col] != ' ')
           {
             col = -1;
             row = -1; 
           }
           board[row][col] = '-';          
        }          
    }
    



  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Code:
    mingw32-gcc.exe -Wall -std=c99  -g  -Wmissing-include-dirs    -c V:\SourceCode\Projects\Test\testc\main.c -o obj\Debug\main.o
    V:\SourceCode\Projects\Test\testc\main.c: In function 'main':
    V:\SourceCode\Projects\Test\testc\main.c:25:11: warning: assignment makes integer from pointer without a cast [enabled by default]
    V:\SourceCode\Projects\Test\testc\main.c:40:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    V:\SourceCode\Projects\Test\testc\main.c:22:17: warning: unused variable 'test' [-Wunused-variable]
    V:\SourceCode\Projects\Test\testc\main.c: In function 'checkWinner':
    V:\SourceCode\Projects\Test\testc\main.c:150:1: warning: control reaches end of non-void function [-Wreturn-type]
    V:\SourceCode\Projects\Test\testc\main.c: In function 'main':
    V:\SourceCode\Projects\Test\testc\main.c:24:11: warning: 'num' is used uninitialized in this function [-Wuninitialized]
    What does "==" mean? What does "=" mean?
    Code:
    while(option2 = -1)
    This line
    Code:
    scanf("%c", &move);
    Likely should be changed to below; the space tells scanf to skip spaces and newlines.
    Code:
    scanf(" %c", &move);

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    i'm having trouble with this part too

    Code:
    while
    Code:
    (size < 2 || size > 9)
    Code:
         {printf("Qual o tamanho da tabela (2...9) : "); scanf("%d", &size); if( size < 2 || size > 9)printf("Tente de novo");elsebreak;


    when i enter the size and click enter the program doesn't enter the data

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    If you use "paste as text" in your browser, and stop posting everything in various shades of pale blue, you might find there are more people who are capable of actually reading your posts.
    stahta01 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone please tell me what is wrong with this code?
    By cloudstrife910 in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2010, 04:17 AM
  2. What is wrong with this code? please help..
    By thebridge in forum C Programming
    Replies: 2
    Last Post: 10-07-2010, 02:06 PM
  3. wad wrong with this code?
    By tantan23434 in forum C Programming
    Replies: 3
    Last Post: 10-06-2009, 11:34 PM
  4. What's wrong with this code...?
    By The SharK in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2005, 09:27 AM
  5. what is wrong with this code?
    By Jennifer in forum Windows Programming
    Replies: 1
    Last Post: 01-06-2002, 07:50 AM

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