Skip to content

Instantly share code, notes, and snippets.

@tristian2
Created August 10, 2016 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristian2/54bc9febfd2be5b30467dfd300e5891d to your computer and use it in GitHub Desktop.
Save tristian2/54bc9febfd2be5b30467dfd300e5891d to your computer and use it in GitHub Desktop.
BattleShips port rom GROOVY to c#
//one
using System;
using System.Collections;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("Start");
String[] shipTypes = new String[] { "battleship", "cruiser", "destroyer", "submarine" };
foreach (String s in shipTypes)
{
Console.WriteLine(s); //no indexer available :-(
}
for(int i=0; i<shipTypes.Length; i++) {
Console.WriteLine(i + " " + shipTypes[i] );
}
Console.WriteLine("End");
}
}
//two consider the ship board...
@tristian2
Copy link
Author

using System;
using System.Collections;
using System.Linq;
using System.Text;

public class Program
{
public static void Main()
{

    StringBuilder sb1 = new StringBuilder();
    sb1.Append("  0 1 2 3 4 5 6 7 8 9 \n");     
    String[] shipTypes = { "battleship", "cruiser", "destroyer", "submarine" };
    String[,] ships = new String[10, 10]{
        {"z","c","x","c","x","c","x","c","x","c"} ,
        {"x","c","x","c","x","c","x","c","x","c"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} ,
        {"c","x","x","c","x","c","x","c","x","x"} };                

    for (int row = 0; row < ships.GetLength(0); row++)
    {               
        sb1.Append("+-");
        for (int col = 0; col < ships.GetLength(1); col++)
        {                                   
            sb1.Append(ships[row,col]); 
            sb1.Append("-");
        }
        sb1.Append("|\n");
    }

    sb1.Append("+-+-+-+-+-+-+-+-+-+-+-+\n");        
    Console.WriteLine(sb1.ToString());
    Console.WriteLine("End");
    Console.Read();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment