code_junkie

place 20 chess pieces on a checkerboard, in random places

Nov 13th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. Piece[] board = whatever();
  5.  
  6. // New random number generator.
  7. Random random = new Random();
  8.  
  9. // LINQ query orders each element by the next random number.
  10. Piece[] randomBoard = (from piece in board
  11. orderby random.Next()
  12. select piece).ToArray();
  13.  
  14. int index = (y * 8) + x;
  15.  
  16. Piece[] pieces = ... your 20 pieces here
  17. Piece[] board = new Piece[8 * 8];
  18. Random r = new Random();
  19. foreach (Piece piece in pieces)
  20. {
  21. while (true)
  22. {
  23. Int32 location = r.Next(board.Length);
  24. if (board[location] == null)
  25. {
  26. board[location] = piece;
  27. break;
  28. }
  29. }
  30. }
Add Comment
Please, Sign In to add comment