Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int D = 3;
  6.  
  7. // Function prototypes
  8.  
  9. void clearScreen();
  10. string getCol(string board[D][D], int c);
  11. string getRow(string board[D][D], int r);
  12. string getDiag(string board[D][D], int d);
  13. bool isWinState(string state);
  14. bool isGameOver(string board[D][D]);
  15. void printBoard(string board[D][D]);
  16. void updateBoard(string board[D][D], int pos, string xo);
  17.  
  18.  
  19. // Main function
  20.  
  21. int main()
  22. {
  23.  
  24. string board[D][D] = {{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}};
  25. int position, turn=0;
  26. string xo;
  27.  
  28.  
  29. while(!isGameOver(board) && turn < 9)
  30. {
  31.  
  32. clearScreen();
  33. printBoard (board);
  34.  
  35. xo = "x";
  36. if (turn%2 == 1) xo = "o";
  37. cout << "Player " << turn%2+1 << "'s turn: ";
  38. cin >> position;
  39. position--;
  40. while(position > 8 || position < 0 || !isdigit(board[position/3][position%3][0]) || cin.fail())
  41. {
  42. cout << "Please enter in a valid position: ";
  43. cin >> position;
  44. position --;
  45. }
  46.  
  47. board[position/3][position%3] = xo;
  48.  
  49. turn ++;
  50. }
  51.  
  52. clearScreen();
  53. printBoard (board);
  54.  
  55. if (turn == 9)
  56. {
  57. cout << "A tie!" << endl;
  58. }
  59. else
  60. {
  61. cout << "Player " << (turn+1)%2 + 1<< " wins!" << endl;
  62. }
  63. }
  64.  
  65. string getCol(string board[3][3], int c)
  66. {
  67. string column = "";
  68. for(int i=0; i < 3; i++) column = column + board[i][c];
  69. return column;
  70. }
  71.  
  72.  
  73. string getRow(string board[3][3], int r)
  74. {
  75. string row = "";
  76. for(int i=0; i < 3; i++) row += board[r][i];
  77. return row;
  78. }
  79.  
  80. string getDiag(string board[3][3], int d)
  81. {
  82. string diagonal = "";
  83. for(int i=0; i < 3; i++) diagonal += board[i][2*d+(1-2*d)*i];
  84. return diagonal;
  85. }
  86.  
  87. bool isWinState(string state)
  88. {
  89. return state[0]==state[1] && state[1]==state[2] && state[0] != ' ';
  90. }
  91.  
  92. bool isGameOver(string board[3][3])
  93. {
  94. for(int i=0; i < 3; i++) if(isWinState(getRow(board, i)) || isWinState(getCol(board,i))) return true;
  95. for(int i=0; i < 2; i++) if(isWinState(getDiag(board, i))) return true;
  96. return false;
  97. }
  98.  
  99. void printBoard(string board[D][D])
  100. {
  101. for(int i=0; i < 2; i++)
  102. {
  103. cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
  104. cout << "---+---+---" << endl;
  105. }
  106. cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
  107. }
  108.  
  109. void updateBoard(string board[3][3], int pos, string xo)
  110. {
  111. board[pos/3][pos%3] = xo;
  112. }
  113.  
  114. void clearScreen()
  115. {
  116. cout << string(100, '\n');
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement