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
|
#include <array>
using triplet = std::array<unsigned, 3>;
std::array<triplet, 8> triplets =
{
{
{0,1,2},
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{6,4,1}
}
};
/*
0 | 1 | 2
--+---+--
3 | 4 | 5
--+---+--
6 | 7 | 8
*/
bool winner(const triplet& trip, char player)
{
unsigned player_count = 0;
unsigned dash_count = 0;
unsigned dash_pos = 0;
for (auto pos : trip)
{
char ch = game[pos / 3][pos % 3];
if (ch == player)
++player_count;
else if (ch == '-')
{
++dash_count;
dash_pos = pos;
}
}
if (player_count == 2 && dash_count == 1)
{
game[dash_pos / 3][dash_pos % 3] = player;
player_count = 3;
}
return player_count == 3;
}
void check(char player = 'O')
{
for (std::size_t i = 0; i < triplets.size(); ++i)
{
if (winner(triplets[i], player))
{
std::cout << "You wIn!!!\n";
go = 0;
break;
}
}
}
|