Thread: Can't get markers to line up properly for Gomoku Game

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Can't get markers to line up properly for Gomoku Game

    Hello everyone,

    I'm working on a project due in about 6 hrs. I'm making a Gomoku game where i have to implement a user vs cpu simulated game. The issue i'm at now in the game is that I'm able to create the board and place a marker on the board for the user input. However, i can't seem to figure out why my marker is not lining up. A TA suggested initialize blank spaces since the X mark isn't recognizing that but I'm not following what he is saying. And I need someone to point me where in my code i need to add something to fix it at least because i spent 3 1/2 hrs and i'm making no progress stuck on a problem i encountered last night. Please help and thanks.

    --Cam
    Attached Files Attached Files
    • File Type: c p3.c (3.9 KB, 159 views)

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    on first compile
    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "p3" "p3.c" (in directory: /tmp/mozilla_userx0)
    p3.c: In function 'main':
    p3.c:63:4: warning: statement with no effect [-Wunused-value]
        board[i][j];
        ^
    p3.c:38:7: warning: unused variable 'game' [-Wunused-variable]
       int game;
           ^
    p3.c:37:7: warning: unused variable 'flag' [-Wunused-variable]
       int flag;
           ^
    p3.c:34:8: warning: unused variable 'o_mark' [-Wunused-variable]
       char o_mark = 'O'; // the cpu's marker
            ^
    p3.c:23:9: warning: variable 'boards' set but not used [-Wunused-but-set-variable]
       FILE *boards;
             ^
    p3.c:20:16: warning: unused parameter 'argc' [-Wunused-parameter]
     int main ( int argc, char *argv[]){
                    ^
    Compilation finished successfully.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    see what you can do with that.
    plus some badly needed error checking.
    Code:
    /* This program has been designed to implement the game Gomoku (also know as five in a row). A user plays the game on a 
     * 15x15 board which is empty initially. The users will take turns to place one game piece (x for computer, O for the user)
     * on the board. 
     * 
     * The player who first creates "five in a row", either horziontally, vertically, or diagonally, wins the game. 
     * 
     * This program will have the following features:
     * (1) interact with the user playing the game, 
     * (2) contain two game strategies for computer
     * (3) a design to show who is the winner. 
     * 
     * Terminal line command: a.out k moves boards
     * Where k is option 0 for random computer moves and 1 is smart computer moves against the user*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <string.h>
    
    #define ROWS 17
    #define COLUMNS 19
    
    void message(char *argv[])
    {
            printf("Usage:\n"
                    "%s [arg] [arg] [arg]\n"
                    "0 for random\n"
                    "1 for smart strategy\n"
                    "Filename for output\n"
                    "Filename for gameboard status\n", argv[0]);
    }
    
    int main ( int argc, char *argv[]){
         
         FILE *moves; 
         FILE *boards; 
         if ( argc < 2)
         {
             message(argv);
             return -1;
         }
         int k; //this will be part of the command for the user to choose 0 for a random strategy against the computer or 1 for smart strategy
         k = atoi(argv[1]);
        
        if ( ( moves  = fopen(argv[2],"w")) == NULL)    // this is the output file name that records te moves made by two players (the CPU and user)
        {
            printf("Cannot open file %s\n", argv[2]);
            return -1;
        }
        if ( ( boards = fopen(argv[3],"w")) == NULL )// will record the game board status after each move
        {
            printf("Cannot open file %s\n", argv[3]);
            return -1;
        }
         
         int i,j;
         char x_mark = 'X'; // the player's marker value
         char o_mark = 'O'; // the cpu's marker
         char x_coord; //the moves the character will input onto the board
         char y_coord; // the move the player will input onto the board; 
         int flag; 
         int game; 
         
         char board[ROWS][COLUMNS]={
                                                
                                    {'o','|'},
                                    {'n','|'},
                                    {'m','|'},
                                    {'l','|'},
                                    {'k','|'},
                                    {'j','|'},
                                    {'i','|'},
                                    {'h','|'},
                                    {'g','|'},
                                    {'f','|'},
                                    {'e','|'},
                                    {'d','|'},
                                    {'c','|'},
                                    {'b','|'},
                                    {'a','|'},
                                    {' ',' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-' },
                                    {' ',' ','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'},        
                                    };
        
         for (i = 0; i<ROWS;i++){
            for(j = 0; j<COLUMNS;j++){
                board[i][j];
            //    printf("%c",board[i][j]);
                
                }
            }
        
    
    for (i=0;i<ROWS;i++)        //Printing a blank board 
        {
            for(j=0;j<COLUMNS;j++)
            {
            printf("%c ",board[i][j]);
            
            }            
            printf("\n");
            }
        
        int i_coord, j_coord;
        
        while (k == 0) //This option is basically where the user faces against the CPU. the CPU has randomly generated moves 
    
        {
            
            // you're taking in char ( as int for both)
            printf("Please choose a move\n"
                    "x :");
            x_coord = getchar(); // grabs x
            getchar(); // stops loop to get next
            printf("please enter y move: ");
            y_coord = getchar(); // grabs y
            
            /*
            scanf("%c",&x_coord); //,&y_coord);
            scanf("%c",&y_coord);
            */
            printf(" Player 1 Moves: x %c %c \n",x_coord, y_coord);
            
            fprintf(moves, " Player 1 Moves: %c%c \n",x_coord, y_coord);
            printf(" User move: %c %c \n",x_coord,y_coord);
        
            switch(x_coord){
                
                case '1': j_coord = 2; printf("case 1 %d\n",j_coord ); break;
                case '2': j_coord = 3; break;
                case '3': j_coord = 4; break;
                case '4': j_coord = 5; break;
                case '5': j_coord = 6; break;
                case '6': j_coord = 7; break;
                case '7': j_coord = 8; break;
                case '8': j_coord = 9; break;
                case '9': j_coord = 10; break;
                case 'A': j_coord = 11; break;
                case 'B': j_coord = 12; break;
                case 'C': j_coord = 13; break;
                case 'D': j_coord = 14; break;
                case 'E': j_coord = 15; break;
                case 'F': j_coord = 16; break;
                }
                
                switch(y_coord){
                    
                    case 'a': i_coord = 14; printf("case a %d\n", i_coord); break; 
                    case 'b': i_coord = 13; break; 
                    case 'c': i_coord = 12; break; 
                    case 'd': i_coord = 11; break; 
                    case 'e': i_coord = 10; break; 
                    case 'f': i_coord = 9; break; 
                    case 'g': i_coord = 8; break; 
                    case 'h': i_coord = 7; break; 
                    case 'i': i_coord = 6; break; 
                    case 'j': i_coord = 5; break; 
                    case 'k': i_coord = 4; break; 
                    case 'l': i_coord = 3; break; 
                    case 'm': i_coord = 2; break; 
                    case 'n': i_coord = 1; break; 
                    case 'o': i_coord = 0; break; 
                    
                }
                printf("x = %d, y = %d \n",j_coord,i_coord);    
                printf("here\n");
            board[i_coord][j_coord]= x_mark;
            
            for (i=0;i<ROWS;i++)        
        {
            for(j=0;j<COLUMNS;j++)
            {
                
            printf("%c ",board[i][j]);
            
            }
                        
            printf("\n");
            }    
                    
            
                
    
         
     }
     if (x_coord >'F' || y_coord >'o' ){
        
        printf("The Users input is invalid. Please Try again! ");
    }
     
         return 0;
    
    }
    results
    Code:
    Segmentation fault
    userx@slackwhere:~/bin
    $ ./p3  0 moves boards
    o |                  
    n |                  
    m |                  
    l |                  
    k |                  
    j |                  
    i |                  
    h |                  
    g |                  
    f |                  
    e |                  
    d |                  
    c |                  
    b |                  
    a |                  
        - - - - - - - - - - - - - - -   
        1 2 3 4 5 6 7 8 9 A B C D E F   
    Please choose a move
    x :1
    please enter y move: a
     Player 1 Moves: x 1 a 
     User move: 1 a 
    case 1 2
    case a 14
    x = 2, y = 14 
    here
    o |                  
    n |                  
    m |                  
    l |                  
    k |                  
    j |                  
    i |                  
    h |                  
    g |                  
    f |                  
    e |                  
    d |                  
    c |                  
    b |                  
    a | X                 
        - - - - - - - - - - - - - - -   
        1 2 3 4 5 6 7 8 9 A B C D E F   
    Please choose a move
    x :3
    please enter y move:  Player 1 Moves: x 
     
     
     User move: 
     
     
    x = 2, y = 14 
    here
    o |                  
    n |                  
    m |                  
    l |                  
    k |                  
    j |                  
    i |                  
    h |                  
    g |                  
    f |                  
    e |                  
    d |                  
    c |                  
    b |                  
    a | X                 
        - - - - - - - - - - - - - - -   
        1 2 3 4 5 6 7 8 9 A B C D E F   
    Please choose a move
    x :3
    please enter y move: e
     Player 1 Moves: x 3 e 
     User move: 3 e 
    x = 4, y = 10 
    here
    o |                  
    n |                  
    m |                  
    l |                  
    k |                  
    j |                  
    i |                  
    h |                  
    g |                  
    f |                  
    e |   X               
    d |                  
    c |                  
    b |                  
    a | X                 
        - - - - - - - - - - - - - - -   
        1 2 3 4 5 6 7 8 9 A B C D E F   
    Please choose a move
    x :
    it was not grabbing your y_coord

    plus you still have all of these to work out of your code as well, along with what I added to help me see what was going on in your code. So you still have a little bit of fun left to do. cheers!
    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "p3" "p3.c" (in directory: /home/userx/bin)
    p3.c: In function 'main':
    p3.c:87:4: warning: statement with no effect [-Wunused-value]
        board[i][j];
        ^
    p3.c:62:7: warning: unused variable 'game' [-Wunused-variable]
       int game;
           ^
    p3.c:61:7: warning: unused variable 'flag' [-Wunused-variable]
       int flag;
           ^
    p3.c:58:8: warning: unused variable 'o_mark' [-Wunused-variable]
       char o_mark = 'O'; // the cpu's marker
            ^
    Compilation finished successfully.
    Last edited by userxbw; 11-26-2017 at 07:57 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it was
    Segmentation fault
    on me so I fixed that, and it prints the x for user but I see no computer stuff (code) so it can play along. I hope that was what you where talking about because it looks like it is lining up properly to me.
    Last edited by userxbw; 11-26-2017 at 08:12 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    OK now look at this I seen what you where talking about after going back and looking at it again.
    fix your array, I got one line started already.
    Code:
     
         char board[ROWS][COLUMNS]={
                                                
                                    {'o','|'},
                                    {'n','|'},
                                    {'m','|'},
                                    {'l','|'},
                                    {'k','|'},
                                    {'j','|'},
                                    {'i','|'},
                                    {'h','|'},
                                    {'g','|'},
                                    {'f','|'},
                                    {'e','|'},
                                    {'d','|'},
                                    {'c','|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    {'b','|'},
                                    {'a','|'},
                                    {' ',' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-' },
                                    {' ',' ','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'},        
                                    };
    output
    Code:
    please enter y move:  Player 1 Moves: x 2 c 
     User move: 2 c 
    x = 3, y = 12 
    here
    o |                  
    n |                  
    m |                  
    l |                  
    k |                  
    j |                  
    i |                  
    h |                  
    g |                  
    f |                  
    e |                  
    d |                  
    c |   x                          
    b |                  
    a |                  
        - - - - - - - - - - - - - - -   
        1 2 3 4 5 6 7 8 9 A B C D E F   
    Please choose a move
    x and y:
    does that look more on the mark now? I made them little x and o. you of course can change them back if you like as well it is not even in your code. lol
    that's with the big X
    Code:
    //I changed your printf to this 
       printf("x = %c, y = %c \n",y_coord,x_coord);
    
    Please choose a move
    x and y:4 c
    please enter y move:  Player 1 Moves: x 4 c 
     User move: 4 c 
    // to get this instead, so player knows what they entered
    // you just need to fix the x and y print out to make them match
    x = c, y = 4 
    here
    o |                  
    n |                  
    m |                  
    l |                  
    k |                  
    j |                  
    i |                  
    h |                  
    g |                  
    f |                  
    e |                  
    d |                  
    c |       X                      
    b |                  
    a |                  
    Y   - - - - - - - - - - - - - - -   
    X   1 2 3 4 5 6 7 8 9 A B C D E F   
    Please choose a move
    x and y:
    I added the letters y and X to give a better perspective. but that is your call.
    Last edited by userxbw; 11-26-2017 at 09:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check Win algorithm for Gomoku with BitBoards
    By patishi in forum Game Programming
    Replies: 3
    Last Post: 09-29-2013, 08:14 AM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. Using variables as place markers for strings?
    By Tigers! in forum C Programming
    Replies: 5
    Last Post: 09-03-2009, 07:09 PM
  4. Net GoMoku
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-15-2004, 10:01 PM
  5. Help me: GoMoku AI Logic
    By LuckY in forum Game Programming
    Replies: 0
    Last Post: 03-10-2003, 04:26 PM

Tags for this Thread