/* * FigureManager.cs * Authors: * Copyright (c) 2007-2008 Cornell University 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. */ using System; using System.Collections.Generic; using System.IO; using Microsoft.Xna.Framework.Content; using Random = Util.Random; namespace StarJack { /// /// This class will load and store the Figure Pairings /// and will be responsible for determnining the next pairs /// class FigureManager { //The keys for the mFigures dictionary will be //what is used for the rest list and dictionaies to keep consistency. //a list of all the figures private readonly Dictionary mFigures; private List mKeys; private List mEmbed; private List mPicked; private const string mKeyToken = "@"; private readonly string[] mSplitToken = new string[]{ " " }; private KeyValuePair mOldPair; //This will hold the pairs between keys and Embeddeds private Dictionary> mPairs; public FigureManager() { mFigures = GroupTextureManager.GetGroupTexture("Figure"); LoadPairDefinitions(); } /// /// Loads the key-figure pairings /// public void LoadPairDefinitions() { mPairs = new Dictionary>(); mKeys = new List(); mEmbed = new List(); mPicked = new List(); String dir = Entity.RootDirectory + Entity.mContentDirect + @"\Figure\KeyPairs.txt"; if (File.Exists(dir)) { using (FileStream tManifest = new FileStream(dir, FileMode.Open, FileAccess.Read, FileShare.Read)) using (StreamReader mIn = new StreamReader(tManifest)) { String line; string[] tSplit; List keyFigures; while (!mIn.EndOfStream) { line = mIn.ReadLine(); if (line.StartsWith(mKeyToken)) { tSplit = line.Split(mSplitToken, StringSplitOptions.RemoveEmptyEntries); string key = tSplit[1]; mKeys.Add(key); keyFigures = new List(); for (int a = 2; a < tSplit.Length; a++) { keyFigures.Add(tSplit[a]); mEmbed.Add(tSplit[a]); } mPairs.Add(key, keyFigures); } } } } } /// /// Gets a random key figure pair. /// The key-figure pair will not be the same as the one before /// /// public KeyValuePair GetNextPair() { string key = mKeys[Random.Next(mKeys.Count)]; while (key == mOldPair.Key) { key = mKeys[Random.Next(mKeys.Count)]; } int correct = Random.Next(0, 100); List mFigureList = correct < 50 ? mPairs[key] : mEmbed; string embeded = mFigureList[Random.Next(mFigureList.Count)]; int cycle = 0; while (embeded == mOldPair.Value && cycle < mFigureList.Count && !mPicked.Contains(embeded) ) { embeded = mFigureList[Random.Next(mFigureList.Count)]; cycle++; } mPicked.Add(embeded); if (mPicked.Count > mEmbed.Count/2) { mPicked.Clear(); } mOldPair = new KeyValuePair(key, embeded); return new KeyValuePair(mFigures[key], mFigures[embeded]); } /// /// This will check if there is a pair between a key and a figure /// public bool IsPair(string pKey, string pFigure) { if (mPairs.ContainsKey(pKey)) { if (mPairs[pKey].Contains(pFigure)) { return true; } } return false; } /// /// Loads all the figures! /// /// public void LoadContent(ContentManager content) { foreach (string ti in mFigures.Keys) { mFigures[ti].LoadContent(content); } } } }