Question on converting 2D array

This is a discussion on Question on converting 2D array within the C Programming forums, part of the General Programming Boards category; I am working on a tic tac toe game and will be accepting coordinates from the players like 2,5 or ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39

    Question on converting 2D array

    I am working on a tic tac toe game and will be accepting coordinates from the players like 2,5 or 1,7 and need to convert the coordinates to actual space numbers. This would be easy to do however the game is setup that the board can be anywhere from a 3x3 to a 9x9. So the space numbers would change depending on board size.
    QUESTION: How do I convert the 2D array of say 2,4 to space 6?

    I hope you all follow me...

    THANKS,

    Dodgetech

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    So, do you want to have a number for each square?...

    I'm not sure what you want
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In a 4X4 board:

    row =2, column = 2, would be:
    row * width of the row (2 * 4) + column or 8 + 2 equals square 10

    Code:
    -----------------
    | 0 | 1 | 2 | 3 |   Row 0
    -----------------
    | 4 | 5 | 6 | 7 |   Row 1
    -----------------
    | 8 | 9 | x |   |   Row 2
    -----------------
      0   1   2   3     <====Columns
    And the x is in square 10, row 2, column 2

    There are other ways to figure this. Find something you're comfortable with.

    No, I didn't draw out the last row - use your imagination.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Adak View Post
    In a 4X4 board:

    row =2, column = 2, would be:
    row * width of the row (2 * 4) + column or 8 + 2 equals square 10

    Code:
    -----------------
    | 0 | 1 | 2 | 3 |   Row 0
    -----------------
    | 4 | 5 | 6 | 7 |   Row 1
    -----------------
    | 8 | 9 | x |   |   Row 2
    -----------------
      0   1   2   3     <====Columns
    And the x is in square 10, row 2, column 2

    There are other ways to figure this. Find something you're comfortable with.

    No, I didn't draw out the last row - use your imagination.

    Here is what I got:

    Code:
    // INCLUDES
    #include <stdio.h>
    #include <stdlib.h>
     
    // DEFINES
    #ifndef __TRUE_FALSE__
    #define __TRUE_FALSE__
    #define TRUE 1
    #define FALSE 0
    #endif
     
    // ROWS and COLS must be between 1 and 9
    #define ROWS 7
    #define COLS 7
     
    // MARKER CODES
    #define MARKONE 'X'
    #define MARKTWO 'O'
    #define BLANK ' '
     
    // VICTORY CODES
    #define NOWIN 0 
    #define MARKONEVICTORY 1 
    #define MARKTWOVICTORY 2 
    #define TIE  3 
    #define ERROR 4 
    #define EPIC_FAIL 5 
     
    // GAME PARAMETER CODES
    #define CONSECUTIVE_MARKS_REQUIRED 3
     
    // PROTOTYPES
    void InitializeBoard(char[ROWS][COLS]);
    void DisplayBoard(char[ROWS][COLS]);
    int PlayerMove(int, int, char[ROWS][COLS], char);
    int VictoryCheck(int, char[ROWS][COLS]);
    void DisplayVictoryMessage(int);
    
    
    //helper fxn
    int helpcheckWin(int[],int, int);
    int movesleft(char[ROWS][COLS]);
    
    
    
    
     
    // MAIN
    int main() {
     // THE CORE LOGIC FOR YOUR GAME GOES HERE
    // declare variables
    char board[ROWS][COLS];   // Size of board
    int go = 0;               // Square selection 
    int row = 0;              // Row index for a square               
    int column = 0;           // Column index for a square    
    int player = 0;           // Player number
    int winner = 0;           // Winner of game
    
    
    
    
    
    
        for(int i = 0; i<49 && winner==0; i++) {  //Game continues up to 49 turns or until there is a winner
    
    
            // initialize board
            InitializeBoard(board);
    
    
            // Display the current board
            DisplayBoard(board);
    
    
            player = rand() %2 + 1;    // Randomly choose an initial player.
    
    
            /* Get valid player square selection */
            do {
                printf("\nPlayer %d, please enter the number of the square "
                        "where you want to place your %c: ", player,(player==1)?'X':'O');
                scanf("%d", &go);
    
    
                row = --go/3;                                 /* Get row index of square      */
                column = go%3;                                /* Get column index of square   */
            }
            while(go<0 || go>49 || board[row][column]>'49');
    
    
            board[row][column] = (player == 1) ? 'X' : 'O';        /* Insert player symbol   */
    
    
        }
        
    // Decide if you have a winner or if the game is over.
    // display victory message
    DisplayVictoryMessage( VictoryCheck(CONSECUTIVE_MARKS_REQUIRED, board) );   
        
        
    
    
    
    
     
    
    
      //exit program
     return 0;
    }
     
    // FUNCTION IMPLEMENTATIONS
    void InitializeBoard(char board[ROWS][COLS]) {
     // INITIALIZE BOARD WITH BLANK SPACES
    
    
        for(int i=0; i<ROWS; i++ ) {
            for(int j=0; j<COLS; j++ ) {
                board[i][j]= BLANK;   
            }
        }
    }
     
    void DisplayBoard(char board[ROWS][COLS]) {
     // DISPLAY BOARD ON SCREEN
    
    
    printf("*************TIC-TAC-TOE**********\n\n");
    
    
        printf( "  " );
        for(int i=0; i<COLS; i++ ) {
            printf( "%d ", i + 1 );
        }
            printf( "\n\n" );
          
        for(int i=0; i<ROWS; i++ ) {
            printf( "%d ", i + 1);
        
            for(int j=0; j<COLS-1; j++ ) {
                printf( "%c|", board[i][j] );
            }
    
    
        printf( "\n  -" );
        for(int j=0; j<COLS-1; j++ ) {
            printf( "+-" , board[i][j]);
        }
        printf( "\n" );
    
    
        }
    }
     
    int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
    // DESCRIBE ILLEGAL PLAYERMOVES
    
    
        if ( row <= 0 || col <= 0 || row >= ROWS || col >= COLS ) {
            printf( "THE MOVE IS NOT ON THE BOARD\n" );
            return FALSE;
        }
    
    
        else if (board[row-1][col-1] == MARKONE || 
                 board[row-1][col-1] == MARKTWO) {
                printf( "THAT SPACE IS ALREADY OCCUPIED\n" );
                return FALSE;
        }
    
    
        else {
            board[row-1][col-1]=symbol;
            return TRUE;
        }
    
    
        return FALSE;
    }
     
    int VictoryCheck(int winRequirement, char board[ROWS][COLS]) {
    // CHECK IF WIN HAS OCCURRED
    
    
    winRequirement = CONSECUTIVE_MARKS_REQUIRED;
    int P1Wins=0,P2Wins=0;
    int cnt[2]={0};//count, 0-P1, 1-P2
    int i,j,k;
        
    //horizontal
        for(int i=0;i<ROWS;i++){
            for(int j=0;j<COLS;j++){
                if(board[i][j]==MARKONE){
                    P1Wins+=helpcheckWin(cnt,0,winRequirement);
                }
                else if(board[i][j]==MARKTWO){
                        P2Wins+=helpcheckWin(cnt,1,winRequirement);
                }
                else{
                    cnt[0]=0;
                    cnt[1]=0;
                }
    
    
            }//end for j
        }//end for i
    
    
    //vertical
    cnt[0]=0;
    cnt[1]=0;
        for(int j=0;j<COLS;j++){
            for(int i=0;i<ROWS;i++){
                if(board[i][j]==MARKONE){
                    P1Wins+=helpcheckWin(cnt,0,winRequirement);
                }
                else if(board[i][j]==MARKTWO){
                        P2Wins+=helpcheckWin(cnt,1,winRequirement);
                }
                else{
                    cnt[0]=0;
                    cnt[1]=0;
                }
            }//end for j
        }//end for i
    
    
    //FILE *fo;
    //fo=fopen("trace.txt","w");
    
    
    ////////////////////////////////////////////////////////
    //diagonal left
    cnt[0]=0;
    cnt[1]=0;
    
    
    i=0;
    
    
        for(int j=0;j<=COLS-winRequirement;j++){
        // fprintf(fo,"\n\n%d\n",i);
            for(int k=0;k+j<COLS &&k+i<ROWS;k++){
        // fprintf(fo,"\nChecking [%d,%d]",i+k,j+k);
                if(board[i+k][j+k]==MARKONE){
                    P1Wins+=helpcheckWin(cnt,0,winRequirement);
                }
                else if(board[i+k][j+k]==MARKTWO){
                        P2Wins+=helpcheckWin(cnt,1,winRequirement);
                }
                else{
                    cnt[0]=0;
                    cnt[1]=0;
                }
            }
    
    
        }
    cnt[0]=0;
    cnt[1]=0;
    j=0;
        for(int i=1;i<=ROWS-winRequirement;i++){
        //fprintf(fo,"\n\n%d\n",i);
            for(int k=0;k+j<COLS &&k+i<ROWS;k++){
            // fprintf(fo,"\nChecking [%d,%d]",i+k,j+k);
                if(board[i+k][j+k]==MARKONE){
                    P1Wins+=helpcheckWin(cnt,0,winRequirement);
                    }
                    else if(board[i+k][j+k]==MARKTWO){
                            P2Wins+=helpcheckWin(cnt,1,winRequirement);
                    }
                    else{
                        cnt[0]=0;
                        cnt[1]=0;
                    }
            }
    
    
        }
    
    
    ////////////////////////////////////////////////////////
    //diagonal right
    
    
    cnt[0]=0;
    cnt[1]=0;
    
    
    i=0;
    
    
        for(j=COLS-1;j-winRequirement+1>=0;j--){
        //fprintf(fo,"\n\n%d\n",i);
            for(k=0;j-k>=0 &&k+i<ROWS;k++){
            // fprintf(fo,"\nChecking [%d,%d]",i+k,j-k);
                if(board[i+k][j-k]==MARKONE){
                    P1Wins+=helpcheckWin(cnt,0,winRequirement);
                }
                else if(board[i+k][j-k]==MARKTWO){
                        P2Wins+=helpcheckWin(cnt,1,winRequirement);
                }
                else{
                    cnt[0]=0;
                    cnt[1]=0;
                }
            }
    
    
        }
    cnt[0]=0;
    cnt[1]=0;
    
    
    j=COLS-1;
        for(i=1;i<=ROWS-winRequirement;i++){
        //fprintf(fo,"\n\n%d\n",i);
            for(k=0;j-k>=0 &&k+i<ROWS;k++){
            //fprintf(fo,"\nChecking [%d,%d]",i+k,j-k);
                if(board[i+k][j-k]==MARKONE){
                    P1Wins+=helpcheckWin(cnt,0,winRequirement);
                }
                else if(board[i+k][j-k]==MARKTWO){
                    P2Wins+=helpcheckWin(cnt,1,winRequirement);
                }
                else{
                    cnt[0]=0;
                    cnt[1]=0;
                }
            }
    
    
        }
    //fclose(fo);
    
    
    //__________________________________________
        if(P1Wins==0&&P2Wins==0){
            if(movesleft(board)==1)
                return NOWIN;
            else
                return TIE;
        }
        else if(P1Wins>0&&P2Wins==0){return MARKONEVICTORY;}
        else if(P1Wins==0&&P2Wins>0){return MARKTWOVICTORY;}
        else if(P1Wins>0&&P2Wins>0){return ERROR;}
        else
            return EPIC_FAIL;
    
    
    }//end ck
    
    
    int helpcheckWin(int arr[],int idx, int req){ //returns either 1 for win or 0 for not win
    arr[idx]++; //increment this idx
    arr[(idx+1)%2]=0; //set other idx to 0
    
    
        if(arr[idx]==req){
            arr[idx]=0; //reset to 0
            return 1; //return 1 to be added to win
        }
    
    
        return 0;//if not a win, return 0
    }
    int movesleft(char b[ROWS][COLS]){
    int i,j;
        for(i=0;i<ROWS;i++){
            for(j=0;j<COLS;j++){
                if(b[i][j]==BLANK)
                    return 1;
            }
        }
        return 0;//if no BLANK found
    
    
    
    
    }
     
    void DisplayVictoryMessage(int victoryCode) {
     // AN IMPLEMENTATION FOR THIS FUNCTION WAS PROVIDED IN A WEEKLY ASSIGNMENT
    
    
     switch(victoryCode) {
     case NOWIN:
     printf("There is still no winner.\n");
     break;
     
     case MARKONEVICTORY:
     printf("MARKONE has won the game.\n");
     break;
     
     case MARKTWOVICTORY:
     printf("MARKTWO has won the game.\n");
     break;
     
     case TIE:
     printf("The game is a draw.\n");
     break;
     
     case ERROR:
     printf("Something bad happened... MARKONE and MARKTWO have both won.\n");
     break;
     
     case EPIC_FAIL:
     printf("Something bad happened... VictoryCheck() has produced an impossible combination of return code indicators.\n");
     break;
     
     default:
     printf("DisplayVictoryMessage() was passed an invalid victoryCode.\n");
     }
    }
    This is a work in progress. All is done except the Main core logic which I am working on....

    THANKS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting tchar array to string array
    By GeekInTraining in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2011, 12:34 AM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. Replies: 8
    Last Post: 11-12-2008, 10:25 AM
  4. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  5. Question involving converting array->int
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2002, 08:51 AM

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