Пример #1
0
        // GEt user guesses and return new board + win status. Assumes user doesnt enter same coordinates again
        public void GetUserGuesses(int shots, String[, ] board, bool isPlayer1Guessing)
        {
            if (isPlayer1Guessing)
            {
                Console.WriteLine("------------------\n\n\nYour turn Player 1");
            }
            else
            {
                Console.WriteLine("------------------\n\n\nYour turn Player 2");
            }

            Console.WriteLine("Enemy board last  :");
            board.PrintBoard(false);

            Console.WriteLine("");
            for (int x = 0; x < shots && CounterPlayer1Hit < WinCount && CounterPlayer2Hit < WinCount; x++)
            {
                Console.WriteLine("Shot " + (x + 1));
                var guessCoord = GetCoord();
                int rowIndex   = guessCoord.Row;
                int colIndex   = guessCoord.Col;
                if (board[rowIndex, colIndex] == "P")
                {
                    Console.WriteLine("You hit the enemy ship!");
                    board[rowIndex, colIndex] = "X";
                    if (isPlayer1Guessing)
                    {
                        CounterPlayer1Hit++;
                    }
                    else
                    {
                        CounterPlayer2Hit++;
                    }
                }
                else
                {
                    Console.WriteLine("You missed!");
                    board[rowIndex, colIndex] = "0";
                }
                board.PrintBoard(false);
            }
        }
Пример #2
0
        /// Places one user ship by User input to given board assuming user will enter begin/end coordinates in a straight line without going over the ship length
        public string[, ] PlaceOneShip(String[, ] board, int size)
        {
            Console.WriteLine("Place " + size + " size ship, Enter begin coordinate");
            Coordinate coordStart = GetCoord();

            //Coordinate coordStart = new Coordinate (3, 3);
            board[coordStart.Row, coordStart.Col] = "P";
            // 2 size ship
            if (size > 1)
            {
                Console.WriteLine("Place " + size + " size ship, Enter end coordinate");
                var coordEnd = GetCoord();
                //var coordEnd = new Coordinate (3, 6);
                int smallCol = Math.Min(coordStart.Col, coordEnd.Col);
                int bigCol   = Math.Max(coordStart.Col, coordEnd.Col);
                int smallRow = Math.Min(coordStart.Row, coordEnd.Row);
                int bigRow   = Math.Max(coordStart.Row, coordEnd.Row);
                //                if ((bigCol - smallCol + 1 != size) || (bigRow - smallRow + 1 != size)) {
                //                  Console.WriteLine(bigCol.ToString()+","+smallCol.ToString()+","+bigRow.ToString()+","+smallRow.ToString());
                //                Console.WriteLine ("Invalid coordinates");
                //              Console.WriteLine ("size" + size.ToString ());
                //        } else
                board[coordEnd.Row, coordEnd.Col] = "P";
                // 3+ size ships
                if (size > 2)
                {
                    if (coordEnd.Row == coordStart.Row)
                    {
                        for (int x = smallCol; x < bigCol; x++)
                        {
                            // Console.WriteLine ("Coordinates:" + coordEnd.Row.ToString () + "" + x.ToString ());
                            board[coordEnd.Row, x] = "P";
                        }
                    }
                    else if (coordEnd.Col == coordStart.Col)
                    {
                        for (int y = smallRow; y < bigRow; y++)
                        {
                            // Console.WriteLine ("Coordinates:" + "" + y.ToString () + coordEnd.Col.ToString ());
                            board[y, coordEnd.Col] = "P";
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid coordinates");
                    }
                }
            }
            Console.WriteLine("You placed your ships as :");
            board.PrintBoard();
            return(board);
        }