-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
150 lines (138 loc) · 4.75 KB
/
Source.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
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
#include<iostream>
#include<Windows.h>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;
HANDLE hConsole;
void printRulesOFTheGame() {
system("cls");
cout << "Èãðà 'Ðîññèéñêèé ôëàã'.Âäîëü äîñêè ðàñïîëîæåíû ëóíêè(êîëè÷åñòâî ëóíîê êðàòíî 3).\n êàæäîé ëóíêå ëåæèò êðàñíûé, áåëûé èëè ñèíèé øàð.Îäíèì õîäîì ðàçðåøàåòñÿ ìåíÿòü ìåñòàìè äâà ëþáûõ øàðà.\nÍåîáõîäèìî äîáèòüñÿ òîãî, ÷òîáû âñå áåëûå øàðû øëè ïåðâûìè, çà íèìè - ñèíèå øàðû, à êðàñíûå - ïîñëåäíèìè.\nÅñëè îáùåå ÷èñëî ëóíîê ðàâíî n, òî äëÿ ðåøåíèÿ çàäà÷è äîñòàòî÷íî ñäåëàòü íå áîëåå n - 1 õîäà.\nÅñëè èãðîê äåëàåò áîëåå ÷åì n - 1 õîä, òî èãðà ïðîèãðàíà.\nÏðîãðàììà ìîæåò èìåòü íåñêîëüêî óðîâíåé ñëîæíîñòè(óðîâíè ñëîæíîñòè ìîãóò çàâèñåòü îò êîëè÷åñòâà õîäîâ).\n";
system("pause");
}
int getNumber(const int &beg, const int &end) {
int n;
while (true) // Áåñêîíå÷íûé öèêë, êîòîðûé âíóòðè ïðåðûâàåòñÿ ïî óñëîâèþ
{
cin >> n; // Ââîä çíà÷åíèÿ ïîëüçîâàòåëåì
if (!cin.fail()){ // Åñëè ââåëè ÷èñëî, à çíà÷èò ïîòîê íå â ñîñòîÿíèè îøèáêè, òî çàâåðøàåì öèêë
if (beg <= n && n <= end)break;
}
cout << " syntax error!" << endl;
cin.clear(); // Ñáðàñûâàåì ñîñòîÿíèå îøèáêè
cin.ignore((numeric_limits<std::streamsize>::max)(), '\n');
}
return n;
};
void swappits(vector <vector <string>>& board, vector<int> & pit1, vector<int>& pit2) {
swap(board [ pit1[0] ] [ pit1[1] ], board [pit2[0] ] [ pit2[1] ]);
}
vector <vector <string>> initialiseBoard(const int &modeGame) {
system("color F6");
srand(time(0));
cout << "Ââåäèòå øèðèíó èãðîâîãî ïîëÿ\n";
int sizeBoard = getNumber(1, 50);
vector<int> pit1(2);
vector<int> pit2(2);
vector <vector <string>> board(4, vector<string>(sizeBoard, "Á"));// áóêâà á
board[0][0] = "*";
for (int i = 1; i < board[0].size(); i++) board[2][i] = "Ñ";// áóêâà ñ
for (int i = 1; i < board[0].size(); i++) board[3][i] = "Ê";// áóêâà ê
for (int i = 1; i < 4; i++) board[i][0] = to_string(i); //áîêîâîé ñòîëåö öèôð
for (int i = 1; i < board[0].size(); i++) board[0][i] = to_string(i); //âåðõíèé ðÿä öèôð
///ïåðåìåùèâàåì áóêâû
for (int i = 0; i < board[0].size() * modeGame; i++) {
pit1[0] = rand() % 3 + 1;
pit1[1] = rand() % ( sizeBoard- 1) + 1;
pit2[0] = rand() % 3 + 1;
pit2[1] = rand() % (sizeBoard - 1) + 1;
swappits(board, pit1, pit2);
}
return board;
}
void printBoard( const vector <vector <string>> &board, int & counter){
system("cls");
SetConsoleCP(1251);
setlocale(LC_ALL, "Rus");
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 15);
cout << " Íà äàííûé ìîìåíò ñîâåðøåííî " << counter << " õîäà(-îâ)" << endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < board[0].size(); j++) {
if (board[i][j] == "Á")
SetConsoleTextAttribute(hConsole, 15);
else if (board[i][j] == "Ñ")
SetConsoleTextAttribute(hConsole, 1);
else if (board[i][j] == "Ê")
SetConsoleTextAttribute(hConsole, 4);
else
SetConsoleTextAttribute(hConsole, 15);
cout << setw(3) << board[i][j];
}
cout << endl;
}
cout << endl;
SetConsoleTextAttribute(hConsole, 15);
}
void movePlayer(vector <vector <string>>& board){
vector<int> pit1(2);
vector<int> pit2(2);
cout << "enter the coordinates of the first pit\n";
pit1[0] = getNumber(1, 3);
pit1[1] = getNumber(1, board[0].size() - 1);
cout << "enter the coordinates of the second pit\n";
pit2[0] = getNumber(1, 3);
pit2[1] = getNumber(1, board[0].size() - 1);
swappits(board, pit1, pit2);
}
bool checkWin(const vector <vector <string>>& board)
{
bool flag = true;
for (int i = 1; i < board[1].size(); i++)
if (board[1][i] != "Á") return false;
for (int i = 1; i < board[2].size(); i++)
if (board[2][i] != "Ñ") return false;
for (int i = 1; i < board[3].size(); i++)
if (board[3][i] != "Ê") return false;
return flag;
}
void start() {
while (true) {
printRulesOFTheGame();
system("cls");
int moveCounter = 0;
cout << "mode game 1- middle; 2 - hard;\n";
int modeGame = getNumber(1, 2);
vector <vector <string>> board = initialiseBoard(modeGame);
system("cls");
while (true) {
printBoard(board, moveCounter);
movePlayer(board);
system("cls");
if (checkWin(board)) {
system("cls");
cout << "Íàøå èñêðåííèå ïîçäðàâëåíèÿ!!!\n";
Sleep(2500);
break;
}
if (moveCounter >= ( board[0].size() - 1 ) * 3 - 1) {
system("cls");
cout << "Âû ïðîãðàëè, âàì ïîòðåáîëàñü ñëèøêîì ìíîãî õîäîâ";
Sleep(2500);
break;
}
moveCounter++;
}
cout << "Äëÿ âûõîäà âûõîäà èç èãðû íàáåðèòå exit, äëÿ ïðîäîëæåíèÿ ëþáîé ñèìâîë\n";
string str;
cin>> str;
if (str == "exit") break;
system("cls");
}
}
int main(){
SetConsoleCP(1251);
setlocale(LC_ALL, "Rus");
start();
return 0;
}