This is my first attempt at anything in C#.
It's based on battleships, but there's only one player and only one ship.
The gameplay is super simple: just try to find the ship... keep guessing until you've found it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FirstApplication
{
class Battleship
{
public int shipPosition;
public string board = "-------------\n| 1 | 2 | 3 |\n-------------\n| 4 | 5 | 6 |\n-------------\n| 7 | 8 | 9 |\n-------------";
public bool shipIsHit = false;
public void PositionShip()
{
Random randomNumber = new Random();
shipPosition = randomNumber.Next(1, 10); // random number between 1 and 9
}
public void HitPosition(int hit)
{
if (hit == shipPosition)
{
board = board.Replace(hit.ToString(),"X");
shipIsHit = true;
}
else
{
board = board.Replace(hit.ToString(), "-");
}
}
}
class Program
{
static void Main(string[] args)
{
Battleship game = new Battleship();
int guess;
bool isInteger;
game.PositionShip();
Console.WriteLine("You are playing battleship... Try to find the ship!");
do
{
Console.WriteLine("\n\nEnter a number between 1 and 9");
Console.WriteLine("Board: \n{0}", game.board);
isInteger = Int32.TryParse(Console.ReadLine(), out guess);
if (isInteger && (guess >= 1 && guess <= 9))
{
game.HitPosition(guess);
}
else
{
Console.WriteLine("That's not a valid number!");
}
if (!game.shipIsHit)
{
Console.WriteLine("You missed!");
}
}
while (!game.shipIsHit);
Console.WriteLine("\n\nBoard: \n{0}", game.board);
Console.WriteLine("BOOM! You found the battleship at position {0}", game.shipPosition);
Console.ReadKey();
}
}
}
I'm looking forward adding more functionality as I progress. If you have any suggestions I'd be interested to hear them too!