1. //Matthew Isnor 8 September 2010
  2.  
  3. #include <iostream>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
  9.  
  10. void showBoard(){
  11. cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
  12. cout << " _ " << " _ " << " _ " << endl;
  13. cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
  14. cout << " _ " << " _ " << " _ " << endl;
  15. cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
  16. }
  17.  
  18. void move(char player){
  19.  
  20. int choice;
  21. bool input = 0;
  22.  
  23. while (input == 0)
  24. {
  25. cout << "Please make a move selection 0-8: " << endl;
  26. cin >> choice;
  27. if (choice > 8 || choice < 0)
  28. {
  29. cout << "Please enter a move selection 0-8: " << endl;
  30. cin >> choice;
  31. }
  32.  
  33. if (board[choice] == 'X' || board[choice] == 'O')
  34. {
  35. cout << "That spot is taken on the board. Choose another 0-8: " << endl;
  36. cin >> choice;
  37. }
  38.  
  39. board[choice] = player;
  40. input = 1;
  41.  
  42.  
  43. }
  44.  
  45. }
  46.  
  47.  
  48. char checkWinner(char player, char winner){
  49.  
  50. if (board[0] == player && board[1] == player && board[2] == player) // Across top
  51. {
  52. return player;
  53. }
  54. else if (board[0] == player && board[4] == player && board[8] == player) //Top left bottom right
  55. {
  56. return player;
  57. }
  58. else if (board[0] == player && board[3] == player && board[6] == player) // Left side
  59. {
  60. return player;
  61. }
  62. else if (board[1] == player && board[4] == player && board[7] == player) //Down middle
  63. {
  64. return player;
  65. }
  66. else if (board[2] == player && board[5] == player && board[8] == player) //right side
  67. {
  68. return player;
  69. }
  70. else if (board[6] == player && board[7] == player && board[8] == player) // bottom row
  71. {
  72. return player;
  73. }
  74. else if(board[3] == player && board[4] == player && board[5] == player) //middle accross
  75. {
  76. return player;
  77. }
  78. else if(board[6] == player && board[4] == player && board[2] == player) // bottom left to top right
  79. {
  80. return player;
  81. }
  82. else
  83. {
  84. return 'f'; // NO ONE FUCKING WON, KEEP GOING.
  85. }
  86. }
  87.  
  88. void winMessage(char player){
  89.  
  90. system("CLS");
  91. showBoard();
  92. cout << "\n\nThe player '" << player << "' has won the match!" << endl;
  93. system("PAUSE");
  94. }
  95.  
  96.  
  97. bool tieCheck(){
  98. if ((board[0] == 'X' || board[0] == 'O') && (board[1] == 'X' || board[1] == 'O') && (board[2] == 'X' || board[2] == 'O') &&
  99. (board[3] == 'X' || board[3] == 'O') && (board[4] == 'X' || board[4] == 'O') && (board[5] == 'X' || board[5] == 'O') &&
  100. (board[6] == 'X' || board[6] == 'O') && (board[7] == 'X' || board[7] == 'O') && (board[8] == 'X' || board[8] == 'O')){
  101.  
  102. cout << "The match has ended in a tie!\n\n";
  103. system("PAUSE");
  104. return 1;
  105.  
  106. }
  107. return 0; // Game did not end in a tie.
  108. }
  109. int main(){
  110. bool winner = false;
  111. char theWinner = 'f';
  112.  
  113. cout << "Tic Tac Toe, C++ Style." << endl;
  114.  
  115.  
  116. while (winner == false)
  117. {
  118. system("CLS");
  119. showBoard();
  120. move('X');
  121. theWinner = checkWinner('X', theWinner);
  122. if (theWinner == 'X')
  123. {
  124. winMessage('X');
  125. return 0; // end the bitch 'X' won.
  126. }
  127.  
  128. winner = tieCheck();
  129. if (winner == 1)
  130. {
  131. return 0; // end the bitch 'O' won.
  132. }
  133.  
  134. system("CLS");
  135. showBoard();
  136. move('O');
  137. theWinner = checkWinner('O', theWinner);
  138. if (theWinner == 'O')
  139. {
  140. winMessage('O');
  141. return 0; // end the bitch 'O' won.
  142. }
  143.  
  144. winner = tieCheck();
  145. if (winner == 1)
  146. {
  147. return 0; // end the bitch 'O' won.
  148. }
  149.  
  150. // else re-loop while
  151. }
  152. }
  153.