Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #define N 5
  3.  
  4. using namespace std;
  5.  
  6. void inizio();
  7. void draw (char matrice[][N]);
  8. void logic (char matrice[][N], int casella, char variabile, int contatore);
  9. int controllo (char matrice[][N]);
  10.  
  11. int main() {
  12.  
  13. char matrice[N][N];
  14. char variabile;
  15. int contatore=0;
  16. int casella;
  17.  
  18. inizio();
  19. draw(matrice);
  20. logic(matrice,casella,variabile,contatore);
  21.  
  22. }
  23.  
  24. void inizio(){
  25. cout<<"Ecco la tabella di gioco:\n";
  26. cout<<"1|2|3\n";
  27. cout<<"-+-+-\n";
  28. cout<<"4|5|6\n";
  29. cout<<"-+-+-\n";
  30. cout<<"7|8|9\n";
  31. cout<<"premi invio";
  32. getchar();
  33. }
  34.  
  35. void draw(char matrice[][N]){
  36.  
  37. system("cls");
  38.  
  39. matrice[0][1]='|';
  40. matrice[0][3]='|';
  41. matrice[1][0]='-';
  42. matrice[1][1]='+';
  43. matrice[1][2]='-';
  44. matrice[1][3]='+';
  45. matrice[1][4]='-';
  46. matrice[2][1]='|';
  47. matrice[2][3]='|';
  48. matrice[3][0]='-';
  49. matrice[3][1]='+';
  50. matrice[3][2]='-';
  51. matrice[3][3]='+';
  52. matrice[3][4]='-';
  53. matrice[4][1]='|';
  54. matrice[4][3]='|';
  55. for(int i=0;i<N;i++){
  56. for(int j=0;j<N;j++){
  57. cout<<matrice[i][j]<<" ";
  58. }
  59. cout<<endl;
  60. }
  61. }
  62.  
  63. void logic(char matrice[][N], int casella, char variabile, int contatore) {
  64.  
  65. int gameover=0;
  66.  
  67. while(gameover==0) {
  68.  
  69. cout<<"Inserisci la casella\n";
  70. cin>>casella;
  71. if(contatore%2==0){
  72. variabile='x';
  73. }
  74. else
  75. variabile='o';
  76. switch(casella){
  77. case 1:
  78. matrice[0][0]=variabile;
  79. break;
  80.  
  81. case 2:
  82. matrice[0][2]=variabile;
  83. break;
  84. case 3:
  85. matrice[0][4]=variabile;
  86. break;
  87. case 4:
  88. matrice[2][0]=variabile;
  89. break;
  90. case 5:
  91. matrice[2][2]=variabile;
  92. break;
  93. case 6:
  94. matrice[2][4]=variabile;
  95. break;
  96. case 7:
  97. matrice[4][0]=variabile;
  98. break;
  99. case 8:
  100. matrice[4][2]=variabile;
  101. break;
  102. case 9:
  103. matrice[4][4]=variabile;
  104. break;
  105. }
  106. contatore++;
  107. draw(matrice);
  108.  
  109. if(contatore>4) {
  110. gameover=controllo(matrice);
  111. logic(matrice,casella,variabile,contatore);
  112. }
  113.  
  114. }
  115.  
  116. }
  117.  
  118. int controllo (char matrice[][N]) {
  119.  
  120. int j=0;
  121. int fine=0;
  122.  
  123.  
  124. for(int i=0;i<N;i+2) {
  125. if((matrice[i][j]==matrice[i][j+2])&&(matrice[i][j]==matrice[i][j+4])) {
  126. fine=1;
  127. cout<<"\nIL VINCITORE E' IL GIOCATORE "<<matrice[i][j]<<"!\n";
  128. }
  129.  
  130. else if((matrice[j][i]==matrice[j+2][i])&&(matrice[j][i]==matrice[j+4][i])) {
  131. fine=1;
  132. cout<<"\nIL VINCITORE E' IL GIOCATORE "<<matrice[j][i]<<"!\n";
  133. }
  134.  
  135. }
  136.  
  137.  
  138. return fine;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement