Guest User

Untitled

a guest
Dec 9th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. int matrix[3][3];
  2. bool playerTurn;
  3. void playing();
  4. int checkwinpc();
  5. int checkwinplayer();
  6. int computer();
  7. int tie();
  8. int checkposition();
  9. int XO,playerone, playertwo, pcpick,final, playerchoice;
  10.  
  11. void playing(int matrix[3][3]){
  12.  
  13. int cont, a, b;
  14. // Limpiara la pantalla
  15. system("CLS");
  16. cont=1;
  17.  
  18. printf("nnnnttt t³t t³ntt");
  19. //La t y las n centran mis textos
  20. for(a=0; a<3; a++){
  21.  
  22. if(a!=0){
  23.  
  24. printf("t t³t t³ntt"); // Posicionará mis lineas verticales en un orden mas descente
  25. }
  26.  
  27. for(b=0; b<3; b++){
  28.  
  29. if ( matrix[a][b]==0){
  30. if(b!=2){
  31.  
  32.  
  33. printf("t%it³",cont );
  34.  
  35. }
  36. else{
  37. printf("t%it",cont );
  38.  
  39. }
  40. }
  41. else{
  42. if (matrix[a][b]==1){
  43. if(b!=2){
  44.  
  45. printf("tXt³");
  46. }
  47. else{
  48. printf("tXt");
  49. }
  50. }
  51. else{
  52. if(b!=2){
  53.  
  54. printf("tOt³");
  55. }
  56. else{
  57. printf("tOt");
  58. }
  59. }
  60. }
  61. cont++;
  62. }
  63.  
  64.  
  65. printf("nttt t³t t³ntt");
  66.  
  67. if(a!=2){
  68.  
  69. printf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄntt"); // Imprimira mis 2 líneas horizontalez
  70. }
  71. }
  72. }
  73.  
  74.  
  75. int main(){
  76.  
  77. srand(time(0));
  78. int playAgain;
  79. int yourscore= 0;
  80. int pcscore= 0;
  81. int tie= 0;
  82. int mode =0;
  83.  
  84. cout<<"Contra quién deseas jugar?"<<endl;
  85. cout<<"1-PvPn 2-PvPc" <<endl;
  86. cin>>mode;
  87.  
  88. if (mode=1){
  89. system("CLS");
  90.  
  91. playing();
  92. int vocal, number, XO, cont,final;
  93.  
  94. vocal = 1;
  95. playing(matrix);
  96.  
  97. if (vocal==1)
  98. printf("nTurno: X");
  99. else
  100. printf("nTurno: O");
  101.  
  102. int checkposition();
  103.  
  104. int checkplayer();
  105.  
  106. if(final==1){
  107.  
  108. playing(matrix);
  109. // Llama a mi función con condiciones para selecionar un ganador.
  110.  
  111. printf("nnGanador: ");
  112. if(vocal==2){
  113. XO=8;
  114. printf("X");
  115. getch();
  116. }
  117. else{
  118. XO=8;
  119. printf("O");
  120. getch();
  121. }
  122. }
  123. }
  124. else if (mode=2){
  125.  
  126. do{
  127.  
  128. system("CLS");
  129.  
  130. int turnFirst = rand()% (2 - 1 + 1)+1;
  131. pcpick=rand()% (9 - 1 + 1)+1;
  132. playing();
  133.  
  134. if (turnFirst==1){
  135. while (!(cin >>playerone))
  136. {
  137. cout << endl;
  138. cout << "Numbers only" << endl;
  139. cin.clear(); cin.ignore(10000,'n');
  140. }
  141. checkposition();
  142.  
  143. system("CLS");
  144. playing();
  145. playerTurn = false;
  146.  
  147. }
  148.  
  149. if (turnFirst==2){
  150. pcpick;
  151. pcpick=pcpick;
  152. checkposition();
  153. cout<<"Turn pc"<<endl;
  154. playing();
  155.  
  156. }
  157.  
  158.  
  159.  
  160. if (playerTurn==true){
  161.  
  162. cout<<"Set your position to place X"<<endl;
  163. while (!(cin >> playerchoice)) //error
  164.  
  165. {
  166. cout << "Numbers only." << endl; cin.clear(); cin.ignore(10000,'n');
  167. }
  168. checkposition();
  169. checkwinplayer();
  170. int tie();
  171. playerTurn=false;
  172.  
  173. }
  174. if(playerTurn == false) {
  175.  
  176. computer();
  177. system("CLS");
  178. playing();
  179. checkwinpc();
  180. int tie();
  181. playerTurn = true;
  182. }
  183. }
  184. while (final>2);
  185.  
  186. if (final==0);
  187. cout<<"tien";
  188. ++tie;
  189.  
  190. if (final==1){
  191. cout<<"You winn";
  192. ++yourscore;
  193. }
  194. if(final==2){
  195. cout<<"PC winsn";
  196. ++pcscore;
  197. }
  198. cout<<"Total score";
  199. cout<<"You :"<<yourscore<<"Pc :"<<pcscore<<"Tie :"<<tie<<endl;
  200. cout<<"Want to play again?n1-Yesn2-No"<<endl;
  201. while(!(cin>>playAgain)){
  202.  
  203. cout << endl;
  204. cout << "Play again?n1-Yesn2-No" << endl;
  205. cin.clear(); cin.ignore(10000,'n');
  206. }
  207. }
  208. while(playAgain == 1);
  209. return 0;
  210.  
  211. }
  212.  
  213. int checkwinpc(){
  214.  
  215. if(XO>3){
  216.  
  217. if((matrix[0][0]+matrix[0][1]+matrix[0][2]!=0)&&(matrix[0][0]==matrix[0][1])&&(matrix[0][1]==matrix[0][2])){
  218. final=2;
  219. }
  220. else{
  221. if((matrix[1][0]+matrix[1][1]+matrix[1][2]!=0)&&(matrix[1][0]==matrix[1][1])&&(matrix[1][1]==matrix[1][2])){
  222. final=2;
  223. }
  224. else{
  225. if((matrix[2][0]+matrix[2][1]+matrix[2]!=0)&&(matrix[2][0]==matrix[2][1])&&(matrix[2][1]==matrix[2][2])){
  226. final=2;
  227. }
  228. else{
  229. if((matrix[0][0]+matrix[1][0]+matrix[2][0]!=0)&&(matrix[0][0]==matrix[1][0])&&(matrix[1][0]==matrix[2][0])){
  230. final=2;
  231. }
  232. else{
  233. if((matrix[0][1]+matrix[1][1]+matrix[2][1]!=0)&&(matrix[0][1]==matrix[1][1])&&(matrix[1][1]==matrix[2][1])){
  234. final=2;
  235. }
  236. else{
  237. if((matrix[0][2]+matrix[1][2]+matrix[2][2]!=0)&&(matrix[0][2]== matrix[1][2])&&(matrix[1][2]==matrix[2][2])){
  238. final=2;
  239. }
  240. else{
  241. if((matrix[0][0]+matrix[1][1]+matrix[2][2]!=0)&&(matrix[0][0]==matrix[1][1])&&(matrix[1][1]==matrix[2][2])){
  242. final=2;
  243. }
  244. else{
  245. if((matrix[2][0]+matrix[1][1]+matrix[0][2]!=0)&&(matrix[2][0]==matrix[1][1])&&(matrix[1][1]==matrix[0][2])){
  246. final=2;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255.  
  256. }
  257.  
  258. }
  259.  
  260. int checkwinplayer(){
  261.  
  262. if(XO>3){
  263.  
  264. if((matrix[0][0]+matrix[0][1]+matrix[0][2]!=0)&&(matrix[0][0]==matrix[0][1])&&(matrix[0][1]==matrix[0][2])){
  265.  
  266. final=1;
  267. }
  268. else{
  269. if((matrix[1][0]+matrix[1][1]+matrix[1][2]!=0)&&(matrix[1][0]==matrix[1][1])&&(matrix[1][1]==matrix[1][2])){
  270. final=1;
  271. }
  272. else{
  273. if((matrix[2][0]+matrix[2][1]+matrix[2]!=0)&&(matrix[2][0]==matrix[2][1])&&(matrix[2][1]==matrix[2][2])){
  274. final=1;
  275. }
  276. else{
  277. if((matrix[0][0]+matrix[1][0]+matrix[2][0]!=0)&&(matrix[0][0]==matrix[1][0])&&(matrix[1][0]==matrix[2][0])){
  278. final=1;
  279. }
  280. else{
  281. if((matrix[0][1]+matrix[1][1]+matrix[2][1]!=0)&&(matrix[0][1]==matrix[1][1])&&(matrix[1][1]==matrix[2][1])){
  282. final=1;
  283. }
  284. else{
  285. if((matrix[0][2]+matrix[1][2]+matrix[2][2]!=0)&&(matrix[0][2]== matrix[1][2])&&(matrix[1][2]==matrix[2][2])){
  286. final=1;
  287. }
  288. else{
  289. if((matrix[0][0]+matrix[1][1]+matrix[2][2]!=0)&&(matrix[0][0]==matrix[1][1])&&(matrix[1][1]==matrix[2][2])){
  290. final=1;
  291. }
  292. else{
  293. if((matrix[2][0]+matrix[1][1]+matrix[0][2]!=0)&&(matrix[2][0]==matrix[1][1])&&(matrix[1][1]==matrix[0][2])){
  294. final=1;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303.  
  304. }
  305.  
  306. }
  307.  
  308. int tie(){
  309. for (int a= 0; a<9;a++){
  310. for (int b=0;b<9;b++){
  311. for(int num=0;num<9;num++){
  312. if (matrix[a][b]!=num){
  313. printf("Empate");
  314.  
  315. }
  316. }
  317. }
  318. final= 0;
  319. }
  320. }
  321.  
  322. int checkposition(){
  323. int matrix[3][3] = {};
  324. int vocal, number, XO;
  325.  
  326. vocal = 1;
  327.  
  328. for(XO=0;XO<9;XO++){
  329.  
  330. switch(number){
  331.  
  332. case 1 :
  333. if(matrix[0][0]==0){
  334. matrix[0][0]=vocal;
  335. if(vocal==1){
  336. vocal=2;
  337. }
  338. else{
  339. vocal=1;
  340. }
  341. }
  342. else{
  343. printf("We can't fill it'"); // Si digita una casillla previamente ocupada
  344. XO=XO--;
  345. getch();
  346. }
  347. break;
  348.  
  349. case 2 :
  350. if(matrix[0][1]==0){
  351. matrix[0][1]=vocal;
  352. if(vocal==1){
  353. vocal=2;
  354. }
  355. else{
  356. vocal=1;
  357. }
  358. }
  359. else{
  360. printf("We can't fill it '");
  361. XO=XO--;
  362. getch();
  363. }
  364. break;
  365.  
  366. case 3 :
  367. if(matrix[0][2]==0){
  368. matrix[0][2]=vocal;
  369. if(vocal==1){
  370. vocal=2;
  371. }
  372. else{
  373. vocal=1;
  374. }
  375. }
  376. else{
  377. printf("We can't fill it");
  378. XO=XO--;
  379. getch();
  380. }
  381. break;
  382.  
  383. case 4 :
  384. if(matrix[1][0]==0){
  385. matrix[1][0]=vocal;
  386. if(vocal==1){
  387. vocal=2;
  388. }
  389. else{
  390. vocal=1;
  391. }
  392. }
  393. else{
  394. printf("We can´t fill it ");
  395. XO=XO--;
  396. getch();
  397. }
  398. break;
  399.  
  400. case 5 :
  401. if(matrix[1][1]==0){
  402. matrix[1][1]=vocal;
  403. if(vocal==1){
  404. vocal=2;
  405. }
  406. else{
  407. vocal=1;
  408. }
  409. }
  410. else{
  411. printf("We can´t fill this square");
  412. XO=XO--;
  413. getch();
  414. }
  415. break;
  416.  
  417. case 6 :
  418. if(matrix[1][2]==0){
  419. matrix[1][2]=vocal;
  420. if(vocal==1){
  421. vocal=2;
  422. }
  423. else{
  424. vocal=1;
  425. }
  426. }
  427. else{
  428. printf("We can't fill this square");
  429. XO=XO--;
  430. getch();
  431. }
  432. break;
  433.  
  434. case 7 :
  435. if(matrix[2][0]==0){
  436. matrix[2][0]= vocal;
  437. if(vocal==1){
  438. vocal=2;
  439. }
  440. else{
  441. vocal=1;
  442. }
  443. }
  444. else{
  445. printf("We can't fill this square'");
  446. XO=XO--;
  447. getch();
  448. }
  449. break;
  450.  
  451. case 8 :
  452. if(matrix[2][1]==0){
  453. matrix[2][1]=vocal;
  454. if(vocal==1){
  455. vocal=2;
  456. }
  457. else{
  458. vocal=1;
  459. }
  460. }
  461. else{
  462. printf("We can't fill it");
  463. XO=XO--;
  464. getch();
  465. }
  466. break;
  467.  
  468. case 9 :
  469. if(matrix[2][2]==0){
  470. matrix[2][2]=vocal;
  471. if(vocal==1){
  472. vocal=2;
  473. }
  474. else{
  475. vocal=1;
  476. }
  477. }
  478. else{
  479. printf("We can't fill this square'");
  480. XO=XO--;
  481. getch();
  482. }
  483. break;
  484.  
  485. default:
  486. printf("This is not a available square");
  487. XO=XO--;
  488. getch();
  489. break;
  490. // Cuando elige una casilla inexistente
  491.  
  492. }
  493. }
  494.  
  495. }
Add Comment
Please, Sign In to add comment