UPD: Теперь алгоритм играбелен, но против тупого рандомайзера. Неокончательная версия.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -37,3 +37,4 @@
|
|||||||
/.idea/misc.xml
|
/.idea/misc.xml
|
||||||
/.idea/modules.xml
|
/.idea/modules.xml
|
||||||
/.idea/TicTacToe.iml
|
/.idea/TicTacToe.iml
|
||||||
|
/.idea/vcs.xml
|
||||||
|
|||||||
158
main.cpp
158
main.cpp
@ -3,8 +3,8 @@
|
|||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <windows.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
typedef unsigned int unint;
|
typedef unsigned int unint;
|
||||||
@ -16,12 +16,12 @@ int main() {
|
|||||||
//МЭЙН
|
//МЭЙН
|
||||||
const int LINES = 3, COLUMNS = 3;
|
const int LINES = 3, COLUMNS = 3;
|
||||||
bool newgame=true, firstnumber;
|
bool newgame=true, firstnumber;
|
||||||
int turn, humanwin=0, botwin=0;
|
int turn, humanwin=0, botwin=0, draw;
|
||||||
string wannaplay="y";
|
string wannaplay="y";
|
||||||
const string INCORRECT="INCORRECT VALUE, TRY AGAIN: ", ENDGAME="\nGame says: ''The game ends here.''";
|
const string INCORRECT="INCORRECT VALUE, TRY AGAIN: ", ENDGAME="\nGame says: ''The game ends here.''";
|
||||||
const string XWON="Game says: ''X wins!''", OWON="O wins!", DRAW="Game says: ''Nobody wins - DRAW!''";
|
const string XWON="Game says: ''X wins!''", OWON="Game says: ''O wins!''", DRAW="Game says: ''Nobody wins - DRAW!''";
|
||||||
const string BOTLOST="Bot says: ''Shee u beat me, I'm worry.''", HUMANLOST="Bot says: ''nahahaha lamer, look how I beat u!''";
|
const string BOTLOST="Bot says: ''Shee u beat me, I'm worry.''", HUMANLOST="Bot says: ''nahahaha lamer, look how I beat u!''";
|
||||||
const string SEP(15, '-');
|
const string SEP(40, '-');
|
||||||
const string BOTTHINK="Bot says: ''Wait, I'm thinking.''";
|
const string BOTTHINK="Bot says: ''Wait, I'm thinking.''";
|
||||||
//
|
//
|
||||||
//КЛАССЫ
|
//КЛАССЫ
|
||||||
@ -30,6 +30,7 @@ int main() {
|
|||||||
public:
|
public:
|
||||||
bool Xwonflag=false;
|
bool Xwonflag=false;
|
||||||
bool Owonflag=false;
|
bool Owonflag=false;
|
||||||
|
int drawcounter=1;
|
||||||
//ИГРОВОЕ ПОЛЕ
|
//ИГРОВОЕ ПОЛЕ
|
||||||
//первое число LINES характеризует строку
|
//первое число LINES характеризует строку
|
||||||
//второе число COLUMNS характеризует столбик
|
//второе число COLUMNS характеризует столбик
|
||||||
@ -53,40 +54,63 @@ int main() {
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void XWon() {
|
void BoardClr() {
|
||||||
if ((board[0][0])=="'X'") {
|
for (int countlines = 0; countlines < LINES; ++countlines) {
|
||||||
if ((board[0][1])=="'X'" and (board[0][2])=="'X'") {Xwonflag=true;}
|
for (int countcolumns = 0; countcolumns < COLUMNS; ++countcolumns) {
|
||||||
else if ((board[1][1])=="'X'" and (board[2][2])=="'X'") {Xwonflag=true;}
|
board[countlines][countcolumns] = "e";
|
||||||
else if ((board[1][0])=="'X'" and (board[2][0])=="'X'") {Xwonflag=true;}
|
}
|
||||||
}
|
}
|
||||||
if (((board[1][0])=="'X'") and ((board[1][1])=="'X'") and ((board[1][2]))=="'X'") {Xwonflag=true;}
|
}
|
||||||
if (((board[0][1])=="'X'") and ((board[1][1])=="'X'") and ((board[2][1]))=="'X'") {Xwonflag=true;}
|
void XWon() {
|
||||||
if (((board[0][2])=="'X'") and ((board[1][2])=="'X'") and ((board[2][2]))=="'X'") {Xwonflag=true;}
|
if (drawcounter>=4) {
|
||||||
if ((board[2][0])=="'X'") {
|
if ((board[0][0])=="'X'") {
|
||||||
if ((board[2][1])=="'X'" and (board[2][2])=="'X'") {Xwonflag=true;}
|
if ((board[0][1])=="'X'" and (board[0][2])=="'X'") {Xwonflag=true;}
|
||||||
else if ((board[1][1])=="'X'" and (board[0][2])=="'X'") {Xwonflag=true;}
|
else if ((board[1][1])=="'X'" and (board[2][2])=="'X'") {Xwonflag=true;}
|
||||||
|
else if ((board[1][0])=="'X'" and (board[2][0])=="'X'") {Xwonflag=true;}
|
||||||
|
}
|
||||||
|
if (((board[1][0])=="'X'") and ((board[1][1])=="'X'") and ((board[1][2]))=="'X'") {Xwonflag=true;}
|
||||||
|
if (((board[0][1])=="'X'") and ((board[1][1])=="'X'") and ((board[2][1]))=="'X'") {Xwonflag=true;}
|
||||||
|
if (((board[0][2])=="'X'") and ((board[1][2])=="'X'") and ((board[2][2]))=="'X'") {Xwonflag=true;}
|
||||||
|
if ((board[2][0])=="'X'") {
|
||||||
|
if ((board[2][1])=="'X'" and (board[2][2])=="'X'") {Xwonflag=true;}
|
||||||
|
else if ((board[1][1])=="'X'" and (board[0][2])=="'X'") {Xwonflag=true;}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void OWon() {
|
void OWon() {
|
||||||
if ((board[0][0])=="'O'") {
|
if (drawcounter>=4) {
|
||||||
if ((board[0][1])=="'O'" and (board[0][2])=="'O'") {Owonflag=true;}
|
if ((board[0][0])=="'O'") {
|
||||||
else if ((board[1][1])=="'O'" and (board[2][2])=="'O'") {Owonflag=true;}
|
if ((board[0][1])=="'O'" and (board[0][2])=="'O'") {Owonflag=true;}
|
||||||
else if ((board[1][0])=="'O'" and (board[2][0])=="'O'") {Owonflag=true;}
|
else if ((board[1][1])=="'O'" and (board[2][2])=="'O'") {Owonflag=true;}
|
||||||
|
else if ((board[1][0])=="'O'" and (board[2][0])=="'O'") {Owonflag=true;}
|
||||||
|
}
|
||||||
|
if (((board[1][0])=="'O'") and ((board[1][1])=="'O'") and ((board[1][2]))=="'O'") {Owonflag=true;}
|
||||||
|
if (((board[0][1])=="'O'") and ((board[1][1])=="'O'") and ((board[2][1]))=="'O'") {Owonflag=true;}
|
||||||
|
if (((board[0][2])=="'O'") and ((board[1][2])=="'O'") and ((board[2][2]))=="'O'") {Owonflag=true;}
|
||||||
|
if ((board[2][0])=="'O'") {
|
||||||
|
if ((board[2][1])=="'O'" and (board[2][2])=="'O'") {Owonflag=true;}
|
||||||
|
else if ((board[1][1])=="'O'" and (board[0][2])=="'O'") {Owonflag=true;}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (((board[1][0])=="'O'") and ((board[1][1])=="'O'") and ((board[1][2]))=="'O'") {Owonflag=true;}
|
}
|
||||||
if (((board[0][1])=="'O'") and ((board[1][1])=="'O'") and ((board[2][1]))=="'O'") {Owonflag=true;}
|
void Draw() {
|
||||||
if (((board[0][2])=="'O'") and ((board[1][2])=="'O'") and ((board[2][2]))=="'O'") {Owonflag=true;}
|
drawcounter=1;
|
||||||
if ((board[2][0])=="'O'") {
|
for (int countlines = 0; countlines < LINES; ++countlines) {
|
||||||
if ((board[2][1])=="'O'" and (board[2][2])=="'O'") {Owonflag=true;}
|
for (int countcolumns = 0; countcolumns < COLUMNS; ++countcolumns) {
|
||||||
else if ((board[1][1])=="'O'" and (board[0][2])=="'O'") {Owonflag=true;}
|
if (board[countlines][countcolumns]!="e") {
|
||||||
|
drawcounter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Board BoardObj;
|
||||||
//Ходы игрока и бота
|
//Ходы игрока и бота
|
||||||
class Turn {
|
class Turn {
|
||||||
public:
|
public:
|
||||||
int turnLINEint, turnCOLint;
|
int turnLINEint, turnCOLint;
|
||||||
string Humancount, turnline, turncol;
|
string Humancount, turnline, turncol;
|
||||||
|
Board BoardObj;
|
||||||
void HumanTurn() {
|
void HumanTurn() {
|
||||||
//ХОД ИГРОКА
|
//ХОД ИГРОКА
|
||||||
//алгоритм на вычленение первого и второго значений координат
|
//алгоритм на вычленение первого и второго значений координат
|
||||||
@ -106,15 +130,15 @@ int main() {
|
|||||||
//рандомит второе значение (столбец)
|
//рандомит второе значение (столбец)
|
||||||
srand((time(0)));
|
srand((time(0)));
|
||||||
turnLINEint=(rand()%3);
|
turnLINEint=(rand()%3);
|
||||||
Sleep((rand()/3));
|
chrono::milliseconds timespan((rand()/9));
|
||||||
srand((time(0)));
|
this_thread::sleep_for(timespan);
|
||||||
turnCOLint=(rand()%3);
|
mt19937 rng;
|
||||||
//cout << "lineBOT: " << turnLINEint << " colBOT: " << turnCOLint << endl;
|
rng.seed(time(nullptr));
|
||||||
|
turnCOLint=(rng()%3);
|
||||||
|
cout << "Bot says:''I've occupied this cell:" << turnLINEint << turnCOLint << "''" << endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//Объявление объектов
|
|
||||||
Turn Game;
|
Turn Game;
|
||||||
Board BoardObj;
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
@ -142,38 +166,51 @@ int main() {
|
|||||||
//Также снимает флаг newgame, который возвращается в конце игры.
|
//Также снимает флаг newgame, который возвращается в конце игры.
|
||||||
cout << "Bot asks: Wanna be an X? (1/0):";
|
cout << "Bot asks: Wanna be an X? (1/0):";
|
||||||
cin >> firstnumber;
|
cin >> firstnumber;
|
||||||
if (firstnumber) {cout << "Bot says: ''Well, u'll be an X.''" << endl;}
|
if (firstnumber) {cout << "Bot says: ''Well, u'll be an X.''" << endl; BoardObj.BoardShow();}
|
||||||
else {cout << "Bot says: ''OK, I'll be an X.''\n" << endl;}
|
else {cout << "Bot says: ''OK, I'll be an X.''" << endl;}
|
||||||
turn=1;
|
turn=1;
|
||||||
newgame=false;
|
newgame=false;
|
||||||
}
|
}
|
||||||
cout << SEP << endl;
|
cout << SEP << endl;
|
||||||
cout << "Game says: ''Now it's " << turn << " turn.''" << endl;
|
//cout << "Game says: ''Now it's " << turn << " turn.''" << endl;
|
||||||
turn++;
|
//turn++;
|
||||||
Game.turnLINEint=3;
|
Game.turnLINEint=3;
|
||||||
Game.turnCOLint=3;
|
Game.turnCOLint=3;
|
||||||
//Если первым номером (Х) выступает игрок
|
//Если первым номером (Х) выступает игрок
|
||||||
if (firstnumber) {
|
while (firstnumber) {
|
||||||
cout << "Enter ur coords:";
|
cout << "HUMAN TURN!\n" << "Enter ur coords:";
|
||||||
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {
|
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {
|
||||||
Game.HumanTurn();
|
Game.HumanTurn();
|
||||||
if ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {cout << INCORRECT;}
|
if ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {cout << INCORRECT;}
|
||||||
}
|
}
|
||||||
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'X'";
|
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'X'";
|
||||||
|
BoardObj.BoardShow();
|
||||||
|
cout << SEP << endl;
|
||||||
|
BoardObj.XWon();
|
||||||
|
BoardObj.Draw();
|
||||||
|
if ((BoardObj.Xwonflag) or (BoardObj.drawcounter==9)) {break;}
|
||||||
Game.turnLINEint=3;
|
Game.turnLINEint=3;
|
||||||
Game.turnCOLint=3;
|
Game.turnCOLint=3;
|
||||||
cout << BOTTHINK << endl;
|
cout << "BOT TURN!\n" << BOTTHINK << endl;
|
||||||
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {Game.BotTurn();}
|
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {Game.BotTurn();}
|
||||||
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'O'";
|
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'O'";
|
||||||
BoardObj.BoardShow();
|
BoardObj.BoardShow();
|
||||||
|
cout << SEP << endl;
|
||||||
|
BoardObj.OWon();
|
||||||
|
BoardObj.Draw();
|
||||||
|
if ((BoardObj.Owonflag) or (BoardObj.drawcounter==9)) {break;}
|
||||||
}
|
}
|
||||||
//Если игрок выступает вторым номером (O)
|
//Если игрок выступает вторым номером (O)
|
||||||
else {
|
while (!firstnumber) {
|
||||||
cout << BOTTHINK << endl;
|
cout << "BOT TURN!\n" << BOTTHINK << endl;
|
||||||
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {Game.BotTurn();}
|
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {Game.BotTurn();}
|
||||||
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'X'";
|
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'X'";
|
||||||
BoardObj.BoardShow();
|
BoardObj.BoardShow();
|
||||||
cout << "Enter ur coords:";
|
cout << SEP << endl;
|
||||||
|
BoardObj.XWon();
|
||||||
|
BoardObj.Draw();
|
||||||
|
if ((BoardObj.Xwonflag) or (BoardObj.drawcounter==9)) {break;}
|
||||||
|
cout << "HUMAN TURN!\n" << "Enter ur coords:";
|
||||||
Game.turnLINEint=3;
|
Game.turnLINEint=3;
|
||||||
Game.turnCOLint=3;
|
Game.turnCOLint=3;
|
||||||
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {
|
while ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {
|
||||||
@ -181,24 +218,29 @@ int main() {
|
|||||||
if ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {cout << INCORRECT;}
|
if ((BoardObj.board[Game.turnLINEint][Game.turnCOLint]!="e")) {cout << INCORRECT;}
|
||||||
}
|
}
|
||||||
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'O'";
|
BoardObj.board[Game.turnLINEint][Game.turnCOLint] = "'O'";
|
||||||
|
BoardObj.BoardShow();
|
||||||
|
cout << SEP << endl;
|
||||||
|
BoardObj.OWon();
|
||||||
|
BoardObj.Draw();
|
||||||
|
if ((BoardObj.Owonflag) or (BoardObj.drawcounter==9)) {break;}
|
||||||
}
|
}
|
||||||
BoardObj.XWon();
|
cout << ENDGAME << endl;
|
||||||
BoardObj.OWon();
|
newgame=true;
|
||||||
if ((BoardObj.Xwonflag) or (BoardObj.Owonflag) or (turn>=9)) {
|
if (BoardObj.Xwonflag) {cout << XWON << endl;}
|
||||||
cout << ENDGAME << endl;
|
else if (BoardObj.Owonflag) {cout << OWON << endl;}
|
||||||
newgame=true;
|
else if ((BoardObj.drawcounter==9) and (!BoardObj.Xwonflag) and (!BoardObj.Owonflag)) {cout << DRAW << endl;}
|
||||||
if (BoardObj.Xwonflag) {cout << XWON << endl;}
|
|
||||||
else if (BoardObj.Owonflag) {cout << OWON << endl;}
|
|
||||||
else if ((turn>=9) and (!BoardObj.Xwonflag) and (!BoardObj.Owonflag)) {cout << DRAW << endl;}
|
|
||||||
|
|
||||||
if ((firstnumber) and (BoardObj.Xwonflag)) {cout << BOTLOST << endl; humanwin=+1;}
|
if ((firstnumber) and (BoardObj.Xwonflag)) {cout << BOTLOST << endl; humanwin++;}
|
||||||
else if ((!firstnumber) and (BoardObj.Owonflag)) {cout << BOTLOST << endl; humanwin=+1;}
|
else if ((!firstnumber) and (BoardObj.Owonflag)) {cout << BOTLOST << endl; humanwin++;}
|
||||||
else {cout << HUMANLOST << endl; botwin=+1;}
|
else if (BoardObj.drawcounter==9) {cout << "Bot says: ''yeah dat was fairplay.''" << endl;}
|
||||||
cout << SEP << endl;
|
else {cout << HUMANLOST << endl; botwin++;}
|
||||||
cout << SEP << endl;
|
BoardObj.BoardClr();
|
||||||
cout << "Wanna play another time?(y/n): ";
|
BoardObj.Xwonflag=false;
|
||||||
cin >> wannaplay;
|
BoardObj.Owonflag=false;
|
||||||
}
|
cout << SEP << endl;
|
||||||
|
cout << SEP << endl;
|
||||||
|
cout << "Wanna play another time?(y/n):";
|
||||||
|
cin >> wannaplay;
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
//СТАТИСТИКА ПРИ ЗАВЕРШЕНИИ ИГРЫ
|
//СТАТИСТИКА ПРИ ЗАВЕРШЕНИИ ИГРЫ
|
||||||
|
|||||||
Reference in New Issue
Block a user