Created
March 8, 2015 21:53
-
-
Save edele/0822dfb500011db416e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace SeaBattle2 | |
| { | |
| class Program | |
| { | |
| #region Fields | |
| enum shipOrientation { Horizontal, Vertical }; | |
| static shipOrientation orientation = shipOrientation.Horizontal; | |
| static bool debugMode = true; | |
| static byte debugInfoXPos = 38; | |
| static byte debugInfoYPos = 20; | |
| static byte playgroundComputerXPos = 5; | |
| static byte playgroundComputerYPos = 2; | |
| static byte playgroundPlayerXPos = 38; | |
| static byte playgroundPlayerYPos = 2; | |
| static int[,] playgroundComputer = new int[12, 12]; | |
| static int[,] playgroundPlayer = new int[12, 12]; | |
| #endregion | |
| #region Methods | |
| static void WriteDebugInfo(int var, int xpos, int ypos, string info) | |
| { | |
| if (debugMode == true) | |
| { | |
| Console.BackgroundColor = ConsoleColor.Black; | |
| Console.SetCursorPosition(xpos, ypos); | |
| Console.Write("{0} - {1}", var, info); | |
| } | |
| } | |
| static void WriteDebugInfo(int xpos, int ypos, string info) | |
| { | |
| if (debugMode == true) | |
| { | |
| Console.BackgroundColor = ConsoleColor.Black; | |
| Console.SetCursorPosition(xpos, ypos); | |
| Console.Write("{0}", info); | |
| } | |
| } | |
| static void DecrementAllTemporary2To0(int[,] m) | |
| { | |
| for (int i = 0; i < m.GetLength(0); i++) | |
| { | |
| for (int j = 0; j < m.GetLength(1); j++) | |
| { | |
| if (m[i, j] > 1) m[i, j] -= 2; | |
| } | |
| } | |
| } | |
| static void SetAllTemporary2To1(int[,] m) | |
| { | |
| for (int i = 0; i < m.GetLength(0); i++) | |
| { | |
| for (int j = 0; j < m.GetLength(1); j++) | |
| { | |
| if (m[i, j] == 2) m[i, j]--; | |
| } | |
| } | |
| } | |
| static void Draw(int x, int y, int[,] m) | |
| { | |
| int Rows = m.GetLength(0); | |
| int Cols = m.GetLength(1); | |
| for (int i = 1; i < Rows-1; i++) | |
| { | |
| for (int j = 1; j < Cols-1; j++) | |
| { | |
| if (m[i, j] >= 0) | |
| { | |
| Console.SetCursorPosition(x + j * 2, y + i); | |
| if (m[i, j] == 0) | |
| { | |
| Console.BackgroundColor = ConsoleColor.Black; | |
| Console.ForegroundColor = ConsoleColor.Gray; | |
| } | |
| else | |
| if (m[i, j] == 1) | |
| { | |
| Console.BackgroundColor = ConsoleColor.Green; | |
| Console.ForegroundColor = ConsoleColor.Black; | |
| } | |
| else | |
| { | |
| Console.BackgroundColor = ConsoleColor.Red; | |
| Console.ForegroundColor = ConsoleColor.White; | |
| } | |
| Console.Write("{0:D2}", m[i, j]); | |
| } | |
| } | |
| } | |
| } | |
| static void PlaceShip(int x, int y, int size, int[,] m) | |
| { | |
| if (orientation == shipOrientation.Horizontal) | |
| { | |
| for (int i = 0; i < m.GetLength(0); i++) | |
| { | |
| for (int j = 0; j < m.GetLength(1); j++) | |
| { | |
| if (x==j && y==i && size>0) | |
| { | |
| m[i, j] += 2; | |
| x++; | |
| size--; | |
| } | |
| } // for j | |
| } // for i | |
| } // if orientation | |
| else | |
| { | |
| for (int i = 0; i < m.GetLength(0); i++) | |
| { | |
| for (int j = 0; j < m.GetLength(1); j++) | |
| { | |
| if (x == j && y == i && size > 0) | |
| { | |
| m[i, j] += 2; | |
| y++; | |
| size--; | |
| } | |
| } | |
| } | |
| } // if orientation | |
| } | |
| static bool ShipPlacementAvailability(int[,] m) | |
| { | |
| // Переменная сбрасывается на false, если двойка соседствует хоть | |
| // с одной однеркой прямо или по диагонали. | |
| for (int i = 1; i < m.GetLength(0)-1; i++) | |
| { | |
| for (int j = 1; j < m.GetLength(1)-1; j++) | |
| { | |
| if (m[i, j] == 3) return false; | |
| if (m[i,j]==2) | |
| { | |
| // По часовой стрелке проверяются клетки вокруг каждой двойки в массиве | |
| // Двойка - это временный корабль, который превратится в однерку | |
| // если пройдет проверку. Если не пройдет - в нулик. | |
| if (m[i + 1, j] == 1 || m[i + 1, j + 1] == 1 || m[i, j + 1] == 1 || | |
| m[i - 1, j - 1] == 1 || m[i - 1, j] == 1 || m[i - 1, j + 1] == 1 || | |
| m[i, j + 1] == 1 || m[i + 1, j + 1] == 1) return false; | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| static bool CheckBeforeShiftingSelection(int x,int y,int size, byte direction,int[,] m) | |
| { | |
| if (direction == 2) | |
| { | |
| while (size>=0) | |
| { | |
| if (m[x + size, y] == 1) return false; | |
| size--; | |
| } | |
| } | |
| return true; | |
| } | |
| static void PlayerPlaceShip(int size) | |
| { | |
| int x = 1, y = 1; | |
| int xCollision = 0, yCollision = 0; | |
| ConsoleKey key; | |
| bool shipSetted = false; | |
| while (shipSetted==false) | |
| { | |
| #region CollisionCounter | |
| if (orientation==shipOrientation.Horizontal) | |
| { | |
| xCollision = size; | |
| yCollision = 1; | |
| } | |
| else | |
| { | |
| xCollision = 1; | |
| yCollision = size; | |
| } | |
| #endregion | |
| DecrementAllTemporary2To0(playgroundPlayer); | |
| PlaceShip(x, y, size, playgroundPlayer); | |
| Draw(playgroundPlayerXPos, playgroundPlayerYPos, playgroundPlayer); | |
| key = Console.ReadKey().Key; | |
| switch (key) | |
| { | |
| case ConsoleKey.DownArrow: | |
| if (y + yCollision < 11) | |
| { | |
| y++; | |
| } | |
| else | |
| { | |
| y = 1; | |
| } | |
| break; | |
| case ConsoleKey.UpArrow: | |
| if (y > 1) | |
| { | |
| y--; | |
| } | |
| else | |
| { | |
| y = 11-yCollision; | |
| } | |
| break; | |
| case ConsoleKey.RightArrow: | |
| if (x + xCollision < 11) | |
| { | |
| x++; | |
| } | |
| else | |
| { | |
| x = 1; | |
| } | |
| break; | |
| case ConsoleKey.LeftArrow: | |
| if (x > 1) | |
| { | |
| x--; | |
| } | |
| else | |
| { | |
| x = 11 - xCollision; | |
| } | |
| break; | |
| case ConsoleKey.Spacebar: | |
| if (orientation == shipOrientation.Horizontal) | |
| orientation = shipOrientation.Vertical; | |
| else | |
| orientation = shipOrientation.Horizontal; | |
| break; | |
| case ConsoleKey.Enter: | |
| if (ShipPlacementAvailability(playgroundPlayer) == true) | |
| { | |
| SetAllTemporary2To1(playgroundPlayer); | |
| Draw(playgroundPlayerXPos, playgroundPlayerYPos, playgroundPlayer); | |
| shipSetted = true; | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| } | |
| static void RandomPlaceShip(int size) | |
| { | |
| Random rnd = new Random(); | |
| int x = 1, y = 1; | |
| while (true) | |
| { | |
| #region RandomOrientation | |
| if (rnd.Next(0, 10) > 5) | |
| orientation = shipOrientation.Horizontal; | |
| else orientation = shipOrientation.Vertical; | |
| #endregion RandomOrientation | |
| #region CollisionCounter_AND_CoordinateGenerator | |
| if (orientation==shipOrientation.Horizontal) | |
| { | |
| x = rnd.Next(1, 10 - size); | |
| y = rnd.Next(1,8); | |
| } | |
| else | |
| { | |
| x = rnd.Next(1, 9); | |
| y = rnd.Next(1, 10 - size); | |
| } | |
| #endregion CollisionCounter_AND_CoordinateGenerator | |
| PlaceShip(x, y, size, playgroundComputer); | |
| if (ShipPlacementAvailability(playgroundComputer) == true) | |
| { | |
| SetAllTemporary2To1(playgroundComputer); | |
| Draw(playgroundComputerXPos, playgroundComputerYPos, playgroundComputer); | |
| return; | |
| } | |
| else DecrementAllTemporary2To0(playgroundComputer); | |
| } | |
| } | |
| static void RandomPlaceSingleBoatShip() | |
| { | |
| Random rnd = new Random(); | |
| int x = 1, y = 1; | |
| while (true) | |
| { | |
| x = rnd.Next(1, 9); | |
| y = rnd.Next(1, 9); | |
| playgroundComputer[x, y] += 2; | |
| if (ShipPlacementAvailability(playgroundComputer) == true) | |
| { | |
| SetAllTemporary2To1(playgroundComputer); | |
| Draw(playgroundComputerXPos, playgroundComputerYPos, playgroundComputer); | |
| return; | |
| } | |
| else DecrementAllTemporary2To0(playgroundComputer); | |
| } | |
| } | |
| #endregion Methods | |
| static void Main(string[] args) | |
| { | |
| Console.CursorVisible = false; | |
| RandomPlaceShip(4); | |
| RandomPlaceShip(3); | |
| RandomPlaceShip(3); | |
| RandomPlaceShip(2); | |
| RandomPlaceShip(2); | |
| RandomPlaceShip(2); | |
| Console.ReadKey(); | |
| RandomPlaceSingleBoatShip(); | |
| Console.ReadKey(); | |
| RandomPlaceSingleBoatShip(); | |
| Console.ReadKey(); | |
| RandomPlaceSingleBoatShip(); | |
| Console.ReadKey(); | |
| RandomPlaceSingleBoatShip(); | |
| #region SettingPlayerShips | |
| PlayerPlaceShip(4); | |
| PlayerPlaceShip(3); | |
| PlayerPlaceShip(3); | |
| PlayerPlaceShip(2); | |
| PlayerPlaceShip(2); | |
| PlayerPlaceShip(2); | |
| PlayerPlaceShip(1); | |
| PlayerPlaceShip(1); | |
| PlayerPlaceShip(1); | |
| PlayerPlaceShip(1); | |
| #endregion SettingPlayerShips | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment