вот улучшенный код
enter code here
#include <iostream>
#include <sstream>
#include <string>
#include <random>
#include <vector>
#include <iomanip>
enum class ending
{
win ,
lose ,
tie
};
enum class States
{
Human ,
robot ,
blank
};
enum class command
{
deletE ,
move
};
class BOARD {
public:
std::vector<States> Cells;
BOARD(States type , int x , int y);
bool wincon(const std::vector<States>& rboard, int coordx, int coordy);
};
//functions
bool legal(const std::vector<States>& Cells, int choice);
void printer(int x, int y, std::vector<States>& Cells, char skin = 'K');
void instructions();
void newpage();
ending mainloop(int realx, int realy , char skin, std::vector<States>& Cells, BOARD board);
void moving(int choice, std::vector<States>& Cells, int cols, command decide , States player , int limit);
int Ai(std::vector<States>& Cells, int coordx, int coordy , BOARD board );
int main()
{
srand(static_cast<unsigned int>(time(0)));
//elements
int realx, realy;
char skin;
//asking player for input and intro
std::cout << "tt 4-CONNNECT nt________________________n";
std::cout << "enter x cordinate please:";
std::cin >> realx;
std::cout << "enter y cordinate please:";
std::cin >> realy;
BOARD board(States::Human, realx, realy);
printer(realx, realy, board.Cells);
instructions();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::cin.get();
newpage();
//get player skin (letter) and pass it into game loop
std::cout << "enter ur skin please:";
std::cin >> skin;
newpage();
//test
mainloop(realx, realy, skin, board.Cells, board);
}
//classes function defintion
BOARD::BOARD(States type , int x , int y)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
Cells.push_back(States::blank);
}
}
}
//function defintion
void printer(int x, int y, std::vector<States>& Cells , char skin )
{
int boardnumber = 0;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
switch (Cells[boardnumber])
{
case States::Human:
std::cout << skin;
std::cout << std::setw(2);
break;
case States::robot:
std::cout << "B";
std::cout << std::setw(2);
break;
case States::blank:
std::cout << boardnumber+1;
if (boardnumber+1 < 10) {
std::cout << std::setw(2);
}
break;
default:
break;
}
std::cout << "|";
boardnumber++;
}
std::cout << "n";
}
}
void instructions()
{
std::cout << "ninstructionsn_________________n";
std::cout << "n";
std::cout << "just search google for instructions for this game :)n";
}
void newpage()
{
for (int i = 0; i < 50; i++)
{
std::cout << "n";
}
}
ending mainloop(int realx, int realy , char skin, std::vector<States>& Cells, BOARD board)
{
//elements
int lobbyposition;
int blank = 1;
int limit = realx * realy;
int userchoice , aichoice;
//first or last
std::cout << "start first(yes = 0 or no = 1):";
std::cin >> lobbyposition;
newpage();
//loob
while (blank != limit) {
if (lobbyposition % 2 == 0) {
printer(realx, realy, Cells, skin);
std::cout << "enter the position:";
std::cin >> userchoice;
moving(userchoice , Cells, realy, command::move, States::Human , limit);
newpage();
printer(realx, realy, Cells, skin);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::cin.get();
newpage();
blank++;
lobbyposition++;
if (board.wincon(Cells, realx, realy))
return ending::win;
}
else if (lobbyposition % 2 != 0) {
aichoice = Ai(Cells, realx, realy, board );
printer(realx, realy, Cells, skin);
std::cout << "nmy result is :" << aichoice;
newpage();
printer(realx, realy, Cells, skin);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::cin.get();
newpage();
blank++;
lobbyposition++;
if (board.wincon(Cells, realx, realy))
return ending::lose;
}
}
}
bool BOARD::wincon(const std::vector<States>& rboard, int coordx, int coordy)
{
bool check = false;
int equ1, equ2 , counter;
counter = 0;
States startvalue = rboard[counter];
for (int x = 0; x < coordx; x++)
{
for (int y = 0; y < coordy; y++)
{
startvalue = rboard[counter];
int possiblex[4][2] = { {0 , 1} , {1 , 0} , {1 , -1} , {1 , 1} };
if (startvalue != States::blank) {
//checkloop
for (int j = 0; j < 4; j++)
{
check = true;
int i = 0;
for (int b = 1; b < 4; ++b) {
equ1 = (x + (b * possiblex[j][i]));
equ2 = (y + (b * possiblex[j][i + 1]));
if (equ1 < 0 || equ1 == coordx) {
check = false;
break;
}
else if (equ2 < 0 || equ2 == coordy) {
check = false;
break;
}
else
{
if (rboard[equ2 + equ1 * coordy] != startvalue) {
check = false;
}
}
}
if (check == true) {
return check;
}
}
}
counter++;
}
}
return check;
}
bool legal(const std::vector<States>& Cells, int choice)
{
int counter = 1;
for (States loop : Cells){
if (counter == choice)
{
if (loop != States::blank) {
return false;
}
else {
return true;
}
}
counter++;
}
return false;
}
void moving(int choice, std::vector<States>& Cells, int cols, command decide, States player , int limit)
{
int position = choice+cols;
while (legal(Cells, position) == true)
{
choice += cols;
position += cols;
}
choice -= 1;
if (decide == command::move) {
Cells[choice] = player;
}
else if ( decide == command::deletE)
{
if (legal(Cells, (choice+1)) != true){
Cells[choice] = States::blank;
}
else {
Cells[choice+cols] = States::blank;
}
}
}
int Ai(std::vector<States>& Cells,int coordx, int coordy , BOARD board )
{
//checkwin and stop win
int limit = coordx * coordy;
int counter = 1;
while (counter < limit) {
if (legal(Cells, counter)) {
//check win
moving(counter, Cells, coordy, command::move, States::robot, limit);
if (board.wincon(Cells, coordx, coordy) == true) {
return counter;
}
moving(counter, Cells, coordy, command::deletE, States::robot, limit);
}
counter++;
}
counter = 1;
while (counter < limit) {
if (legal(Cells, counter)) {
//check enemy
moving(counter, Cells, coordy, command::move, States::Human, limit);
if (board.wincon(Cells, coordx, coordy) == true) {
moving(counter, Cells, coordy, command::deletE, States::Human, limit);
moving(counter, Cells, coordy, command::move, States::robot, limit);
return counter;
}
moving(counter, Cells, coordy, command::deletE, States::Human, limit);
}
counter++;
}
//random number generatror (sry was lazy to updata to c++ 11, its too complicated for my brain)
for (int i = 0; i < 1000; i++)
{
counter = (rand() % limit)+1;
if (legal(Cells , counter)){
moving(counter, Cells, coordy, command::move, States::robot , limit);
return counter + 1;
}
}
}
это улучшенная версия из моего первого поста о 4 соединениях, я добавил классы и перечисления и никаких многомерных векторов, как мне кажется.
Я также сделал код лучше и без детских имен.