Best method for baseball program

Im an amateur programmer so be kind.

I assistant coach a baseball team here in town, lately the head coach has been upset taking up most of his time thinking of a line up for infield and outfield positions at games. This has to do with the rules:

No child may play more than 2 innings infield
No child may play more than 1 inning outfield
No child can play the same position two innings in a row (ex. First Base two innings straight)
And head coach has also said that he wants the last pitcher benched next inning.

He asked i write up a program for him that will do this alot faster. I thought it would be good practice.

I cant figure out how i should do this. Its not a matter of what to write in as code but what methods in doing so.

My idea so far:
Using a struct to store an index of names entered in by the coach, while using another struct to hold positions (ex. first, second etc.). Values declared in the struct will be character arrays 11 spaces long because it would be a rarity one of our kid's names would be over 10 spaces in length. Values declared in the
position struct will be character arrays also. This way, in main, i can pass the name values to the position values via loop. Between passing the name values i want to setup some checks in main so as to include the logic of the program but i dont know exactly how i should go about doing that. I thought i might use the rand() function to make sure its not always a predictable line up (mixing up the names) but i think that would just gunk up the works. I think loops do the best job of checking conditions so i wanted to start there. Then of course, displaying the line up.

So is this a good method? Anything at all wrong with this what so ever?
Any comment helps.

EDIT: Source information ive been using: C++ For Dummies 5th Edition
Last edited on
Seeing as the majority of the community has been stalling (which I think is ridiculous and I should get out my Roc whip), here's a few ideas for the problem (I haven't too many clues about baseball rules, but I think I can understand what is required here).

You'll want to make a loop that runs nine times, randomly selecting players out of a list of potential ones (I recommend a vector<childdata> for this) for each position. If a player is decided by a series of logical checks (which shouldn't be too hard to figure out given your conditions) to be unable to play a certain location, delete him or her from the vector and try again without reiterating the loop until you get one that is. After you make the selection, when the next iteration comes, clear the vector and repopulate it with all the available players (you'll want another vector<childdata> for keeping track of who is available), and something to hold a list of all the players, as I take it you will need to repeat the process of selecting people (another vector<childdata> ?), which you will update as children are chosen. When you need to repeat the whole loop, delete all the data in the first two vectors, and refill them with the data from this third vector.

These code snippets might be of interest. Note that you can think of a vector as a glorified array.
Note: i is a placeholder for an integer that changes depending on the result of your random function.

vector <childdata> name; //Creates a vector of childrendata.

name.push_back(masterlist.at(i)); //Adds a child to the vector.

name.length(); //Gets the number of children in your vector.

name.at(i); //Access a variable in your vector.

name.erase(naem.begin()+i); //Erase data from the vector.

name.clear(); //Delete all the data in the vector.

webbrowser.open("http://cplusplus.com/reference/stl/vector/");

If you don't understand what something does, just ask.

-Albatross
Last edited on
Ive never heard the term vector in regards to C++. Could you explain more on how i could set up a vector as im not sure what exactly that is. I looked in my book and it told me what you did, "a vector is a glorified array..." The answer may be in my face, but if you'd be so kind sir, please educate me on vectors.

Aside from that, the idea is solid and actually sounds miles simpiler then my own idea and best of all the only curve ball was the vector.

Thank you for your response as well :)
closed account (D80DSL3A)
This sounds like an interesting programming problem!
I'd like to be sure I understand the conditions set by your 4 given rules. I'll refer to them in the order given.
It seems that rule #1 and rule #2 imply that no child may play in more than 3 innings - twice infield (1st base, 2nd base, 3rd base, shortstop, pitcher and catcher) and once outfield (left field, center field and right field). To fill all 9 positions in all 9 innings you would need 3 full sets of players. You have at least 27 players? Big team!!

If your team has more than 27 players then some players could be excluded from play altogether in a random assignment. Should the program seek to avoid this, perhaps by assigning all players at least one position during the game then filling the rest of the positions in accordance with your 4 rules?

Would you really want to assign all players as a pitcher? I recall from my little league days that only a few can play this position well. Should a sub list of the players be applied here?

I'm afraid I haven't learned to use the vector or string classes yet so I would have to base a solution on more basic methods, although I'm sure the methods recommended by Albatross are superior.

As far as just getting the position assignments filled it seems that working in terms of indexes may work. If there are N players then the numbers 1 through N could be used to refer to them. Also, since the # of innings and positions are fixed, a 2 dimensional array of integers may work to organize it all in.
 
int PositionAssigns[9][9] = {0};// this initializes all values to zero - indicating  unfilled positions 

Here the 1st index is the inning # and the 2nd index is the position #. If the pitcher is position=0
then this:
 
PositionAssigns[x][0] = y;

Would correspond to assigning player y the position of pitcher in inning x. Player y would be skipped over in filling PositionAssigns[x+1] as rule #4 requires the benching of the last innings pitcher.

Compliance with rule #3 should be fairly easy. When filling positions for inning x:
1
2
if( PositionAssigns[x-1][j] != y)// if player y was not assigned position j last inning
      PositionAssigns[x][j] = y;// it is ok to assign player y position j this inning 


But, do I have the rules right? They seem to imply a very large team.
Ok let me explain just a bit further. First of all there are 11 kids on our team, so its actually small. Secondly, we don't play nine innings just because the age group that we coach (6-9 yrs old). We play 4 innings or until an hour and fifteen minutes is reached. Thirdly, right now is time for them to have fun and our goal is basically to succeed in teaching them all the fundamentals of baseball without the pressure of having to win or "to be the best" and so on (ya know, no pressure from any angle). Still, they have games and so we need a line up.

About the rules, the basic idea is that no player plays the same spot twice with the exception of the infield in which we can field them only twice in an infield position in a row. We may not field anyone twice in a row in the outfield. And then of course, the pitcher to bench as a general rule of choice by the head coach. I hope this makes more sense then before, im trying to explain as best i can. We would like to be as random as possible though, regardless of the fact that we could field them twice if we wanted to.

So your method is actually simpler the albatrosses idea in terms of experience. I haven't delved into vector classes either so that was a bit of a curve ball. Ive thought of this 2-D array before and i couldnt quite grasp how i could do it from that angle but you explained it in an excellent way and i could probably whip up something right now just on your notes.

On a side note, i would still love to have a short hand explanation of how to use vector classes correctly. Maybe its simpler than i think.
Could you explain more on how i could set up a vector as im not sure what exactly that is.

A vector simply put, is a dynamic array with available functions. It is basically an array that is more usable in terms of what you can do with it compared to a regular array. For example, finding the size of the vector (how many elements it contains), expanding the size of vector, removing elements from the vector, and so on... Here is a link with all the info you will need:

http://www.cplusplus.com/reference/stl/vector/
closed account (D80DSL3A)
I couldn't resist taking a crack at this problem. Here is a program which handles the core logic involved. The N players are represented by the numbers 0 to N-1. The positions played are also indexed as explained in comments accompanying the code. Positions which can't be filled due to too few players get the number -1 in the output. The program works for only the 4 inning case but allows entry of any # of players - not just 11. It also presumes that 9 positions are to be filled each inning. I hope this is correct. I wondered if little kids are playing catcher or not.
I leave it to you to associate actual player names with the player #'s, perform file I/O ( so the resulting lineups can be printed out ), etc. It turns out that 11 players are not enough to fill all positions by the rules - 3 positions are left unfilled in the 4th inning. All positions are filled and everyone is fielded 3 times when there are 12 players. The program includes a listing of the # of times each player has been assigned infield and outfield positions. This listing shows that for a team size larger than 12 not everyone is fielded 3 times. Please let me know if this program works well. I hope the output is sensible enough.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//  baseBall lineup problem
#include<iostream>
#include<cstring>
#include<ctime>// for seeding random #'s

using namespace std;

void INIT(void);
char repeat = 'n';// generate another lineup?
int N = 11;// tentative # of players
int PosAssign[4][9] = {0};// however, -1 will indicate position not filled.

struct player// to record # of in/outfield assignments for each player
{
	int inFld;
	int outFld;
};
player* P;// for dynamic allocation of array of players (so # of players can vary)

int main()
{    
	
	int y = 0;// index to player = 0 to N-1
	int i=0,j=0;// general purpose counters
	int posNum = 0;// index to position = 0 to 8 ( infield = 0 to 5 (pitcher=0) and outfield = 6,7,8 )
	srand((unsigned)time(0));// comment out for repeatable lineup sequences

	cout << "Each run produces a full game lineup\n";
	
	do// allows repeat runs and keeps window open so results can be seen
	{
		cout << "Enter number of players: ";
		cin >> N;
		P = new player[N];
		INIT();// reset all for this lineup

		//** 1st inning **
		y = rand()%N;// start with random player index
		for(j=0; j<=5; j++)// assign infield positions
		{
			PosAssign[0][j] = y;
			P[y].inFld = 1;
			y = (1+y)%N;// next player index, or 1st if hit end of roster
		}
		for(j=6; j<=8; j++)// assign outfield positions
		{
			PosAssign[0][j] = y;
			P[y].outFld = 1;
			y = (1 + y)%N;// next player index, or 1st if hit end of roster
		}

		//** innings 2 through 4 ***
		for( i=1; i<4; i++)
		{
			y = PosAssign[i-1][6];// start with previous innings outfielders
			posNum = 0;// will be filling positions 0 thru 5
			j = 0;
			do// assign infield positions
			{
				
				//     y didn't pitch           or play posNum              not infield twice
				if( (PosAssign[i-1][0] != y)&&(PosAssign[i-1][posNum] != y)&&( P[y].inFld < 2) )
				{
					PosAssign[i][posNum] = y;// make assignment					
					P[y].inFld++;// infield play count increased
					posNum++;// position filled
				}
				
				j++;// this is just to prevent infinite loop in case not all positions can be filled
				y = (1 + y)%N;// next player index				
			}while( (posNum <= 5)&&(j < 100) );
			j = 0;
			posNum = 6;// now for positions 6 thru 8
			do// assign outfield positions
			{	//  y didn't pitch last inning && not outfield yet
				if(  (PosAssign[i-1][0] != y)&&(P[y].outFld < 1) )// simpler condition here!
				{
					PosAssign[i][posNum] = y;// make assignment
					P[y].outFld = 1;// outfield play done for player y
					posNum++;// position filled
				}
				y = (1 + y)%N;// next player index
				j++;
			}while( (posNum <= 8)&&(j < 100) );	
		}// end each inning lineup

		//** report results **
		for( i=0; i<4; i++)
		{
			cout << endl << endl <<"lineup for inning " << i+1 << " is\n";
			for( j=0; j<=8; j++)
				cout << PosAssign[i][j] << "  ";
		//	cout << endl << endl;
		}

		//** check on # of infield and outfield assigns **
		cout << endl << endl << "# of in/outfield assigns for each player are " << endl;
		for(j=0; j<N; j++)
			 cout <<P[j].inFld << P[j].outFld << " ";

		delete [] P;// done with player array - delete dynamically allocated memory

		cout << endl << "repeat (y/n)? " << flush;
		cin >> repeat;
	}while( repeat=='y');
    return 0;
}// end of main()

	void INIT(void)
	{
		int j=0, k=0;// for looping

		for(j=0; j<4; j++)
			for(k=0; k<9; k++)
				PosAssign[j][k] = -1;

		for(j=0; j<N; j++)
			P[j].inFld = P[j].outFld = 0;

		return;
	}// end of INIT() 
Topic archived. No new replies allowed.