Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- char tab[3][3];
- int i,j,numer,pyt,tabs[9];
- void wypelnij_na_pusto(char tab[3][3]){
- for (i=0; i<3; i++){
- for (j=0; j<3; j++){
- tab[i][j]='-';
- }
- }
- }
- void wyswietl_pole(char tab[3][3]){
- for (i=0; i<3; i++){
- for (j=0; j<3; j++){
- printf(" %c",tab[i][j]);printf(" ");
- }
- printf("\n");
- }
- }
- void instrukcja(){
- printf("\n\nINSTRUKCJA GRY:\nPola sa oznaczone od 1 do 9, co trzy pola wiersz sie zawija.\n");
- printf("A wiec dla przykladu, gdy wybierzesz pole numer 4 wpiszesz znak X do pola:\n");
- printf("- - -\nX - -\n- - -\n");
- printf("GRACZ 1: Stawia O \nGRACZ 2: Stawia X\n\nZACZYNAJMY WIEC GRE!!!\n\n");
- }
- void instrukcja_komp(){
- printf("\n\nINSTRUKCJA GRY:\nPola sa oznaczone od 1 do 9, co trzy pola wiersz sie zawija.\n");
- printf("A wiec dla przykladu, gdy wybierzesz pole numer 4 wpiszesz znak X do pola:\n");
- printf("- - -\nX - -\n- - -\n");
- printf("GRACZ 1: to Ty stawiasz O\nGRACZ 2: to komputer\n\nZACZYNAJMY WIEC GRE!!!\n\n");
- }
- void wpisz_znak_gracz1(char tab[3][3], int numer){
- if (numer == 1){tab[0][0]='O';}
- if (numer == 2){tab[0][1]='O';}
- if (numer == 3){tab[0][2]='O';}
- if (numer == 4){tab[1][0]='O';}
- if (numer == 5){tab[1][1]='O';}
- if (numer == 6){tab[1][2]='O';}
- if (numer == 7){tab[2][0]='O';}
- if (numer == 8){tab[2][1]='O';}
- if (numer == 9){tab[2][2]='O';}
- }
- void wpisz_znak_gracz2(char tab[3][3], int numer){
- if (numer == 1){tab[0][0]='X';}
- if (numer == 2){tab[0][1]='X';}
- if (numer == 3){tab[0][2]='X';}
- if (numer == 4){tab[1][0]='X';}
- if (numer == 5){tab[1][1]='X';}
- if (numer == 6){tab[1][2]='X';}
- if (numer == 7){tab[2][0]='X';}
- if (numer == 8){tab[2][1]='X';}
- if (numer == 9){tab[2][2]='X';}
- }
- int sprawdz_gracz1(char tab[3][3]){
- if ((tab[0][0]=='O') && (tab[0][1]=='O') && (tab[0][2]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[1][0]=='O') && (tab[1][1]=='O') && (tab[1][2]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[2][0]=='O') && (tab[2][1]=='O') && (tab[2][2]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[0][0]=='O') && (tab[1][0]=='O') && (tab[2][0]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[0][1]=='O') && (tab[1][1]=='O') && (tab[2][1]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[0][2]=='O') && (tab[1][2]=='O') && (tab[2][2]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[0][0]=='O') && (tab[1][1]=='O') && (tab[2][2]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- if ((tab[0][2]=='O') && (tab[1][1]=='O') && (tab[2][0]=='O')){
- printf("Wygral gracz 1");
- return 1;
- }
- else{
- return 0;
- }
- }
- int sprawdz_gracz2(char tab[3][3]){
- if ((tab[0][0]=='X') && (tab[0][1]=='X') && (tab[0][2]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[1][0]=='X') && (tab[1][1]=='X') && (tab[1][2]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[2][0]=='X') && (tab[2][1]=='X') && (tab[2][2]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[0][0]=='X') && (tab[1][0]=='X') && (tab[2][0]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[0][1]=='X') && (tab[1][1]=='X') && (tab[2][1]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[0][2]=='X') && (tab[1][2]=='X') && (tab[2][2]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[0][0]=='X') && (tab[1][1]=='X') && (tab[2][2]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- if ((tab[0][2]=='X') && (tab[1][1]=='X') && (tab[2][0]=='X')){
- printf("Wygral gracz 2");
- return 1;
- }
- else{
- return 0;
- }
- }
- int sprawdz_tablice(char tab[3][3]){
- int licznik=0;
- for (i=0; i<3; i++){
- for (j=0; j<3; j++){
- if (tab[i][j]=='-'){
- licznik++;
- }
- }
- }
- if (licznik==0){printf("No niestety nikt nie wygral, ale dzieki za zabawe!! :)");return 0;}
- else{return 1;}
- }
- int sprawdz_pole(int tabs[9], int numer){
- int licznik;
- licznik=0;
- for (i=0; i<9; i++){
- if (tabs[i]==numer){
- licznik++;
- }
- }
- if (licznik==0){
- return 1;
- }
- else{printf("Te pole jest zajete, wprowadz poprawny numer pola! :(\n");return 0;}
- }
- int sprawdz_pole_komp(int tabs[9], int numer){
- int licznik;
- licznik=0;
- for (i=0; i<9; i++){
- if (tabs[i]==numer){
- licznik++;
- }
- }
- if (licznik==0){
- return 1;
- }
- else{return 0;}
- }
- int main(){
- srand(time(0));
- int wybor,licznik,losowa;
- numer=0; licznik=0;
- wypelnij_na_pusto(tab);
- printf("Siemanko!\nWybierz w jaki tryb gry chcesz zagrac: \n");
- printf("[1] - wersja z komputerem\n");
- printf("[2] - wersja dla 2 osob\n");
- scanf("%d",&wybor);
- if (wybor == 1){
- system("cls");
- printf("Tak wyglada plansza:\n");
- wyswietl_pole(tab);
- instrukcja_komp();
- do{
- for (i=0; i<9; i++){tabs[i]=0;}
- wypelnij_na_pusto(tab);
- licznik=0;
- do{
- if ((licznik % 2)==0){
- printf("GRACZ 1: podaj numer gdzie mam postawic O?\n");
- do{
- scanf("%d",&numer);
- }
- while(sprawdz_pole(tabs,numer)==0);
- tabs[licznik]=numer;
- system("cls");
- wpisz_znak_gracz1(tab,numer);
- wyswietl_pole(tab);
- }
- else{
- do{
- losowa=rand() % 10;
- }
- while(sprawdz_pole_komp(tabs,losowa)==0);
- tabs[licznik]=losowa;
- wpisz_znak_gracz2(tab,losowa);
- system("cls");
- printf("Komputer wybral pole: %d",losowa);printf("\n");
- wyswietl_pole(tab);
- }
- licznik++;
- }
- while ((sprawdz_gracz1(tab)==0)&&(sprawdz_gracz2(tab)==0)&&(sprawdz_tablice(tab)==1));
- printf("\nCzy chcesz zagrac jeszcze raz 1 - tak 0 - nie\n");
- scanf("%d",&pyt);
- system("cls");
- }
- while (pyt==1);
- }
- else if (wybor==2){
- system("cls");
- printf("Tak wyglada aktualnie plansza:\n");
- wyswietl_pole(tab);
- instrukcja();
- do{
- for (i=0; i<9; i++){tabs[i]=0;}
- wypelnij_na_pusto(tab);
- licznik=0;
- do{
- if ((licznik % 2)==0){
- printf("GRACZ 1: podaj numer gdzie mam postawic O?\n");
- do{
- scanf("%d",&numer);
- }
- while(sprawdz_pole(tabs,numer)==0);
- tabs[licznik]=numer;
- system("cls");
- wpisz_znak_gracz1(tab,numer);
- wyswietl_pole(tab);
- }
- else{
- printf("GRACZ 2: podaj numer gdzie mam postawic X?\n");
- do{
- scanf("%d",&numer);
- }
- while(sprawdz_pole(tabs,numer)==0);
- tabs[licznik]=numer;
- system("cls");
- wpisz_znak_gracz2(tab,numer);
- wyswietl_pole(tab);
- }
- licznik++;
- }
- while ((sprawdz_gracz1(tab)==0)&&(sprawdz_gracz2(tab)==0)&&(sprawdz_tablice(tab)==1));
- printf("\nCzy chcesz zagrac jeszcze raz 1 - tak 0 - nie\n");
- scanf("%d",&pyt);
- system("cls");
- }
- while (pyt==1);
- }
- else{
- system("cls");
- printf("Podaj poprawny numer gamoniu!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement