Thread: Genaral programming question

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    73

    Genaral programming question

    Hi. I am sorry if this question is not so relevant to this forum/board, but i've got much help in here in the past and this forum is great so i decided to ask here.

    Actually i am programming a board game in C (Gomoku), and I need to make a function that can tell if a certain square on the board (the board is represented by an array of size 225, e.g 15x15 board) is a border square / edge square. one can visualize the array as follows:
    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
    15,16,17..... and so on. when the upper line is the North border and the 0,15 is the west border etc.

    I thought of two ways to do it: i can make a simple function that takes an int (the square in question) and check one by one, with a conditional, to see if it matches to any of the border numbers.

    Code:
    int checkBorder(int n){
         if(n == 0 || n == 1 || ....
            return 1;
         }
         else{
              return 0;
          }
    Or i can do it like this: i can insert all the border indexes into a static array of 60 numbers which is already sorted, and do a binary search.
    what will be the fastest method?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you use a 2D array like
    Code:
    #define SIZE 15
    int board[SIZE][SIZE];
    then if either index is 0 or SIZE-1 then it's a border.

    If you have to represent your 2D array as a 1D array, then you could write your checkBorder function like this:
    Code:
    int checkBorder(int n) {
        int row = n / SIZE;
        int col = n % SIZE;
        // now you have the same indices you'd use with a true 2D array
        // ...
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    Sorry for my ignorance, but i didn't quite understand your solution. to be more clear, this is how the board looks like:
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
    15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

    (The first 7 rows should be lined up nicely..it should look like a square, but hope you get the picture)

    and need to know if a certain number is either one of the most upper row(the north border), or one of the most left column (the left border), or one of the most right column(the right border) of one of the most lower row (the south border). can you please elaborate?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I understand (and understood) what you mean. Here's the complete function.
    Code:
    int checkBorder(int n) {
        int row = n / SIZE;
        int col = n % SIZE;
        return row == 0 || col == 0 || row == SIZE-1 || col == SIZE-1;
    }
    Recall that integer division truncates (does not round) the result.
    And the modulus operator % gives the integer remainder of the division.
    Those two values give you the row and column of the given integer.
    Those row and column values can be used to see if it's a border square.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Okay, let's say you want to check whether square #161 is on any border.
    The row number is then 161 / 15 = 10 (remember that this is integer division, remainders get thrown away)
    The column number is 161 % 15 = 11 (modulo operation)

    Is the row number zero? No. So it's not on the top edge.
    Is the row number 14? No. So it's not on the bottom edge.
    Is the column number zero? Nope, so it's not on the left edge.
    Is the column number 14? No, so it's not on the right edge.
    Since it's not on any of the edges, it's not a border square.

    Now try it for square #179. It's in row number 179 / 15 = 11, and column number 179 % 15 = 14. You should be able to deduce it's on the right edge, so it is an edge square.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    OK thanks now i get it. In my stupidity i was doing n/224 LOL (the size of the array instead the size of the board). that's a great trick! and it is very very fast which is exactly what i need thank you very much (for both the explanations)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programming question
    By jmartin in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 05:04 AM
  2. programming question
    By invisibleghost in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-10-2005, 03:50 PM
  3. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM
  4. Programming question
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:00 AM