/// <summary> /// create the different gameboard with the label associate, /// add the game button /// add the boats /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { //this.fm = new FileManager(PATH, "Bob"); // fix the size of the form compared to the size of one case this.Size = new System.Drawing.Size(XFORM * size, YFORM * size); this.GameStarting = false; // creation of new gameboard for the battleship platAlly = new GameBoard("Your field", 10, 10); platEnemy = new GameBoard("Enemy field", this.platAlly.Size.Width + 150, this.platAlly.Location.Y); // add button gameButton(); // boats for (int i = 0; i < NITEMBOAT; i++) { // 3 mines if (i < NMINES) { nShip = 0; } else { nShip = i - 2; } ships.Add(new Ship(nShip, size, new Point(size, (size + 10) * i))); } gameInProgress = true; //GameFinish("Victoire !", Color.Green); //fm = new FileManager(PATH, PSEUDO); }
static void Main(string[] args) { GameBoard game = new GameBoard(); Console.WriteLine(game); }
static void Main(string[] args) { int size = 15; GameBoard gb = new GameBoard(size); GameBoard hidden = new GameBoard(size); KeyHandler kh = new KeyHandler(); gb.ships.Add(new Ship(1, 'P', "Ponorka", new List <Point>() { new Point(5, 5, true, 'P') }, size, new List <Point>() { new Point(5, 5, true, 'P') }, new List <Point>() { new Point(5, 5, true, 'P') }, new List <Point>() { new Point(5, 5, true, 'P') })); gb.ships.Add(new Ship(2, 'T', "Torpedoborec", new List <Point>() { new Point(1, 1, true, 'T'), new Point(2, 1, true, 'T') }, size, new List <Point>() { new Point(1, 1, true, 'T'), new Point(1, 2, true, 'T') }, new List <Point>() { new Point(1, 1, true, 'T'), new Point(2, 1, true, 'T') }, new List <Point>() { new Point(1, 1, true, 'T'), new Point(1, 2, true, 'T') })); gb.ships.Add(new Ship(3, 'K', "Kriznik", new List <Point>() { new Point(1, 2, true, 'K'), new Point(2, 2, true, 'K'), new Point(3, 2, true, 'K') }, size, new List <Point>() { new Point(2, 1, true, 'K'), new Point(2, 2, true, 'K'), new Point(2, 3, true, 'K') }, new List <Point>() { new Point(1, 2, true, 'K'), new Point(2, 2, true, 'K'), new Point(3, 2, true, 'K') }, new List <Point>() { new Point(2, 1, true, 'K'), new Point(2, 2, true, 'K'), new Point(2, 3, true, 'K') })); gb.ships.Add(new Ship(4, 'B', "Bitevni lod", new List <Point>() { new Point(1, 2, true, 'B'), new Point(2, 2, true, 'B'), new Point(3, 2, true, 'B'), new Point(4, 2, true, 'B') }, size, new List <Point>() { new Point(2, 1, true, 'B'), new Point(2, 2, true, 'B'), new Point(2, 3, true, 'B'), new Point(2, 4, true, 'B') }, new List <Point>() { new Point(1, 2, true, 'B'), new Point(2, 2, true, 'B'), new Point(3, 2, true, 'B'), new Point(4, 2, true, 'B') }, new List <Point>() { new Point(2, 1, true, 'B'), new Point(2, 2, true, 'B'), new Point(2, 3, true, 'B'), new Point(2, 4, true, 'B') })); gb.ships.Add(new Ship(5, 'L', "Letadlova lod", new List <Point>() { new Point(1, 3, true, 'L'), new Point(2, 3, true, 'L'), new Point(3, 3, true, 'L'), new Point(4, 3, true, 'L'), new Point(5, 3, true, 'L') }, size, new List <Point>() { new Point(3, 1, true, 'L'), new Point(3, 2, true, 'L'), new Point(3, 3, true, 'L'), new Point(3, 4, true, 'L'), new Point(3, 5, true, 'L') }, new List <Point>() { new Point(1, 3, true, 'L'), new Point(2, 3, true, 'L'), new Point(3, 3, true, 'L'), new Point(4, 3, true, 'L'), new Point(5, 3, true, 'L') }, new List <Point>() { new Point(3, 1, true, 'L'), new Point(3, 2, true, 'L'), new Point(3, 3, true, 'L'), new Point(3, 4, true, 'L'), new Point(3, 5, true, 'L') })); gb.ships.Add(new Ship(6, 'Z', "Pristavaci zakladna", new List <Point>() { new Point(1, 1, true, 'Z'), new Point(2, 1, true, 'Z'), new Point(3, 1, true, 'Z'), new Point(1, 2, true, 'Z'), new Point(2, 2, true, 'Z'), new Point(3, 2, true, 'Z') }, size, new List <Point>() { new Point(1, 1, true, 'Z'), new Point(2, 1, true, 'Z'), new Point(1, 2, true, 'Z'), new Point(2, 2, true, 'Z'), new Point(1, 3, true, 'Z'), new Point(2, 3, true, 'Z') }, new List <Point>() { new Point(1, 1, true, 'Z'), new Point(2, 1, true, 'Z'), new Point(3, 1, true, 'Z'), new Point(1, 2, true, 'Z'), new Point(2, 2, true, 'Z'), new Point(3, 2, true, 'Z') }, new List <Point>() { new Point(1, 1, true, 'Z'), new Point(2, 1, true, 'Z'), new Point(1, 2, true, 'Z'), new Point(2, 2, true, 'Z'), new Point(1, 3, true, 'Z'), new Point(2, 3, true, 'Z') })); gb.ships.Add(new Ship(6, 'H', "Hydroplan", new List <Point>() { new Point(1, 2, true, 'H'), new Point(2, 1, true, 'H'), new Point(3, 2, true, 'H') }, size, new List <Point>() { new Point(2, 1, true, 'H'), new Point(3, 2, true, 'H'), new Point(2, 3, true, 'H') }, new List <Point>() { new Point(1, 2, true, 'H'), new Point(2, 3, true, 'H'), new Point(3, 2, true, 'H') }, new List <Point>() { new Point(1, 2, true, 'H'), new Point(2, 1, true, 'H'), new Point(2, 3, true, 'H') })); gb.ships.Add(new Ship(6, 'R', "Kriznik II", new List <Point>() { new Point(2, 1, true, 'R'), new Point(1, 2, true, 'R'), new Point(2, 2, true, 'R'), new Point(3, 2, true, 'R') }, size, new List <Point>() { new Point(3, 2, true, 'R'), new Point(2, 1, true, 'R'), new Point(2, 2, true, 'R'), new Point(2, 3, true, 'R') }, new List <Point>() { new Point(2, 3, true, 'R'), new Point(1, 2, true, 'R'), new Point(2, 2, true, 'R'), new Point(3, 2, true, 'R') }, new List <Point>() { new Point(2, 1, true, 'R'), new Point(1, 2, true, 'R'), new Point(2, 2, true, 'R'), new Point(2, 3, true, 'R') })); gb.ships.Add(new Ship(6, 't', "Tezky Kriznik", new List <Point>() { new Point(2, 1, true, 't'), new Point(1, 2, true, 't'), new Point(2, 2, true, 't'), new Point(3, 2, true, 't'), new Point(2, 3, true, 't') }, size, new List <Point>() { new Point(2, 1, true, 't'), new Point(1, 2, true, 't'), new Point(2, 2, true, 't'), new Point(3, 2, true, 't'), new Point(2, 3, true, 't') }, new List <Point>() { new Point(2, 1, true, 't'), new Point(1, 2, true, 't'), new Point(2, 2, true, 't'), new Point(3, 2, true, 't'), new Point(2, 3, true, 't') }, new List <Point>() { new Point(2, 1, true, 't'), new Point(1, 2, true, 't'), new Point(2, 2, true, 't'), new Point(3, 2, true, 't'), new Point(2, 3, true, 't') })); gb.ships.Add(new Ship(6, 'C', "Katamaran", new List <Point>() { new Point(1, 1, true, 'c'), new Point(3, 1, true, 'c'), new Point(1, 2, true, 'c'), new Point(2, 2, true, 'c'), new Point(3, 2, true, 'c'), new Point(1, 3, true, 'c'), new Point(3, 3, true, 'c') }, size, new List <Point>() { new Point(1, 1, true, 'c'), new Point(3, 1, true, 'c'), new Point(2, 1, true, 'c'), new Point(2, 2, true, 'c'), new Point(2, 3, true, 'c'), new Point(1, 3, true, 'c'), new Point(3, 3, true, 'c') }, new List <Point>() { new Point(1, 1, true, 'c'), new Point(3, 1, true, 'c'), new Point(1, 2, true, 'c'), new Point(2, 2, true, 'c'), new Point(3, 2, true, 'c'), new Point(1, 3, true, 'c'), new Point(3, 3, true, 'c') }, new List <Point>() { new Point(1, 1, true, 'c'), new Point(3, 1, true, 'c'), new Point(2, 1, true, 'c'), new Point(2, 2, true, 'c'), new Point(2, 3, true, 'c'), new Point(1, 3, true, 'c'), new Point(3, 3, true, 'c') })); gb.ships.Add(new Ship(6, 'b', "Lehka bitevni lod", new List <Point>() { new Point(2, 1, true, 'b'), new Point(2, 2, true, 'b'), new Point(3, 2, true, 'b') }, size, new List <Point>() { new Point(2, 1, true, 'b'), new Point(2, 2, true, 'b'), new Point(3, 1, true, 'b') }, new List <Point>() { new Point(2, 1, true, 'b'), new Point(3, 2, true, 'b'), new Point(3, 1, true, 'b') }, new List <Point>() { new Point(3, 1, true, 'b'), new Point(2, 2, true, 'b'), new Point(3, 2, true, 'b') })); gb.ships.Add(new Ship(6, 'l', "Lehka bitevni lod", new List <Point>() { new Point(4, 2, true, 'l'), new Point(5, 2, true, 'l'), new Point(1, 3, true, 'l'), new Point(2, 3, true, 'l'), new Point(3, 3, true, 'l'), new Point(4, 3, true, 'l'), new Point(5, 3, true, 'l') }, size, new List <Point>() { new Point(4, 4, true, 'l'), new Point(4, 5, true, 'l'), new Point(3, 1, true, 'l'), new Point(3, 2, true, 'l'), new Point(3, 3, true, 'l'), new Point(3, 4, true, 'l'), new Point(3, 5, true, 'l') }, new List <Point>() { new Point(1, 4, true, 'l'), new Point(2, 4, true, 'l'), new Point(1, 3, true, 'l'), new Point(2, 3, true, 'l'), new Point(3, 3, true, 'l'), new Point(4, 3, true, 'l'), new Point(5, 3, true, 'l') }, new List <Point>() { new Point(2, 1, true, 'l'), new Point(2, 2, true, 'l'), new Point(3, 1, true, 'l'), new Point(3, 2, true, 'l'), new Point(3, 3, true, 'l'), new Point(3, 4, true, 'l'), new Point(3, 4, true, 'l') })); bool runGame = true; while (runGame) { //int[] coo = kh.GetCooInput(); //gb.Hit(coo[0], coo[1]); for (int i = 0; i < gb.ships.Count; i++) { Ship ship = gb.ships[i]; bool run = true; while (run) { Console.Clear(); gb.UpdateByShip(ship); gb._Render(); Console.WriteLine("Uprav pozici lode " + ship.Name + "a potvrd ji klavesou enter. Pro rotaci stiskni r"); ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.UpArrow) { //UP ship.MoveShip(3); } else if (keyInfo.Key == ConsoleKey.DownArrow) { //DOWN ship.MoveShip(1); } else if (keyInfo.Key == ConsoleKey.LeftArrow) { ship.MoveShip(2); } else if (keyInfo.Key == ConsoleKey.RightArrow) { ship.MoveShip(0); } else if (keyInfo.Key == ConsoleKey.R) { ship.Rotation++; if (ship.Rotation == 4) { ship.Rotation = 0; } ship.rotate(ship.Rotation); } else if (keyInfo.Key == ConsoleKey.Enter) { if (gb.CheckCollision(ship)) { Console.WriteLine("Nemuzes umistit lod na lod"); } else { run = false; } } ship.Rendered = true; gb.UpdateByShip(ship); } } Console.WriteLine("Lode ulozeny. Nyni musis trefit sve lode. Pamatujes si, kde je jaka?"); bool runHledani = true; Point activePoint = new Point(size, 1, false, 'X', true); while (runHledani) { Console.Clear(); hidden._Render(); Point oldPoint = activePoint; ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.UpArrow) { //UP if (activePoint.YPos == 1) { Console.WriteLine("Nemuzes jit mimo border"); } else { activePoint.YPos--; } } else if (keyInfo.Key == ConsoleKey.DownArrow) { //DOWN if (activePoint.YPos == size) { Console.WriteLine("Nemuzes jit mimo border"); } else { activePoint.YPos++; } } else if (keyInfo.Key == ConsoleKey.LeftArrow) { if (activePoint.XPos == 1) { Console.WriteLine("Nemuzes jit mimo border"); } else { activePoint.XPos--; } } else if (keyInfo.Key == ConsoleKey.RightArrow) { if (activePoint.XPos == size) { Console.WriteLine("Nemuzes jit mimo border"); } else { activePoint.XPos++; } } else if (keyInfo.Key == ConsoleKey.Enter) { //runHledani = false; int indexShip = -1; int realIndex = -1; gb.ships.ForEach(ship => { int i = ship.Points.FindIndex(x => (x.XPos == activePoint.XPos) && (x.YPos == activePoint.YPos)); if (i != -1) { indexShip = i; realIndex = gb.ships.IndexOf(ship); } }); if (indexShip != -1) { string name = gb.ships[realIndex].Name; Console.WriteLine("Bravo, trefil si " + name); activePoint.PrintChar = 'h'; } else { activePoint.PrintChar = 'M'; } hidden.hits.Add(activePoint); } int index = hidden.points.FindIndex(x => (x.XPos == activePoint.XPos) && (x.YPos == activePoint.YPos)); int index2 = hidden.points.FindIndex(x => (x.XPos == oldPoint.XPos) && (x.YPos == oldPoint.YPos)); int index3 = hidden.hits.IndexOf(oldPoint); if (index != -1) { if (index3 != -1) { hidden.points[index2].PrintChar = oldPoint.PrintChar; } else { hidden.points[index2].PrintChar = ' '; } //hidden.points[index].PrintChar = activePoint.PrintChar; } } } }
static void PlaceTokens(GameBoard inGameBoard, Battleships[] inPlayerShips) { Console.Clear(); PrintSingleBoard(inGameBoard); for (int ship = 0; ship < inPlayerShips.Length; ship++) { int[] gridReference = new int[2]; bool shipPlaced = false; Console.WriteLine("BOARD SETUP"); while (shipPlaced == false) { Console.WriteLine("Please select an initial co-ord, then a direction for it to extend towards.\n"); Console.WriteLine($"Ship Name: {inPlayerShips[ship]._id} | Ship Length: {inPlayerShips[ship]._shipLength}"); Console.Write("Select initial coordinate: "); gridReference = CoordinateGetter(Console.ReadLine()); Console.WriteLine("'UP', 'DOWN', 'LEFT', 'RIGHT'"); Console.Write("Enter direction to place ship: "); switch (Console.ReadLine().ToUpper()) { case "RIGHT": if (gridReference[1] + inPlayerShips[ship]._shipLength <= inGameBoard._boardLayout.GetLength(1)) //Check to see that ship can be placed along { //the lateral length of the array bool isValid = false; for (int x = gridReference[1]; x < gridReference[1] + inPlayerShips[ship]._shipLength; x++) { if (inGameBoard._boardLayout[gridReference[0], x] != "V") { isValid = true; } else { isValid = false; break; } } if (isValid) { for (int x = gridReference[1]; x < gridReference[1] + inPlayerShips[ship]._shipLength; x++) { inGameBoard._boardLayout[gridReference[0], x] = "V"; inPlayerShips[ship].RowPosition[x - gridReference[1]] = gridReference[0]; //Two lines of code trying to assign grid co-ords. Not working currently. YOU ARE HERE inPlayerShips[ship].ColumnPosition[x - gridReference[1]] = x; } shipPlaced = true; } } break; case "LEFT": if (gridReference[1] - inPlayerShips[ship]._shipLength >= 0) { bool isValid = false; for (int x = gridReference[1]; x > gridReference[1] - inPlayerShips[ship]._shipLength; x--) { if (inGameBoard._boardLayout[gridReference[0], x] != "V") { isValid = true; } else { isValid = false; break; } } if (isValid) { for (int x = gridReference[1]; x > gridReference[1] - inPlayerShips[ship]._shipLength; x--) { inGameBoard._boardLayout[gridReference[0], x] = "V"; inPlayerShips[ship].RowPosition[gridReference[1] - x] = gridReference[0]; inPlayerShips[ship].ColumnPosition[gridReference[1] - x] = x; } shipPlaced = true; } } break; case "DOWN": if (gridReference[0] + inPlayerShips[ship]._shipLength <= inGameBoard._boardLayout.GetLength(0)) { bool isValid = false; for (int x = gridReference[0]; x < gridReference[0] + inPlayerShips[ship]._shipLength; x++) { if (inGameBoard._boardLayout[x, gridReference[1]] != "V") { isValid = true; } else { isValid = false; break; } } if (isValid) { for (int x = gridReference[0]; x < gridReference[0] + inPlayerShips[ship]._shipLength; x++) { inGameBoard._boardLayout[x, gridReference[1]] = "V"; inPlayerShips[ship].RowPosition[x - gridReference[0]] = x; inPlayerShips[ship].ColumnPosition[x - gridReference[0]] = gridReference[1]; } shipPlaced = true; } } break; case "UP": if (gridReference[0] - inPlayerShips[ship]._shipLength >= 0) { bool isValid = false; for (int x = gridReference[0]; x > gridReference[0] - inPlayerShips[ship]._shipLength; x--) { if (inGameBoard._boardLayout[x, gridReference[1]] != "V") { isValid = true; } else { isValid = false; break; } } if (isValid) { for (int x = gridReference[0]; x > gridReference[0] - inPlayerShips[ship]._shipLength; x--) { inGameBoard._boardLayout[x, gridReference[1]] = "V"; inPlayerShips[ship].RowPosition[gridReference[0] - x] = x; inPlayerShips[ship].ColumnPosition[gridReference[0] - x] = gridReference[1]; } shipPlaced = true; } } break; default: shipPlaced = false; break; } if (shipPlaced == false) { Console.WriteLine("\n-Invalid Placement-"); Console.WriteLine("Please select a valid initial co-ord and ensure the placement does not intersect with another ship \nor extend past the board."); } } Console.Clear(); PrintSingleBoard(inGameBoard); } EnterToContinue(); Console.Clear(); }
static void Main(string[] args) { Console.SetWindowSize(100, 50);//sets window to a larger than average size in order to play the game fluently Start:GameBoard gb = new GameBoard();//create a new Gameboard gb SetGame(gb);//creates and places the 5 ships on the gameboard int[,] board = gb.GetBoard();//keeps a local copy of the filled gameboard //String[] coords;//coordinates given by user input String coords; int score = 100;//score starts at 100 bool isDev = false;//debugging purposes String name = null;//string to record the player's name Console.WriteLine("__________ __ __ .__ _________.__ .__ "); Console.WriteLine("\\______ \\_____ _/ |__/ |_| | ____ / _____/| |__ |__|_____ "); Console.WriteLine(" | | _/\\__ \\\\ __\\ __\\ | _/ __ \\ \\_____ \\ | | \\| \\____ \\ "); Console.WriteLine(" | | \\ / __ \\| | | | | |_\\ ___/ / \\| Y \\ | |_> >"); Console.WriteLine(" |______ /(____ /__| |__| |____/\\___ > /_______ /|___| /__| __/ "); Console.WriteLine(" \\/ \\/ \\/ \\/ \\/ |__| \n"); Console.WriteLine("Press \"Enter\" to Continue"); Console.ReadLine(); Console.WriteLine("Enter your name please:");//prompts user for name name = Console.ReadLine(); if (name.Equals("debug"))//if the name is debug then it is an admin { isDev = true; name = "Admin"; } while (!gb.IsEmpty())//while the gameboard still has ships on it { Console.Clear(); if(isDev)//if it is an admin then print the board showing where the ships are Printer(board); Console.WriteLine("\nScore={0} | Num. Sunken ships={1}\n", score, gb.GetNumSunk());//show the user their current score score--;//subtract one point from their score for the current move gb.GridPrinter();//print the board for the user to make guesses Console.WriteLine(); Idiots: try//try catch statement to prevent users from entering invalid coordinates { System.Console.Write("Enter your desired guess (in the format a0):");//prompt user for coordinate guess coords = Console.ReadLine(); int x = char.Parse(coords.Substring(0, 1).ToUpper()) - 65; int y = int.Parse(coords.Substring(1,1)); while (gb.GetGrid()[y, x] == 'X' || gb.GetGrid()[y, x] == '-')//if the location hasbeen guessed before then prompt the user to enter new values { System.Console.Write("That location has been guessed, please choose another using the same format:"); coords = Console.ReadLine(); x = char.Parse(coords.Substring(0, 1).ToUpper()) - 65; y = int.Parse(coords.Substring(1, 1)); } gb.HitMiss(y,x); /* if (board[y, x] == 1)//if guessed location is a hit print such gb.SetHit(y, x); else gb.SetMiss(y, x);//if it is not that print sych as well */ } catch { Console.WriteLine("You Sir cannot read directions! Please make another attempt."); goto Idiots;//if the user does not enter the coordinates properly this will catch the error } gb.UpdateShips(); }//after all ships are sunk the user will be shown their final scored and be asked if they wish to play again Console.WriteLine("You won {1}! Your final score was {0}\nWould you like to play again?", score++, name); if (Console.ReadLine().Substring(0, 1).ToLower().Equals("y")) { Console.Clear(); goto Start; } else Console.WriteLine("Thank You for Playing!"); Console.ReadLine(); }
//this function creates sets the ships on the gameboard private static void SetGame(GameBoard gb) { Ship[] ships = new Ship[7]; ships[0] = new Ship(5); ships[1] = new Ship(4); ships[2] = new Ship(3); ships[3] = new Ship(2); ships[4] = new Ship(2); //new ships ships[5] = new Ship(1); ships[6] = new Ship(1); gb.AddListToGrid(ships); }
/// <summary> /// check if the boats are in the ally platform and fit him to case /// </summary> /// <param name="mouseX">position x of the mouse cursor</param> /// <param name="mouseY">position y of the mouse cursor</param> public void IsInThePlatform(int mouseX, int mouseY, GameBoard platform) { /*int divX = 0, divY = 0; divX = mouseX / platform.CASE_SIZE + platform.Location.X; divY = mouseY / platform.CASE_SIZE + platform.Location.Y; if ((divX >= platform.Location.X) && (divX < platform.Location.X) && (divY >= 1) && (divY < 11)) { this.X = divX * this._size + platform.Location.X; this.Y = divY * this._size + platform.Location.Y; }*/ if ((mouseX > platform.Location.X) && (mouseX < platform.Location.X + platform.Size.Width) && (mouseY > platform.Location.Y) && (mouseY < platform.Location.Y + platform.Size.Height)) { // calcul and trunc the position of the mouse on the platform this.X = platform.Location.X + platform.CASE_SIZE + (mouseX - platform.Location.X - platform.CASE_SIZE) / platform.CASE_SIZE * platform.CASE_SIZE; this.Y = platform.Location.Y + platform.CASE_SIZE + (mouseY - platform.Location.Y - platform.CASE_SIZE) / platform.CASE_SIZE * platform.CASE_SIZE; } }