-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
118 lines (113 loc) · 3.45 KB
/
input.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "input.hpp"
using namespace std;
pair<int,int> defaultDimensions(vector<string> &words){
int maxlen = 0;
for(int i=0;i<words.size();++i){
if(words[i].size()>maxlen){
maxlen = words[i].size();
}
}
pair <int,int> dimensions = {maxlen,words.size()*3/2};
return dimensions;
}
void getinformation(string &title,vector<string> &words,int &width,int &height) {
cout << "You are creating a word search, please input the title of your word search." << endl;
cin >> title;
cout << "Your puzzle is titled " << title << endl << "What width do you want your puzzle? (If default, enter 0)(minimum 10)" << endl;
cin >> width;
while (width!=0 && (width <10 || width > 25)){
cout << "Try again " << endl;
cin >> width;
}
if (width==0) {
height = 0;
}
else {
cout << "What height do you want your puzzle? (cannot be default)(minimum 10,maximum 25)" << endl;
cin >> height;
while (height <10 && height>25){
cout << "Try again " << endl;
cin >> height;
}
}
cout << "Now we will begin the process of inputting words . . . " << endl;
cin.ignore();
pair<int,int> maxwords = wordAmount(width,height);
int bigwords = maxwords.first, smallwords = maxwords.second;
if (width!=0) {
while ((maxwords.first+maxwords.second>words.size())){
string word;
cout << "Word: ";
getline(cin,word);
// check for invariants
bool flag = false;
for (int j=0;j<word.size();++j){
if (static_cast<int>(word[j])<97||static_cast<int>(word[j])>122){
flag=true;
break;
}
}
if (flag) {
cout << "Non lower-case letter character detected in word. Try again" << endl;
continue;
}
if (word.size()>min(width,height)&&word.size()<max(width,max(height,25))&&bigwords>0){
--bigwords;
words.push_back(word);
continue;
}
else if (word.size()>max(width,height)){
cout << "Inputted word was too large. Try again" << endl;
}
else if (word.size()<min(width,height)&&(bigwords>0||smallwords>0)&&word.size()>1){
if (smallwords>0){--smallwords;}
else {--bigwords;}
words.push_back(word);
continue;
}
else {
cout << "Inputted word was too small. Try again" << endl;
}
}
}
else {
while (true){
string word;
cout << "Word (type 'BREAK' to break): ";
getline(cin,word);
// check for invariants
bool flag = false;
if(word=="BREAK"){
break;
}
for (int j=0;j<word.size();++j){
if (static_cast<int>(word[j])<97||static_cast<int>(word[j])>122){
flag=true;
break;
}
}
if (flag) {
cout << "Non lower-case letter character detected in word. Try again" << endl;
continue;
}
if (word.size()>1&&word.size()<20){
words.push_back(word);
}
else {
cout << "Inputted word was too small or large (bounds 1-20 chars). Try again" << endl;
}
}
pair <int,int> dims = defaultDimensions(words);
width = dims.first;
height = dims.second;
}
cout << "Words have been compiled. Now creating a word search " << width << " x " << height << endl;
}
pair <int,int> wordAmount(const int width, const int height){
pair <int,int> amt = {min(width,height) - abs(width-height)/5, max(abs(width-height)/3,0)};
return amt;
}