using System; using System.Collections.Generic; using System.Text; namespace BoardGame { class Board // 2D board game { // Attributes private char[,] _board; private int _width; private int _height; private char _reset; // Represents empty cell on board // Constructors public Board(int width, int height, char resetChar) { _width = width; _height = height; _reset = resetChar; _board = new char[_width, _height]; Reset(); // Empty the board } // Properties // Methods public int GetWidth() { return _width; } public int GetHeight() { return _height; } public void Reset() { for (int y = 0; y < _height; y++) { for (int x = 0; x < _width; x++) { _board[x, y] = _reset; // Fill board with "empty" cell char } } } public void Display() { // Replace the line below with the following // line if you need y=0 at top left of display //for (int y = 0; y < _height; y++) for (int y = _height-1; y >= 0; y--) { for (int x = 0; x < _width; x++) { Console.Write("{0} ", _board[x, y]); } Console.WriteLine(); // Move to next line at end of row } } // Is the provided position legal public bool IsValidPosition(int x, int y) { if (x >= 0 && x < _width) { if (y >= 0 && y < _height) { return true; } } return false; } public bool SetValue(int x, int y, char value) { if (IsValidPosition(x, y) == true) { _board[x, y] = value; return true; } return false; } public bool GetValue(int x, int y, out char value) { value = ' '; // Bogus to avoid compile error if (IsValidPosition(x, y) == true) { value = _board[x, y]; return true; } return false; } } }