Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. main.cpp
  2.  
  3. #include <iostream>
  4. #include "XOBoard.h"
  5. int main()
  6. {
  7. XOBoard board1{ 4 };
  8. XOBoard board2 = board1;
  9. cout << board1 << std::endl;
  10. cout << "--------------------------" << std::endl;
  11.  
  12. //board2 = 'X';
  13. cout << board2 << std::endl;
  14. //cout << board1 << endl; /* Shows an empty board, as above */
  15. // cout << board2 << endl; /* Shows a board with an X at top-left */
  16. }
  17.  
  18.  
  19. cell.cpp
  20.  
  21. #pragma once
  22. #include "Cell.h"
  23. #include <iostream>
  24.  
  25. Cell::Cell(char ch) {
  26. if (ch == 'X' || ch == 'O' || ch == '.') {
  27. this->ch = ch;
  28. }
  29. else {
  30. this->ch = '.';
  31. }
  32. }
  33. char Cell::getCellValue() const { return ch; }
  34.  
  35. void Cell::setCellValue(char ch) {
  36. if (ch == 'X' || ch == 'O' || ch == '.') {
  37. this->ch = ch;
  38. }
  39. else {
  40. std::cout << "Error occurd!" << std::endl;
  41. }
  42. }
  43.  
  44.  
  45. cell.h
  46.  
  47. #pragma once
  48. #include <iostream>
  49. class Cell {
  50. private:
  51. char ch;
  52. public:
  53. Cell(char ch = '.');
  54. char getCellValue() const;
  55. void setCellValue(char nch);
  56. friend std::ostream& operator<<(std::ostream& output, const Cell& Cell) {
  57. output << Cell.ch;
  58. return output;
  59. }
  60. char operator[](Cell cell) {
  61. return cell.getCellValue();
  62. }
  63.  
  64. };
  65. XOBoard.h
  66.  
  67. #pragma once
  68. #include "Cell.h"
  69. #include <iostream>
  70. using namespace std;
  71.  
  72.  
  73. class XOBoard {
  74. private:
  75. int n;
  76. Cell **Board;
  77. public:
  78. XOBoard(int n = 3);
  79. XOBoard(const XOBoard& another); // copy contructor
  80. XOBoard(XOBoard&& another); // move constructor
  81. ~XOBoard();
  82. /*const XOBoard& operator=(const char& ch) {
  83. if (ch == 'X' || ch == 'O' || ch == '.') {
  84. for (int i = 0; i < n; i++) {
  85. for (int j = 0; j < n; j++) {
  86. Board[i][j].setCellValue(ch);
  87. }
  88. }
  89. return *this;
  90. }
  91. else {
  92. cout << "Illegal char" << std::endl;
  93. }
  94. }*/
  95. XOBoard operator=(const XOBoard& another) {
  96. if (this != &another) {
  97. n = another.n;
  98. for (int i = 0; i < n; i++) {
  99. for (int j = 0; j < n; j++) {
  100. Board[i][j] = another.Board[i][j]; // take values from original board into the new board
  101. }
  102. }
  103. }
  104. return *this;
  105. }
  106. friend ostream& operator<<(ostream& output, const XOBoard& XOBoard) {
  107. for (int i = 0; i < XOBoard.n; i++) {
  108. for (int j = 0; j < XOBoard.n; j++) {
  109. output << XOBoard.Board[i][j].getCellValue();
  110. }
  111. output << endl;
  112. }
  113. return output;
  114. }
  115. Cell& operator[](std::pair<int, int> pos) {
  116. if (pos.first >= 0 && pos.first < n || pos.second >= 0 && pos.second < n) {
  117. return Board[pos.first][pos.second];
  118. }
  119. else {
  120. cout << "Illegal[]" << std::endl;
  121. }
  122. }
  123. };
  124.  
  125.  
  126. xoboard.cpp
  127.  
  128. #pragma once
  129. #include "Cell.h"
  130. #include "XOBoard.h"
  131. #include <iostream>
  132.  
  133. XOBoard::XOBoard(int n) {
  134. this->n = (n >= 3) ? n : 3;
  135. Board = new Cell*[n];
  136. for (int i = 0; i < n; i++) {
  137. Board[i] = new Cell[n];
  138. }
  139. }
  140. XOBoard::XOBoard(const XOBoard& another) { // copy constructor
  141. int n = another.n;
  142. Board = new Cell*[n];
  143. for (int i = 0; i < n; i++) {
  144. Board[i] = new Cell[n];
  145. }
  146. for (int i = 0; i < n; i++) {
  147. for (int j = 0; j < n; j++) {
  148. Board[i][j].setCellValue(another.Board[i][j].getCellValue());
  149. }
  150. }
  151. }
  152. XOBoard::XOBoard(XOBoard&& another) { // move constructor
  153. int n = another.n;
  154. Board = new Cell*[n];
  155. Board = another.Board;
  156. another.Board = nullptr;
  157. another.n = 0;
  158. }
  159. XOBoard::~XOBoard() { // deconstructor
  160. for (int i = 0; i < n; i++) {
  161. delete[] Board[i];
  162. }
  163. delete[] Board;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement