Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <math.h>
- #include <time.h>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- #include <cstring>
- #include <string.h>
- using namespace std;
- int boardSize;
- int a[100][100];
- int seedValue;
- char arrow;
- int counter = 0;
- int exchange;
- bool finish = 0;
- string s;
- int x, y;
- void make_board() {
- for (int i = 1; i <= boardSize; i++) {
- for (int j = 1; j <= boardSize; j++) {
- if (i == 1 && j == 1)
- a[i][j] = -1;
- else
- a[i][j] = rand() % 10;
- }
- }
- }
- void matrica() {
- for (int i = 1; i <= boardSize; i++){
- for (int j = 1; j <= boardSize; j++){
- cout << " ---";
- }
- cout << endl;
- for (int j = 1; j <= boardSize; j++){
- if (a[i][j] == -1){
- cout << "| " << " ";
- }
- else{
- cout << "| " << a[i][j] << " ";
- }
- }
- cout << "|";
- cout << endl;
- }
- for (int j = 1; j <= boardSize; j++){
- cout << " ---";
- }
- }
- void win() {
- bool ok = true;
- for (int i = 1; i <= boardSize; i++){
- for (int j = 1; j <= boardSize; j++){
- if (i == boardSize && j == boardSize)
- break;
- if (a[i][j] < a[i][j - 1] && j > 1)
- ok = false;
- if (a[i][j] < a[i - 1][boardSize] && i > 1 && j == 1)
- ok = false;
- }
- }
- if (a[boardSize][boardSize] != -1)
- ok = false;
- if (ok == false)
- return;
- else {
- cout << "Complete!" << endl;
- exit(0);
- }
- }
- bool ok(char h) {
- if (h == 'l' && y - 1 == 0)
- return false;
- else if (h == 'r' && y + 1 == boardSize + 1)
- return false;
- else if (h == 'u' && x - 1 == 0)
- return false;
- else if (h == 'd' && x + 1 == boardSize + 1)
- return false;
- return true;
- }
- void direction() {
- while (true) {
- cout << "Slide the empty cell:";
- cin >> arrow;
- if (ok(arrow))
- break;
- }
- if (arrow == 'r'){
- swap(a[x][y], a[x][y + 1]);
- y = y + 1;
- }
- if (arrow == 'l'){
- swap(a[x][y], a[x][y - 1]);
- y = y - 1;
- }
- if (arrow == 'd'){
- swap(a[x][y], a[x + 1][y]);
- x = x + 1;
- }
- if (arrow == 'u'){
- swap(a[x][y], a[x - 1][y]);
- x = x - 1;
- }
- }
- int main() {
- cout << "Welcome to Sliding Tiles!" << endl;
- while (true){
- cout << "Choose a board size [3+]:";
- cin >> boardSize;
- getline(cin, s);
- cout << endl;
- if (boardSize >= 3 && boardSize <= 4){
- break;
- }
- else if (boardSize < 3) {
- cout << boardSize << "is an invalid number";
- }
- else{
- cout << boardSize << " is not supported" << endl;
- }
- }
- cout << "Input a seed value:";
- cin >> seedValue;
- getline(cin, s);
- cout << endl;
- srand(seedValue);
- make_board();
- x = 1;
- y = 1;
- while (true){
- matrica();
- win();
- direction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement