Permalink
Browse files

Moved game varieties from C# to XML files. Renamed Great Detective to…

… Master Detective, and added HasSpyglass game variety rule.

darcs-hash:20070625150914-79701-27a86e0abc000cf009cfba19e35efc9db5b48351.gz
  • Loading branch information...
AArnott committed Jun 25, 2007
1 parent c24aebc commit 91e5e4785af3044b0e91b83fc3cb7dbc26a9c2b7
View
@@ -2,10 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.Xml.Serialization;
namespace ClueBuddy {
[Serializable]
public class Card {
+ public Card() { }
protected Card(string name) {
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
this.Name = name;
@@ -21,9 +23,10 @@ public class Card {
/// <summary>
/// The name of the weapon, place or suspect.
/// </summary>
+ [XmlAttribute]
public string Name {
get { return name; }
- private set { name = value; }
+ set { name = value; }
}
public override string ToString() {
@@ -32,6 +35,7 @@ public class Card {
}
[Serializable]
public class Weapon : Card {
+ public Weapon() { }
public Weapon(string name) : base(name) { }
public static IEnumerable<Weapon> Generate(params string[] names) {
@@ -40,6 +44,7 @@ public class Weapon : Card {
}
[Serializable]
public class Place : Card {
+ public Place() { }
public Place(string name) : base(name) { }
public static IEnumerable<Place> Generate(params string[] names) {
@@ -48,6 +53,7 @@ public class Place : Card {
}
[Serializable]
public class Suspect : Card {
+ public Suspect() { }
public Suspect(string name) : base(name) { }
public static IEnumerable<Suspect> Generate(params string[] names) {
View
@@ -14,7 +14,7 @@ public partial class Game : INotifyPropertyChanged {
/// </summary>
/// <param name="rules">The set of rules the game will play by.</param>
/// <param name="cards">The cards in this game.</param>
- Game(string varietyName, GameRules rules, IEnumerable<Card> cards) {
+ internal Game(string varietyName, GameRules rules, IEnumerable<Card> cards) {
if (string.IsNullOrEmpty(varietyName)) throw new ArgumentNullException("varietyName");
if (cards == null) throw new ArgumentNullException("cards");
if (cards.Count() == 0)
View
@@ -10,5 +10,10 @@ public struct GameRules {
/// Whether a player's turn ends at the first disproval resulting from a suspicion.
/// </summary>
public bool DisprovalEndsTurn;
+ /// <summary>
+ /// Whether the board has certain spaces that allow a player to look at a random card
+ /// of another player.
+ /// </summary>
+ public bool HasSpyglass;
}
}
View
@@ -2,38 +2,90 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.IO;
+using System.Xml;
+using System.Xml.Serialization;
namespace ClueBuddy {
- public partial class Game {
- public static IEnumerable<Game> Varieties {
- get {
- return new Game[] { GreatDetective, Simpsons };
- }
+ [XmlType(Namespace = TypeNamespace)]
+ public class GameVariety {
+ public const string DefaultFileExtension = "clueVariety";
+ const string TypeNamespace = "http://www.nerdbank.net/clue/variety";
+ static XmlSerializer getSerializer() {
+ XmlSerializer serializer = new XmlSerializer(typeof(GameVariety), TypeNamespace);
+ return serializer;
+ }
+ public static GameVariety LoadFrom(Stream stream) {
+ return (GameVariety)getSerializer().Deserialize(stream);
+ }
+ public void Save(Stream stream) {
+ getSerializer().Serialize(stream, this);
}
- public static Game GreatDetective {
- get {
- return new Game("Great Detective", new GameRules() {
- DisprovalEndsTurn = false
- }, Card.Generate(
- Suspect.Generate("Brunette", "Gray", "Green", "Mustard", "Peach", "Peacock", "Plum", "Rose", "Scarlet", "White"),
- Weapon.Generate("Knife", "Candlestick", "Horseshoe", "Lead pipe", "Poison", "Revolver", "Rope", "Wrench"),
- Place.Generate("Billiard room", "Carriage House", "Conservatory", "Courtyard", "Dining room", "Drawing room", "Fountain", "Gazebo", "Kitchen", "Library", "Studio", "Trophy room")
- )
- );
- }
+ /// <summary>
+ /// Constructs a <see cref="Game"/> based on the variety described by this
+ /// <see cref="GameVariety"/> instance.
+ /// </summary>
+ public Game Initialize() {
+ return new Game(Name, Rules, cards);
+ }
+
+ GameRules rules;
+ /// <summary>
+ /// The set of rules this game is playing by.
+ /// </summary>
+ public GameRules Rules {
+ get { return rules; }
+ set { rules = value; }
}
- public static Game Simpsons {
+
+ List<Weapon> weapons = new List<Weapon>();
+ /// <summary>
+ /// The weapon cards in the game.
+ /// </summary>
+ public List<Weapon> Weapons {
+ get { return weapons; }
+ set { weapons = value; }
+ }
+
+ List<Suspect> suspects = new List<Suspect>();
+ /// <summary>
+ /// The suspect cards in the game.
+ /// </summary>
+ public List<Suspect> Suspects {
+ get { return suspects; }
+ set { suspects = value; }
+ }
+
+ List<Place> places = new List<Place>();
+ /// <summary>
+ /// The place cards in the game.
+ /// </summary>
+ public List<Place> Places {
+ get { return places; }
+ set { places = value; }
+ }
+
+ [XmlIgnore]
+ IEnumerable<Card> cards {
get {
- return new Game("Simpsons", new GameRules() {
- DisprovalEndsTurn = true
- }, Card.Generate(
- Suspect.Generate("Green", "Mustard", "Peacock", "Plum", "Scarlet", "White"),
- Weapon.Generate("Poison donut", "Plutonium rod", "Saxophone", "Slingshot", "Necklace", "Extend-o-glove"),
- Place.Generate("Simpson house", "Frying dutchman", "Androids dungeon", "Burns manor", "Krusty loo studios", "Barneys Bowl o rama", "Kwik e mart", "Nuclear power plant", "Springfield retirement castle")
- )
- );
+ foreach (var card in Suspects) {
+ yield return card;
+ }
+ foreach (var card in Weapons) {
+ yield return card;
+ }
+ foreach (var card in Places) {
+ yield return card;
+ }
}
}
+
+ string name;
+ [XmlAttribute]
+ public string Name {
+ get { return name; }
+ set { name = value; }
+ }
}
}
@@ -14,8 +14,9 @@ class Program {
int cardColumnWidth = 10;
int playerColumnWidth = 10;
static ConsoleColor questionColor = ConsoleColor.Yellow;
- OpenFileDialog openDialog = new OpenFileDialog();
- SaveFileDialog saveDialog = new SaveFileDialog();
+ OpenFileDialog openVarietyDialog = new OpenFileDialog();
+ OpenFileDialog openGameDialog = new OpenFileDialog();
+ SaveFileDialog saveGameDialog = new SaveFileDialog();
Game game;
/// <summary>
@@ -28,8 +29,13 @@ class Program {
Player suggestingPlayer;
public Program() {
- setupFileDialog(openDialog);
- setupFileDialog(saveDialog);
+ setupGameFileDialog(openGameDialog);
+ setupGameFileDialog(saveGameDialog);
+
+ openVarietyDialog.DefaultExt = GameVariety.DefaultFileExtension;
+ openVarietyDialog.Filter = string.Format("Clue Varieties (*.{0})|*.{0}|All files (*.*)|*.*", GameVariety.DefaultFileExtension);
+ openVarietyDialog.FilterIndex = 0;
+ openVarietyDialog.Title = "Choose which game variety to load";
}
[STAThread]
@@ -114,6 +120,7 @@ class Program {
break;
case 'N':
chooseGame();
+ if (game == null) break;
setupPlayers();
game.Start();
learnOwnHand();
@@ -143,10 +150,10 @@ class Program {
}
bool? saveGame() {
- bool? result = saveDialog.ShowDialog();
+ bool? result = saveGameDialog.ShowDialog();
if (result.HasValue && result.Value) {
IFormatter formatter = new BinaryFormatter();
- using (Stream s = saveDialog.OpenFile()) {
+ using (Stream s = saveGameDialog.OpenFile()) {
formatter.Serialize(s, game);
formatter.Serialize(s, interactivePlayer.Name);
}
@@ -155,26 +162,31 @@ class Program {
}
bool? loadGame() {
- bool? result = openDialog.ShowDialog();
+ bool? result = openGameDialog.ShowDialog();
if (result.HasValue && result.Value) {
IFormatter formatter = new BinaryFormatter();
- using (Stream s = openDialog.OpenFile()) {
+ using (Stream s = openGameDialog.OpenFile()) {
game = (Game)formatter.Deserialize(s);
interactivePlayer = game.Players.First(p => p.Name.Equals(formatter.Deserialize(s)));
}
- saveDialog.FileName = openDialog.FileName;
+ saveGameDialog.FileName = openGameDialog.FileName;
}
return result;
}
- void setupFileDialog(FileDialog dlg) {
+ void setupGameFileDialog(FileDialog dlg) {
dlg.DefaultExt = "clueBuddy";
dlg.Filter = "ClueBuddy games (*.clueBuddy)|*.clueBuddy|All Files|*.*";
dlg.FilterIndex = 0;
}
void chooseGame() {
- game = choose("Which game variety are you starting?", false, v => v.VarietyName, Game.Varieties.ToArray());
+ bool? result = openVarietyDialog.ShowDialog();
+ if (!result.HasValue || !result.Value) return;
+ using (Stream s = openVarietyDialog.OpenFile()) {
+ game = GameVariety.LoadFrom(s).Initialize();
+ Console.WriteLine("Starting {0}...", game.VarietyName);
+ }
}
void setupPlayers() {
@@ -208,7 +220,7 @@ class Program {
suggestingPlayer = choosePlayer("Whose turn is it?", true, true);
if (suggestingPlayer == null) return;
try {
- if (suggestingPlayer == interactivePlayer) {
+ if (suggestingPlayer == interactivePlayer && game.Rules.HasSpyglass) {
while (true) {
var turnMenu = new Dictionary<char, string>();
turnMenu.Add('S', "Make a suggestion");
@@ -115,6 +115,14 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
+ <Content Include="..\Master Detective.clueVariety">
+ <Link>Master Detective.clueVariety</Link>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ <Content Include="..\Simpsons.clueVariety">
+ <Link>Simpsons.clueVariety</Link>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -13,6 +13,10 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using ClueBuddy;
+using Microsoft.Win32;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.IO;
namespace ClueBuddyGui {
/// <summary>
@@ -25,7 +29,9 @@ public partial class Main : Window {
public Main() {
InitializeComponent();
- game = Game.GreatDetective;
+ using (Stream s = new FileStream("Master Detective.clueVariety", FileMode.Open)) {
+ game = GameVariety.LoadFrom(s).Initialize();
+ }
game.Players.Add(new Player("Andrew"));
game.Players.Add(new Player("Cheryl"));
game.Players.Add(new Player("Jeff"));
@@ -71,7 +71,7 @@ public class FullGameTest : TestBase {
[TestMethod]
public void Wrigleys() {
- game = Game.Simpsons;
+ game = Simpsons;
game.AutoAnalysis = false;
Player andrew, cheryl, jeff, julia;
@@ -172,7 +172,7 @@ public class FullGameTest : TestBase {
[TestMethod]
public void Webbs() {
- game = Game.GreatDetective;
+ game = MasterDetective;
game.AutoAnalysis = false; // speeds up test if we call Analyze just once.
Player andrew, cheryl, rebecca, dan, table;
View
@@ -14,22 +14,22 @@ public class GameTest : TestBase {
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void StartWithNoPlayersTest() {
- Game g = Game.GreatDetective;
+ Game g = MasterDetective;
g.Start();
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void StartWithPlayersWithoutHandSizesTest() {
- Game g = Game.GreatDetective;
+ Game g = MasterDetective;
g.Players.AddRange(players);
g.Start();
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void StartWithoutHandSizesAppropriatelySized() {
- Game g = Game.GreatDetective;
+ Game g = MasterDetective;
g.Players.AddRange(players);
g.AssignApproximatePlayerHandSizes();
Debug.Assert(players[0].CardsHeldCount > 1);
@@ -59,7 +59,7 @@ public class GameTest : TestBase {
[TestMethod]
public void CluesTest() {
- Game newGame = Game.GreatDetective;
+ Game newGame = MasterDetective;
Assert.IsNull(newGame.Clues);
}
@@ -102,19 +102,19 @@ public class GameTest : TestBase {
[TestMethod]
public void CaseFileTest() {
- Game g = Game.GreatDetective;
+ Game g = MasterDetective;
Assert.IsNull(g.CaseFile);
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void AssignApproximatePlayerHandSizesBeforeAddingPlayersTest() {
- Game.GreatDetective.AssignApproximatePlayerHandSizes();
+ MasterDetective.AssignApproximatePlayerHandSizes();
}
[TestMethod]
public void AssignApproximatePlayerHandSizesTest() {
- Game g = Game.GreatDetective;
+ Game g = MasterDetective;
g.Players.AddRange(players);
g.AssignApproximatePlayerHandSizes();
// Make sure that all cards (except 3 for the Case File) are distributed as evenly as possible.
Oops, something went wrong.

0 comments on commit 91e5e47

Please sign in to comment.