utils.c 1019 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include "utils.h"
  6. #include "../globals.h"
  7. extern char board[BRD_SIZE_Y][BRD_SIZE_X];
  8. int randomInt(const int start, const int end, const unsigned int seed) {
  9. /*
  10. * Gets random int from range
  11. * [start, end[
  12. */
  13. srand(seed);
  14. int result = rand() % end;
  15. result = result >= start ? result : result + start;
  16. return result;
  17. }
  18. int randomX(const clock_t initClock) {
  19. int x;
  20. const unsigned int seed = clock() - initClock;
  21. for (int i = randomInt(0, 100, seed); ; i++) {
  22. x = randomInt(PL_BRD_XS, PL_BRD_XE, i);
  23. if (x % 2 == 0)
  24. break;
  25. }
  26. return x;
  27. }
  28. int randomY(const clock_t initClock) {
  29. const unsigned int seed = clock() - initClock;
  30. return randomInt(PL_BRD_YS, PL_BRD_YE, seed);
  31. }
  32. void sleep_ms(const int ms) {
  33. usleep(ms * 1000);
  34. }
  35. void printBoard() {
  36. for (int i = 0; i < BRD_SIZE_Y; i++) {
  37. for (int j = 0; j < BRD_SIZE_X; j++) {
  38. printf("%c", board[i][j]);
  39. }
  40. printf("\n");
  41. }
  42. }