gnome-chess/engine/ai-profile.vala

102 lines
3.9 KiB
Vala
Raw Permalink Normal View History

2014-01-07 02:19:07 +00:00
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
2013-07-07 21:14:29 +00:00
* Copyright (C) 2010-2013 Robert Ancell
2016-03-15 17:12:58 +00:00
* Copyright (C) 2013-2016 Michael Catanzaro
2013-07-07 21:14:29 +00:00
*
* 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 3 of the License, or (at your option) any later
2013-07-07 21:14:29 +00:00
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
* license.
*/
2014-06-24 13:14:59 +00:00
public class AIProfile : Object
2011-01-13 19:00:57 +00:00
{
public string name { get; private set; }
public string protocol { get; private set; }
public string binary { get; private set; }
public string path { get; private set; }
public uint delay_seconds { get; private set; default = 2; }
public string[] easy_args { get; private set; }
public string[] normal_args { get; private set; }
public string[] hard_args { get; private set; }
public string[] easy_options { get; private set; }
public string[] normal_options { get; private set; }
public string[] hard_options { get; private set; }
public string[] easy_uci_go_options { get; private set; }
public string[] normal_uci_go_options { get; private set; }
public string[] hard_uci_go_options { get; private set; }
public static List<AIProfile> load_ai_profiles (string filename)
{
var profiles = new List<AIProfile> ();
var file = new KeyFile ();
try
{
file.load_from_file (filename, KeyFileFlags.NONE);
}
2011-01-01 01:42:50 +00:00
catch (KeyFileError e)
{
warning ("Failed to load AI profiles: %s", e.message);
return profiles;
}
catch (FileError e)
{
warning ("Failed to load AI profiles: %s", e.message);
return profiles;
}
foreach (string name in file.get_groups ())
2011-01-13 19:00:57 +00:00
{
debug ("Loading AI profile %s", name);
var profile = new AIProfile ();
try
{
profile.name = name;
profile.protocol = file.get_value (name, "protocol");
profile.binary = file.get_value (name, "binary");
profile.easy_args = load_array (file, name, "arg", "easy");
profile.normal_args = load_array (file, name, "arg", "normal");
profile.hard_args = load_array (file, name, "arg", "hard");
profile.easy_options = load_array (file, name, "option", "easy");
profile.normal_options = load_array (file, name, "option", "normal");
profile.hard_options = load_array (file, name, "option", "hard");
profile.easy_uci_go_options = load_array (file, name, "uci-go-option", "easy");
profile.normal_uci_go_options = load_array (file, name, "uci-go-option", "normal");
profile.hard_uci_go_options = load_array (file, name, "uci-go-option", "hard");
if (file.has_key (name, "delay-before-move"))
profile.delay_seconds = file.get_integer (name, "delay-before-move");
}
catch (KeyFileError e)
{
warning ("Error reading AI profiles: %s", e.message);
continue;
}
var path = Environment.find_program_in_path (profile.binary);
if (path != null)
{
profile.path = path;
profiles.append (profile);
}
2011-01-13 19:00:57 +00:00
}
return profiles;
}
private static string[] load_array (KeyFile file, string name, string type, string difficulty) throws KeyFileError
{
int count = 0;
while (file.has_key (name, "%s-%s-%d".printf (type, difficulty, count)))
count++;
2011-01-09 21:36:38 +00:00
string[] options = new string[count];
for (var i = 0; i < count; i++)
options[i] = file.get_value (name, "%s-%s-%d".printf (type, difficulty, i));
2011-01-09 21:36:38 +00:00
return options;
}
2011-01-09 21:36:38 +00:00
}