简体   繁体   中英

How can I override ToString in multi constructor with 2d array?

A question from a C# beginner that can't find the solution in web.

Here is a constructor with an 2d array

public MineSweeperBoard() { // new board board = new BoardItem[MineSweeper_Constants.RowNum, >MineSweeper_Constants.ColNum]; // put mines and set value InitialiseBoard();
}

private BoardItem [,] board;

board.ToString is not working that it return the type instead of override it.

public override string ToString()
{
    string a = "";
    for (int j = 0; j < MineSweeper_Constants.ColNum; j++)
    {
        for (int i = 0; i < MineSweeper_Constants.RowNum; i++)
        {
            a += board[i, j].ToString();
        }
    }
}

Here is another constructor BoardItem:

public BoardItem(char a)
{
    this.itemContents = a;
    this.visibleItemContents = ProjectTwo.MineSweeper_Constants.character[10];
}

Here BoardItem.ToString() is working.

public override string ToString()
{
    char[] b = new char[1];
    b[0] = GetVisibleItemContents();
    string a = new string(b);
    return a;            
}

How can I print the string of board with override ToString(my teacher said I must use this)?

>{
>    class BoardItem
>    {
>        readonly char itemContents;
>        char visibleItemContents;
>
>        public BoardItem(char a)
>        {
>            // 1. The actual contents of the item (use Minesweeper_Constants) 
>            this.itemContents = a;
>
>            // 2. The visible contents of the item (use Minesweeper_Constants) and set to >unknown
>            this.visibleItemContents = ProjectTwo.MineSweeper_Constants.character[10];
>        }
>
>        public char GetItemContents()
>        {
>            return this.itemContents;
>        }
>
>        public override string ToString()
>        {
>                char[] b = new char[1];
>               b[0] = GetVisibleItemContents();
>                string a = new string(b);
>                return a;
>            
>        }
>   
>    }
>}
>

another class

enter code here

>    class MineSweeperBoard
>    {
>        private BoardItem [,] board;
>        protected static int[,] mineLocation;
>        protected static int numberOfMine;
>public MineSweeperBoard()
>        {
>            // new board
>            board = new BoardItem[MineSweeper_Constants.RowNum, >MineSweeper_Constants.ColNum];
>            // put mines and set value
>            InitialiseBoard();            
>        }
>public override string ToString()
>{
>    string a = "";
>    for (int j = 0; j < MineSweeper_Constants.ColNum; j++)
>    {
>        for (int i = 0; i < MineSweeper_Constants.RowNum; i++)
>        {
>            a += board[i, j].ToString();
>        }
>    }
>}        

main class:

enter code here

>static void Main(string[] args){
>GameLoop()
>}
>GameLoop(){
MineSweeperBoard board = new MineSweeperBoard();
>Console.WriteLine(board.ToString());
>}

You could do something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Board
{
    class Program
    {
        static void Main(string[] args)
        { 
            // Create the board
            Board b = new Board();
            // Print the maze
            Console.Write(b.ToString());
            Console.ReadKey();
        }
    }

    class Board
    {
        private BoardItem[,] items = new BoardItem[10, 10];

        // Board contructor - populate the multidimensional array - do you own login here to populate the cells.
        public Board()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {                        
                    items[i, j] = new BoardItem();
                }
            }
        }

        // Print each cell from the MineSweeper
        public override string ToString()
        {
            string ret = "";
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    ret += items[i, j].ToString();
                }
                ret += Environment.NewLine;
            }

            return ret;
        }
    }

    class BoardItem
    {
        char BoardItemCharToPriint = '-';

        // Print the current cell
        public override string ToString()
        {
            return BoardItemCharToPriint.ToString();
        }
    }
}

As you can see I created a Board class encapsualting the board items. Each class has it's ToString() method overridden. If you go something like this you can make your task as your teacher wanted, by overriding the ToString methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM