Skip to content

Instantly share code, notes, and snippets.

@androidovshchik
Last active September 29, 2017 07:22
Show Gist options
  • Save androidovshchik/f02d5ea48db4789810e9debb5b94368d to your computer and use it in GitHub Desktop.
Save androidovshchik/f02d5ea48db4789810e9debb5b94368d to your computer and use it in GitHub Desktop.
Крестики-нолики
#include <iostream>
#include <cstdio>
#include <windows.h>
using namespace std;
static int a[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int extremal = 0, player;
char* Rus(const char* text);
void win(int d) {
int tr, kol1 = 0, kol2 = 0;
if (d == 10) {
cout << Rus("-----Победила ДРУЖБА!!!-----\n");
extremal = -1;
} else {
for (int wer = 0; wer < 3; wer++) {
kol1 = 0;
kol2 = 0;
for (int yu = 0; yu < 3; yu++) {
if (a[wer][yu] == 1) kol1++;
if (a[wer][yu] == 2) kol2++;
tr = yu;
}
if (kol1 == 3) extremal = 1;
if (kol2 == 3) extremal = 2;
}
/////////////////////////////
for (int wer = 0; wer < 3; wer++) {
kol1 = 0;
kol2 = 0;
for (int yu = 0; yu < 3; yu++) {
if (a[yu][wer] == 1) kol1++;
if (a[yu][wer] == 2) kol2++;
}
if (kol1 == 3) extremal = 1;
if (kol2 == 3) extremal = 2;
}
/////////////////////////////
if (a[0][0] == 1 && a[1][1] == 1 && a[2][2] == 1) extremal = 1;
if (a[0][0] == 2 && a[1][1] == 2 && a[2][2] == 2) extremal = 2;
if (a[0][2] == 1 && a[1][1] == 1 && a[2][0] == 1) extremal = 1;
if (a[0][2] == 2 && a[1][1] == 2 && a[2][0] == 2) extremal = 2;
/////////////////////////////
if (extremal == player) {
cout << Rus("-----Вы победили!!!-----\n");
extremal = -1;
}
if (extremal != player && extremal > 0) {
cout << Rus("-----Компьютер победил!!!-----\n");
extremal = -1;
}
}
}
int main(char*) {
int g, h, s, d = 1, time, move;
cout << Rus("////////// Крестики-нолики //////////\n");
cout << Rus(
"Выберите себе номер игрока:\nЕсли хотите быть первым - нажмите 1,\n "
"иначе - 2:\n");
cin >> player;
if (player == 1) {
cout << Rus("Вы - первый игрок!\n");
time = 2;
s = 2;
} else if (player == 2) {
cout << Rus("Вы - второй игрок!\n");
time = 1;
s = 1;
} else
abort();
cout << Rus("Делайте ход, нажимая соответствующий номер:\n");
cout << Rus("-------------------------------------\n");
cout << Rus("------ 1 ----- 2 ---- 3 -------\n");
cout << Rus("-------------------------------------\n");
cout << Rus("------ 4 ----- 5 ---- 6 -------\n");
cout << Rus("-------------------------------------\n");
cout << Rus("------ 7 ----- 8 ---- 9 -------\n");
cout << Rus("-------------------------------------\n");
while (d < 10) {
if (time % 2 == 0) {
cout << Rus("Делайте ход:\n");
cin >> move;
switch (move) {
case 1:
if (a[0][0] != 0) abort();
a[0][0] = player;
break;
case 2:
if (a[0][1] != 0) abort();
a[0][1] = player;
break;
case 3:
if (a[0][2] != 0) abort();
a[0][2] = player;
break;
case 4:
if (a[1][0] != 0) abort();
a[1][0] = player;
break;
case 5:
if (a[1][1] != 0) abort();
a[1][1] = player;
break;
case 6:
if (a[1][2] != 0) abort();
a[1][2] = player;
break;
case 7:
if (a[2][0] != 0) abort();
a[2][0] = player;
break;
case 8:
if (a[2][1] != 0) abort();
a[2][1] = player;
break;
case 9:
if (a[2][2] != 0) abort();
a[2][2] = player;
break;
default:
abort();
}
win(d);
if (extremal == -1) break;
cout << Rus("*************************************\n");
for (int wer = 0; wer < 3; wer++) {
for (int yu = 0; yu < 3; yu++) cout << a[wer][yu] << " ";
cout << endl;
}
cout << Rus("*************************************\n");
} else {
if (extremal != -1) cout << Rus("Компьютер делает ход...\n");
int z, w, sos, off;
sos = 0;
z = 0;
off = 0;
while (off < 1) {
do {
w = 0;
if (a[z][w] * a[z][w] + a[z][w + 1] * a[z][w + 1] +
a[z][w + 2] * a[z][w + 2] ==
2 ||
a[z][w] + a[z][w + 1] + a[z][w + 2] == 4) {
w = 0;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
z++;
} else {
z++;
}
} while (z < 3);
w = 0;
z = 0;
if (sos > 0) break;
do {
w = 0;
if (a[w][z] * a[w][z] + a[w + 1][z] * a[w + 1][z] +
a[w + 2][z] * a[w + 2][z] ==
2 ||
a[w][z] + a[w + 1][z] + a[w + 2][z] == 4) {
w = 0;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 1;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
z++;
} else
z++;
} while (z < 3);
w = 0;
z = 0;
if (sos > 0) break;
{
if (a[z][w] * a[z][w] + a[z + 1][w + 1] * a[z + 1][w + 1] +
a[z + 2][w + 2] * a[z + 2][w + 2] ==
2 ||
a[z][w] + a[z + 1][w + 1] + a[z + 2][w + 2] == 4) {
w = 0;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 2;
z = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
}
}
w = 0;
z = 0;
if (sos > 0) break;
{
if (a[z][w + 2] * a[z][w + 2] + a[z + 1][w + 1] * a[z + 1][w + 1] +
a[z + 2][w] * a[z + 2][w] ==
2 ||
a[z][w + 2] + a[z + 1][w + 1] + a[z + 2][w] == 4) {
w = 0;
if (a[z][w + 2] == 0) {
a[z][w + 2] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 0;
z = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
}
}
w = 0;
z = 0;
if (sos > 0) break;
off++;
}
off = 0;
z = 0;
switch (s) {
case 1: {
if (sos > 0) break;
do {
w = 0;
if (a[z][w] * a[z][w] + a[z][w + 1] * a[z][w + 1] +
a[z][w + 2] * a[z][w + 2] ==
1) {
w = 0;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
z++;
} else {
z++;
}
} while (z < 3);
w = 0;
z = 0;
if (sos > 0) break;
do {
w = 0;
if (a[w][z] * a[w][z] + a[w + 1][z] * a[w + 1][z] +
a[w + 2][z] * a[w + 2][z] ==
1) {
w = 0;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 1;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
z++;
} else
z++;
} while (z < 3);
if (sos > 0) break;
z = 0;
w = 0;
{
if (a[z][w] * a[z][w] + a[z + 1][w + 1] * a[z + 1][w + 1] +
a[z + 2][w + 2] * a[z + 2][w + 2] ==
1) {
w = 0;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 2;
z = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
}
}
w = 0;
z = 0;
if (sos > 0) break;
{
if (a[z][w + 2] * a[z][w + 2] + a[z + 1][w + 1] * a[z + 1][w + 1] +
a[z + 2][w] * a[z + 2][w] ==
1) {
w = 0;
if (a[z][w + 2] == 0) {
a[z][w + 2] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 0;
z = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
}
}
w = 0;
z = 0;
if (sos > 0) break;
}
case 2: {
if (sos > 0) break;
do {
w = 0;
if (a[z][w] * a[z][w] + a[z][w + 1] * a[z][w + 1] +
a[z][w + 2] * a[z][w + 2] ==
4) {
w = 0;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
z++;
} else {
z++;
}
} while (z < 3);
w = 0;
z = 0;
do {
w = 0;
if (sos > 0) break;
if (a[w][z] * a[w][z] + a[w + 1][z] * a[w + 1][z] +
a[w + 2][z] * a[w + 2][z] ==
4) {
w = 0;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 1;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
z++;
} else
z++;
} while (z < 3);
z = 0;
w = 0;
if (sos > 0) break;
{
if (a[z][w] * a[z][w] + a[z + 1][w + 1] * a[z + 1][w + 1] +
a[z + 2][w + 2] * a[z + 2][w + 2] ==
4) {
w = 0;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 2;
z = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
}
}
w = 0;
z = 0;
if (sos > 0) break;
{
if (a[z][w + 2] * a[z][w + 2] + a[z + 1][w + 1] * a[z + 1][w + 1] +
a[z + 2][w] * a[z + 2][w] ==
4) {
w = 0;
if (a[z][w + 2] == 0) {
a[z][w + 2] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 1;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
w = 0;
z = 2;
if (a[z][w] == 0) {
a[z][w] = s;
sos++;
if (sos > 0) break;
}
}
}
w = 0;
z = 0;
}
}
off = 0;
z = 0;
w = 0;
z = 0;
int q;
w = 0;
z = 0;
q = 0;
off = 0;
while (off < 1) {
if (sos > 0) break;
if (a[1][1] == 0) {
a[1][1] = s;
sos++;
} else {
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 0;
z = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 2;
z = 0;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 2;
z = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 2;
z = 1;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 0;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 0;
z = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 1;
z = 2;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
w = 0;
z = 1;
if (a[w][z] == 0) {
a[w][z] = s;
sos++;
if (sos > 0) break;
}
}
if (sos > 0) break;
off++;
}
cout << Rus("*************************************\n") << endl;
for (g = 0; g < 3; g++) {
for (h = 0; h < 3; h++) cout << a[g][h] << " ";
cout << endl;
}
cout << Rus("*************************************\n");
win(d);
if (extremal == -1) break;
}
d++;
time++;
}
if (extremal != -1) win(d);
return 0;
}
char bufRus[256];
char* Rus(const char* text) {
CharToOem(text, bufRus);
return bufRus;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment