I am programming the game Othello in c#. i have managed to generate the grid however the starting pieces are not in the right position. I think it because the indexing from 0 when I want it to start from 1. Can someone try and help me out.

private const string White_piece = "X"; // players piece
    private const string Black_piece = "O"; // computers piece
    private const string Empty = " ";
    const int Row = 9;
    const int Column = 9;
    private string[,] board = new string[9,9];

    public void Draw_Board() // creates the reversi board
    {
        // Codes row 1-8
        for (int Row = 1; Row < board.GetLength(1); Row++)
        {
            if (Row < 10)
            {
                Console.Write(" ");
            }
            Console.Write(String.Format("\t{0}", Row + " |"));
        }
        Console.WriteLine();

        // codes -------------------------------------------------
        for (int Row = 1; Row < board.GetLength(1) * 7 + 5; Row++)
        {
            Console.Write("-");

        }
        Console.WriteLine();

        // codes the column
        for (int Column = 1; Column < board.GetLength(1); Column++)
        {
            // 1| ..........
            Console.Write(Column + "|");

            for (int Row = 1; Row < board.GetLength(0); Row++)
            {
                Console.Write(String.Format("\t{0}", " " + " " + "|"));

                if (Row == 8)
                {
                    Console.WriteLine();
                }

                if (board[Row, Column] == White_piece)
                {
                    Console.Write(White_piece);
                }

                if (board[Row, Column] == Black_piece)
                {
                    Console.Write(Black_piece);
                }

            }

        }
        New_coordinate();

    }

    public void ResetBoard()
    {
        board[4, 4] = White_piece;
        board[5, 4] = Black_piece;
        board[4, 5] = Black_piece;
        board[5, 5] = White_piece;
    }

reversi.JPG

Recommended Answers

All 6 Replies

Your formatting problem is in here:

for (int Row = 1; Row<board.GetLength(0); Row++)
{
    Console.Write(String.Format("\t{0}", " " + " " + "|"));
    if (Row == 8)
    {
        Console.WriteLine();
    }
    if (board[Row, Column] == White_piece)
    {
        Console.Write(White_piece);
    }
    if (board[Row, Column] == Black_piece)
    {
        Console.Write(Black_piece);
    }
}

You're writing the space and the bar before you're writing the piece. Part of the reason for making this kind of mistake is the whole method is very complicated.

To simplify, first change the 2D array to a 2D jagged array. This makes it much easier to initialize. The reason this matters is, part of the simplifying is to just print out the array instead of checking what's in the array and deciding what to print.

Initialize the array to contain the values that you need.

Since everything you're working with is in reality characters, it would make more sense to store them as characters rather than strings. This has the effect of cutting the storage requirement in half, since every string includes a terminator which is a null char(\0).

When printing a table to the console that includes a header, it is usually easier and simpler to create a constant string, rather than creating the header with a loop. the same goes for the separator line.

When all this is put together, not only does the display get corrected, but your code is made much simpler:

    private const int BOARDSIZE = 9;
    private const char White_piece = 'X'; // players piece
    private const char Black_piece = 'O'; // computers piece
    private static readonly string line = new string('-', 68);
    private static readonly string header = $"\t {1} |\t {2} |\t {3} |\t {4} |\t {5} |\t {6} |\t {7} |\t {8} |";
    private const char Empty = ' ';
    const int Row = BOARDSIZE;
    const int Column = BOARDSIZE;
    private char[][] board = new char[BOARDSIZE][];

    public void Draw_Board()
    {
        Console.WriteLine(Othello.header);
        Console.WriteLine(Othello.line);

        for (int row = 1; row < BOARDSIZE; ++row)
        {
            Console.Write($"{row}|");
            for (int col = 1; col < BOARDSIZE; ++col)
            {
                Console.Write($"\t {board[row][col]} |");
            }
            Console.WriteLine();
        }
    }
    public void ResetBoard()
    {
        for (int row = 1; row < BOARDSIZE; ++row)
        {
            board[row] = "         ".ToCharArray();
        }
        board[4][4] = White_piece;
        board[5][4] = Black_piece;
        board[4][5] = Black_piece;
        board[5][5] = White_piece;
    }

You'll notice I made the readonly strings static. This is to prevent copying these strings for every instance, of the class, that is created

commented: private const int BOARDSIZE = 9; private const string White_piece = "X"; // players piece private const string Black_piece = "O"; // c +0

I tired that and it solved the problem however it messed up the alignment. How do I fix this.

reversi_board.JPG

What are those asterisks(*) for? They aren't in your original code.

What are you trying to do? What code are you using?

commented: They are stars. A Klingon would be a K. +14

The asterisks display where the player can place his piece . I have got rid off that bit of code and it is still isn't aligned properly.

What code are you using?

If you are adding asterisks to the printout, you will have to adjust the spacing to allow for that. I would suggest changing the appropriate spots on the board to asterisks then the spacing will remain the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.