Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <iostream>
- #include "XOBoard.h"
- int main()
- {
- XOBoard board1{ 4 };
- XOBoard board2 = board1;
- cout << board1 << std::endl;
- cout << "--------------------------" << std::endl;
- //board2 = 'X';
- cout << board2 << std::endl;
- //cout << board1 << endl; /* Shows an empty board, as above */
- // cout << board2 << endl; /* Shows a board with an X at top-left */
- }
- cell.cpp
- #pragma once
- #include "Cell.h"
- #include <iostream>
- Cell::Cell(char ch) {
- if (ch == 'X' || ch == 'O' || ch == '.') {
- this->ch = ch;
- }
- else {
- this->ch = '.';
- }
- }
- char Cell::getCellValue() const { return ch; }
- void Cell::setCellValue(char ch) {
- if (ch == 'X' || ch == 'O' || ch == '.') {
- this->ch = ch;
- }
- else {
- std::cout << "Error occurd!" << std::endl;
- }
- }
- cell.h
- #pragma once
- #include <iostream>
- class Cell {
- private:
- char ch;
- public:
- Cell(char ch = '.');
- char getCellValue() const;
- void setCellValue(char nch);
- friend std::ostream& operator<<(std::ostream& output, const Cell& Cell) {
- output << Cell.ch;
- return output;
- }
- char operator[](Cell cell) {
- return cell.getCellValue();
- }
- };
- XOBoard.h
- #pragma once
- #include "Cell.h"
- #include <iostream>
- using namespace std;
- class XOBoard {
- private:
- int n;
- Cell **Board;
- public:
- XOBoard(int n = 3);
- XOBoard(const XOBoard& another); // copy contructor
- XOBoard(XOBoard&& another); // move constructor
- ~XOBoard();
- /*const XOBoard& operator=(const char& ch) {
- if (ch == 'X' || ch == 'O' || ch == '.') {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- Board[i][j].setCellValue(ch);
- }
- }
- return *this;
- }
- else {
- cout << "Illegal char" << std::endl;
- }
- }*/
- XOBoard operator=(const XOBoard& another) {
- if (this != &another) {
- n = another.n;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- Board[i][j] = another.Board[i][j]; // take values from original board into the new board
- }
- }
- }
- return *this;
- }
- friend ostream& operator<<(ostream& output, const XOBoard& XOBoard) {
- for (int i = 0; i < XOBoard.n; i++) {
- for (int j = 0; j < XOBoard.n; j++) {
- output << XOBoard.Board[i][j].getCellValue();
- }
- output << endl;
- }
- return output;
- }
- Cell& operator[](std::pair<int, int> pos) {
- if (pos.first >= 0 && pos.first < n || pos.second >= 0 && pos.second < n) {
- return Board[pos.first][pos.second];
- }
- else {
- cout << "Illegal[]" << std::endl;
- }
- }
- };
- xoboard.cpp
- #pragma once
- #include "Cell.h"
- #include "XOBoard.h"
- #include <iostream>
- XOBoard::XOBoard(int n) {
- this->n = (n >= 3) ? n : 3;
- Board = new Cell*[n];
- for (int i = 0; i < n; i++) {
- Board[i] = new Cell[n];
- }
- }
- XOBoard::XOBoard(const XOBoard& another) { // copy constructor
- int n = another.n;
- Board = new Cell*[n];
- for (int i = 0; i < n; i++) {
- Board[i] = new Cell[n];
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- Board[i][j].setCellValue(another.Board[i][j].getCellValue());
- }
- }
- }
- XOBoard::XOBoard(XOBoard&& another) { // move constructor
- int n = another.n;
- Board = new Cell*[n];
- Board = another.Board;
- another.Board = nullptr;
- another.n = 0;
- }
- XOBoard::~XOBoard() { // deconstructor
- for (int i = 0; i < n; i++) {
- delete[] Board[i];
- }
- delete[] Board;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement