File: score.cc

package info (click to toggle)
groundhog 1.1-8
  • links: PTS
  • area: main
  • in suites: potato
  • size: 320 kB
  • ctags: 327
  • sloc: cpp: 1,816; makefile: 106; sh: 18
file content (103 lines) | stat: -rw-r--r-- 2,294 bytes parent folder | download
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
/* Groundhog -- a simple logic game
 * Copyright (C) 1998-1999 Maurits Rijk
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 "score.h"

ostream&
operator<<(ostream& s, const Score& score)
{
   return s << "(\"" << score._name << "\" " << score._seconds << ')';
}

istream&
operator>>(istream& s, Score& score)
{
   char c;
   int flag = s.flags();
   s.flags(flag & ~ios::skipws);

   score._name = "";
   s >> c >> c;
   do {
      if (c != '\"')
	 score._name += c;
      s >> c;
   } while (c != '\"');
   s.flags(flag);

   s >> score._seconds >> c;
   return s;
}

int
Score::operator<(Score& score)
{
   return _seconds < score._seconds;
}

bool 
ScoreList::IsNewHighScore(int seconds)
{
   list<Score>::iterator last = _list.end();
   return _list.size() < 10 || seconds < (*--last).GetSeconds();
}

void 
ScoreList::AddHighScore(Score& score)
{
   list<Score>::iterator i;
   for (i = _list.begin(); i != _list.end(); i++)
      if (score < *i)
	 break;
   if (i == _list.end()) {
      _list.push_back(score);
   } else {
      _list.insert(i, score);
   }
   if (_list.size() > 10)
      _list.pop_back();
}

ostream&
operator<<(ostream& s, const ScoreList& slist)
{
   s << '(' << endl;
   for (list<Score>::const_iterator i = slist._list.begin(); 
	i != slist._list.end(); i++)
      s << *i << endl;
   return s << ')' << endl;
}

istream&
operator>>(istream& s, ScoreList& slist)
{
   slist._list.clear();
   char c;
   s >> c;
   s >> c;
   if (c == '(') {
      do {
	 Score score;
	 s.putback(c);
	 s >> score >> c;
	 slist._list.push_back(score);
      } while (c != ')');
   }
   return s;
}