%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % WAR SIMULATOR % by: Eric Sorensen % 10/23/2009 % http://www.esorensen.com % % Usage: Adjust variables in the USER SETUP section to run the simulation. % After all games are finished (it may take awhile if you run alot) stat- % istics can be found in the variables starting with g_: % % g_rounds: Rounds for the game to end % g_wars: Wars in the game % g_nest: Maximum number of "nested" wars in a game % g_winner: Winner of the round. In the case of a neverending game, % this is the player with the most cards when the round limit % is reached. % % g_stuck: 1 if the round limit is reached, 0 otherwise. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% format compact; clear; %%%%%%%%%%%%%%%%%%%%USER SETUP************************ %how many games to play number_of_games = 5000; %number of face-down cards included in each war (usually 2 or 3) war_cards = 3; %point at which a game is declared unwinnable round_limit = 5000; %hand generation: cards_in_hand = 26; %include random factor? random_factor = 1; %0 = NO, 1 = YES %If the random factor is enabled, the order in which cards are placed on %the bottom of the deck is randomly flipped. This simulates how *most* %people probably play war. If the random factor is disabled, the same %procedure is used in determining card order on every hand. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %----------Do not edit below this line-----------------------% %initialize multi-game stat variables g_stuck = zeros(1,number_of_games); g_winner = zeros(1,number_of_games); g_wars = zeros(1,number_of_games); g_nest = zeros(1,number_of_games); g_rounds = zeros(1,number_of_games); for game = 1:number_of_games %setup number of games to play and collect stats for %setup the deck: deck_indexes = randperm(52); %fifty two random deck locations %randomly place all 52 cards deck = zeros(1,52); for x = 1:13 for y = 1:4 position = (y * 13) - 13 + x; index = deck_indexes(position); deck(index) = x; end end %setup each hand: deal1 = deck(1:cards_in_hand); deal2 = deck(cards_in_hand+1:2*cards_in_hand); %setup hands in working variables hand1 = deal1; hand2 = deal2; %initialize stash as an empty vector stash = []; %setup statistic variables rounds = 0; wars = 0; hand_stats = zeros(round_limit,1); in_war = 0; max_war = 0; current_war = 0; if(mod(game, 10) == 0) disp(['Calculating game: ', num2str(game)]); end while(isempty(hand1) == 0 && isempty(hand2) == 0 && rounds < round_limit) rounds = rounds + 1; % if(mod(rounds,26) == 0 && in_war == 0) % hand1 = hand1(randperm(length(hand1))); % hand2 = hand2(randperm(length(hand2))); % end hand_stats(rounds) = length(hand1); %save action cards key1 = hand1(1); %save action cards to compare key2 = hand2(1); %remove action cards from decks hand1(1) = []; hand2(1) = []; %setup stash if(randi(random_factor+1,1) == 1) %random factor to prevent endless games. % randi(2,1) :: random :: random_factor = 1 % randi(1,1) :: notrandom :: random_factor = 0 stash(end+1) = key1; stash(end+1) = key2; else stash(end+1) = key2; stash(end+1) = key1; end if(key1 > key2) %player 1 wins, add cards to his deck hand1 = [hand1, stash]; stash = []; %empty stash in_war = 0; elseif(key2 > key1) %player 2 wins, add cards to his deck hand2 = [hand2, stash]; stash = []; %empty stash in_war = 0; else %WAR key1 = key2 wars = wars + 1; if(in_war == 1) current_war = current_war + 1; %track what "nest level" the war is at else in_war = 1; current_war = 1; end if(current_war > max_war) max_war = current_war; %determine the highest nest achieved in this game end %add as many cards as possible (up to war_cards) to the stash upper_bound = min(length(hand1),length(hand2)); upper_bound = upper_bound - 1; %leave room for comparison card if(upper_bound > war_cards) upper_bound = war_cards; end %add the war cards to the stash, remove them from hands if(upper_bound > 0) stash = [stash, hand1(1:upper_bound), hand2(1:upper_bound)]; hand1(1:upper_bound) = []; hand2(1:upper_bound) = []; end end end if(length(hand1) > length(hand2)) g_winner(game) = 1; else %disp('Player 2 wins!'); g_winner(game) = 2; end if(rounds == round_limit) g_stuck(game) = 1; else g_stuck(game) = 0; end %update stats g_rounds(game) = rounds; g_wars(game) = wars; g_nest(game) = max_war; %UNCOMMENT TO SEE CONSOLE SUMMARY OF EACH GAME % disp(['WINNER:', num2str(g_winner(game))]); % disp(['GAME: ', num2str(game)]); % disp(['Rounds: ', num2str(rounds)]); % disp(['Wars: ', num2str(wars)]); % disp(['Largest Nested War:', num2str(max_war)]); %UNCOMMENT TO SEE GRAPH OF EACH GAME: % close all; % midline(1:rounds) = cards_in_hand; % plot(hand_stats(1:rounds)); % xlabel('Rounds'); % ylabel('Cards in Player 1''s Hand'); % title('War'); % hold on; % plot(midline, 'r'); % ylim([0 cards_in_hand*2]); % xlim([1 rounds]); % pause end %update for the final round rounds = rounds + 1; hand_stats(rounds) = length(hand1); %final recap: disp(' '); disp(['Average Rounds: ', num2str(mean(g_rounds(:,g_rounds(1,:)~=round_limit)))]); %doesn't average games which reached the round limit; disp(['Average Victor: ', num2str(mean(g_winner))]); %should approach 1.5 disp(['Average Wars: ', num2str(mean(g_wars))]); disp(['Stuck Percentage: ', num2str(mean(g_stuck) * 100)]); disp(['Longest Game: ', num2str(max(g_rounds(:,g_rounds(1,:)~=round_limit)))]); %doesn't include stuck games