public static string AIShoot(GameBoard board, GameBoard map) { Domain.Orientation orientation = Orientation.Unknown; Random rnd = new Random(); for (int i = 0; i < map.Board.Count; i++) //TOdo: does not work correctly { for (int j = 0; j < map.Board[i].Count; j++) //look at board { if (map.Board[i][j] == BoardSquareState.Hit) //found a hit { //check for other hits //up try { if (map.Board[i - 1][j] == BoardSquareState.Hit) { orientation = Orientation.Vertical; } } catch (Exception e) { } //down try { if (map.Board[i + 1][j] == BoardSquareState.Hit) { orientation = Orientation.Vertical; } } catch (Exception e) { } //right try { if (map.Board[i][j + 1] == BoardSquareState.Hit) { orientation = Orientation.Horisontal; } } catch (Exception e) { } //left try { if (map.Board[i][j - 1] == BoardSquareState.Hit) { orientation = Orientation.Horisontal; } } catch (Exception e) { } //guessing orientation if (orientation == Orientation.Unknown) { if (rnd.Next(0, 2) == 1) { orientation = Orientation.Horisontal; } else { orientation = Orientation.Vertical; } } //shoot if (orientation == Orientation.Horisontal) { try { if (map.Board[i][j + 1] == BoardSquareState.Empty) { return(GameBoard.Shoot(board, new[] { i, j + 1 }, map)); } }catch (Exception e) { } try { if (map.Board[i][j - 1] == BoardSquareState.Empty) { return(GameBoard.Shoot(board, new[] { i, j - 1 }, map)); } }catch (Exception e) { } } if (orientation == Orientation.Vertical) { try { if (map.Board[i + 1][j] == BoardSquareState.Empty) { return(GameBoard.Shoot(board, new[] { i + 1, j }, map)); } }catch (Exception e) { } try { if (map.Board[i - 1][j] == BoardSquareState.Empty) { return(GameBoard.Shoot(board, new[] { i - 1, j }, map)); } }catch (Exception e) { } } } } } return(GameBoard.Shoot(board, randomCoords(map), map)); }
public static void PlayGame(Player Player1, Player Player2, bool P2turn = false) { //game starts Abort = false; var coords = new int[2] { 0, 0 }; var status = "|"; while (Abort == false)//cycle { if (!P2turn) { coords = Target(GetBoardString(Player1.Map), Player1, coords, true); if (Player1.Map.Board[coords[0]][coords[1]] != BoardSquareState.Empty || Abort) { continue; } status = GameBoard.Shoot(Player2.Board, coords, Player1.Map); Draw(Player1, GetBoardString(Player1.Board), GetBoardString(Player1.Map), status); if (status == "MISS ") { P2turn = true; if (!Player2.AI) { coords[0] = 0; coords[1] = 0; } } } else { if (Player2.AI) { FullscreenMessage("The AI is making a move."); status = AI.AIShoot(Player1.Board, Player2.Map); Draw(Player1, GetBoardString(Player1.Board), GetBoardString(Player1.Map), status); if (status == "MISS ") { P2turn = false; } } else { Draw(Player2, GetBoardString(Player2.Board), GetBoardString(Player2.Map)); coords = Target(GetBoardString(Player2.Map), Player2, coords, true); if (Player2.Map.Board[coords[0]][coords[1]] != BoardSquareState.Empty || Abort) { continue; } status = GameBoard.Shoot(Player1.Board, coords, Player2.Map); Draw(Player2, GetBoardString(Player2.Board), GetBoardString(Player2.Map), status); if (status == "MISS ") { P2turn = false; coords[0] = 0; coords[1] = 0; } } } //check win if (AI.GetWinner(Player1, Player2) != null) { StringBuilder sb = new StringBuilder(); sb.Append(AI.GetWinner(Player1, Player2)); sb.Append(" won!"); var wstate = new State(new Player(Player1), new Player(Player2), Rules.CanTouch, P2turn); wstate.Status = "[Finished: " + sb.ToString() + "]"; SaveSystem.GameStates.Add(wstate); FullscreenMessage(sb.ToString()); break; } Console.WriteLine("PRESS ENY KEY TO CONTINUE"); switch (Console.ReadKey(true).Key) { case ConsoleKey.Enter: break; } if (status == "MISS " && !Player1.AI && !Player2.AI) { FullscreenMessage("PASS THE PC TO THE NEXT PLAYER"); } var state = new State(new Player(Player1), new Player(Player2), Rules.CanTouch, P2turn); SaveSystem.GameStates.Add(state); } if (SaveSystem.GameStates.Count != 0) { if (SaveSystem.GameStates.Last().Status == null) { SaveSystem.GameStates.Last().Status = "[" + SaveSystem.GameStates.Last().P1.ToString() + ": " + SaveSystem.GameStates.Last().P1.Board.Ships.Count + " Ships, " + SaveSystem.GameStates.Last().P2.ToString() + ": " + SaveSystem.GameStates.Last().P2.Board.Ships.Count + " Ships]"; } SaveSystem.SavesList.Add(new List <State>(SaveSystem.GameStates)); } SaveSystem.GameStates = new List <State>(); }