YorKnEz

pb6

Oct 8th, 2022 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct Tile {
  4. int x, y;
  5. };
  6.  
  7. struct Game {
  8. int size;
  9. char **board;
  10. Tile white;
  11. bool turn; // 0 - blue, 1 - green
  12. };
  13.  
  14. bool isOutOfBounds(int x, int y, int size);
  15.  
  16. bool checkWin(Game g);
  17.  
  18. int main() {
  19. Game g;
  20.  
  21. g.size = 5;
  22. g.turn = 0;
  23.  
  24. g.white.x = g.white.y = g.size / 2;
  25.  
  26. g.board = new char*[g.size];
  27.  
  28. for (int i = 0; i < g.size; i++) {
  29. g.board[i] = new char[g.size];
  30.  
  31. for (int j = 0; j < g.size; j++) {
  32. if (i % 2 == 0)
  33. g.board[i][j] = j % 2 ? 'B' : 'G';
  34.  
  35. if (i % 2)
  36. g.board[i][j] = j % 2 ? 'G' : 'B';
  37. }
  38. }
  39.  
  40. g.board[g.white.x][g.white.y] = 'W';
  41.  
  42. for (int i = 0; i < g.size; i++) {
  43. for (int j = 0; j < g.size; j++) {
  44. printf("%c ", g.board[i][j]);
  45. }
  46. printf("\n");
  47. }
  48.  
  49. printf("%d\n", checkWin(g));
  50.  
  51. return 0;
  52. }
  53.  
  54. bool isOutOfBounds(int x, int y, int size) {
  55. return x < 0 || x >= size || y < 0 || y >= size;
  56. }
  57.  
  58. bool checkWin(Game g) {
  59. int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  60. int l, c;
  61.  
  62. char charToLookFor = g.turn ? 'G' : 'B';
  63. bool foundChar = false;
  64.  
  65. for (int i = 0; i < 4; i++) {
  66. l = g.white.x + dx[i];
  67. c = g.white.y + dy[i];
  68.  
  69. if (!isOutOfBounds(l, c, g.size) && g.board[l][c] == charToLookFor) {
  70. foundChar = true;
  71. }
  72. }
  73.  
  74. if (!foundChar)
  75. return true;
  76.  
  77. return false;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment