Guest User

Untitled

a guest
May 21st, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. char grid[3][3];
  6. void showGrid();
  7. void clearGrid();
  8. char checkWinner();
  9.  
  10. int getOWin();
  11. int preventXWin();
  12.  
  13. char currentWinner;
  14. char currentPlayer;
  15.  
  16. int main(){
  17. int location = 0;
  18.  
  19. srand(time(0));
  20.  
  21. bool legalMove;
  22. int choice;
  23. currentPlayer = 'X';
  24. int tie=0, xWin=0, oWin=0;
  25. int mode;
  26.  
  27. do{
  28. printf("Enter '1' for single-player mode, or '2' for two-player mode: ");
  29. scanf("%i",&mode);
  30. }while((mode != 1)&&(mode != 2));
  31.  
  32.  
  33. do{
  34. currentWinner = '.';
  35. clearGrid();
  36. showGrid();
  37. do{
  38. if((mode == 1)&&(currentPlayer=='O')){
  39.  
  40. printf("Computer plays.\n");
  41.  
  42. location = getOWin();
  43. if(location == -1){
  44. location = preventXWin();
  45. if(location == -1){
  46. location = rand()%9;
  47. }
  48. }
  49.  
  50. do{
  51. if((grid[location/3][location%3]!='X')&&(grid[location/3][location%3]!='O')&&(location>=0)&&(location<=8)){
  52. legalMove = true;
  53. }
  54. else{
  55. legalMove = false;
  56. location = rand()%9;
  57. }
  58.  
  59. }while(!legalMove);
  60. }
  61. else{
  62. printf("Player %c, please choose a valid location: ",currentPlayer);
  63. scanf("%i",&location);
  64. location--;
  65. }
  66.  
  67. if((grid[location/3][location%3]!='X')&&(grid[location/3][location%3]!='O')&&(location>=0)&&(location<=8)){
  68. grid[location/3][location%3] = currentPlayer;
  69. legalMove = true;
  70. }
  71. else{
  72. legalMove = false;
  73. }
  74.  
  75. showGrid();
  76.  
  77. if(legalMove){
  78. currentWinner = checkWinner();
  79.  
  80. if(currentPlayer == 'X'){
  81. currentPlayer = 'O';
  82. }
  83. else{
  84. currentPlayer = 'X';
  85. }
  86. }
  87. else{
  88. printf("Illegal move.\n");
  89. }
  90.  
  91. }while(currentWinner=='.');
  92. if(currentWinner=='Z'){
  93. printf("Game is a tie.\n");
  94. tie++;
  95. }
  96. else{
  97. printf("Winner of this round is player: %c\n",currentWinner);
  98. if(currentWinner == 'X'){
  99. xWin++;
  100. }
  101. else{
  102. oWin++;
  103. }
  104. }
  105. printf("Press '1' to continue, or any other number to quit: ");
  106. scanf("%i",&choice);
  107. }while(choice==1);
  108.  
  109. printf("X wins: %i, O wins: %i, tie game: %i\n\n", xWin, oWin, tie);
  110.  
  111. printf("Press any key followed by enter to exit: ");
  112. scanf("%c",&currentWinner);
  113. return 0;
  114. }
  115.  
  116. int getOWin(){
  117. int i;
  118. int loc = -1;
  119. char win;
  120. char prev;
  121.  
  122. for(i = 0; i<9; i++){
  123. prev = grid[i/3][i%3];
  124. if((grid[i/3][i%3]!='X')&&(grid[i/3][i%3]!='O')){
  125.  
  126. grid[i/3][i%3] = 'O';
  127. win = checkWinner();
  128. if(win == 'O'){
  129. loc = i;
  130. }
  131. }
  132. grid[i/3][i%3] = prev;
  133. }
  134. return loc;
  135. }
  136.  
  137. int preventXWin(){
  138. int i;
  139. int loc = -1;
  140. char win;
  141. char prev;
  142.  
  143. for(i = 0; i<9; i++){
  144. prev = grid[i/3][i%3];
  145. if((grid[i/3][i%3]!='X')&&(grid[i/3][i%3]!='O')){
  146.  
  147. grid[i/3][i%3] = 'X';
  148. win = checkWinner();
  149. if(win == 'X'){
  150. loc = i;
  151. }
  152. }
  153. grid[i/3][i%3] = prev;
  154. }
  155. return loc;
  156. }
  157.  
  158. char checkWinner(){
  159. int i, j;
  160. char cW = '.';
  161. for(i=0;i<3;i++){
  162. if((grid[i][0]==grid[i][1])&&(grid[i][0]==grid[i][2])){
  163. cW = grid[i][0];
  164. }
  165. if((grid[0][i]==grid[1][i])&&(grid[0][i]==grid[2][i])){
  166. cW = grid[0][i];
  167. }
  168. }
  169.  
  170. if((grid[0][0]==grid[1][1])&&(grid[0][0]==grid[2][2])){
  171. cW = grid[0][0];
  172. }
  173. if((grid[0][2]==grid[1][1])&&(grid[0][2]==grid[2][0])){
  174. cW = grid[0][2];
  175. }
  176.  
  177. bool noEmptySpots = true;
  178. if(cW == '.'){
  179. for(i=0;i<3;i++){
  180. for(j=0;j<3;j++){
  181. if((grid[i][j]!='X')&&(grid[i][j]!='O')){
  182. noEmptySpots = false;
  183. }
  184. }
  185. }
  186. if(noEmptySpots){
  187. cW = 'Z';
  188. }
  189. }
  190.  
  191. return cW;
  192. }
  193.  
  194. void clearGrid(){
  195. int i, j;
  196. for(i=0;i<3;i++){
  197. for(j=0;j<3;j++){
  198. grid[i][j] = (i*3)+j+49;
  199. }
  200. }
  201. }
  202.  
  203. void showGrid(){
  204. printf("+---+---+---+\n");
  205. printf("| %c | %c | %c |\n",grid[0][0],grid[0][1],grid[0][2]);
  206. printf("+---+---+---+\n");
  207. printf("| %c | %c | %c |\n",grid[1][0],grid[1][1],grid[1][2]);
  208. printf("+---+---+---+\n");
  209. printf("| %c | %c | %c |\n",grid[2][0],grid[2][1],grid[2][2]);
  210. printf("+---+---+---+\n");
  211. }
Add Comment
Please, Sign In to add comment