Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. /******************************************************************************
  2. * Description : Tic Tac Toe game
  3. * Author : dsl
  4. *
  5. * Write a program that lets two humans play a game of Tic Tac Toe in a
  6. * terminal. The program should let the players take turns to input their
  7. * moves. The program should report the outcome of the game.
  8. *
  9. * During your interview, you will pair on adding support for a computer player
  10. * to your game. You can start with random moves and make the AI smarter if you
  11. * have time.
  12. ****************************************************************************/
  13.  
  14. #include <stdio.h>
  15.  
  16. int isWinner();
  17. int status;
  18. int move;
  19. int selected;
  20. int player = 1;
  21.  
  22. char position[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  23. char playerSymbol;
  24.  
  25. void gameBoard()
  26. {
  27. int boardSize = sizeof(position)/sizeof(position[0]);
  28.  
  29. for (int i = 0; i < boardSize; i++) {
  30. printf(" %c |", position[i]);
  31. if ( i == 2 || i == 5) {
  32. printf("\n------------\n");
  33. }
  34. }
  35. }
  36.  
  37. int play()
  38. {
  39. const int MIN = 0;
  40. const int MAX = 9;
  41.  
  42. status = isWinner(); // -1 when in play
  43.  
  44. while (status == -1) {
  45. if (player == 1) {
  46. playerSymbol = 'X';
  47. } else {
  48. playerSymbol = 'O';
  49. }
  50.  
  51. printf("\n\nOk %c, it's your move. Which square? ", playerSymbol);
  52.  
  53. do {
  54. scanf("%d", &move);
  55.  
  56. if (move < MIN || move > MAX) {
  57. printf("\nWrong input\n");
  58. fflush(stdin);
  59. }
  60. } while (move < MIN || move > MAX);
  61.  
  62. printf("\n\n");
  63. selected = move - 1 ;
  64.  
  65. if (player == 1) {
  66. position[selected] = 'X';
  67. player--;
  68. } else {
  69. position[selected] = 'O';
  70. player++;
  71. }
  72.  
  73. gameBoard();
  74. status = isWinner();
  75. }
  76.  
  77. if (status == 1) {
  78. printf("\nWe have a WIENER.\n");
  79. if (player == 0) {
  80. printf("X wins!\n");
  81. } else if (player == 1) {
  82. printf("O wins!\n");
  83. }
  84. }
  85.  
  86. return 0;
  87. }
  88.  
  89. int isWinner()
  90. {
  91. if (position[0] == position[1] && position[1] == position[2]) {
  92. return 1;
  93. } else if (position[3] == position[4] && position[4] == position[5]) {
  94. return 1;
  95. } else if (position[6] == position[7] && position[7] == position[8]) {
  96. return 1;
  97. } else if (position[0] == position[3] && position[3] == position[6]) {
  98. return 1;
  99. } else if (position[1] == position[4] && position[4] == position[7]) {
  100. return 1;
  101. } else if (position[2] == position[5] && position[5] == position[8]) {
  102. return 1;
  103. } else if (position[0] == position[4] && position[4] == position[8]) {
  104. return 1;
  105. } else if (position[2] == position[4] && position[4] == position[6]) {
  106. return 1;
  107. } else if (position[0] != '1' && position[1] != '2' && position[2] != '3' && position[3] != '4' && position[4] != '5' && position[5] != '6' && position[6] != '7' && position[7] != '8' && position[8] != '9'){
  108. printf("\nYou both failed to best each other. For shame! Game is null.\n");
  109. return 0;
  110. } else {
  111. return -1;
  112. }
  113. }
  114.  
  115. int main()
  116. {
  117. printf("Hello, players!\nX starts.\n\nHere be the board: \n\n");
  118. gameBoard();
  119. play();
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement