Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- bool gameOver;
- class Game {
- private:
- static const int _x = 19;
- static const int _y = 19;
- static const int _c = 5;
- int posX;
- int posY;
- int r1, r2;
- char str[_x][_y];
- int pl;
- int mas[3][3];
- int p = 0;
- public:
- Game() {
- posX = 1;
- posY = 1;
- r1 = posX;
- r2 = posY;
- pl = 1;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- mas[i][j] = 0;
- }
- }
- for (int i = 0; i < _x; i++) {
- for (int j = 0; j < _y; j++) {
- if (j % 6 == 0)
- str[i][j] = '#';
- else if (i % 6 == 0)
- str[i][j] = '#';
- else
- str[i][j] = ' ';
- }
- }
- }
- void clear() { system("cls"); }
- void Draw();
- void Cursor();
- void update();
- void DrawPl1();
- void DrawPl2();
- void Proverka();
- void Result(int k);
- };
- void Game::Draw() {
- for (int i = 0; i < _x; i++) {
- for (int j = 0; j < _y; j++) {
- cout << str[i][j];
- }
- cout << endl;
- }
- }
- void Game::Cursor() {
- for (int i = 0; i < _x; i++) {
- for (int j = 0; j < _y; j++) {
- if (6 * r1 + 1 == j && 6 * r2 + 1 == i) {
- for (int t1 = i; t1 < i + _c; t1++) {
- for (int t2 = j; t2 < j + _c; t2++) {
- if (t1 == i)
- str[t1][t2] = ' ';
- else if (t1 == i + _c - 1)
- str[t1][t2] = ' ';
- if (t2 == i)
- str[t1][t2] = ' ';
- else if (t2 == j + _c - 1)
- str[t1][t2] = ' ';
- }
- }
- }
- }
- }
- for (int i = 0; i < _x; i++) {
- for (int j = 0; j < _y; j++) {
- if (6 * posX + 1 == j && 6 * posY + 1 == i) {
- r1 = posX;
- r2 = posY;
- for (int t1 = i; t1 < i + _c; t1++) {
- for (int t2 = j; t2 < j + _c; t2++) {
- if (t1 == i)
- str[t1][t2] = 'A';
- else if (t1 == i + _c - 1)
- str[t1][t2] = 'A';
- if (t2 == i)
- str[t1][t2] = 'A';
- else if (t2 == j + _c - 1)
- str[t1][t2] = 'A';
- }
- }
- }
- }
- }
- if (pl > 0)
- cout << endl << "Ходит игрок №1" << endl;
- else cout << endl << "Ходит игрок №2" << endl;
- }
- void Game::update() {
- if (_kbhit()) {
- switch (_getch()) {
- case 'a':
- case 'A':
- case 75:
- posX--;
- break;
- case 's':
- case 'S':
- case 80:
- posY++;
- break;
- case 'w':
- case 'W':
- case 72:
- posY--;
- break;
- case 'd':
- case 'D':
- case 77:
- posX++;
- break;
- case 'f':
- case 'F':
- case VK_SPACE:
- if (pl > 0) {
- if (mas[posX][posY] == 0) {
- DrawPl1();
- mas[posX][posY] = pl;
- pl = -pl;
- }
- else {
- cout << endl << "Ошибка, клетка занята";
- }
- }
- else {
- if (mas[posX][posY] == 0) {
- DrawPl2();
- mas[posX][posY] = pl;
- pl = -pl;
- }
- else {
- cout << endl << "Ошибка, клетка занята";
- }
- }
- Proverka();
- break;
- }
- }
- if (posX < 0)
- posX = 0;
- else if (posX > 2)
- posX = 2;
- else if (posY < 0)
- posY = 0;
- else if (posY > 2)
- posY = 2;
- }
- void Game::DrawPl1() {
- for (int i = 0; i < _x; i++) {
- for (int j = 0; j < _y; j++) {
- if (6 * posX + 2 == j && 6 * posY + 2 == i) {
- str[i][j] = 'x';
- str[i + 1][j + 1] = 'x';
- str[i + 2][j + 2] = 'x';
- str[i + 2][j] = 'x';
- str[i][j + 2] = 'x';
- }
- }
- }
- }
- void Game::DrawPl2() {
- for (int i = 0; i < _x; i++) {
- for (int j = 0; j < _y; j++) {
- if (6 * posX + 2 == j && 6 * posY + 2 == i) {
- str[i + 1][j] = '0';
- str[i][j + 1] = '0';
- str[i + 2][j + 1] = '0';
- str[i + 1][j + 2] = '0';
- }
- }
- }
- }
- void Game::Proverka() {
- if ( mas[0][0] == 1 && mas[0][1] == 1 && mas[0][2] == 1 ||
- mas[1][0] == 1 && mas[1][1] == 1 && mas[1][2] == 1 ||
- mas[2][0] == 1 && mas[2][1] == 1 && mas[2][2] == 1 ||
- mas[0][0] == 1 && mas[1][0] == 1 && mas[2][0] == 1 ||
- mas[0][1] == 1 && mas[1][1] == 1 && mas[2][1] == 1 ||
- mas[0][2] == 1 && mas[1][2] == 1 && mas[2][2] == 1 ||
- mas[0][0] == 1 && mas[1][1] == 1 && mas[2][2] == 1 ||
- mas[2][0] == 1 && mas[1][1] == 1 && mas[0][2] == 1
- ) {
- Result(1);
- }
- else if (mas[0][0] == -1 && mas[0][1] == -1 && mas[0][2] == -1 ||
- mas[1][0] == -1 && mas[1][1] == -1 && mas[1][2] == -1 ||
- mas[2][0] == -1 && mas[2][1] == -1 && mas[2][2] == -1 ||
- mas[0][0] == -1 && mas[1][0] == -1 && mas[2][0] == -1 ||
- mas[0][1] == -1 && mas[1][1] == -1 && mas[2][1] == -1 ||
- mas[0][2] == -1 && mas[1][2] == -1 && mas[2][2] == -1 ||
- mas[0][0] == -1 && mas[1][1] == -1 && mas[2][2] == -1 ||
- mas[2][0] == -1 && mas[1][1] == -1 && mas[0][2] == -1
- ) {
- Result(2);
- }
- else {
- p = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (mas[i][j] != 0) {
- p++;
- if (p == 9) {
- Result(0);
- }
- }
- }
- }
- }
- }
- void Game::Result(int k) {
- gameOver = true;
- if (k == 0)
- cout << endl << "Ничья";
- else if (k == 1)
- cout << endl << "Выиграл игрок №1";
- else if (k == 2)
- cout << endl << "Выиграл игрок №2";
- }
- int main() {
- setlocale(LC_ALL, "Rus");
- Game go;
- while (!gameOver) {
- go.clear();
- go.Draw();
- go.Cursor();
- go.update();
- }
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment