internal void StartNew() { player1 = true; shooting = false; startednewgame = true; placedships = false; Gameboard1 = new BattleBoard(); Gameboard2 = new BattleBoard(); Gameboardshoot1 = new BattleBoard(); Gameboardshoot2 = new BattleBoard(); }
public void Shoot(BattleBoard Enemy) { bool running = true; int y = 0; int x = 0; BattleBoard Dummy = new BattleBoard(); while (running) { Dummy.GameBoard = GameBoard.Clone() as char[, ]; Dummy.GameBoard[y, x] = '+'; Console.Clear(); Console.WriteLine(Dummy.getplayerview()); Console.WriteLine(); Console.WriteLine("Use arrows to move marker."); Console.WriteLine("Use Enter to shoot at current position."); var ch = Console.ReadKey(false).Key; switch (ch) { case ConsoleKey.UpArrow: if (y > 0) { y -= 1; } break; case ConsoleKey.DownArrow: if (y < 9) { y += 1; } break; case ConsoleKey.LeftArrow: if (x > 0) { x -= 1; } break; case ConsoleKey.RightArrow: if (x < 9) { x += 1; } break; case ConsoleKey.Enter: if (x < 10 && x > -1 && y < 10 && y > -1 && GameBoard[y, x] != 'X' && GameBoard[y, x] != 'O') { int counter = 0; foreach (char c in Enemy.GameBoard) { if (c == Enemy.GameBoard[y, x]) { counter++; } } if (Enemy.GameBoard[y, x] != ' ') { GameBoard[y, x] = 'X'; Enemy.GameBoard[y, x] = 'X'; if (counter <= 1) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(getplayerview()); Console.Beep(600, 150); Console.Beep(1000, 150); Console.WriteLine("SUNK!"); Console.ResetColor(); } else { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(getplayerview()); Console.Beep(750, 300); Console.WriteLine("HIT!"); Console.ResetColor(); } running = false; } else { GameBoard[y, x] = 'O'; Enemy.GameBoard[y, x] = 'O'; Console.Clear(); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine(getplayerview()); Console.Beep(300, 300); Console.WriteLine("SPLASH!"); Console.ResetColor(); running = false; } Thread.Sleep(2000); } else { Console.WriteLine(); Console.WriteLine("That is not a valid position."); Console.WriteLine("Press enter to continue."); Console.ReadLine(); } break; default: break; } } }
private void placeShips(bool placeMode, BattleShip current) // Function which handles placement of ships { BattleBoard Dummy = new BattleBoard(); int y = 0, x = 0; bool vertical = true; while (placeMode) { Dummy.GameBoard = GameBoard.Clone() as char[, ]; for (int i = 0; i < current.Length; i++) { if (vertical) { Dummy.GameBoard[y + i, x] = Convert.ToChar(counter + 48); } else { Dummy.GameBoard[y, x + i] = Convert.ToChar(counter + 48); } } Console.Clear(); Console.WriteLine(Dummy.getplayerview()); Console.WriteLine(); Console.WriteLine("Use arrows to move ship."); Console.WriteLine("Press R to rotate ship."); Console.WriteLine("Press ENTER to place ship."); var ch = Console.ReadKey(false).Key; switch (ch) { case ConsoleKey.R: vertical = !vertical; // Switch between true and false y = 0; x = 0; break; case ConsoleKey.UpArrow: if (y > 0) { y -= 1; } break; case ConsoleKey.DownArrow: if (vertical) { if (y < 10 - current.Length) { y += 1; } } else { if (y < 9) { y += 1; } } break; case ConsoleKey.LeftArrow: if (x > 0) { x -= 1; } break; case ConsoleKey.RightArrow: if (vertical) { if (x < 9) { x += 1; } } else { if (x < 10 - current.Length) { x += 1; } } break; case ConsoleKey.Enter: bool valid = false; for (int i = 0; i < current.Length; i++) { if (vertical) { if (GameBoard[y + i, x] == ' ') { valid = true; } else { valid = false; break; } } else { if (GameBoard[y, x + i] == ' ') { valid = true; } else { valid = false; break; } } } if (valid) { for (int i = 0; i < current.Length; i++) { if (vertical) { GameBoard[y + i, x] = Convert.ToChar(counter + 48); } else { GameBoard[y, x + i] = Convert.ToChar(counter + 48); } } counter++; placeMode = false; } else { Console.Clear(); Console.WriteLine(); Console.WriteLine("Placement not valid."); Thread.Sleep(2000); } //Console.WriteLine(y.ToString() +", "+ x.ToString()); break; } } }