Using a dynamic programming formulation, an analysis is presented of the innings of the team which bats first and the innings of the team which bats second. This allows a calculation, at any stage of the innings, of the optimal scoring rate, along with an estimate of the total number of runs to be scored or the chance of winning. The analysis is used to shed some light on possible batting tactics, to quantify the effects of selecting extra batsmen in a side, and to suggest a method for the development of alternative measures of player performance. Results suggest that scoring rates should be more uniform than at present, and that the team batting second has an advantage. Possible extensions to the model are discussed.