Home > Software design >  Int array for board game
Int array for board game

Time:10-21

        int[,] board = 
        {
            {0, 0, 2, 0, 0, 0, 0, 0},
            {0, 0, 0, 1, 2, 2, 0, 0},
            {0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 0, 1, 2, 1, 0, 0},
            {0, 0, 1, 1, 2, 0, 0, 0},
            {0, 0, 0, 0, 2, 0, 0, 0},
            {0, 0, 0, 0, 2, 0, 0, 0},
            {0, 0, 0, 0, 1, 0, 0, 0},
        }; 

Here is my array code. I want to show all 0s as " ", all 1s as "X" and all 2s as "O". I am doing this to make a board game and want the output to look something like this. Array output wanted

I have tried making a method called "DisplayBoard" and here is what I have so far.

static void DisplayBoard(int[,] board) {

        Write("     ");
        for (int i = 0; i < BOARD_SIZE;   i)
        {
            Write(" {0}  ", i);
        }

        Console.WriteLine();
        Console.WriteLine("    ---------------------------------");

        for (int row = 0; row < BOARD_SIZE; row  )
        {

            Write(" {0}  |",row);
            for (int col = 0; col < BOARD_SIZE;   col)
            {
                Console.Write(String.Format(" {0} |", board[row, col]));
            }
            Console.WriteLine();
            Console.WriteLine("    ---------------------------------");

        }
        WriteLine();
    }

CodePudding user response:

You can define a local function like this:

static string getMark(int n) => n switch {
    1 => "X",
    2 => "O",
    _ => " "
};

And replace this line of yours

Console.Write(String.Format(" {0} |", board[row, col]));

with

Console.Write($" {getMark(board[row, col])} |");

CodePudding user response:

Let's generalize the problem and convert any board to string:

private static string BoardToString(int[,] board) {
  if (null == board || board.GetLength(0) <= 0 || board.GetLength(1) <= 0)
    return "";

  StringBuilder sb = new StringBuilder();

  string marks = " XO";
  string line = "  "   new string('-', board.GetLength(1) * 4 - 1);

  sb.Append(line);

  for (int r = 0; r < board.GetLength(0);   r) {
    sb.AppendLine();

    for (int c = 0; c < board.GetLength(1);   c) {
      sb.Append(" | ");
      sb.Append(marks[board[r, c]]);
    }

    sb.AppendLine(" | ");
    sb.Append(line);
  }

  return sb.ToString();
}

Then you can put

   static void DisplayBoard(int[,] board) {  
     Console.Write(BoardToString(board)); 
   }  

Demo:

  int[,] board = {
    {0, 0, 2, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 2, 2, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 1, 2, 1, 0, 0},
    {0, 0, 1, 1, 2, 0, 0, 0},
    {0, 0, 0, 0, 2, 0, 0, 0},
    {0, 0, 0, 0, 2, 0, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0},
  };

  DisplayBoard(board);

Outcome:

  -------------------------------
 |   |   | O |   |   |   |   |   | 
  -------------------------------
 |   |   |   | X | O | O |   |   | 
  -------------------------------
 |   |   |   |   | X |   |   |   | 
  -------------------------------
 |   |   |   | X | O | X |   |   | 
  -------------------------------
 |   |   | X | X | O |   |   |   | 
  -------------------------------
 |   |   |   |   | O |   |   |   | 
  -------------------------------
 |   |   |   |   | O |   |   |   | 
  -------------------------------
 |   |   |   |   | X |   |   |   | 
  -------------------------------

CodePudding user response:

A tile is going to be a very important object in your program. So you may as well give it a proper class. If you give it a class, you have a natural place to put code that converts it to and from various formats. In your case, you need to be able to convert it from an integer and convert it to a string or character.

class Tile
{
    protected readonly int _value;

    public Tile(int integerValue)
    {
        if (integerValue < 0 || integerValue >= _map.Length) throw new ArgumentException("Tile value invalid");
        _value = integerValue;
    }

    public override string ToString() => ((char)this).ToString();

    static char[] _map = new char[] { ' ', 'X', 'O' }; 

    public static implicit operator char(Tile input) => _map[input._value];

    public static implicit operator Tile(int input) => new Tile(input);
}

Once that is done, the rest of the logic is lighter weight:

var board = new Tile[,]
{
    {0, 0, 2, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 2, 2, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 1, 2, 1, 0, 0},
    {0, 0, 1, 1, 2, 0, 0, 0},
    {0, 0, 0, 0, 2, 0, 0, 0},
    {0, 0, 0, 0, 2, 0, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0},
};

for (var row = 0; row < board.GetUpperBound(0); row  )
{
    for (var column = 0; column < board.GetUpperBound(1); column  )
    {
        Console.Write("|{0}", board[row, column]);
    }
    Console.WriteLine("|");
}

Output:

| | |O| | | | |
| | | |X|O|O| |
| | | | |X| | |
| | | |X|O|X| |
| | |X|X|O| | |
| | | | |O| | |
| | | | |O| | |
  • Related