-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWarEngine.py
198 lines (143 loc) · 4.9 KB
/
WarEngine.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import math
from Player import Player
import numpy as np
import pandas as pd
import sys
master_deck = []
player_one = Player([], [], [], 0, 0)
player_two = Player([], [], [], 0, 0)
num_single_wars = [0]
num_double_wars = [0]
num_total_wars = [0]
num_concated_wars = [0]
num_draws = [0]
war_counter = 0
def main():
"""Start of program...!"""
try:
i: int = int(input("Please enter the amount of trials to run: "))
except:
return
run(i)
def export_data(data):
data.to_csv("data.csv", index=False)
def display_DataFrame():
# MAKE DYNAMIC FRAME
# Have to subtract b/c double wars are their own thing, but got tallied as a single war at one point
data = pd.DataFrame({
"Single-Wars": num_single_wars,
"Single-War-Percentages": single_war_percentages[0],
" Double-Wars": num_double_wars,
"Double-War-Percentages": double_war_percentages[0],
" Concated-Wars": num_concated_wars,
"Concated-War-Percentages": concated_war_percentages[0],
"Total-Wars": num_total_wars,
"Player-1-Wins": player_one.num_wins,
"Player-2-Wins": player_two.num_wins,
"Draws": num_draws,
})
print(data)
export_data(data)
def calc_data_percentages(epochs):
num_single_wars[0] -= (num_double_wars[0] + num_concated_wars[0])
num_total_wars[0] = num_single_wars[0] + \
num_double_wars[0] + num_concated_wars[0]
global single_war_percentages, double_war_percentages, concated_war_percentages
single_war_percentages = [(num_single_wars[0] / num_total_wars[0]) * 100]
double_war_percentages = [(num_double_wars[0] / num_total_wars[0]) * 100]
concated_war_percentages = [
(num_concated_wars[0] / num_total_wars[0]) * 100]
def run(epochs: int):
"""Run the simulation a set amount of times"""
for i in range(4):
for j in range(2, 15):
master_deck.append(j)
for i in range(1, epochs+1):
print(str(i) + " | " + "{:.2f}".format((i/epochs)*100) + "%")
# % Program Finished
# Run one simulation (Saves output globally)
simulate()
calc_data_percentages(epochs)
display_DataFrame()
def simulate():
set_decks()
simulate_game()
def set_decks():
# Clear Player's Deck From Prev. Game
player_one.deck.clear()
player_two.deck.clear()
# Clear Player's Side-Deck From Prev Game player_one.side_deck.clear()
player_one.side_deck.clear()
player_two.side_deck.clear()
np.random.shuffle(master_deck)
# Set Player Decksp
player_one.deck = master_deck[0:26]
player_two.deck = master_deck[26:]
def win_round(winning_player, losing_player):
# Draw Cards
for i in winning_player.war_hand:
winning_player.side_deck.append(i)
for j in losing_player.war_hand:
winning_player.side_deck.append(j)
# Clear Side Deck
# Add Drawn Cards to war_hand
winning_player.war_hand.clear()
losing_player.war_hand.clear()
def score_nth_war():
global war_counter, num_single_wars, num_double_wars, num_concated_wars
if(war_counter == 1):
num_single_wars[0] += 1
elif(war_counter == 2):
num_double_wars[0] += 1
else:
num_concated_wars[0] += 1
def play_war():
# Draw next three cards
global war_counter
if(player_one.has_cards_for_war() and player_two.has_cards_for_war()):
for i in range(4):
player_one.draw_card(0)
player_two.draw_card(0)
simulate_round(
(player_one.war_hand[-1]), (player_two.war_hand[-1]))
elif(player_one.has_cards_for_war()):
win_round(player_one, player_two)
return
elif(player_two.has_cards_for_war()):
win_round(player_two, player_one)
return
def simulate_round(player_one_war_card, player_two_war_card):
if(player_one_war_card > player_two_war_card):
win_round(player_one, player_two)
elif(player_two_war_card > player_one_war_card):
win_round(player_two, player_one)
else:
global war_counter, num_total_wars
war_counter += 1
score_nth_war()
play_war()
def calc_win_losses():
# Did player_one lose?
if(player_one.has_cards()):
player_one.num_wins += 1
# Did player_two lose?
elif(player_two.has_cards()):
player_two.num_wins += 1
# They Drawed!
else:
num_draws += 1
def simulate_game():
current_card_index = 0
# End of each while loop represents one round of play
while(player_one.has_cards() and player_two.has_cards()):
player_one_war_card = player_one.draw_card(current_card_index)
player_two_war_card = player_two.draw_card(current_card_index)
simulate_round(player_one_war_card,
player_two_war_card)
global war_counter
war_counter = 0
calc_win_losses()
# Reset_counter
# Shuffle Deck
if __name__ == '__main__':
main()