Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // TicTacToe
  4. //
  5. // Created by Aldís Eik Armarsdóttir on 10/12/17.
  6. // Copyright © 2017 Aldís Eik Armarsdóttir . All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. const int MAX_SIZE = 3;
  13.  
  14. void print_char (char tictactoe[][MAX_SIZE])
  15. {
  16. for(int i = 0; i < MAX_SIZE; i++)
  17. {
  18. for(int j = 0; j < MAX_SIZE; j++)
  19. {
  20. cout << tictactoe[i][j] << " ";
  21. }
  22. cout << endl;
  23. }
  24. }
  25.  
  26. void game_players(int& position, char& player, char playerX)
  27. {
  28. if(player == playerX)
  29. {
  30. cout << "X position: ";
  31. cin >> position;
  32. }
  33. else
  34. {
  35. cout << "O position: ";
  36. cin >> position;
  37. }
  38. }
  39.  
  40. void players_switch(int& position, char& player, char playerX, char playerO)
  41. {
  42. if(player == playerX)
  43. {
  44. player = playerO;
  45. }
  46. else
  47. {
  48. player = playerX;
  49. }
  50. }
  51.  
  52. bool game_playing (char tictactoe[][MAX_SIZE], int& position, char& player, char playerX, char playerO)
  53. {
  54. bool positionOK = false;
  55.  
  56. do{
  57. if (position > 9 || position < 1 || position < - 1 || position == playerX || position == playerO)
  58. {
  59. cout << "Illegal move!" << endl;
  60. game_players(position, player, playerX);
  61. }
  62. else if (position > 0 && position <= 9)
  63. {
  64. positionOK = true;
  65. tictactoe[0][position - 1] = player;
  66. }
  67. }while (!positionOK);
  68.  
  69. return position;
  70. }
  71.  
  72. bool lookforwin(char tictactoe[][MAX_SIZE], bool& win, char& player)
  73. {
  74. for(int i = 0; i < 3; i++)
  75. {
  76. if ((tictactoe[i][0] == tictactoe[i][1] && tictactoe[i][1] == tictactoe[i][2]) || (tictactoe[0][i] == tictactoe[1][i] && tictactoe[1][i] == tictactoe[2][i]) || (tictactoe[0][0] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][2]) || (tictactoe[0][2] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][0]))
  77. {
  78. win = true;
  79. }
  80. }
  81.  
  82. if (win == true) {
  83. cout << "Winner is: " << player << endl;
  84. }
  85.  
  86. return win;
  87. }
  88.  
  89.  
  90. int main()
  91. {
  92. char tictactoe[MAX_SIZE][MAX_SIZE] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
  93. char playerX = 'X', playerO = 'O';
  94. char player = playerX;
  95. int position = 0;
  96. bool win = false;
  97. int counter = 0;
  98.  
  99. print_char(tictactoe);
  100.  
  101.  
  102. do
  103. {
  104. if (counter == 9) {
  105. cout << "It's a draw!!!!!!!!" << endl;
  106. break;
  107. }
  108. game_players(position, player, playerX);
  109. //errors ef char, eða 'X', eða 'O' virka ekki
  110. game_playing(tictactoe, position, player, playerX, playerO);
  111. print_char(tictactoe);
  112. lookforwin(tictactoe, win, player);
  113. //lykkjan þarf að stoppa eftir að win eða draw er til staðar + prentar þrisvar þegar skáruna er
  114. //setja draw in eftir 9 tilraunir
  115. players_switch(position, player, playerX, playerO);
  116. counter++;
  117. }while(!win); //setja draw inní dowhile eftir 9 skipti
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement