alex.angels Posted August 31, 2012 Report Posted August 31, 2012 (edited) Bun? ziua ,am f?cut un program în care s? joci x ?i 0 în c++ ... dar am o problem? , la sfâr?it ca s? joc înc? o dat? îmi d? remiz? dup? ce pun numele juc?torilor .?sta e codul , ce trebuie s? adaug ca s? joc din nou f?r? s? închid programul ?#include "iostream.h"#include "conio.h"#include "stdlib.h"int main(){ int i,j,k=0; char a[3][3],nume1[30],nume2[30],q,v,castig='r'; begin: system("color A"); cout<<"jucator1:";cin>>nume1; system("cls"); cout<<"jucator2:";cin>>nume2; system("cls"); a[0][0]='1'; a[0][1]='2'; a[0][2]='3'; a[1][0]='4'; a[1][1]='5'; a[1][2]='6'; a[2][0]='7'; a[2][1]='8'; a[2][2]='9'; while((k<=9)&&(castig=='r')) { cout<<"Introduceti un numar:"<<endl; cout<<a[0][0]<<" "<<a[0][1]<<" "<<a[0][2]<<endl; cout<<a[1][0]<<" "<<a[1][1]<<" "<<a[1][2]<<endl; cout<<a[2][0]<<" "<<a[2][1]<<" "<<a[2][2]<<endl; cout<<nume1<<":"; cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='X'; k++; if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])) castig=a[0][0]; if((a[0][0]==a[2][0])&&(a[2][0]==a[1][0])) castig=a[0][0]; if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])) castig=a[1][0]; if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])) castig=a[2][0]; if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])) castig=a[0][1]; if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])) castig=a[0][2]; if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])) castig=a[0][0]; if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])) castig=a[0][0]; system("cls"); if(castig=='r') { cout<<"Introduceti un numar:"<<endl; cout<<a[0][0]<<" "<<a[0][1]<<" "<<a[0][2]<<endl; cout<<a[1][0]<<" "<<a[1][1]<<" "<<a[1][2]<<endl; cout<<a[2][0]<<" "<<a[2][1]<<" "<<a[2][2]<<endl; cout<<nume2<<":"; cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='0'; k++; system("cls"); if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])) castig=a[0][0]; if((a[0][0]==a[2][0])&&(a[2][0]==a[1][0])) castig=a[0][0]; if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])) castig=a[1][0]; if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])) castig=a[2][0]; if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])) castig=a[0][1]; if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])) castig=a[0][2]; if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])) castig=a[0][0]; if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])) castig=a[0][0]; } } system("color C"); if(castig=='X') cout<<nume1<<" a castigat"<<endl; if(castig=='0') cout<<nume2<<" a castigat"<<endl; if(castig=='r') cout<<"REMIZA!"<<endl; cout<<"Apasati q pentru a iesi si c pentru a mai juca:"<<endl; cin>>v; if(v=='q') return 0; if(v=='c') { system("cls"); goto begin; }} Edited September 4, 2012 by alex.angels Quote
fulminator Posted August 31, 2012 Report Posted August 31, 2012 (edited) simplu. nu folosi goto.Citeste ce a zis Denis Ritchie despre "goto" si "label". Sunt foarte puternice, dar... mai bine fara. Incearca sa pui un label inainte de begin, desi nu am habar cum se foloseste labelul.Parerea mea: inca o bucla while cu un meniu.while (cin>>opt != "I") {1. [J]OC NOU2. [R]EZULTATE3. ESIRE}Cu un switch le imparti in bucati de cod. Bucla while se executa pana cand este apasata tasta "I" sau "3". Ai inca un quest acum: implementeaza highscore. Edited August 31, 2012 by fulminator Quote
cmiN Posted August 31, 2012 Report Posted August 31, 2012 Sa-ti pui tot "jocul" intr-o functie, iar in main sa pui o bucla care apeleaza acea functie si se intrerupe doar cand utilizatorul raspunde cu (N, NU, Nu, 0, etc.) la intrebarea daca mai vrea sa mai joace o data. Te sfatuiesc sa lasi la o parte obiceiul malefic de a folosi goto. Solutia nu este una particulara, de aceea nici nu am dat prea multa atentie la cod, ci un mod general de a repeta un task: pui instructiunile intr-o functie si reapelezi acea functie cat timp este nevoie. Quote
fulminator Posted August 31, 2012 Report Posted August 31, 2012 Sa-ti pui tot "jocul" intr-o functie, iar in main sa pui o bucla care apeleaza acea functie si se intrerupe doar cand utilizatorul raspunde cu (N, NU, Nu, 0, etc.) la intrebarea daca mai vrea sa mai joace o data. Te sfatuiesc sa lasi la o parte obiceiul malefic de a folosi goto. Solutia nu este una particulara, de aceea nici nu am dat prea multa atentie la cod, ci un mod general de a repeta un task: pui instructiunile intr-o functie si reapelezi acea functie cat timp este nevoie.Bre... omu' nu lucreaza OOP. lucreaza structurat. da-i niste timp. Quote
ionut.hulub Posted August 31, 2012 Report Posted August 31, 2012 (edited) adauga linia "k = 0; castig = 'r';" imediat sub eticheta begin.eventual poti sa stergi initializarile din dreptul declaratiilor de variabile.faci niste greseli foarte mari sa stii.in primu rand obisnuieste'te sa folosesti:#include <iostream>using namespace std;in loc de #include <iostream.h>Variata a doua e invechita, mie imi dadea si eraore de compilare in visual studio 2012.Pe deasupra se vede ca nu stapanesti bine limbaju.Nu vreau sa te critic... iti dau doar niste sfaturi ca sa inveti mai repede. a[0][0]='1'; a[0][1]='2'; a[0][2]='3'; a[1][0]='4'; a[1][1]='5'; a[1][2]='6'; a[2][0]='7'; a[2][1]='8'; a[2][2]='9';trebuia sa scrii asa:for (char contor = '1'; contor <= '9'; contor++) a[(contor - 48) / 3][(contor - 48) % 3] = contorcu asta ai salvat sapta randuri.apropo, contor fiind de tip char, o sa contina numarul 49 in loc de 1, 50 in loc de 2 samd. din cauza asta faza cu contor-48mai departe: cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='X';se scrie mai frumos asa:cin>>q;a[(q - 48) / 3][(q - 48) % 3] == q ? a[(q - 48) / 3][(q - 48) % 3] = 'X' : cout<<""; //do nothingMai repeta putin sintaxa limbajului si mai ales invata algoritmica.In rest felicitari... programul merge bine * Edited August 31, 2012 by ionut.hulub Quote
ionut.hulub Posted August 31, 2012 Report Posted August 31, 2012 Bre... omu' nu lucreaza OOP. lucreaza structurat. da-i niste timp.Ce treaba are OOP cu ce l'a sfatuit cmiN?O bucla while nu inseamna programare orientata obiect.Are dreptate cmiN de altfel, am uitat sa adaug in postul meu, dar in general etichetele in combinatie cu "goto" ar trebui efitate ori de cate ori este posibil.Face codul foarte greu de urmarit, mai ales in proiecte mari, si mai apar si alte complicatii. Quote
banks Posted August 31, 2012 Report Posted August 31, 2012 bagi tot in int a=1;while(a==1){//jocustring b;cout<<"Vrei sa joci iara?;cin>>b;if(b!="da")break;} Quote
SilviuSDS Posted August 31, 2012 Report Posted August 31, 2012 (edited) Simplu, 2 functii, una sa verifice castigatorul, alta cu codul jocului, functia ce verifica castigatorul e de tipul string si returneaza numele castigatorului sau egalitate, in cazul in care e egalitate returneaza draw, daca jocul inca nu s-a terminat returneaza null. Cand jocul s-a terminat, ai o var end de ex pe care o setezi true iar asta se intampla intr-o alta functie care nu este main. In main pui asa: while(!exit) { //exit e un boolean//afisezi meniucin<<menu; //int menu;switch(menu) {case 1: gamefunc(); //joc noucase 2: //afisezi rezultate bla bla blacase 3: exit = true; //iesi din program;}gamefunc()while(!end) {play();if(verifCastigator() != null) {end = true;//afisezi castigator sau egalitate si setezi parametri ce memoreaza rezultatele}}Ia de aici joc de X si O facut de mine cu UI in c++ folosind VS 2010. Se schimba culorile scorului atunci cand e egalitate sau cand cineva este in avantaj si bla bla bla, dig in and see.http://www.mediafire.com/?cr56blb868z2c8r Edited August 31, 2012 by SilviuSDS Quote
parazitul29 Posted September 1, 2012 Report Posted September 1, 2012 #include "iostream.h"#include "conio.h"#include "stdlib.h"int main(){ int i,j,k=0; char a[3][3],nume1[30],nume2[30],q,v,castig='r'; system("color A"); cout<<"jucator1:";cin>>nume1; system("cls"); cout<<"jucator2:";cin>>nume2; system("cls"); a[0][0]='1'; a[0][1]='2'; a[0][2]='3'; a[1][0]='4'; a[1][1]='5'; a[1][2]='6'; a[2][0]='7'; a[2][1]='8'; a[2][2]='9'; while((k<=9)&&(castig=='r')) { cout<<"Introduceti un numar:"<<endl; cout<<a[0][0]<<" "<<a[0][1]<<" "<<a[0][2]<<endl; cout<<a[1][0]<<" "<<a[1][1]<<" "<<a[1][2]<<endl; cout<<a[2][0]<<" "<<a[2][1]<<" "<<a[2][2]<<endl; cout<<nume1<<":"; cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='X'; k++; if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])) castig=a[0][0]; if((a[0][0]==a[2][0])&&(a[2][0]==a[1][0])) castig=a[0][0]; if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])) castig=a[1][0]; if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])) castig=a[2][0]; if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])) castig=a[0][1]; if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])) castig=a[0][2]; if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])) castig=a[0][0]; if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])) castig=a[0][0]; system("cls"); if(castig=='r') { cout<<"Introduceti un numar:"<<endl; cout<<a[0][0]<<" "<<a[0][1]<<" "<<a[0][2]<<endl; cout<<a[1][0]<<" "<<a[1][1]<<" "<<a[1][2]<<endl; cout<<a[2][0]<<" "<<a[2][1]<<" "<<a[2][2]<<endl; cout<<nume2<<":"; cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='0'; k++; system("cls"); if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])) castig=a[0][0]; if((a[0][0]==a[2][0])&&(a[2][0]==a[1][0])) castig=a[0][0]; if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])) castig=a[1][0]; if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])) castig=a[2][0]; if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])) castig=a[0][1]; if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])) castig=a[0][2]; if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])) castig=a[0][0]; if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])) castig=a[0][0]; } } system("color C"); if(castig=='X') cout<<nume1<<" a castigat"<<endl; if(castig=='0') cout<<nume2<<" a castigat"<<endl; if(castig=='r') cout<<"REMIZA!"<<endl; cout<<"Apasati q pentru a iesi si c pentru a mai juca:"<<endl; cin>>v; if(v=='q') return 0; if(v=='c') { system("cls"); main(); }}in loc de begin executi iar functia main() Quote
alex.angels Posted September 1, 2012 Author Report Posted September 1, 2012 #include "iostream.h"#include "conio.h"#include "stdlib.h"int main(){ int i,j,k=0; char a[3][3],nume1[30],nume2[30],q,v,castig='r'; system("color A"); cout<<"jucator1:";cin>>nume1; system("cls"); cout<<"jucator2:";cin>>nume2; system("cls"); a[0][0]='1'; a[0][1]='2'; a[0][2]='3'; a[1][0]='4'; a[1][1]='5'; a[1][2]='6'; a[2][0]='7'; a[2][1]='8'; a[2][2]='9'; while((k<=9)&&(castig=='r')) { cout<<"Introduceti un numar:"<<endl; cout<<a[0][0]<<" "<<a[0][1]<<" "<<a[0][2]<<endl; cout<<a[1][0]<<" "<<a[1][1]<<" "<<a[1][2]<<endl; cout<<a[2][0]<<" "<<a[2][1]<<" "<<a[2][2]<<endl; cout<<nume1<<":"; cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='X'; k++; if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])) castig=a[0][0]; if((a[0][0]==a[2][0])&&(a[2][0]==a[1][0])) castig=a[0][0]; if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])) castig=a[1][0]; if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])) castig=a[2][0]; if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])) castig=a[0][1]; if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])) castig=a[0][2]; if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])) castig=a[0][0]; if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])) castig=a[0][0]; system("cls"); if(castig=='r') { cout<<"Introduceti un numar:"<<endl; cout<<a[0][0]<<" "<<a[0][1]<<" "<<a[0][2]<<endl; cout<<a[1][0]<<" "<<a[1][1]<<" "<<a[1][2]<<endl; cout<<a[2][0]<<" "<<a[2][1]<<" "<<a[2][2]<<endl; cout<<nume2<<":"; cin>>q; for(i=0;i<=2;i++) for(j=0;j<=2;j++) if(q==a[i][j]) a[i][j]='0'; k++; system("cls"); if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])) castig=a[0][0]; if((a[0][0]==a[2][0])&&(a[2][0]==a[1][0])) castig=a[0][0]; if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])) castig=a[1][0]; if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])) castig=a[2][0]; if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])) castig=a[0][1]; if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])) castig=a[0][2]; if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])) castig=a[0][0]; if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])) castig=a[0][0]; } } system("color C"); if(castig=='X') cout<<nume1<<" a castigat"<<endl; if(castig=='0') cout<<nume2<<" a castigat"<<endl; if(castig=='r') cout<<"REMIZA!"<<endl; cout<<"Apasati q pentru a iesi si c pentru a mai juca:"<<endl; cin>>v; if(v=='q') return 0; if(v=='c') { system("cls"); main(); }}in loc de begin executi iar functia main()Mul?umesc , a func?ionat nu creadeam c? este a?a u?or. Quote