-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
155 lines (130 loc) · 5.47 KB
/
main.cpp
File metadata and controls
155 lines (130 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Tablero.h"
using namespace std;
int const ROWS = 10;
int const COLUMNS = 10;
bool searchLocation(char (&board)[ROWS][COLUMNS], string, int, int, int, bool, int);
int main() {
// Tablero tableroActual(10);
// char tableroDatos = tableroActual.obtenerTablero();
//
// for (int i = 0; i < (sizeof(tableroDatos)/sizeof(tableroDatos[0])); i++) {
// for (int j = 0; j < sizeof(tableroDatos[0]); j++) {
// if (!tableroDatos[i][j]) {
// cout << "* ";
// } else {
// cout << tableroDatos[i][j] << " ";
// }
// }
// cout << endl;
// }
return 0;
srand(time(NULL));
char letters[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
string words[] = {"HOLA", "CASA", "NADA", "PAPA", "MAMA", "MEME", "AAAAA"};
char board[ROWS][COLUMNS] = {0};
string usedWords[0];
for (int i = 0; i < (sizeof(board)/sizeof(board[0])); i++) {
for (int j = 0; j < sizeof(board[0]); j++) {
if (!board[i][j]) {
cout << "* ";
} else {
cout << board[i][j] << " ";
}
}
cout << endl;
}
int currentWord = rand() % (sizeof(words)/sizeof(string));
int orientation = (rand() % 3)+1; // 1 horizontal, 2 vertical, 3 descending, 4 ascending
int currentRow = rand() % ((sizeof(board)/sizeof(board[0]))-(words[currentWord].length()-1));
int currentColumn = rand() % (sizeof(board[0])-(words[currentWord].length()-1));
int reverve = rand() % 2;
cout << currentWord << ": " << words[currentWord] << ":" << words[currentWord].length() << " " << orientation << " [" << currentRow << "][" << currentColumn << "]: " << board[currentRow][currentColumn] << endl;
if (!searchLocation(board, words[currentWord], orientation, currentRow, currentColumn, false, 0)) {
cout << "We need another location for the word." << endl;
} else {
cout << "Spot was free." << endl;
}
searchLocation(board, words[currentWord], orientation, currentRow, currentColumn, true, reverve);
for (int i = 0; i < (sizeof(board)/sizeof(board[0])); i++) {
for (int j = 0; j < sizeof(board[0]); j++) {
if (!board[i][j]) {
cout << "* ";
} else {
cout << board[i][j] << " ";
}
}
cout << endl;
}
currentWord = rand() % (sizeof(words)/sizeof(string));
orientation = (rand() % 3)+1; // 1 horizontal, 2 vertical, 3 descending, 4 ascending
currentRow = rand() % ((sizeof(board)/sizeof(board[0]))-(words[currentWord].length()-1));
currentColumn = rand() % (sizeof(board[0])-(words[currentWord].length()-1));
reverve = rand() % 2;
cout << currentWord << ": " << words[currentWord] << ":" << words[currentWord].length() << " " << orientation << " [" << currentRow << "][" << currentColumn << "]: " << board[currentRow][currentColumn] << endl;
if (!searchLocation(board, words[currentWord], orientation, currentRow, currentColumn, false, 0)) {
cout << "We need another location for the word." << endl;
} else {
cout << "Spot was free." << endl;
searchLocation(board, words[currentWord], orientation, currentRow, currentColumn, true, reverve);
}
for (int i = 0; i < (sizeof(board)/sizeof(board[0])); i++) {
for (int j = 0; j < sizeof(board[0]); j++) {
if (!board[i][j]) {
cout << "* ";
} else {
cout << board[i][j] << " ";
}
}
cout << endl;
}
return 0;
}
/*
* Busca
*/
bool searchLocation(char (&board)[ROWS][COLUMNS], string currentWord, int orientation, int currentRow, int currentColumn, bool saveLocation, int reverse) {
int searchRow = currentRow;
int searchColumn = currentColumn;
int wordIndex = 0;
if(reverse) {
wordIndex = currentWord.length()-1;
}
while (true) {
// Comprueba si la posicion actual tiene valores
if (board[searchRow][searchColumn]) {
cout << "Found a letter and compating: " << board[searchRow][searchColumn] << " == " << currentWord[wordIndex] << " ?" << endl;
if (board[searchRow][searchColumn] != currentWord[wordIndex]) {
return false;
}
}
if (saveLocation) {
board[searchRow][searchColumn] = currentWord[wordIndex];
}
if (orientation == 1 && searchColumn < (searchColumn+currentWord.length()-1) && searchColumn < sizeof(board[0])-1) {
searchColumn++;
} else if (orientation == 2 && searchRow < (searchRow+currentWord.length()-1) && searchRow < (sizeof(board)/sizeof(board[0]))-1) {
searchRow++;
} else if(orientation == 3 && searchRow < (searchRow+currentWord.length()-1) && searchRow < (sizeof(board)/sizeof(board[0]))-1 && searchColumn < (searchColumn+currentWord.length()-1) && searchColumn < sizeof(board[0])-1) {
searchRow++;
searchColumn++;
} else {
return true;
}
if (reverse) {
if (wordIndex > 0) {
wordIndex--;
} else {
return true;
}
} else {
if (wordIndex < currentWord.length()-1) {
wordIndex++;
} else {
return true;
}
}
}
}