using System.Collections.Generic;
public Position(int row, int col) {
if (value < 1 || value > 8)
throw new ArgumentOutOfRangeException("" + value + " is invalid");
if (value < 1 || value > 8)
throw new ArgumentOutOfRangeException("" + value + " is invalid");
public override String ToString() {
return "Position(" + Row + "," + Col + ")";
public class ChessPiece {
protected Position CurrentPos {
public Position GetPosition() {
public ChessPiece(Position startPos) {
this.CurrentPos = startPos;
public void Move(Position newPos) {
List<Position> moves = LegalMoves();
foreach (Position move in moves) {
if (move.Row == newPos.Row && move.Col == newPos.Col) {
this.CurrentPos = newPos;
throw new Exception("Illegal move!");
protected virtual List<Position> LegalMoves() {
return new List<Position>();
public class Pawn : ChessPiece {
public Pawn(Position startPos) : base(startPos) {
protected override List<Position> LegalMoves() {
List<Position> legalPositions = new List<Position>();
if (CurrentPos.Row - 1 >= 1) {
legalPositions.Add(new Position(CurrentPos.Row - 1, CurrentPos.Col));
public static void Main()
Pawn pawn = new Pawn(new Position(7, 6));
pawn.Move(new Position(6, 6));
Console.WriteLine("Pawn is now at " + pawn.GetPosition());
pawn.Move(new Position(5, 6));
Console.WriteLine("Pawn is now at " + pawn.GetPosition());
pawn.Move(new Position(8, 8));
Console.WriteLine("Pawn is now at " + pawn.GetPosition());