Advertisement
askarulytarlan

Sliding Tiles

Nov 6th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include <string>
  10. #include <cstring>
  11. #include <string.h>
  12.  
  13.  
  14.  
  15. using namespace std;
  16.  
  17. int boardSize;
  18. int a[100][100];
  19. int seedValue;
  20. char arrow;
  21. int counter = 0;
  22. int exchange;
  23. bool finish = 0;
  24. string s;
  25. int x, y;
  26.  
  27. void make_board() {
  28. for (int i = 1; i <= boardSize; i++) {
  29. for (int j = 1; j <= boardSize; j++) {
  30. if (i == 1 && j == 1)
  31. a[i][j] = -1;
  32. else
  33. a[i][j] = rand() % 10;
  34. }
  35. }
  36. }
  37.  
  38. void matrica() {
  39. for (int i = 1; i <= boardSize; i++){
  40. for (int j = 1; j <= boardSize; j++){
  41. cout << " ---";
  42. }
  43. cout << endl;
  44. for (int j = 1; j <= boardSize; j++){
  45. if (a[i][j] == -1){
  46. cout << "| " << " ";
  47. }
  48. else{
  49. cout << "| " << a[i][j] << " ";
  50. }
  51. }
  52. cout << "|";
  53. cout << endl;
  54. }
  55. for (int j = 1; j <= boardSize; j++){
  56. cout << " ---";
  57. }
  58. }
  59.  
  60. void win() {
  61. bool ok = true;
  62. for (int i = 1; i <= boardSize; i++){
  63. for (int j = 1; j <= boardSize; j++){
  64. if (i == boardSize && j == boardSize)
  65. break;
  66. if (a[i][j] < a[i][j - 1] && j > 1)
  67. ok = false;
  68. if (a[i][j] < a[i - 1][boardSize] && i > 1 && j == 1)
  69. ok = false;
  70. }
  71. }
  72. if (a[boardSize][boardSize] != -1)
  73. ok = false;
  74. if (ok == false)
  75. return;
  76. else {
  77. cout << "Complete!" << endl;
  78. exit(0);
  79. }
  80.  
  81. }
  82.  
  83. bool ok(char h) {
  84. if (h == 'l' && y - 1 == 0)
  85. return false;
  86. else if (h == 'r' && y + 1 == boardSize + 1)
  87. return false;
  88. else if (h == 'u' && x - 1 == 0)
  89. return false;
  90. else if (h == 'd' && x + 1 == boardSize + 1)
  91. return false;
  92. return true;
  93. }
  94.  
  95. void direction() {
  96. while (true) {
  97. cout << "Slide the empty cell:";
  98. cin >> arrow;
  99. if (ok(arrow))
  100. break;
  101. }
  102. if (arrow == 'r'){
  103. swap(a[x][y], a[x][y + 1]);
  104. y = y + 1;
  105. }
  106. if (arrow == 'l'){
  107. swap(a[x][y], a[x][y - 1]);
  108. y = y - 1;
  109. }
  110. if (arrow == 'd'){
  111. swap(a[x][y], a[x + 1][y]);
  112. x = x + 1;
  113. }
  114. if (arrow == 'u'){
  115. swap(a[x][y], a[x - 1][y]);
  116. x = x - 1;
  117. }
  118. }
  119.  
  120. int main() {
  121.  
  122.  
  123. cout << "Welcome to Sliding Tiles!" << endl;
  124.  
  125. while (true){
  126. cout << "Choose a board size [3+]:";
  127. cin >> boardSize;
  128. getline(cin, s);
  129. cout << endl;
  130. if (boardSize >= 3 && boardSize <= 4){
  131. break;
  132. }
  133. else if (boardSize < 3) {
  134. cout << boardSize << "is an invalid number";
  135. }
  136. else{
  137. cout << boardSize << " is not supported" << endl;
  138. }
  139. }
  140.  
  141.  
  142. cout << "Input a seed value:";
  143. cin >> seedValue;
  144. getline(cin, s);
  145. cout << endl;
  146.  
  147.  
  148. srand(seedValue);
  149.  
  150. make_board();
  151.  
  152. x = 1;
  153. y = 1;
  154.  
  155. while (true){
  156. matrica();
  157. win();
  158. direction();
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement