Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6. char tab[3][3];
  7. int i,j,numer,pyt,tabs[9];
  8.  
  9. void wypelnij_na_pusto(char tab[3][3]){
  10. for (i=0; i<3; i++){
  11. for (j=0; j<3; j++){
  12. tab[i][j]='-';
  13. }
  14. }
  15. }
  16.  
  17. void wyswietl_pole(char tab[3][3]){
  18.  
  19. for (i=0; i<3; i++){
  20. for (j=0; j<3; j++){
  21. printf(" %c",tab[i][j]);printf(" ");
  22. }
  23. printf("\n");
  24. }
  25. }
  26.  
  27. void instrukcja(){
  28. printf("\n\nINSTRUKCJA GRY:\nPola sa oznaczone od 1 do 9, co trzy pola wiersz sie zawija.\n");
  29. printf("A wiec dla przykladu, gdy wybierzesz pole numer 4 wpiszesz znak X do pola:\n");
  30. printf("- - -\nX - -\n- - -\n");
  31. printf("GRACZ 1: Stawia O \nGRACZ 2: Stawia X\n\nZACZYNAJMY WIEC GRE!!!\n\n");
  32. }
  33.  
  34. void instrukcja_komp(){
  35. printf("\n\nINSTRUKCJA GRY:\nPola sa oznaczone od 1 do 9, co trzy pola wiersz sie zawija.\n");
  36. printf("A wiec dla przykladu, gdy wybierzesz pole numer 4 wpiszesz znak X do pola:\n");
  37. printf("- - -\nX - -\n- - -\n");
  38. printf("GRACZ 1: to Ty stawiasz O\nGRACZ 2: to komputer\n\nZACZYNAJMY WIEC GRE!!!\n\n");
  39. }
  40.  
  41. void wpisz_znak_gracz1(char tab[3][3], int numer){
  42.  
  43.  
  44. if (numer == 1){tab[0][0]='O';}
  45. if (numer == 2){tab[0][1]='O';}
  46. if (numer == 3){tab[0][2]='O';}
  47. if (numer == 4){tab[1][0]='O';}
  48. if (numer == 5){tab[1][1]='O';}
  49. if (numer == 6){tab[1][2]='O';}
  50. if (numer == 7){tab[2][0]='O';}
  51. if (numer == 8){tab[2][1]='O';}
  52. if (numer == 9){tab[2][2]='O';}
  53. }
  54.  
  55. void wpisz_znak_gracz2(char tab[3][3], int numer){
  56. if (numer == 1){tab[0][0]='X';}
  57. if (numer == 2){tab[0][1]='X';}
  58. if (numer == 3){tab[0][2]='X';}
  59. if (numer == 4){tab[1][0]='X';}
  60. if (numer == 5){tab[1][1]='X';}
  61. if (numer == 6){tab[1][2]='X';}
  62. if (numer == 7){tab[2][0]='X';}
  63. if (numer == 8){tab[2][1]='X';}
  64. if (numer == 9){tab[2][2]='X';}
  65. }
  66.  
  67. int sprawdz_gracz1(char tab[3][3]){
  68. if ((tab[0][0]=='O') && (tab[0][1]=='O') && (tab[0][2]=='O')){
  69. printf("Wygral gracz 1");
  70. return 1;
  71. }
  72. if ((tab[1][0]=='O') && (tab[1][1]=='O') && (tab[1][2]=='O')){
  73. printf("Wygral gracz 1");
  74. return 1;
  75. }
  76. if ((tab[2][0]=='O') && (tab[2][1]=='O') && (tab[2][2]=='O')){
  77. printf("Wygral gracz 1");
  78. return 1;
  79. }
  80. if ((tab[0][0]=='O') && (tab[1][0]=='O') && (tab[2][0]=='O')){
  81. printf("Wygral gracz 1");
  82. return 1;
  83. }
  84. if ((tab[0][1]=='O') && (tab[1][1]=='O') && (tab[2][1]=='O')){
  85. printf("Wygral gracz 1");
  86. return 1;
  87. }
  88. if ((tab[0][2]=='O') && (tab[1][2]=='O') && (tab[2][2]=='O')){
  89. printf("Wygral gracz 1");
  90. return 1;
  91. }
  92. if ((tab[0][0]=='O') && (tab[1][1]=='O') && (tab[2][2]=='O')){
  93. printf("Wygral gracz 1");
  94. return 1;
  95. }
  96. if ((tab[0][2]=='O') && (tab[1][1]=='O') && (tab[2][0]=='O')){
  97. printf("Wygral gracz 1");
  98. return 1;
  99. }
  100. else{
  101. return 0;
  102. }
  103. }
  104.  
  105. int sprawdz_gracz2(char tab[3][3]){
  106. if ((tab[0][0]=='X') && (tab[0][1]=='X') && (tab[0][2]=='X')){
  107. printf("Wygral gracz 2");
  108. return 1;
  109. }
  110. if ((tab[1][0]=='X') && (tab[1][1]=='X') && (tab[1][2]=='X')){
  111. printf("Wygral gracz 2");
  112. return 1;
  113. }
  114. if ((tab[2][0]=='X') && (tab[2][1]=='X') && (tab[2][2]=='X')){
  115. printf("Wygral gracz 2");
  116. return 1;
  117. }
  118. if ((tab[0][0]=='X') && (tab[1][0]=='X') && (tab[2][0]=='X')){
  119. printf("Wygral gracz 2");
  120. return 1;
  121. }
  122. if ((tab[0][1]=='X') && (tab[1][1]=='X') && (tab[2][1]=='X')){
  123. printf("Wygral gracz 2");
  124. return 1;
  125. }
  126. if ((tab[0][2]=='X') && (tab[1][2]=='X') && (tab[2][2]=='X')){
  127. printf("Wygral gracz 2");
  128. return 1;
  129. }
  130. if ((tab[0][0]=='X') && (tab[1][1]=='X') && (tab[2][2]=='X')){
  131. printf("Wygral gracz 2");
  132. return 1;
  133. }
  134. if ((tab[0][2]=='X') && (tab[1][1]=='X') && (tab[2][0]=='X')){
  135. printf("Wygral gracz 2");
  136. return 1;
  137. }
  138. else{
  139. return 0;
  140. }
  141.  
  142. }
  143.  
  144. int sprawdz_tablice(char tab[3][3]){
  145. int licznik=0;
  146.  
  147. for (i=0; i<3; i++){
  148. for (j=0; j<3; j++){
  149. if (tab[i][j]=='-'){
  150. licznik++;
  151. }
  152. }
  153. }
  154. if (licznik==0){printf("No niestety nikt nie wygral, ale dzieki za zabawe!! :)");return 0;}
  155. else{return 1;}
  156. }
  157.  
  158. int sprawdz_pole(int tabs[9], int numer){
  159. int licznik;
  160. licznik=0;
  161. for (i=0; i<9; i++){
  162. if (tabs[i]==numer){
  163. licznik++;
  164. }
  165. }
  166. if (licznik==0){
  167. return 1;
  168. }
  169. else{printf("Te pole jest zajete, wprowadz poprawny numer pola! :(\n");return 0;}
  170. }
  171.  
  172. int sprawdz_pole_komp(int tabs[9], int numer){
  173. int licznik;
  174. licznik=0;
  175. for (i=0; i<9; i++){
  176. if (tabs[i]==numer){
  177. licznik++;
  178. }
  179. }
  180. if (licznik==0){
  181. return 1;
  182. }
  183. else{return 0;}
  184. }
  185.  
  186. int main(){
  187. srand(time(0));
  188.  
  189. int wybor,licznik,losowa;
  190. numer=0; licznik=0;
  191. wypelnij_na_pusto(tab);
  192.  
  193. printf("Siemanko!\nWybierz w jaki tryb gry chcesz zagrac: \n");
  194. printf("[1] - wersja z komputerem\n");
  195. printf("[2] - wersja dla 2 osob\n");
  196. scanf("%d",&wybor);
  197.  
  198. if (wybor == 1){
  199.  
  200. system("cls");
  201. printf("Tak wyglada plansza:\n");
  202. wyswietl_pole(tab);
  203. instrukcja_komp();
  204.  
  205. do{
  206. for (i=0; i<9; i++){tabs[i]=0;}
  207. wypelnij_na_pusto(tab);
  208. licznik=0;
  209. do{
  210. if ((licznik % 2)==0){
  211. printf("GRACZ 1: podaj numer gdzie mam postawic O?\n");
  212. do{
  213. scanf("%d",&numer);
  214. }
  215. while(sprawdz_pole(tabs,numer)==0);
  216. tabs[licznik]=numer;
  217. system("cls");
  218. wpisz_znak_gracz1(tab,numer);
  219. wyswietl_pole(tab);
  220. }
  221. else{
  222. do{
  223. losowa=rand() % 10;
  224. }
  225. while(sprawdz_pole_komp(tabs,losowa)==0);
  226. tabs[licznik]=losowa;
  227. wpisz_znak_gracz2(tab,losowa);
  228. system("cls");
  229. printf("Komputer wybral pole: %d",losowa);printf("\n");
  230. wyswietl_pole(tab);
  231. }
  232. licznik++;
  233. }
  234. while ((sprawdz_gracz1(tab)==0)&&(sprawdz_gracz2(tab)==0)&&(sprawdz_tablice(tab)==1));
  235. printf("\nCzy chcesz zagrac jeszcze raz 1 - tak 0 - nie\n");
  236. scanf("%d",&pyt);
  237. system("cls");
  238. }
  239. while (pyt==1);
  240. }
  241. else if (wybor==2){
  242. system("cls");
  243. printf("Tak wyglada aktualnie plansza:\n");
  244. wyswietl_pole(tab);
  245.  
  246. instrukcja();
  247. do{
  248. for (i=0; i<9; i++){tabs[i]=0;}
  249. wypelnij_na_pusto(tab);
  250. licznik=0;
  251. do{
  252. if ((licznik % 2)==0){
  253. printf("GRACZ 1: podaj numer gdzie mam postawic O?\n");
  254. do{
  255. scanf("%d",&numer);
  256. }
  257. while(sprawdz_pole(tabs,numer)==0);
  258. tabs[licznik]=numer;
  259. system("cls");
  260. wpisz_znak_gracz1(tab,numer);
  261. wyswietl_pole(tab);
  262. }
  263. else{
  264. printf("GRACZ 2: podaj numer gdzie mam postawic X?\n");
  265. do{
  266. scanf("%d",&numer);
  267. }
  268. while(sprawdz_pole(tabs,numer)==0);
  269. tabs[licznik]=numer;
  270. system("cls");
  271. wpisz_znak_gracz2(tab,numer);
  272. wyswietl_pole(tab);
  273. }
  274. licznik++;
  275. }
  276. while ((sprawdz_gracz1(tab)==0)&&(sprawdz_gracz2(tab)==0)&&(sprawdz_tablice(tab)==1));
  277. printf("\nCzy chcesz zagrac jeszcze raz 1 - tak 0 - nie\n");
  278. scanf("%d",&pyt);
  279. system("cls");
  280. }
  281. while (pyt==1);
  282. }
  283. else{
  284. system("cls");
  285. printf("Podaj poprawny numer gamoniu!");
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement