File: scores.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (94 lines) | stat: -rw-r--r-- 2,519 bytes parent folder | download | duplicates (2)
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
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2001 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <riq@corest.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; only version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include "scores.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <assert.h>
#include <unistd.h>
#include <time.h>

#include "common.h"


static struct HighScores highscores;

void insert_score(SCORES const* score)
{
	insert_highscore(&highscores, score);
}

void insert_highscore(struct HighScores* hs, SCORES const* score)
{
	size_t insert_pos;
	for(insert_pos = 0;
	        /* *** WARNING *** this loop can walk one element past the array.
	         *
	         * The code in the loop jumps out if the test index goes behind the
	         * valid array elements. When you change this code, make sure to not
	         * access invalid elements (index >= count) */
	        (insert_pos <= hs->count);
	        insert_pos++) {
		if(insert_pos >= SCORES_MAX) {
			// the new high score is lower (or equal) to each existing one
			return;
		}

		if((insert_pos >= hs->count) // behind the last existing element

		        // the existing element has a lower score
		        || (score->score > hs->highscores[insert_pos].score)) {
			// we found the insertion point
			break;
		}
	}

	if(hs->count < (SCORES_MAX)) {
		// The highscore list is not full, so expand it
		hs->count++;
	}

	// do we need to move existing elements?
	if(insert_pos < (hs->count-1)) {
		assert(hs->count > 1);
		for(size_t i=(hs->count-1); i>insert_pos; i--) {
			assert(i >= 1);
			hs->highscores[i] = hs->highscores[i-1];
		}
	}

	// put the new highscore into its place
	hs->highscores[insert_pos] = *score;
}

void scores_map(scores_map_func func, void* user)
{
	for(size_t i=0; i < highscores.count; i++) {
		func(highscores.highscores+i, user);
	}
}

void scores_init()
{
	highscores.count = 0;
}