Luke
Luke

Reputation: 387

Advice on setting up game of draughts/checkers

I'm new to C# and OOP and am trying my hand at a game of draughts. My issue is that my game has a Board class, with a constructor that makes a board out of a 2D array of 'tiles'. My Tile class has a constructor that has an x coord, a y coord, and a bool to check whether it's occupied or not. The idea is that a player can move a piece to a tile only if it's not in an occupied state.

My issue is getting my board to hold actual tiles. I first create the board (as an empty 2D array of tiles). I then cycle through two 'for' loops to create tiles (giving them x and y coords and an occupied status). I want to add those tiles into the board at the correct spot, but I can't access it. Any tips?


This is not homework, it's a personal project to help me improve. Any advice would be greatly appreciated.


As requested, here are the relevant parts of code:

public class Board
{
    public Tile[,] board;
    public const int DIMENSION = 8;

    public Board()
    {
        board = new Tile [DIMENSION, DIMENSION];
    }
}

And within the GameController class I have:

public void GameLoop()
{
    Board board = new Board();
    Board tiledBoard = AssignTilesToCoords(board);
}

public Board AssignTilesToCoords(Board board)
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            Tile tile = new Tile(i, j, false);
            //I'm assuming here I need to assign, otherwise board isn't actually modified at all 
        }
    }
    return board;
}

Upvotes: 1

Views: 159

Answers (1)

Aldert
Aldert

Reputation: 4313

You where very close. rename the Tile[,] board to tiles (this makes more sense). Then :

public Board AssignTilesToCoords(Board board)
    {
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                Tile tile = new Tile(i, j, false);
                board.tiles[i, j] = tile;
            }
        }
        return board;
    }

Upvotes: 1

Related Questions