/* This may look like c to you, but it's really -*-c++-*- */ /* GMA or "The Go-moku Apprentice" is a go-moku implementation that learns * the game from its own (and its opponents') mistakes. * Copyright (C) 1998 Johan Walles (d92-jwa@nada.kth.se) * * 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 #include #include "scores.h" int Scores::get_score(const Pattern pattern) const { int result = 0; int current_score; int i; scoremap::const_iterator iter; if (pattern.winner() != -1) return INT_MAX; for (i = 0; i < 4; i++) { iter = lines.find(pattern.nth_axis(i)); if (iter != lines.end()) { current_score = (*iter).second; // Making a certain move is never *bad*; it may do different // amounts of good though. Hence, no negative scores. if (current_score > 0) result += current_score; } } return result; } void Scores::mark_good(const Pattern pattern, int goodness) { // Mark this position as beneficial int i; unsigned int line; for (i = 0; i < 4; i++) { line = pattern.nth_axis(i); if (line != 0) lines[line] += goodness; } } bool Scores::init(const char *filename) { FILE *infile; unsigned int combo; int score; clear(); infile = fopen(filename, "r"); if (!infile) return false; while (!feof(infile)) { if (fscanf(infile, "%x;%d ", &combo, &score) != 2) { fclose(infile); clear(); return false; } lines[combo] = score; } fclose(infile); return true; } bool Scores::dump(const char *filename) { Scores readback; FILE *outfile; scoremap::const_iterator iter; // If we don't have anything to save, we don't save anything if (count() == 0) return true; outfile = fopen(filename, "w"); if (!outfile) return false; // Dump the lines to the file for (iter = lines.begin(); iter != lines.end(); iter++) { // Don't try to save zero scores if ((*iter).second != 0) { if (fprintf(outfile, "%x;%d\n", (*iter).first, (*iter).second) == 0) { fclose(outfile); return false; } } } fclose(outfile); // Check that the data can be read back properly return readback.init(filename); }