Advertisement
davsank

Untitled

May 7th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.88 KB | None | 0 0
  1. /* Assignment: 4
  2. Campus: Ashdod
  3. Author1: David Sankovsky , ID: 308604065
  4. Author2: Galit Nahari, ID: 201156650
  5. Date: 5/5/2011 */
  6. #include "Team.h"
  7. #include "Defender.h"
  8. #include "Goalkeeper.h"
  9. #include "Striker.h"
  10. #include <iostream>
  11. #include <assert.h>
  12. using namespace std;
  13.  
  14. /*
  15. The following two methods are overloaded "print" methods, the first, printing all available players, the second one, all available teams.
  16. */
  17. void print(Rabbid **players,int size)
  18. {
  19.     for (int i=0;i<size;i++)
  20.     {
  21.         players[i]->Print();
  22.     }//end of the for loop
  23. }//end of the player printing function
  24.  
  25. void print(Team **teams,int size)
  26. {
  27.     for(int i=0;i<size;i++)
  28.     {
  29.         teams[i]->print();
  30.     }//end of the for loop
  31. }//end of the team printing function
  32.  
  33. /*
  34. 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.
  35. */
  36. bool enoughPlayersToCreateTeam(Rabbid **players,int size)
  37. {
  38.     int numOfKeepers=0,numOfStrikers=0,numOfDefenders=0;
  39.     for (int i=0;i<size;i++)
  40.     {
  41.         switch (players[i]->getClassType())
  42.         {
  43.         case 0:
  44.             numOfKeepers++;
  45.             break;
  46.         case 1:
  47.             numOfDefenders++;
  48.             break;
  49.         case 2:
  50.             numOfStrikers++;
  51.             break;
  52.         }//end of the switch clause/
  53.     }//end of the for loop
  54.     return (numOfKeepers>0 && numOfStrikers>0 && numOfDefenders>0);
  55. }//end of the "enoughPlayersToCreateTeam" function
  56.  
  57. /*
  58. 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.
  59. */
  60. bool isNameAvailable(string name,Rabbid **players,int size,Team **teams,int numOfTeams)
  61. {
  62.     for (int i=0;i<size;i++)
  63.     {
  64.         if(name.compare(players[i]->getName()))
  65.             return false;
  66.     }//end of the for loop.
  67.     for (int i=0;i<numOfTeams;i++)
  68.     {
  69.         for (int j=0;j<3;j++)
  70.         {
  71.             if(name.compare(teams[i]->getPlayerName(j)))
  72.             {
  73.                 return false;
  74.             }//end of the if clause.
  75.         }//end of the nested for loop.
  76.     }//end of the harboring for loop.
  77.     return true;
  78. }//end of the isNameAvailable function.
  79.  
  80. /*
  81. 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.
  82. */
  83.  
  84. Rabbid** addPlayer(Rabbid **players,int size,Team **teams,int numOfTeams)
  85. {
  86.     int playerType,buildType,reaction,kick,tackle;
  87.     string name;
  88.     Rabbid **temp= new Rabbid*[size];
  89.     assert(temp!=NULL);
  90.     for (int i=0;i<size;i++)
  91.     {
  92.         temp[i]=players[i];
  93.     }//end of the for loop.
  94.     players = new Rabbid*[size+1];
  95.     assert(players!=NULL);
  96.     for (int i=0;i<size;i++)
  97.     {
  98.         players[i]=temp[i];
  99.     }//end of the for loop
  100.     do
  101.     {
  102.         system("cls");
  103.         cout<<"Please select the type of player you want to create:"<<endl<<"1)\tGoalkeeper"<<endl<<"2)\tStriker"<<endl<<"3) \tDefender"<<endl;
  104.         cin>>playerType;
  105.     }while(!(playerType==1 || playerType==2 ||playerType==3));//end of the do while clause.
  106.     switch (playerType)
  107.     {
  108.     case 1:
  109.         do
  110.         {
  111.             cout<<"Would you like to build a random Goalkeeper (0) or set his name and reaction yourself (1)?"<<endl;
  112.             cin>>buildType;
  113.         }while (!(buildType==0 || buildType==1));//end of the do while clause.
  114.         switch (buildType)
  115.         {
  116.         case 0:
  117.             players[size]=new Goalkeeper();
  118.             assert(players[size]!=NULL);
  119.             break;//end the 0 case of the buildType switch clause.
  120.         case 1:
  121.             do
  122.             {
  123.                 cout<<"Please enter the name of the Goalkeeper you would like to create:"<<endl;
  124.                 cin>>name;
  125.             }while (!(isNameAvailable(name,players,size,teams,numOfTeams)));//end of the do while clause
  126.             cout<<"Please enter the reaction for the Goalkeeper you would like to create:"<<endl;
  127.             cin>>reaction;
  128.             players[size]=new Goalkeeper(name,reaction);
  129.             assert(players[size]!=NULL);
  130.             break;//end of the 1 case of the buildType switch clause.
  131.         }//end of the buildType switch clause.
  132.         break;//end of the 1 case of the playerType switch clause.
  133.     case 2:
  134.         do
  135.         {
  136.             cout<<"Would you like to build a random Striker (0) or set his name and kick yourself (1)?"<<endl;
  137.             cin>>buildType;
  138.         }while (!(buildType==0 || buildType==1));//end of the do while clause
  139.         switch (buildType)
  140.         {
  141.         case 0:
  142.             players[size]=new Striker();
  143.             assert(players[size]!=NULL);
  144.             break;//end of the 0 case of the buildType switch clause.
  145.         case 1:
  146.             do
  147.             {
  148.                 cout<<"Please enter the name of the Striker you would like to create:"<<endl;
  149.                 cin>>name;
  150.             }while(!(isNameAvailable(name,players,size,teams,numOfTeams)));//end of the do while clause
  151.             cout<<"Please enter the kick for the Striker you would like to create:"<<endl;
  152.             cin>>kick;
  153.             players[size]=new Striker(name,kick);
  154.             assert(players[size]!=NULL);
  155.             break;//end of the 1 case of the buildType switch clause.
  156.         }//end of the buildType switch clause.
  157.         break;//end of the 2 case of the platerType switch clause.
  158.     case 3:
  159.         do
  160.         {
  161.             cout<<"Would you like to build a random Defender (0) or set his name and tackle yourself (1)?"<<endl;
  162.             cin>>buildType;
  163.         }while (!(buildType==0 || buildType==1));//end of the do while clause.
  164.         switch (buildType)
  165.         {
  166.         case 0:
  167.             players[size]=new Defender();
  168.             assert(players[size]!=NULL);
  169.             break;//end of the 0 case of the buildType switch clause.
  170.         case 1:
  171.             do
  172.             {
  173.                 cout<<"Please enter the name of the Defender you would like to create:"<<endl;
  174.                 cin>>name;
  175.             }while(!(isNameAvailable(name,players,size,teams,numOfTeams)));
  176.             cout<<"Please enter the tackle for the Defender you would like to create:"<<endl;
  177.             cin>>tackle;
  178.             players[size]=new Striker(name,tackle);
  179.             assert(players[size]!=NULL);
  180.             break;//end of the 1 case of the buildType switch clause.
  181.         }//end of the buildType witch clause.
  182.         break;//end of the 3 case of the playerType switch clause.
  183.     }//end of the playerType switch clause.
  184.    
  185.     return players;
  186. }//end of the
  187.  
  188. Rabbid** removePlayer(Rabbid** players,int numOfPlayers,int index1,int index2,int index3)
  189. {
  190.     Rabbid **temp=new Rabbid*[numOfPlayers-3];
  191.     int first,second,third;
  192.     if (index1<index2 && index2<index3)
  193.     {
  194.         first = index1;
  195.         second = index2;
  196.         third = index3;
  197.     }
  198.     if (index1<index2 && index2>index3)
  199.     {
  200.         if(index1<index3)
  201.         {
  202.             first = index1;
  203.             second = index3;
  204.             third = index2;
  205.         }
  206.         else
  207.         {
  208.             first = index3;
  209.             second = index1;
  210.             third = index2;
  211.         }
  212.     }
  213.     if (index1>index2 && index2>index3)
  214.     {
  215.         first=index3;
  216.         second = index2;
  217.         third = index1;
  218.     }
  219.     if (index1>index2 && index2<index3)
  220.     {
  221.         if (index1<index3)
  222.         {
  223.             first = index2;
  224.             second = index1;
  225.             third = index3;
  226.         }
  227.         else
  228.         {
  229.             first = index2;
  230.             second = index3;
  231.             third = index1;
  232.         }
  233.     }
  234.     for (int i=0;i<first;i++)
  235.     {
  236.         temp[i]=players[i];
  237.     }
  238.     for (int i=first;i<second;i++)
  239.     {
  240.         temp[i]=players[i+1];
  241.     }
  242.     for (int i=second;i<third;i++)
  243.     {
  244.         temp[i-1]=players[i+1];
  245.     }
  246.     for (int i=third;i<numOfPlayers;i++)
  247.     {
  248.         temp[i-3]=players[i];
  249.     }
  250.    
  251.     return temp;
  252. }
  253.  
  254. void tournament(Team** teams,int numOfTeams)
  255. {
  256. //  int rounds;
  257. }
  258.  
  259. int main (void)
  260. {
  261.     srand(time(NULL));
  262.     int numOfPlayers=0,numOfTeams=0,userSelection;
  263.     Rabbid **players= new Rabbid* [0];
  264.     assert(players!=NULL);
  265.     Team **teams = new Team*[0];
  266.     assert (teams!=NULL);
  267.     cout<<"Welcome to Ravid Rabbids!!!"<<endl<<"This menu will allow you to create the players and teams needed to play the game"<<endl<<endl;
  268.     do
  269.     {
  270.     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;
  271.     cin>>userSelection;
  272.     switch (userSelection)
  273.     {
  274.     case 1:
  275.         players=addPlayer(players,numOfPlayers,teams,numOfTeams);
  276.         numOfPlayers++;
  277.         break;
  278.     case 2:
  279.         if (enoughPlayersToCreateTeam(players,numOfPlayers))
  280.         {
  281.             numOfTeams++;
  282.             teams=new Team*[numOfTeams];
  283.             teams[numOfTeams-1]=new Team(players,numOfPlayers);
  284.         }
  285.         else
  286.         {
  287.             cout<<"There are not enough players to create a team!"<<endl<<"Try to build some more players first."<<endl;
  288.         }
  289.         break;
  290.     case 3:
  291.         tournament(teams,numOfTeams);
  292.         break;
  293.     case 4:
  294.         exit(0);
  295.         break;
  296.     default:
  297.         cout<<"Wrong selection!"<<endl;
  298.     }
  299.     }while(true);
  300.    
  301.     return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement