fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Board {
  5.  
  6. private:
  7. char grid[3][3];
  8. public:
  9. Board();
  10. int makeMove(int xIn, int yIn,char playerTurnIn);
  11. void print();
  12.  
  13. };
  14.  
  15. /*default constructor which initializes an empty array with .*/
  16. Board::Board():
  17. grid{{'.','.','.'},{'.','.','.'},{'.','.','.'}}
  18. { }
  19.  
  20. /*declare this in the board class
  21.  
  22. make sure to add Board:: for makeMove and print functions*/
  23. int Board::makeMove(int xIn, int yIn,char playerTurnIn) {
  24. if (grid[xIn][yIn]=='.') {
  25. grid[xIn][yIn] = playerTurnIn;
  26. return true;
  27. }
  28. else {
  29. return false;
  30. }
  31. }
  32.  
  33. void Board::print() {
  34. std::cout<<" 0 1 2"<<std::endl;
  35. for (int row = 0; row < 3; row++) {
  36. std::cout<<row<<' ';
  37. for (int col = 0; col < 3; col++) {
  38. std::cout<<grid[row][col]<<' ';
  39. }
  40. std::cout<<std::endl;
  41.  
  42. }
  43. }
  44.  
  45. int main() {
  46. Board board1;
  47. board1.print();
  48. board1.makeMove(0,0,'x');
  49. board1.print();
  50. if(board1.makeMove(0,0,'x'))
  51. std::cout<<"true"<<std::endl;
  52. else
  53. std::cout<<"false"<<std::endl;
  54. std::cout<<"finished!"<<std::endl;
  55. return 0;
  56. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
 0 1 2
0 . . . 
1 . . . 
2 . . . 
 0 1 2
0 x . . 
1 . . . 
2 . . . 
false
finished!