repl.it
@kironroy/

Battleship Console Application

C#

This a online version of this repo: https://github.com/kironroy/BattleShip_Console_App

fork
loading
Files
  • main.cs
  • CenterText.cs
  • DisplayToConsole.cs
  • Enums.cs
  • GameLogic.cs
  • GameTitles.cs
  • GridspotModel.cs
  • main.exe
  • PlayerInfoModel.cs
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using BattleshipLiteLibrary;
using BattleshipLiteLibrary.Models;
using BattleshipLiteLibrary.Style;
using System;
using System.Globalization;

namespace BattleShipLite
{
    class Program
    {
        static void Main(string[] args)
        {

            
            GameTitles.BattleshipOpeningTitle();
            DisplayToConsole.WelcomeMessage();

            PlayerInfoModel activePlayer = DisplayToConsole.CreatePlayer("Player 1");
            PlayerInfoModel opponent = DisplayToConsole.CreatePlayer("Player 2");
            PlayerInfoModel winner = null;

            do
            {
                DisplayShotGrid(activePlayer);

                RecordPlayerShot(activePlayer, opponent);

                bool doesGameContinue = GameLogic.PlayerStillActive(opponent);

                if (doesGameContinue == true)
                {


                    // Use Tuple swap positions
                    (activePlayer, opponent) = (opponent, activePlayer);


                }
                else
                {
                    winner = activePlayer;
                }

            } while (winner == null);

            IdentifyWinner(winner);

            Console.ForegroundColor = ConsoleColor.Blue;

            DisplayShotGrid(winner);

            GameTitles.BattleshipEndingTitle();

            Console.ReadLine();
        }

        // private methods

        private static void IdentifyWinner(PlayerInfoModel winner)
        {
            Console.WriteLine($"  Congratulations to {winner.UsersName} for winning!");
            Console.WriteLine($"  {winner.UsersName} took {GameLogic.GetShotCount(winner)} shots.");
            Console.WriteLine();
        }

        private static void RecordPlayerShot(PlayerInfoModel activePlayer, PlayerInfoModel opponent)
        {
            bool isValidShot = false;
            string row = "";
            int column = 0;

            do
            {
                string shot = AskForShot(activePlayer);
                try
                {
                    (row, column) = GameLogic.SplitShotIntoRowAndColumn(shot);
                    isValidShot = GameLogic.ValidateShot(activePlayer, row, column);
                }
                catch (Exception ex)
                {

                    Console.WriteLine("Error: " + ex.Message);
                    isValidShot = false;
                }

                if (isValidShot == false)
                {
                    Console.WriteLine("Invalid Shot Location.  Please try again.");
                }

            } while (isValidShot == false);

            bool isAHit = GameLogic.IdentifyShotResult(opponent, row, column);

            GameLogic.MarkShotResult(activePlayer, row, column, isAHit);

            DisplayShotResults(row, column, isAHit);
        }

        private static void DisplayShotResults(string row, int column, bool isAHit)
        {
            if (isAHit)
            {
                Console.WriteLine($"  {row}{column} is a Hit!");
            }
            else
            {
                Console.WriteLine($"  {row}{column} is a miss.");

            }

            Console.WriteLine();
        }

        private static string AskForShot(PlayerInfoModel player)
        {
            Console.Write($"  {player.UsersName}, please enter your shot selection: ");
            string output = Console.ReadLine().Trim();

            return output;
        }

        private static void DisplayShotGrid(PlayerInfoModel activePlayer)
        {
            string currentRow = activePlayer.ShotGrid[0].SpotLetter;

            foreach (var gridSpot in activePlayer.ShotGrid)
            {
                if (gridSpot.SpotLetter != currentRow)
                {
                    Console.WriteLine();
                    currentRow = gridSpot.SpotLetter;
                }

                if (gridSpot.Status == GridSpotStatus.Empty)
                {
                    Console.Write($" {gridSpot.SpotLetter}{gridSpot.SpotNumber} ");

                }
                else if (gridSpot.Status == GridSpotStatus.Hit)
                {
                    Console.Write(" X  ");
                }
                else if (gridSpot.Status == GridSpotStatus.Miss)
                {
                    Console.Write(" O  ");
                }
                else
                {
                    Console.Write(" ?  ");
                }
            }

            Console.WriteLine();
            Console.WriteLine();

        }




    }
}