Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Assignment: 4
- Campus: Ashdod
- Author1: David Sankovsky , ID: 308604065
- Author2: Galit Nahari, ID: 201156650
- Date: 5/5/2011 */
- #include "Team.h"
- #include "Defender.h"
- #include "Goalkeeper.h"
- #include "Striker.h"
- #include <iostream>
- #include <assert.h>
- using namespace std;
- /*
- The following two methods are overloaded "print" methods, the first, printing all available players, the second one, all available teams.
- */
- void print(Rabbid **players,int size)
- {
- for (int i=0;i<size;i++)
- {
- players[i]->Print();
- }//end of the for loop
- }//end of the player printing function
- void print(Team **teams,int size)
- {
- for(int i=0;i<size;i++)
- {
- teams[i]->print();
- }//end of the for loop
- }//end of the team printing function
- /*
- the following method recieves all currently availble players (available players defined as a legitimate player - which means it has a legitimate name and numrical value and not assigned to a team). then counts how many players are there in the three types (keepers, strikers and defenders) and determines if there are enough players to build a valid team - used before calling the team ctor that build the best available team to insure it has what to choose from.
- */
- bool enoughPlayersToCreateTeam(Rabbid **players,int size)
- {
- int numOfKeepers=0,numOfStrikers=0,numOfDefenders=0;
- for (int i=0;i<size;i++)
- {
- switch (players[i]->getClassType())
- {
- case 0:
- numOfKeepers++;
- break;
- case 1:
- numOfDefenders++;
- break;
- case 2:
- numOfStrikers++;
- break;
- }//end of the switch clause/
- }//end of the for loop
- return (numOfKeepers>0 && numOfStrikers>0 && numOfDefenders>0);
- }//end of the "enoughPlayersToCreateTeam" function
- /*
- the following function recieves a potential name for a player, then checks all the players in the game (currently free players and those already in teams) for their names. If that name already exsists, the function will return a false value, indicating this name cannot be used. used in the addPlayer function.
- */
- bool isNameAvailable(string name,Rabbid **players,int size,Team **teams,int numOfTeams)
- {
- for (int i=0;i<size;i++)
- {
- if(name.compare(players[i]->getName()))
- return false;
- }//end of the for loop.
- for (int i=0;i<numOfTeams;i++)
- {
- for (int j=0;j<3;j++)
- {
- if(name.compare(teams[i]->getPlayerName(j)))
- {
- return false;
- }//end of the if clause.
- }//end of the nested for loop.
- }//end of the harboring for loop.
- return true;
- }//end of the isNameAvailable function.
- /*
- The following method adds a new player to the available players list - it allows to create one of the three available classes and allows the creation of devault and custom ones of each and every one of them. The function also uses the teams list but only in order to send it to the "isNameAvailable" function that checks if the name selected by the user is free to use or already given to another player.
- */
- Rabbid** addPlayer(Rabbid **players,int size,Team **teams,int numOfTeams)
- {
- int playerType,buildType,reaction,kick,tackle;
- string name;
- Rabbid **temp= new Rabbid*[size];
- assert(temp!=NULL);
- for (int i=0;i<size;i++)
- {
- temp[i]=players[i];
- }//end of the for loop.
- players = new Rabbid*[size+1];
- assert(players!=NULL);
- for (int i=0;i<size;i++)
- {
- players[i]=temp[i];
- }//end of the for loop
- do
- {
- system("cls");
- cout<<"Please select the type of player you want to create:"<<endl<<"1)\tGoalkeeper"<<endl<<"2)\tStriker"<<endl<<"3) \tDefender"<<endl;
- cin>>playerType;
- }while(!(playerType==1 || playerType==2 ||playerType==3));//end of the do while clause.
- switch (playerType)
- {
- case 1:
- do
- {
- cout<<"Would you like to build a random Goalkeeper (0) or set his name and reaction yourself (1)?"<<endl;
- cin>>buildType;
- }while (!(buildType==0 || buildType==1));//end of the do while clause.
- switch (buildType)
- {
- case 0:
- players[size]=new Goalkeeper();
- assert(players[size]!=NULL);
- break;//end the 0 case of the buildType switch clause.
- case 1:
- do
- {
- cout<<"Please enter the name of the Goalkeeper you would like to create:"<<endl;
- cin>>name;
- }while (!(isNameAvailable(name,players,size,teams,numOfTeams)));//end of the do while clause
- cout<<"Please enter the reaction for the Goalkeeper you would like to create:"<<endl;
- cin>>reaction;
- players[size]=new Goalkeeper(name,reaction);
- assert(players[size]!=NULL);
- break;//end of the 1 case of the buildType switch clause.
- }//end of the buildType switch clause.
- break;//end of the 1 case of the playerType switch clause.
- case 2:
- do
- {
- cout<<"Would you like to build a random Striker (0) or set his name and kick yourself (1)?"<<endl;
- cin>>buildType;
- }while (!(buildType==0 || buildType==1));//end of the do while clause
- switch (buildType)
- {
- case 0:
- players[size]=new Striker();
- assert(players[size]!=NULL);
- break;//end of the 0 case of the buildType switch clause.
- case 1:
- do
- {
- cout<<"Please enter the name of the Striker you would like to create:"<<endl;
- cin>>name;
- }while(!(isNameAvailable(name,players,size,teams,numOfTeams)));//end of the do while clause
- cout<<"Please enter the kick for the Striker you would like to create:"<<endl;
- cin>>kick;
- players[size]=new Striker(name,kick);
- assert(players[size]!=NULL);
- break;//end of the 1 case of the buildType switch clause.
- }//end of the buildType switch clause.
- break;//end of the 2 case of the platerType switch clause.
- case 3:
- do
- {
- cout<<"Would you like to build a random Defender (0) or set his name and tackle yourself (1)?"<<endl;
- cin>>buildType;
- }while (!(buildType==0 || buildType==1));//end of the do while clause.
- switch (buildType)
- {
- case 0:
- players[size]=new Defender();
- assert(players[size]!=NULL);
- break;//end of the 0 case of the buildType switch clause.
- case 1:
- do
- {
- cout<<"Please enter the name of the Defender you would like to create:"<<endl;
- cin>>name;
- }while(!(isNameAvailable(name,players,size,teams,numOfTeams)));
- cout<<"Please enter the tackle for the Defender you would like to create:"<<endl;
- cin>>tackle;
- players[size]=new Striker(name,tackle);
- assert(players[size]!=NULL);
- break;//end of the 1 case of the buildType switch clause.
- }//end of the buildType witch clause.
- break;//end of the 3 case of the playerType switch clause.
- }//end of the playerType switch clause.
- return players;
- }//end of the
- Rabbid** removePlayer(Rabbid** players,int numOfPlayers,int index1,int index2,int index3)
- {
- Rabbid **temp=new Rabbid*[numOfPlayers-3];
- int first,second,third;
- if (index1<index2 && index2<index3)
- {
- first = index1;
- second = index2;
- third = index3;
- }
- if (index1<index2 && index2>index3)
- {
- if(index1<index3)
- {
- first = index1;
- second = index3;
- third = index2;
- }
- else
- {
- first = index3;
- second = index1;
- third = index2;
- }
- }
- if (index1>index2 && index2>index3)
- {
- first=index3;
- second = index2;
- third = index1;
- }
- if (index1>index2 && index2<index3)
- {
- if (index1<index3)
- {
- first = index2;
- second = index1;
- third = index3;
- }
- else
- {
- first = index2;
- second = index3;
- third = index1;
- }
- }
- for (int i=0;i<first;i++)
- {
- temp[i]=players[i];
- }
- for (int i=first;i<second;i++)
- {
- temp[i]=players[i+1];
- }
- for (int i=second;i<third;i++)
- {
- temp[i-1]=players[i+1];
- }
- for (int i=third;i<numOfPlayers;i++)
- {
- temp[i-3]=players[i];
- }
- return temp;
- }
- void tournament(Team** teams,int numOfTeams)
- {
- // int rounds;
- }
- int main (void)
- {
- srand(time(NULL));
- int numOfPlayers=0,numOfTeams=0,userSelection;
- Rabbid **players= new Rabbid* [0];
- assert(players!=NULL);
- Team **teams = new Team*[0];
- assert (teams!=NULL);
- cout<<"Welcome to Ravid Rabbids!!!"<<endl<<"This menu will allow you to create the players and teams needed to play the game"<<endl<<endl;
- do
- {
- cout<<"--------------------------------------------"<<endl<<"Please choose an option from the menu below:"<<endl<<"--------------------------------------------"<<endl<<"1)\tCreate a new player."<<endl<<"2)\tCreate a new team (requires at least one player of each kind)."<<endl<<"3)\tStart the game!"<<endl<<"4)\tExit."<<endl;
- cin>>userSelection;
- switch (userSelection)
- {
- case 1:
- players=addPlayer(players,numOfPlayers,teams,numOfTeams);
- numOfPlayers++;
- break;
- case 2:
- if (enoughPlayersToCreateTeam(players,numOfPlayers))
- {
- numOfTeams++;
- teams=new Team*[numOfTeams];
- teams[numOfTeams-1]=new Team(players,numOfPlayers);
- }
- else
- {
- cout<<"There are not enough players to create a team!"<<endl<<"Try to build some more players first."<<endl;
- }
- break;
- case 3:
- tournament(teams,numOfTeams);
- break;
- case 4:
- exit(0);
- break;
- default:
- cout<<"Wrong selection!"<<endl;
- }
- }while(true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement