Scoring function in C

This is what our subgroup came up with for scoring. The array “temp” is useful for properly scoring “rows”. It could be a little more clean in the columns, but identifying singletons isn’t too easy.

Any ideas for something a little more efficient?

score.doc (22 KB)

score.doc (22 KB)

You can use the

 vb code tag to display your code with the proper formatting.

It looks like the score function assumes that the global field array will be populated, and to calculate each teams score, you call it twice. The rack array can use any two characters that you want and the character to count are passed to the score function.

Is that correct?

Sounds about right, and perhaps the commenting leaves something to be desired.

int score(char piece)
returns the (integer) score of a particular team(passed as a char, “piece”).

char rack]] contains character “pieces” (X’s and O’s, if you like)


(spoilers actually score correctly, but only because their char (piece) would be different from that passed to the function on the call)

Your algorithm gives the same results as mine (and I think yours is faster, although I haven’t really tested it).

The single cout seems to be unnecessary, and removing it saves someone from having to include iostream (if they aren’t already using it).

Also, declaring your variables inside the for construct is only legal in c++ not c.

Sounds good. The <cout> for debugging has been removed. Those singletons were troublesom for us, probably not too uncommon an issue for anyone messing with this.

I wish things were a bit more elegant for the columns, but when it’s only 3 high, what are you going to do…

I just have a quick question about this code. Was it intentional to define the rack array as being 9x4? In the game, the rack is 8x3. If I followed the code correctly, it appears that none of the extra spots are used, meaning that there are 12 useless cells in the array.

Otherwise, this looks good. I’m trying to get my own scoring algorithm done and also having a hard time with the singletons.

From what I recall, the 9x4 array was a holdover (no longer used) from a draft where we thought it may be useful to know the sum of pieces active in each row and column, kind of like this image:


Not that mixing ints and chars is good practice, but it was just a draft that grew.


As far as execution speed, I’m not sure that this would make much of a difference, but would it be equivalent to replace lines 16 through 22 with:

while(c<8 && rack[c]**==piece) c++;

Also, how fast is the pow function compared to say, an array that you make that just contains the correct number of points for a given length? This would also allow you not to use math.h.


int run_points[9]={0,0,4,8,16,32,64,128,256};

and then replace:




-Corrected a couple of brackets.

-I’ve also written my own version of a score keeping program, though I’m sure it runs many times slower than yours because it does fun things like passing lots of structures by value. Depending on what your using it for though, performance might not really be an issue and you might want to take a look. See:**