I have come up with System verilog constraints for rook placement rule on an 8x8 board: rooks don’t attack each other horizontally or vertically. Any other solution possible apart from below one?

EDA playground code -EDA Playground


module tb_rook8;

  class rook8;
  // One rook per row i=0..7, placed in column col[i]
  rand int unsigned col[8];

  // Bounds and non-attacking (unique columns)
  constraint c_rooks {
    foreach (col[i]) col[i] inside {[0:7]}; // columns 0..7
    unique { col };                          // no two in same column
  }
endclass
  

  initial begin
    rook8 r = new();

    // Try a few random rook placements
    repeat (5) begin
      if (r.randomize()) begin
        $display("\n=== New rook placement ===");
        // Print coordinates row by row
        foreach (r.col[i]) begin
          $display("Row %0d -> Col %0d", i, r.col[i]);
        end

        // Optional: pretty-print an 8x8 board
        $display("Board view:");
        for (int i = 0; i < 8; i++) begin
          for (int j = 0; j < 8; j++) begin
            if (r.col[i] == j)
              $write(" R "); // rook
            else
              $write(" . "); // empty
          end
          $write("\n");
        end
      end
      else begin
        $error("Randomization failed!");
      end
    end

    $finish;
  end
endmodule