public void AddSeaToRender(Sea sea) { State[,] field = sea.GetBody() as State[, ]; fieldRows = field.GetLength(0); fieldCols = field.GetLength(1); scene.Append(string.Format(" 0 1 2 3 4 5 6 7 8 9" + Environment.NewLine)); scene.Append(string.Format(" -------------------" + Environment.NewLine)); for (int row = 0; row < fieldRows; row++) { scene.Append(string.Format("{0} |", row)); for (int col = 0; col < fieldCols; col++) { if (sea is PlayerSea) { if (field[row, col] == State.Empty) { scene.Append((char)State.Empty); scene.Append(" "); } else if (field[row, col] == State.MissedHit) { scene.Append((char)State.MissedHit); scene.Append(" "); } else if (field[row, col] == State.Ship) { scene.Append((char)State.Ship); scene.Append(" "); } else if (field[row, col] == State.TargetHit) { scene.Append((char)State.TargetHit); scene.Append(" "); } else { throw new ArgumentException("Invalid State in Sea matrix."); } } else if (sea is BootSea) { if (field[row, col] == State.Empty || field[row, col] == State.Ship) { scene.Append((char)State.Empty); scene.Append(" "); } else if (field[row, col] == State.MissedHit) { scene.Append((char)State.MissedHit); scene.Append(" "); } else if (field[row, col] == State.TargetHit) { scene.Append((char)State.TargetHit); scene.Append(" "); } else { throw new ArgumentException("Invalid State in Sea matrix."); } } } scene.Append(Environment.NewLine); } }
//protected Position GenerateRobotShoot() //{ // Position robotShot=new Position(); // robotShot.X=randGenerator.Next(0, seas[0].GetBody().GetLength(0)); // robotShot.Y = randGenerator.Next(0, seas[0].GetBody().GetLength(0)); // return robotShot; //} public void ConfigureGameField(Sea someSea, int shipMaxLength) { if (shipMaxLength > someSea.GetBody().GetLength(0) - 2) { throw new OutOfShipLengthException("Exceeded maximal ship length.", someSea.GetBody().GetLength(0) - 2); } for (int shipLength = shipMaxLength; shipLength >= 2; shipLength--) { while (true) { Ship currentShip; if (someSea is PlayerSea) { string message = string.Format("Ship`s length: {0}. Enter ships parameters in format: \r\nStart position: X Y:\r\nAlignment:", shipLength); renderer.AddMessageToRender(message); renderer.RenderAll(); renderer.ClearRenderObjects(); currentShip = this.userInput.ReadInputShip(shipLength); } else { currentShip = this.GenerateShip(shipLength, someSea.GetBody().GetLength(0)); } Position[] currentShipBody = currentShip.GetBody() as Position[]; int freePositions = 0; foreach (var position in currentShipBody) { if (someSea is PlayerSea) { if ((position.X < someSea.GetBody().GetLength(0) && position.Y < someSea.GetBody().GetLength(1)) && (someSea[position.X, position.Y] == State.Empty)) { freePositions++; } } else if (someSea[position.X, position.Y] == State.Empty) { freePositions++; } } if (freePositions == shipLength) { foreach (var position in currentShipBody) { someSea[position.X, position.Y] = State.Ship; } if (someSea is PlayerSea) { this.AddPlayerShips(currentShip); string message = string.Format("Ship with length {0} was created", shipLength); renderer.AddMessageToRender(message); renderer.RenderAll(); renderer.ClearRenderObjects(); } else { this.AddRobotShips(currentShip); } break; } else if (someSea is PlayerSea) { string message = string.Format("Ship with length {0} was NOT created", shipLength); renderer.AddMessageToRender(message); renderer.RenderAll(); renderer.ClearRenderObjects(); } } } this.AddSea(someSea); }