static bool CargaPartida(WallE w, Map m, out string respuesta) { Console.WriteLine("Cargar partida guardada (si/no)"); respuesta = Console.ReadLine(); if (respuesta == "si") { if (!File.Exists("partida guardada.txt")) { // crea el archivo con los comandos ejecutados StreamWriter comandosTxt = new StreamWriter("partida guardada.txt"); comandosTxt.Close(); } StreamReader txt = new StreamReader("partida guardada.txt"); LeeComandos(w, m, "partida guardada.txt", txt); txt.Close(); Console.Clear(); return(true); } else if (respuesta == "no") { return(true); } else { Console.WriteLine("Comando desconocido"); return(false); } }
static void GuardaProgreso(string usuario, WallE w, Map m) { int pos = w.GetPosition(); string bag = w.Bag(m); m.GuardaPartida(usuario, pos, bag); }
static void Main() { Map m = new Map(NPLACES, NITEMS); WallE w = new WallE(); m.ReadMap("madrid.map"); //StreamReader SelectorCarga(w, m); MuestraComandos(); //StreamWriter StreamWriter archivo = new StreamWriter("partida guardada.txt"); while (!quit && !w.atSpaceShip(m)) { string input = LeeInput(); try{ ProcesaInput(input, w, m); } catch (Exception e) { Console.WriteLine(e.Message); } if (input != "quit") { GuardaComandos(input, archivo); } } archivo.Close(); }
public void atSpaceShip() { //Arrange WallE.WallE w = new WallE.WallE(); Map mapa = new Map(1, 1); mapa.places[0].spaceShip = true; //Assert Assert.AreEqual(true, w.atSpaceShip(mapa), "Está en Spaceship"); }
static void EjecutaComandos(string file, WallE w, Map m) { StreamReader entrada = new StreamReader(file); string linea = entrada.ReadLine();; while (entrada.EndOfStream == false && linea != "") { ProcesaInput(linea, w, m); linea = entrada.ReadLine(); } entrada.Close(); }
static void ArchivosDeGuardado(string usuario, WallE w, Map m) { if (File.Exists(usuario)) { GuardaProgreso(usuario, w, m); } else { Console.WriteLine(usuario + " es su nuevo nombre de usuario."); GuardaProgreso(usuario, w, m); } }
public void BagVacia() { //Arrange Map mapa = new Map(1, 1); WallE.WallE w = new WallE.WallE(); //Act string resultado = w.Bag(mapa); //Assert Assert.AreEqual("Empty bag...", resultado, "No hay objeto en Bag"); }
public void atSpaceShipNoPlace() { //Arrange WallE.WallE w = new WallE.WallE(); Map mapa = new Map(0, 1); try { mapa.places[0].spaceShip = true; Assert.Fail("FALLO: No lanza una excepción cuando debería lanzarla"); } catch (AssertFailedException) { throw; } catch (Exception) { } }
static void ProcesaInput(string com, WallE w, Map m) { string[] splitcom = com.Split(' '); if (splitcom[0] != "go" && splitcom[0] != "pick" && splitcom[0] != "drop" && splitcom[0] != "info" && splitcom[0] != "items" && splitcom[0] != "bag" && splitcom[0] != "quit") { throw new Exception(""); } else { switch (splitcom[0]) { case "go": if (splitcom[1] == "north" || splitcom[1] == "south" || splitcom[1] == "east" || splitcom[1] == "west") { w.Move(m, String2Dir(splitcom[1])); } else { throw new Exception(""); } break; case "pick": w.PickItem(m, int.Parse(splitcom[1])); break; case "drop": w.DropItem(m, int.Parse(splitcom[1])); break; case "items": Console.WriteLine(m.GetItemsPlace(w.GetPosition())); break; case "info": Console.WriteLine(m.GetPlaceInfo(w.GetPosition())); break; case "bag": Console.WriteLine(w.Bag(m)); break; case "quit": break; } } }
static void Main(string[] args) { Map m = new Map(10, 3); WallE w = new WallE(); m.ReadMap("madrid.map.txt"); Console.WriteLine("¿Quieres cargar una partida?" + '\n' + "SI o NO"); string resp = Console.ReadLine().ToLower(); if (resp == "si") { Console.WriteLine("Introduzca un nombre de usuario : "); string usuarioCarga = Console.ReadLine(); CargaProgreso(usuarioCarga, w, m); } string comando = null; bool fin = false; while (!fin && comando != "quit") { Console.Write(">"); comando = Console.ReadLine(); try { ProcesaInput(comando, w, m); } catch { Console.WriteLine("Introduce un comando válido"); } fin = m.isSpaceship(w.GetPosition()); } if (comando == "quit") { Console.WriteLine("¿Desea guardar la partida?" + '\n' + "SI O NO"); string res = Console.ReadLine().ToLower(); if (res == "si") { Console.WriteLine("Introduzca un nombre de usuario : "); string usuarioGuardado = Console.ReadLine(); ArchivosDeGuardado(usuarioGuardado, w, m); } } }
//Lee y ejecuta los comandos de un archivo de texto llamado "comandos" static void LeeComandos(WallE w, Map m, string txt, StreamReader archivo) { string comando; while ((comando = archivo.ReadLine()) != null) { // try-catch por si el jugador se ha confundido en algún comando try { ProcesaInput(comando, w, m); } catch { } } archivo.Close(); }
// - - - - - - Tests WallE - - - - - - - public void WallEMove10() { //Arrange Map mapa = new Map(2, 1); WallE.WallE w = new WallE.WallE(); w.pos = 1; mapa.GeneraConexionesAuxiliar2(mapa, 0); //Act w.Move(mapa, Direction.North); //Assert Assert.AreEqual(0, w.GetPosition(), "WallE no se ha movido a la posicion 3"); }
public void WallEMove31() { //Arrange Map mapa = new Map(4, 1); WallE.WallE w = new WallE.WallE(); w.pos = 3; mapa.GeneraConexionesAuxiliar2(mapa, 2); //Act w.Move(mapa, Direction.West); //Assert Assert.AreEqual(0, w.GetPosition(), "WallE no se ha movido a la posicion 1"); }
public void DropItemGeneral() { //Arrange Map mapa = new Map(1, 1); WallE.WallE w = new WallE.WallE(); //Act mapa.places[0].itemsInPlace = new Lista(); mapa.places[0].itemsInPlace.InsertaItem(0); w.PickItem(mapa, 0); int resultado = w.bag.ItemsLista(); //Assert Assert.AreEqual(1, resultado, "Hay 1 objeto en Bag"); }
public void DropItemNoItem() { //Arrange Map mapa = new Map(1, 0); WallE.WallE w = new WallE.WallE(); //Act-Assert try { w.DropItem(mapa, 0); Assert.Fail("FALLO: No lanza una excepción cuando debería lanzarla"); } catch (AssertFailedException) { throw; } catch (Exception) { } }
public void WallEMove31NoPlace() { //Arrange Map mapa = new Map(1, 1); WallE.WallE w = new WallE.WallE(); w.pos = 3; //Act-Assert try { w.Move(mapa, Direction.West); Assert.Fail("FALLO: No lanza una excepción cuando debería lanzarla"); } catch (AssertFailedException) { throw; } catch (Exception) { } }
public void WallEMoveNoInterseccion() { //Arrange Map mapa = new Map(2, 1); WallE.WallE w = new WallE.WallE(); mapa.GeneraConexionesAuxiliar(mapa, 0); //Act try { w.Move(mapa, Direction.North); Assert.Fail("FALLO: No lanza una excepción cuando debería lanzarla"); } catch (AssertFailedException) { throw; } catch (Exception) { } }
public void Bag() { //Arrange Map mapa = new Map(1, 1); WallE.WallE w = new WallE.WallE(); string[] texto = { "garbage", "0", "Newspapers1", "place", "0", "\"News\"" }; mapa.places[0].itemsInPlace = new Lista(); mapa.CreateItem(texto); w.PickItem(mapa, 0); //Act string resultado = w.Bag(mapa); //Assert Assert.AreEqual("0: Newspapers1 \"News\"\n", resultado, "No hay objeto en Bag"); }
static void SelectorCarga(WallE w, Map m) { string respuesta = ""; bool control = false; while (!control) { control = CargaPartida(w, m, out respuesta); } if (respuesta == "no") { control = false; while (!control) { control = CargaComandos(w, m); } } }
public void PickItemUltimo() { //Arrange Map mapa = new Map(1, 2); WallE.WallE w = new WallE.WallE(); //Act mapa.places[0].itemsInPlace = new Lista(); mapa.places[0].itemsInPlace.InsertaItem(0); mapa.places[0].itemsInPlace.InsertaItem(1); w.PickItem(mapa, 1); int resultado = w.bag.ItemsLista(); int resultado1 = mapa.places[0].itemsInPlace.ItemsLista(); //Assert Assert.AreEqual(1, resultado, "Hay 1 objeto en Bag"); Assert.AreEqual(1, resultado1, "Hay 1 objeto en el lugar"); }
static bool CargaComandos(WallE w, Map m) { Console.WriteLine("Cargar un archivo de instrucciones (si/no)"); string respuesta = Console.ReadLine(); if (respuesta == "si") { StreamReader txt = new StreamReader("comandos.txt"); LeeComandos(w, m, "comandos.txt", txt); txt.Close(); return(true); } else if (respuesta == "no") { return(true); } else { Console.WriteLine("Comando desconocido"); return(false); } }
static void CargaProgreso(string usuario, WallE w, Map m) { StreamReader f = new StreamReader(usuario); w.CargaPartida(f, m); }
static void ProcesaInput(string com, WallE w, Map m) { string[] coms; coms = com.Split(' '); switch (coms[0]) { case ("go"): //convertidor a direcciones switch (coms[1]) { case "north": w.Move(m, Direction.North); break; case "south": w.Move(m, Direction.South); break; case "east": w.Move(m, Direction.East); break; case "west": w.Move(m, Direction.West); break; default: throw new Exception("Dirección no reconocida"); } break; case ("pick"): try { w.PickItem(m, int.Parse(coms[1])); } catch { throw new Exception("El item " + "\"" + coms[1] + "\" no está en este lugar"); } break; case ("drop"): try { w.DropItem(m, int.Parse(coms[1])); } catch { throw new Exception("El item " + "\"" + coms[1] + "\" no está en la bolsa"); } break; case ("items"): Console.WriteLine(m.GetItemsPlace(w.GetPosition())); break; case ("info"): Console.WriteLine(m.GetPlaceInfo(w.GetPosition())); break; case ("bag"): Console.WriteLine(w.Bag(m)); break; case ("quit"): quit = true; break; default: throw new Exception("Comando no reconocido"); } }