Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <time.h>
- using namespace std;
- void WypiszPlansze(char plansza[3][3])
- {
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<3; j++)
- {
- cout<<plansza[i][j]<<" | ";
- }
- cout<<endl;
- }
- }
- bool IsWon(char plansza[3][3])
- {
- char X = 'X';
- int i =0, j=0;
- bool vartical = false;
- for(i=0; i<3; i++)
- {
- int howManyX =0;
- int howManyO =0;
- for(j=0; j<3; j++)
- {
- if(vartical = false)
- {
- if(plansza[i][j] == 'X')
- {
- howManyX +=1;
- }
- else if(plansza[i][j] == 'O')
- {
- howManyO +=1;
- }
- }
- else
- {
- if(plansza[j][i] == 'X')
- {
- howManyX +=1;
- }
- else if(plansza[j][i] == 'O')
- {
- howManyO +=1;
- }
- }
- }
- if(howManyX == 3)
- {
- cout<<"Wygral X"<<endl;
- return true;
- }
- else if(howManyO == 3)
- {
- cout<<"Wygral O"<<endl;
- return true;
- }
- if(i == 3)
- {
- vartical = true;
- i=0;
- }
- }
- for(i = 0; i<3; i++)
- {
- if(plansza[i][i]) //TODO: z ukosami
- }
- for(int i=0; i<2; i++)
- {
- if(plansza[0][0]==X&&plansza[0][1]==X&&plansza[0][2]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[1][0]==X&&plansza[1][1]==X&&plansza[1][2]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[2][0]==X&&plansza[2][1]==X&&plansza[2][2]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[0][0]==X&&plansza[1][0]==X&&plansza[2][0]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[0][1]==X&&plansza[1][1]==X&&plansza[2][1]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[0][2]==X&&plansza[1][2]==X&&plansza[2][2]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[0][0]==X&&plansza[1][1]==X&&plansza[2][2]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- if(plansza[0][2]==X&&plansza[1][1]==X&&plansza[2][0]==X)
- {
- cout<<"wygrana "<<X<< endl;
- return true;
- }
- X = 'O';
- }
- return false;
- }
- bool IsGameFinished(char plansza[3][3])
- {
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<3; j++)
- {
- if(plansza[i][j]==' ')
- {
- return false;
- }
- }
- }
- return true;
- }
- bool IsPlaceTaken(char plansza[3][3], int x, int y)
- {
- if(plansza[x][y] != ' ')
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int main()
- {
- char plansza[3][3];
- char X = 'X';
- char O = 'O';
- int wspolrzedne[2];
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<3; j++)
- {
- plansza[i][j]=' ';
- }
- }
- WypiszPlansze(plansza);
- cout<<"Podaj wspolrzedne Twojego ruchu:\n";
- char focusedPlayer = X;
- while(true)
- {
- cout<<"Kolejny ruch:\n";
- cin>>wspolrzedne[0];
- cin>>wspolrzedne[1];
- if(IsPlaceTaken(plansza, wspolrzedne[0], wspolrzedne[1]))
- {
- cout<<"Podane miejsce jest zajete\n";
- continue;
- }
- plansza[wspolrzedne[0]] [wspolrzedne[1]] = focusedPlayer;
- if(IsWon(plansza))
- {
- return 0;
- }
- else if(IsGameFinished(plansza))
- {
- cout<<"nikt nie wygral"<< endl;
- return 0;
- }
- if(focusedPlayer == X)
- {
- focusedPlayer = O;
- }
- else
- {
- focusedPlayer = X;
- }
- system("cls");
- WypiszPlansze(plansza);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement