public static void MakeNewApple(Rendering snake, double areaWidth, double areaHeight, ref Apple apple) { Point coords; Random rand = new Random(); int ch = rand.Next(1, 11); if (ch > 0 && ch < 4) { apple = new SpoiledApple(apple.X, apple.Y, new Uri(System.IO.Directory.GetCurrentDirectory().ToString() + "\\snake-graphics.gif")); } else { apple = new Apple(apple.X, apple.Y, new Uri(System.IO.Directory.GetCurrentDirectory().ToString() + "\\snake-graphics.gif")); } bool goodPoint; double fieldHorizCells = areaWidth / snake.HeadSprite.FrameSize.Width; double fieldVertCells = areaHeight / snake.HeadSprite.FrameSize.Height; for (;;) { coords = new Point( rand.Next(0, Convert.ToInt32(fieldHorizCells) - 1) * snake.HeadSprite.FrameSize.Width, // X rand.Next(0, Convert.ToInt32(fieldVertCells) - 1) * snake.HeadSprite.FrameSize.Height); // Y goodPoint = true; if ((coords.X == snake.Tail.X && coords.Y == snake.Tail.Y) || (coords.X == snake.Head.X && coords.Y == snake.Head.Y)) { continue; // Collision, next cycle } for (int i = 0; i < snake.BodyLength; i++) { if (coords.X == snake.BodyPoints[i].X && coords.Y == snake.BodyPoints[i].Y) { goodPoint = false; break; } } if (goodPoint) { break; } } apple.X = coords.X; apple.Y = coords.Y; }
public Game(int level) { rendering = new Rendering(); player = new Human(); this.level = level; switch (level) { case 1: computer = new EasyLevel(); break; case 2: computer = new NormalLevel(); break; case 3: computer = new HardLevel(); break; } sw = new StreamWriter(rendering.fs, Encoding.Unicode); }
public static void CheckSnakeCollisions(Rendering snake, out bool gameOver) { gameOver = false; // Head collided with the tail if (snake.Head.X == snake.Tail.X && snake.Head.Y == snake.Tail.Y) { gameOver = true; return; } for (int i = 0; i < snake.BodyLength; i++) { if (snake.Head.X == snake.BodyPoints[i].X && snake.Head.Y == snake.BodyPoints[i].Y) { gameOver = true; return; } } }
public override int TakeShot(Human player, Rendering rendering) { int[,] possibilityMap; int currentHighestX = 0; int currentHighestY = 0; int currentHighestScore = 0; int tempScore = 0; int turn; possibilityMap = CalculatePossiblePlacements(); //clear all previously fired on tiles just to be safe for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { if (c_player.EnemyMap[x, y] != 0) { possibilityMap[x, y] = 0; } } } //choose most likely location of the ship for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { tempScore = possibilityMap[x, y]; if (tempScore > currentHighestScore) { currentHighestX = x; currentHighestY = y; currentHighestScore = tempScore; } } } turn = player.SquareHit(currentHighestX, currentHighestY, c_player, rendering); return(turn); }
/// This method handles hits, determining which ships, if any, were hit and updating the instances accordingly. It will return a 0 if no ships were destroyed and a 1 if some were. public int SquareHit(int posX, int posY, Computer computer, Rendering rendering) { int hitShipID; if (Map[posX, posY] != 0 && Map[posX, posY] < 11) //if the map square is a ship. { hitShipID = Map[posX, posY] - 1; if (Ships[hitShipID].ShipHit() == 0) { computer.EnemyMap[posX, posY] = 2; Map[posX, posY] = 12; rendering.DrawGameScreens(this); rendering.UpdateLog("The enemy shot hits!"); } else { for (int count = 0; count < Ships[hitShipID].Length; count++) { computer.EnemyMap[Ships[hitShipID].Coords[count, 0], Ships[hitShipID].Coords[count, 1]] = 3; //Make it known to the computer that a ship has been destroyed. Map[Ships[hitShipID].Coords[count, 0], Ships[hitShipID].Coords[count, 1]] = 13; } //Map[posX, posY] = 13; rendering.DrawGameScreens(this); rendering.UpdateLog(Ships[hitShipID].Name + " destroyed!"); } return(0); } else { computer.EnemyMap[posX, posY] = 1; Map[posX, posY] = 11; rendering.DrawGameScreens(this); rendering.UpdateLog("The enemy shot misses!"); return(-1); } }
static private void PlaceShips(Human p, Rendering r) { int shipLength; int shipX, shipY; int userInput; bool shipPlaced; ShipOrientation so; for (int shipNumber = 0; shipNumber < 10; shipNumber++) { shipLength = p.Ships[shipNumber].Length; so = p.Ships[shipNumber].Orientation; shipX = 0; shipY = 0; shipPlaced = false; Console.BackgroundColor = ConsoleColor.Black; Console.SetCursorPosition(25, 4); Console.Write("(use arrows + space)"); Console.SetCursorPosition(25, 5); Console.Write("(r - rotate)"); r.UpdateLog("Place " + p.Ships[shipNumber].Name); while (!shipPlaced) { r.DrawGameScreens(p); r.DrawShipPlacement(shipX, shipY, shipLength, so); userInput = (int)Console.ReadKey(true).Key; switch (userInput) { case 82: //'r' - rotate if (so == ShipOrientation.Vertical) { if (shipX + shipLength > 10) { shipX = 10 - shipLength; } } else { if (shipY + shipLength > 10) { shipY = 10 - shipLength; } } p.Ships[shipNumber].Rotate(); so = p.Ships[shipNumber].Orientation; break; case 37: //left arrow if (shipX - 1 >= 0) { shipX--; } break; case 38: //up arrow if (shipY - 1 >= 0) { shipY--; } break; case 39: //right arrow if (so == ShipOrientation.Vertical) { if (shipX + 1 < 10) { shipX++; } } else { if (shipX + shipLength < 10) { shipX++; } } break; case 40: //down arrow if (so == ShipOrientation.Vertical) { if (shipY + shipLength < 10) { shipY++; } } else { if (shipY + 1 < 10) { shipY++; } } break; case 32: //Space bar if (ShipCollision(shipX, shipY, shipLength, so, p) == false) { for (int i = 0; i < shipLength; i++) { if (so == ShipOrientation.Vertical) { p.Map[shipX, shipY + i] = shipNumber + 1; //this will be a unique identifier in order to allow quick lookups of hit ships. } else { p.Map[shipX + i, shipY] = shipNumber + 1; //this will be a unique identifier in order to allow quick lookups of hit ships. } } p.Ships[shipNumber].PlaceShip(shipX, shipY, so); shipPlaced = true; } break; } } } r.UpdateLog("All ships placed!"); Console.BackgroundColor = ConsoleColor.Black; Console.SetCursorPosition(25, 5); Console.Write(" "); Console.SetCursorPosition(25, 4); Console.Write(" "); }
static public void Arrange(ref Human p, Rendering r) { ShipAdd(ref p); PlaceShips(p, r); }
/// This method handles the computer's turn. public abstract int TakeShot(Human player, Rendering rendering);
public override int TakeShot(Human player, Rendering rendering) { int[,] possibilityMap; int currentHighestX = 0; int currentHighestY = 0; int currentHighestScore = 0; int tempScore = 0; int turn; Random rnd = new Random(); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { if (chMap[x, y] == rnd.Next(1, 10)) { turn = player.SquareHit(x, y, c_player, rendering); chMap[x, y] = 15; return(turn); } } } Hunting = true; for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { if (c_player.EnemyMap[x, y] == 2) //if there is a hit ship that has not been fully destroyed... { Hunting = false; //go into target mode } } } if (Hunting) { possibilityMap = CalculatePossiblePlacements(); } else { possibilityMap = CalculateTargetedPlacements(); } //clear all previously fired on tiles just to be safe for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { if (c_player.EnemyMap[x, y] != 0) { possibilityMap[x, y] = 0; } } } //choose most likely location of the ship for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { tempScore = possibilityMap[x, y]; if (tempScore > currentHighestScore) { currentHighestX = x; currentHighestY = y; currentHighestScore = tempScore; } } } turn = player.SquareHit(currentHighestX, currentHighestY, c_player, rendering); return(turn); }
/// This method handles the player making a shot on the enemy ships. public int TakeShot(Computer computer, Rendering rendering) { int xSelection = 0; int ySelection = 0; bool shotFired = false; int userInput; int turn = -1; while (shotFired == false) { rendering.DrawGameScreens(this); rendering.UpdateLog("Select Target"); bool innerLoop = true; while (innerLoop) { userInput = (int)Console.ReadKey(true).Key; if (userInput < 75 && userInput > 64) //if the key pressed is a to j { xSelection = userInput - 65; //converts the keycode to an x co-ordinate; innerLoop = false; } } rendering.DrawTarget(this, xSelection); innerLoop = true; while (innerLoop) { userInput = (int)Console.ReadKey(true).Key; if (userInput < 58 && userInput > 47) //if the key pressed is 0 to 9 { ySelection = userInput - 48; innerLoop = false; } } rendering.DrawTarget(this, xSelection, ySelection); rendering.UpdateLog("Ready to Fire"); innerLoop = true; while (innerLoop) { userInput = (int)Console.ReadKey(true).Key; if (userInput == 32 || userInput == 13) //spacebar or enter { if (EnemyMap[xSelection, ySelection] != 0) { rendering.UpdateLog("Error: You've already fired at that square!"); } else { turn = computer.SquareHit(xSelection, ySelection, this, rendering); shotFired = true; } innerLoop = false; } else if (userInput == 8) //backspace { rendering.UpdateLog("Shot cancelled"); innerLoop = false; } } } return(turn); }