static void Main(string[] args) { Console.WriteLine("DualSnake Server v1.0\r\nCopyright (C) Solymosi Máté 2011\r\n"); if (args.Length > 0) { try { LoadLevel(args[0]); } catch { return; } } else { try { LoadLevel("Default.level"); } catch { Console.WriteLine("Using built-in default map."); Map = Tools.CreateDefaultMap(SnakeGame.FieldWidth, SnakeGame.FieldHeight); } } Server <SnakePlayer> Server = new Server <SnakePlayer>(1991); Server.Connected += new Server <SnakePlayer> .ConnectedDelegate(Server_Connected); Server.Listen(); Console.WriteLine("\r\nListening on port 1991. Press CTRL+C to exit."); while (true) { System.Threading.Thread.Sleep(10000); } }
public static SnakeMap CreateDefaultMap(int Width, int Height) { string BlankLevel = "Blank level | 8 | Right | Left\n"; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { if (new int[] { 0, Height - 1 }.Contains(i) || new int[] { 0, Width - 1 }.Contains(j)) { BlankLevel += "%"; continue; } BlankLevel += (i == Height / 2 ? (j == 10 ? "1" : (j == Width - 11 ? "2" : " ")) : " "); } BlankLevel += "\n"; } return(SnakeMap.Parse(BlankLevel, Width, Height)); }
static void LoadLevel(string F) { string Input = ""; SnakeMap LoadedMap; Console.WriteLine("Loading map " + F + "..."); try { Input = File.ReadAllText(F); } catch { Console.WriteLine("Could not load " + F + ". Make sure the file exists and it is readable."); throw new Exception(); } try { LoadedMap = SnakeMap.Parse(Input, SnakeGame.FieldWidth, SnakeGame.FieldHeight); } catch { Console.WriteLine("The map " + F + " contains errors. Fix them and try again."); throw new Exception(); } Console.WriteLine("Map loaded successfully."); Map = LoadedMap; }
static void Main(string[] args) { Console.WriteLine("DualSnake Server v1.0\r\nCopyright (C) Solymosi Máté 2011\r\n"); if (args.Length > 0) { try { LoadLevel(args[0]); } catch { return; } } else { try { LoadLevel("Default.level"); } catch { Console.WriteLine("Using built-in default map."); Map = Tools.CreateDefaultMap(SnakeGame.FieldWidth, SnakeGame.FieldHeight); } } Server<SnakePlayer> Server = new Server<SnakePlayer>(1991); Server.Connected += new Server<SnakePlayer>.ConnectedDelegate(Server_Connected); Server.Listen(); Console.WriteLine("\r\nListening on port 1991. Press CTRL+C to exit."); while (true) { System.Threading.Thread.Sleep(10000); } }
public SnakeGame(SnakeMap Map) { this.Map = Map; }
static public SnakeMap Parse(string Level, int FieldWidth, int FieldHeight) { SnakeMap Map = new SnakeMap(true); string[] Rows = Level.Split('\n').Select(new Func<string, string>(delegate(string start) { return start.TrimEnd(new char[] { '\r' }); })).Where(new Func<string, bool>(delegate(string w) { return w != ""; })).ToArray(); string[] Info = Rows[0].Split('|').Select(new Func<string, string>(delegate(string start) { return start.Trim(); })).ToArray(); Map.StartLength = int.Parse(Info[1]); Map.StartDirection[0] = Tools.ParseDirection(Info[2]); Map.StartDirection[1] = Tools.ParseDirection(Info[3]); if (Rows.Length != FieldHeight + 1) { throw new ArgumentException(); } for (int Row = 1; Row < Rows.Length; Row++) { for (int j = 0; j < FieldWidth; j++) { int Column = j + 1; if (Rows[Row][j] == '%') { Map.Walls.Add(new Point(Column, Row)); } } } int Count = 0; for (int Row = 1; Row < Rows.Length; Row++) { for (int j = 0; j < FieldWidth; j++) { int Column = j + 1; if (new char[] { '1', '2' }.Contains(Rows[Row][j])) { Count++; if (Count > 2) { throw new ArgumentException(); } int Index = Rows[Row][j] == '1' ? 0 : 1; if (Map.StartLocation[Index] != null) { throw new ArgumentException(); } for (int i = 0; i < Map.StartLength; i++) { Point MP; switch (Map.StartDirection[Index]) { case Direction.Up: MP = new Point(Column, Row + i); break; case Direction.Down: MP = new Point(Column, Row - i); break; case Direction.Left: MP = new Point(Column + i, Row); break; case Direction.Right: MP = new Point(Column - i, Row); break; default: throw new ArgumentException(); } MP = Tools.ModPoint(MP, FieldWidth, FieldHeight); if (Map.Walls.Any(new Func<Point, bool>(delegate(Point p) { return p.X == MP.X && p.Y == MP.Y; })) || (Count > 1 ? Map.StartLocation[1 - Index].Equals(MP) : false)) { throw new ArgumentException(); } } Map.StartLocation[Index] = new Point(Column, Row); } } } if (Map.StartLocation[0] == null || Map.StartLocation[1] == null) { throw new ArgumentException(); } return Map; }
static public SnakeMap Parse(string Level, int FieldWidth, int FieldHeight) { SnakeMap Map = new SnakeMap(true); string[] Rows = Level.Split('\n').Select(new Func <string, string>(delegate(string start) { return(start.TrimEnd(new char[] { '\r' })); })).Where(new Func <string, bool>(delegate(string w) { return(w != ""); })).ToArray(); string[] Info = Rows[0].Split('|').Select(new Func <string, string>(delegate(string start) { return(start.Trim()); })).ToArray(); Map.StartLength = int.Parse(Info[1]); Map.StartDirection[0] = Tools.ParseDirection(Info[2]); Map.StartDirection[1] = Tools.ParseDirection(Info[3]); if (Rows.Length != FieldHeight + 1) { throw new ArgumentException(); } for (int Row = 1; Row < Rows.Length; Row++) { for (int j = 0; j < FieldWidth; j++) { int Column = j + 1; if (Rows[Row][j] == '%') { Map.Walls.Add(new Point(Column, Row)); } } } int Count = 0; for (int Row = 1; Row < Rows.Length; Row++) { for (int j = 0; j < FieldWidth; j++) { int Column = j + 1; if (new char[] { '1', '2' }.Contains(Rows[Row][j])) { Count++; if (Count > 2) { throw new ArgumentException(); } int Index = Rows[Row][j] == '1' ? 0 : 1; if (Map.StartLocation[Index] != null) { throw new ArgumentException(); } for (int i = 0; i < Map.StartLength; i++) { Point MP; switch (Map.StartDirection[Index]) { case Direction.Up: MP = new Point(Column, Row + i); break; case Direction.Down: MP = new Point(Column, Row - i); break; case Direction.Left: MP = new Point(Column + i, Row); break; case Direction.Right: MP = new Point(Column - i, Row); break; default: throw new ArgumentException(); } MP = Tools.ModPoint(MP, FieldWidth, FieldHeight); if (Map.Walls.Any(new Func <Point, bool>(delegate(Point p) { return(p.X == MP.X && p.Y == MP.Y); })) || (Count > 1 ? Map.StartLocation[1 - Index].Equals(MP) : false)) { throw new ArgumentException(); } } Map.StartLocation[Index] = new Point(Column, Row); } } } if (Map.StartLocation[0] == null || Map.StartLocation[1] == null) { throw new ArgumentException(); } return(Map); }