Setting up the Game:  Deck and Rules

StartWar[]:=Module[
{WarDeck,Shuffle,Hand1,Hand2},
(*Creates a list of a 52 card deck.  Each card has a number assigned to it.  2-10 is assigned it's own value, with jack = 11, queen = 12, king = 13, ace = 14.*)  
WarDeck=Flatten[Table[i,{i,2,14},{4}]];
(*randomizes the list*)
Shuffle=RandomSample[WarDeck];
(*Splits the deck into two hands.*)
{Take[Shuffle,26],Take[Shuffle,-26],{}}
]

WarStepper[{P1In_,P2In_,WIn_},WarDumpNumber_]:= Module[
{P1=P1In, P2=P2In,W=WIn,LastCard1,LastCard2,TableCards,m},
(* P1 and P2 each play a card from the bottom of their decks *)
LastCard1=P1[[-1]];  P1=Drop[P1, -1];
LastCard2=P2[[-1]];  P2=Drop[P2, -1];
TableCards={LastCard1,LastCard2};
TableCards=RandomSample[TableCards];
(* Player with the highest card takes cards on the table. *)
Which[
LastCard1>LastCard2, P1=Join[TableCards,W,P1];W={},
(*Gives the cards to P2 if P2's card is higher*)
LastCard1<LastCard2, P2=Join[TableCards,W,P2];W={}, 
(*Creates a War if the players tie.  Each plays 2 face down cards after the cards that tied.  Play then resumes*)
LastCard1==LastCard2, 
	m=Min[WarDumpNumber,Min[Length[P1],Length[P2]]];
       (* *)
	TableCards=Join[TableCards,P1[[-m;;-1]]];P1=Drop[P1,-m];
	TableCards=Join[TableCards,P2[[-m;;-1]]];P2=Drop[P2,-m];
         (* Adding the table cards to W*)
	W=Join[RandomSample[TableCards],W]
];
{P1,P2,W}
]

Peace[{P1_,P2_,W_}]:=Module[
{},
Min[Length[P1],Length[P2]]>0
]

Play the Game!

State=StartWar[];
NumberofGames=10;
WarDumpNumber=2;

(*This loop plays the game until one hand has all of the cards (52),and the other hand has no cards*)
WarGame[WarDumpNumber_]:= Module[
{State,DataWar},
State=StartWar[];
DataWar=NestWhileList[WarStepper[#,WarDumpNumber]&, State,Peace];
Transpose[Map[Length,DataWar,{2}]]
]
(*Plays the game.  Gives lengths of hands at each turn. *)
(*WarGame[2]*)

(*This will play the game as many times as input.  It gives the number of cards in a players hands for every round of each game, which is why it is suppressed below.*)
Clear[MultiWar]
MultiWar[WarDumpNumber_,NumofGames_]:=Module[
{},
Table[
WarGame[WarDumpNumber],
{NumofGames}]
]
(*NumberofGames was initialized in an earlier cell, when the State of the game is given.*)
(*Timing[MultiWar[2,NumberofGames]]*)

Collect some Data

(*This cell counts the number of wars in a game, and can play as many games as the user wishes.  *)
Clear[DataWar]
WarCount[Datai_]:=Module[
{},
Tally[Datai[[3]]]
]
(*This gives the breakdown of each game by the number of each type of round a game has by the number of cards in the war pile.  For example {{0,10},{6,2},{12,1}} says there were 10 normal rounds were there was a winner right away, there were 2 wars, and one double war.*)
DataWar[WarDumpNumber_,NumberofGames_]:=Module[
{myDataWar},
myDataWar=MultiWar[WarDumpNumber,NumberofGames];
Map[WarCount,myDataWar]
]

(*This totals the number of hands played of each type.  For example, it will total the number of normal hands played, the number of wars, double wars, and so on, giving the total number of turns a game takes*)
NumofTurns[RoundCounti_]:=Module[
{Temp},
Temp=Transpose[RoundCounti][[2]];
Apply[Plus,Temp]
]

WARDATA[WarDumpNumber_,NumberofGames_]:=Module[
{WarPiles,TurnsperGame,NumofWars,NumofDoubles,WarProbability,MaxTurns,MinTurns,MaxWars,MinWars,MaxDouble,Winner,WinnerSet,P2Winpercent,RoundCount,AllGames},
RoundCount=DataWar[WarDumpNumber,NumberofGames];
AllGames=MultiWar[WarDumpNumber,NumberofGames];

WarPiles=Table[AllGames[[i]][[3]],{i,1,NumberofGames}];
(*This gives the number of turns each game that was played took, and the average of those numbers*)
TurnsperGame=Table[NumofTurns[RoundCount[[i]]],{i,1,NumberofGames}];


(*This gives the number of single wars in a game, and the average*)
NumofWars=Table[Count[WarPiles[[i]],6],{i,1,NumberofGames}];


(*This gives the number of double wars and the average.*)
NumofDoubles=Table[Count[WarPiles[[i]],12],{i,1,NumberofGames}];


(*This gives the probability that a round will result in a war*)
(*WarProbability=NumberofGames*1./Total[NumofWars];*)

Winner=AllGames;
WinnerSet=Table[Winner[[i]][[1]][[-1]],{i,1,NumberofGames}];
(*P2Winpercent=Count[WinnerSet,0]/NumberofGames*1.;*)

(*These give the most number of turns, minimum number of turns, most number of wars, and minimum number of wars of all the games played*)
MaxTurns=Max[TurnsperGame];
MinTurns=Min[TurnsperGame];
MaxWars=Max[NumofWars];
MaxDouble=Max[NumofDoubles];
(*Gives a nice tidy list of the values we've calculated*)
{TurnsperGame,NumofWars,NumofDoubles,MaxTurns,MinTurns,MaxWars,MaxDouble}
]

(*to change the amount of runs saved to each file, go to the cell just under the "Play The Game!" title and change NumberofGames*)
SetDirectory["/home/campus03/struther/public_html/Courses/2990/WArriorRuns"];
Do[
Data=ParallelTable[
WARDATA[3,3*(10^4)],
{4}];
Print[DateList[]];
FileName=ToString[StringForm[ "WarGameData_``.dat", n]];
Export[FileName,Data],
{n,1,(10^4)}]