Окончательная неокончательная версия. Мэйн расшит на отдельные файлы для классов. Дальше расшивать лень.

This commit is contained in:
2024-11-07 11:43:09 +05:00
parent ae50933cbb
commit e282072e93
7 changed files with 254 additions and 148 deletions

71
BoardClass/Board.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "Board.h"
//
//
//Функция графического вывода поля.
//Пустая клетка (со значением "е") выводится координатами, а занятая - своим знаком.
void Board::BoardShow() {
for (unint countlines = 0; countlines < LINES; ++countlines) {
for (unint countcolumns = 0; countcolumns < COLUMNS; ++countcolumns) {
if (board[countlines][countcolumns]=="e") {
cout << countlines << countcolumns << " ";
}
else {
cout << board[countlines][countcolumns] << " ";
}
}
cout << endl;
}
}
//Функция очистки поля. Вызывается в новой итерации игрового цикла.
void Board::BoardClr() {
for (unint countlines = 0; countlines < LINES; ++countlines) {
for (unint countcolumns = 0; countcolumns < COLUMNS; ++countcolumns) {
board[countlines][countcolumns] = "e";
}
}
}
//Функция проверки победы Х.
void Board::XWon() {
if (drawcounter>=4) {
if ((board[0][0])=="'X'") {
if ((board[0][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 Board::OWon() {
if (drawcounter>=4) {
if ((board[0][0])=="'O'") {
if ((board[0][1])=="'O'" and (board[0][2])=="'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;}
}
}
}
//Функция, считающая заполненные клетки.
void Board::Draw() {
drawcounter=0;
for (int countlines = 0; countlines < LINES; ++countlines) {
for (int countcolumns = 0; countcolumns < COLUMNS; ++countcolumns) {
if (board[countlines][countcolumns]!="e") {
drawcounter++;
}
}
}
}

40
BoardClass/Board.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#ifndef TICTACTOE_BOARD_H
#define TICTACTOE_BOARD_H
#include <iostream>
#include <string>
typedef unsigned int unint;
using namespace std;
//
const int LINES = 3, COLUMNS = 3;
//
//Класс, отвечающий за формирование и вывод поля
class Board {
public:
//
//ПЕРЕМЕННЫЕ
//Флаги победы первого и второго номеров, а также счётчик занятых клеток
bool Xwonflag=false;
bool Owonflag=false;
unint drawcounter=1;
//ИГРОВОЕ ПОЛЕ
//первое число LINES характеризует строку
//второе число COLUMNS характеризует столбик
string board[LINES][COLUMNS] = {
{"e", "e", "e"},
{"e", "e", "e"},
{"e", "e", "e"}
};
void BoardShow();
void BoardClr();
void XWon();
void OWon();
void Draw();
};
#endif //TICTACTOE_BOARD_H