/// <summary> /// Check the board state relative to a piece /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="p"></param> public void CheckBoard(int x, int y, Piece p) { this.MoveCount++; //check end conditions //check column for (int i = 0; i < this.Size; i++) { if (this.Cells[x, i].Form != p.Form) break; if (i == this.Size - 1) { this.Winner = p.Form; } } //check row for (int i = 0; i < this.Size; i++) { if (this.Cells[i, y].Form != p.Form) break; if (i == this.Size - 1) { this.Winner = p.Form; } } //check diagonal if (x == y) { //travel in diagonal for (int i = 0; i < this.Size; i++) { if (this.Cells[i, i].Form != p.Form) break; if (i == this.Size - 1) { this.Winner = p.Form; } } } //check anti diagonal for (int i = 0; i < this.Size; i++) { if (Cells[i, (this.Size - 1) - i].Form != p.Form) break; if (i == this.Size - 1) { this.Winner = p.Form; } } //check draw if (MoveCount == (this.Size * this.Size - 1)) { this.Winner = PieceForm.Blank; } }
/// <summary> /// Add the piece to the board at the X and Y position from the piece /// </summary> /// <param name="p">Piece</param> public void Move(int x, int y, Piece p) { if (this.Cells[x, y] == null) this.Cells[x, y] = p; CheckBoard(x, y, p); }