Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- using namespace std;
- int boardsize;
- class some{
- public:
- int x;
- int y;
- int cl;
- int num;
- int max;
- };
- some squares[5][5];
- int turn = 0;
- int cordavez = 0, qdejogadores = 3;
- bool jogando = true;
- bool Putball(int x, int y, int cor);
- void Corrector();
- bool Verify();
- void Print();
- int Findwinners();
- bool Isdead(int cor);
- int main()
- {
- boardsize = 5;
- int i =0;
- int j =0;
- for(i = 0; i < boardsize;i++){
- for(j=0; j<boardsize; j++){
- squares[i][j].cl= -1;
- squares[i][j].x = i;
- squares[i][j].y = j;
- squares[i][j].num = 0;
- squares[i][j].max = 3;
- if(i == boardsize || i == 0 || j == boardsize || j == 0){
- squares[i][j].max = 2;
- }
- if(i == boardsize && j == boardsize || i == boardsize && j == 0|| i==0 && j ==0 || i==0 && j== boardsize){
- squares[i][j].max = 1;
- }
- }
- }
- while(jogando)
- {
- if(Findwinners() != -1){
- break;
- }
- while(Verify())
- {
- Corrector();
- Print();
- }
- Print();
- int xescolhido, yescolhido;
- Corrector();
- while(Isdead(cordavez)){
- cordavez++;
- if(cordavez > qdejogadores )
- {
- cordavez = 0;
- }
- }
- cout <<"jogador " ;
- cout << cordavez;
- cout << " sua vez";
- cin >> xescolhido;
- cin >> yescolhido;
- bool validmove;
- validmove = Putball(xescolhido,yescolhido,cordavez);
- if(validmove == true){
- cordavez++;
- turn++;
- }
- if(cordavez > qdejogadores)
- {
- cordavez = 0;
- }
- }
- cout <<"jogador " ;
- cout << Findwinners();
- cout << " ganha";
- system("PAUSE");
- }
- bool Putball(int x, int y, int cor)
- {
- int nextright,nextleft,nextup,nextdown;
- nextright = x+1;
- nextleft = x-1;
- nextup = y+1;
- nextdown = y-1;
- if(x>boardsize || x < 0 || y > boardsize || y<0){
- return false;
- }
- if(squares[x][y].num == 0 || (squares[x][y].num != 0 && squares[x][y].cl == cor))
- {
- squares[x][y].num++;
- squares[x][y].cl = cor;
- if(squares[x][y].num > squares[x][y].max)
- {
- squares[x][y].num = 0;
- if(nextright < 0 || nextright > boardsize)
- {
- }else{
- squares[x+1][y].num++;
- squares[x+1][y].cl = cor;
- }
- if(nextleft < 0 || nextleft > boardsize)
- {
- }else{
- squares[x-1][y].num++;
- squares[x-1][y].cl = cor;
- }
- if(nextup < 0 || nextup > boardsize)
- {
- }else{
- squares[x][y+1].num++;
- squares[x][y+1].cl = cor;
- }
- if(nextdown < 0 || nextdown > boardsize)
- {
- }else{
- squares[x][y-1].num++;
- squares[x][y-1].cl = cor;
- }
- }
- return true;
- }else{
- return false;
- }
- }
- void Corrector()
- {
- bool exit = false;
- int i,j;
- for(i = 0; i < boardsize;i++){
- for(j=0; j<boardsize; j++){
- int nextright,nextleft,nextup,nextdown;
- int x,y;
- x = i;
- y = j;
- nextright = x+1;
- nextleft = x-1;
- nextup = y+1;
- nextdown = y-1;
- if(squares[i][j].num > squares[i][j].max)
- {
- squares[x][y].num = 0;
- if(nextright < 0 || nextright > boardsize)
- {
- }else{
- squares[x+1][y].num++;
- squares[x+1][y].cl = cordavez-1;
- }
- if(nextleft < 0 || nextleft > boardsize)
- {
- }else{
- squares[x-1][y].num++;
- squares[x-1][y].cl = cordavez-1;
- }
- if(nextup < 0 || nextup > boardsize)
- {
- }else{
- squares[x][y+1].num++;
- squares[x][y+1].cl = cordavez-1;
- }
- if(nextdown < 0 || nextdown > boardsize)
- {
- }else{
- squares[x][y-1].num++;
- squares[x][y-1].cl = cordavez-1;
- } //Putball(i,j,squares[i][j].cl);
- exit = true;
- break;
- }
- }
- if(exit)
- {
- break;
- }
- }
- }
- bool Verify()
- {
- bool exit = false;
- int i,j;
- for(i = 0; i < boardsize;i++){
- for(j=0; j<boardsize; j++){
- if(squares[i][j].num > squares[i][j].max)
- {
- //Putball(i,j,squares[i][j].cl);
- exit = true;
- break;
- }
- }
- if(exit)
- {
- break;
- }
- }
- if(exit){
- return true;
- }else{
- return false;
- }
- }
- void Print(){
- int i,j;
- for(i = 0; i < boardsize;i++){
- for(j=0; j<boardsize; j++){
- cout << squares[i][j].num;
- cout << " c > " ;
- cout << squares[i][j].cl;
- cout << " " ;
- }
- cout << "" << endl;
- }
- }
- int Findwinners()
- {
- if(turn < qdejogadores)
- {
- return -1;
- }
- bool clexist[qdejogadores];
- bool anothercontenderisalive = false;
- bool nowinner = false;
- int i,j,k,p,g, winner;
- for(k = 0; k < qdejogadores; k++)
- {
- clexist[k] = false;
- }
- for(i = 0; i < boardsize;i++){
- for(j=0; j<boardsize; j++){
- if(squares[i][j].cl != -1)
- {
- clexist[squares[i][j].cl] = true;
- }
- }
- }
- for(p = 0; p < qdejogadores;p++)
- {
- if(clexist[p] == true){
- winner = p;
- for(g = 0; g < qdejogadores; g++)
- {
- if(p != g)
- {
- if(clexist[p] == clexist[g])
- {
- nowinner = true;
- }
- }
- }
- }
- }
- if(!nowinner)
- {
- return winner;
- }else{
- return -1;
- }
- }
- bool Isdead(int cor)
- {
- bool exit = false;
- int i,j;
- if(turn <= qdejogadores)
- {
- return false;
- }
- for(i = 0; i < boardsize;i++){
- for(j=0; j<boardsize; j++){
- if(squares[i][j].cl == cor)
- {
- exit = true;
- break;
- }
- }
- if(exit)
- {
- break;
- }
- }
- if(exit)
- {
- return false;
- }else{
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement