File: main.cc

package info (click to toggle)
animals 201007161925-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 200 kB
  • sloc: cpp: 1,809; makefile: 69
file content (310 lines) | stat: -rw-r--r-- 8,476 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "Record.h"
#include "Animal.h"
#include "Question.h"

#include "db4++-stuff.h"
#include "util.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
// #include <gdbm.h>
#include <malloc.h>
#include <signal.h>

#include <string>
#include <string.h>

// Copyright (c) 1997 by Jim Lynch.
// Copyright (c) 2010 by Philipp Schafft.
// This software comes with NO WARRANTY WHATSOEVER.
//
// 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; version 2 dated June, 1991, 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., 51 Franklin Street, Fifth Floor, Boston, MA
//    02110-1301 USA
//
// On Debian Linux systems, the complete text of the GNU General
// Public License can be found in `/usr/doc/copyright/GPL' (on some
// installations) or /usr/share/common-licenses/GPL (on newer 
// ones).

// There are two possible datum formats, both strings; and the records
// in both formats are separated by |.
//
// The first format is for an animal. Its format is simply:
//
// "a|nameofanimal|".
//
// The second format is for a yes-or-no question, which has a pair
// of gdbm keys in it. The format is:
//
// "q|text of question without the question mark|yes key|no key|".
//
// The keys will be simply numeric strings, converted using a strstream.
// Because of this, the program can later be expanded to allow larger
// numbers of animals and questions, without changing the file format
// _at_ _all_.
//
// There will be one key/value pair in the database, called "last" whose
// value is the ascii digits of the key of the last added. If 0, there
// are no animals.

Db *theFileHandle = NULL;
std::string progName;
std::string lineBuf;

void PlayGame(Db &handle)
{
  std::string theAnimal;
  std::string last(GetLast(handle));
  std::string dbtype_singular(GetDbType(handle, false));
  std::string dbtype_plural(GetDbType(handle, true));
  
  if(last == "0")
  {
    std::cout << "I give up! You win! What was your " << dbtype_singular << "? " << std::flush;
    readline(std::cin, theAnimal);
    while (theAnimal.length() < 1)
    {
	std::cout << "What was your " << dbtype_singular << "? " << std::flush;
	readline(std::cin, theAnimal);
    }
    
    Animal it(theAnimal);
    it.Write(handle, "1");
    SetLast(handle, "1");
    
    std::cout << "I now know 1 " << dbtype_plural << "!\07" << std::endl << std::endl;
  }
  else
  {
    std::string browseIndex = "1";
    Record *theRecordPtr;
    int isAnimal = IsAnimal(handle, browseIndex);
    
    if(isAnimal)
      theRecordPtr = new Animal(handle, browseIndex);
    else
      theRecordPtr = new Question(handle, browseIndex);
    
    while(! isAnimal)
    {
      Question theQuestion = *((Question *) theRecordPtr);
      delete theRecordPtr;
      
      std::cout << theQuestion << "? ";
      
      readline(std::cin, lineBuf);
      
      if(lineBuf[0] == 'y')
      {
	browseIndex = theQuestion.GetYesKey();
      }
      else if(lineBuf[0] == 'n')
      {
	browseIndex = theQuestion.GetNoKey();
      }

      isAnimal = IsAnimal(handle, browseIndex);
      
      if(isAnimal)
	theRecordPtr = new Animal(handle, browseIndex);
      else
	theRecordPtr = new Question(handle, browseIndex);
    }
    
    std::cout << std::endl << "I think I'll try a guess now...\07" << std::endl;
    std::cout << "Is your " << dbtype_singular << " a " << (*theRecordPtr) << "? " << std::flush;
    readline(std::cin, lineBuf);
    if(lineBuf[0] == 'y')
    {
      std::cout << "I win! I guessed your " << dbtype_singular << "!" << std::endl << std::endl;
    }
    else
    {
      std::cout << std::endl;
      std::cout << "I give up! You win! What was your " << dbtype_singular << "? " << std::flush;
      readline(std::cin, theAnimal);

      while (theAnimal.length() < 1)
      {
	std::cout << "What was your " << dbtype_singular << "? " << std::flush;
	readline(std::cin, theAnimal);
      }
      
      std::string questionBuf;
      int goodQuestion = 0;
      long guessedAnimalKey;
      long newAnimalKey;
      int guessedAnimalAnswer;
      int newAnimalAnswer;
      Animal newAnimal;
      Animal guessedAnimal = *((Animal *) theRecordPtr);
      
      while(! goodQuestion)
      {
	newAnimal = Animal(theAnimal);
	
	std::cout << std::endl;
	std::cout << "I need a yes-or-no question so I can later" << std::endl;
	std::cout << "tell the difference between a " << guessedAnimal;
	std::cout << " and a " << newAnimal << "." << std::endl;
	
	readline(std::cin, questionBuf);
	
	ChopQuestionMark(questionBuf);
	
	guessedAnimalKey = CStringToLong( GetLast(handle).c_str() ) + 1;
	newAnimalKey = guessedAnimalKey + 1;
	
	newAnimalAnswer = -1;
	
	while(newAnimalAnswer == -1)
	{
	  std::cout << "what would be the answer be for a ";
	  std::cout << newAnimal << "? " << std::flush;
	  
	  readline(std::cin, lineBuf);
	  
	  newAnimalAnswer = ((lineBuf[0] == 'y') ? 1 :
			     (lineBuf[0] == 'n') ? 0 : -1);
	}
	
	guessedAnimalAnswer = -1;
	
	while(guessedAnimalAnswer == -1)
	{
	  std::cout << "what would be the answer be for a ";
	  std::cout << guessedAnimal << "? " << std::flush;
	  
	  readline(std::cin, lineBuf);
	  
	  guessedAnimalAnswer = ((lineBuf[0] == 'y') ? 1 :
				 (lineBuf[0] == 'n') ? 0 : -1);
	}
	
	if(newAnimalAnswer != guessedAnimalAnswer)
	  goodQuestion = 1;
	else
	{
	  std::cout << std::endl;
	  std::cout << "I don't understand! If both answers are the";
	  std::cout << std::endl;
	  std::cout << "same, maybe I can't use the question! Maybe";
	  std::cout << std::endl;
	  std::cout << "I misunderstood? Please tell me again!";
	  std::cout << std::endl;
	}
      }
      
      const char *newKeyString = LongToNewCString(newAnimalKey);
      const char *guessedKeyString = LongToNewCString(guessedAnimalKey);
      Question newQuestion
	(
	  // watch for memory leaks in the next line
	  questionBuf.c_str(),
	  (newAnimalAnswer ? newKeyString : guessedKeyString),
	  (newAnimalAnswer ? guessedKeyString : newKeyString)
	);
      
      newQuestion.Write(handle, browseIndex.c_str());
      guessedAnimal.Write(handle, guessedKeyString);
      newAnimal.Write(handle, newKeyString);
      
      SetLast(handle, newKeyString);
      
      std::cout << std::endl;
      std::cout << "I now know " << (newAnimalKey + 1) / 2 << " " << dbtype_plural << "!";
      std::cout << std::endl;
      std::cout << std::endl;
    }
  }
}

void SigHandler(int sig)
{
  if (theFileHandle) {
    CloseFile(*theFileHandle);
  }
  std::cout << "Killed..." << std::endl;
  exit(0);
}

int main(int argc, char * argv[])
{
  std::string wantsToContinue("y");
  std::string dbtype_singular;
  const char * dbfile = DbName;
  
  for (int i = 1; i < argc; i++) {
   if ( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") ) {
    printf("Usage: %s [DatabaseFile [TypeString [PluralTypeString]]]\n", argv[0]);
    printf("   Or: %s {-h|--help}\n", argv[0]);
    return 0;
   }
  }

  progName = argv[0];

  if ( argc >= 2 )
  {
    dbfile = argv[1];
  }

  signal(SIGHUP, SigHandler);
  signal(SIGTERM, SigHandler);
  signal(SIGINT, SigHandler);
 
  std::cout << std::endl << "Welcome to animals!" << std::endl << std::endl;
  
  do
  {
    theFileHandle = OpenFile(dbfile);

    if ( argc >= 3 )
    {
	SetDbType(*theFileHandle, argv[2], false);
	if ( argc >= 4 )
	{
	    SetDbType(*theFileHandle, argv[3], true);
	}
    }

    dbtype_singular = GetDbType(*theFileHandle, false);

    if ( theFileHandle == NULL )
    {
	return 1;
    }

    
    std::cout << "Think of an " << dbtype_singular << "." << std::endl;
    std::cout << "Press <Enter> or <Return> to continue..." << std::flush;
    readline(std::cin, lineBuf);
    std::cout << std::endl;

    PlayGame(*theFileHandle);
    CloseFile(*theFileHandle);
    
    std::cout << "Want to play again? " << std::flush;
    readline(std::cin, lineBuf);
    std::cout << std::endl;
    
    wantsToContinue = lineBuf[0];
  }
  while(wantsToContinue == "y");

  return 0;
}