Пример #1
0
        public static void PlayReplay(Save save)//todo:replays times are wrong
        {
            var replay = new List <State>();

            for (int i = 0; i < save.States.Count; i++)
            {
                var   p1gb    = save.States[i].Player1GB.GetDomainBoard();
                var   p1map   = save.States[i].Player1Map.GetDomainBoard();
                var   player1 = save.Player1.GetDomainPlayer(p1gb, p1map);
                var   player2 = save.Player2.GetDomainPlayer(save.States[i].Player2GB.GetDomainBoard(), save.States[i].Player2Map.GetDomainBoard());
                State state   = save.States[i].GetDomainState(player1, player2, save.Rules.CanTouch);
                state.time = save.States[i].TimeStamp;
                replay.Add(state);
            }

            var rePlayer = new Player("Replay", replay[0].P1.Board, replay[0].P1.Board);
            int index    = 0;

            while (true)
            {
                if (index < 0)
                {
                    index = 0;
                }

                if (index > replay.Count - 1)
                {
                    break;
                }

                var state = replay[index];
                rePlayer.Name = state.time.ToString();
                Draw(rePlayer, state.P1 + new string(' ', state.P1.Board.Board[0].Count * 4 + 3 - state.P1.Name.Length) + "\n" + GetBoardString(state.P1.Board), state.P2 + "\n" + GetBoardString(state.P2.Board));
                Console.WriteLine("---------------------------------------------" +
                                  "\nX - Back, <- Play Backward, Play Forward -> ");
                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.X:
                    index = replay.Count;
                    break;

                case ConsoleKey.Enter:
                    continue;

                case ConsoleKey.RightArrow:
                    index++;
                    continue;

                case ConsoleKey.LeftArrow:
                    index--;
                    continue;
                }
            }
//            for (int i = 0; i < replay.Count; i++)
//            {
//                var state = replay[i];
//                rePlayer.Name = state.time.ToString();
//                Draw(rePlayer,state.P1 + new string(' ', state.P1.Board.Board[0].Count * 4 +3 - state.P1.Name.Length) +"\n" + GetBoardString(state.P1.Board),state.P2 + "\n" + GetBoardString(state.P2.Board));
//                switch (Console.ReadKey(true).Key)
//                {
//                    case ConsoleKey.Enter:
//                        break;
//                }
//            }
        }
Пример #2
0
        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>();
        }