-
Notifications
You must be signed in to change notification settings - Fork 0
/
WarSimulation.R
170 lines (141 loc) · 5.01 KB
/
WarSimulation.R
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
require(R.utils)
numberOfGamesToBePlayed <<- 100
setUpGame <- function() {
deck <<- list(1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13)
shuffleIndex <<- sample(1:52, 26, replace=F)
playerOneHand <<- list()
playerTwoHand <<- list()
playerOneTable <<- list()
playerTwoTable <<- list()
for (index in shuffleIndex){
playerOneHand[[length(playerOneHand)+1]] <<- deck[[index]]
}
for (index in sort(shuffleIndex, decreasing = TRUE)){
deck[[index]] <<- NULL
}
playerTwoHand <<- sample(deck)
}
runGame <- function() {
gameFinished <<- FALSE
firstWarWinner <<- 0
gameWinner <<- 0
comparisonCount <<- 0
numberOfWars <<- 0
while(length(playerOneHand) != 0 & length(playerTwoHand) != 0){
cardsTheSame <<-FALSE
playerOneTable[[length(playerOneTable)+1]] <<- playerOneHand[[1]]
playerOneHand[[1]] <<- NULL
playerTwoTable[[length(playerTwoTable)+1]] <<- playerTwoHand[[1]]
playerTwoHand[[1]] <<- NULL
#while the top cards are the same, remove 4 and check to see if the top cards are different
while(playerOneTable[[length(playerOneTable)]] == playerTwoTable[[length(playerTwoTable)]]){
cardsTheSame <<-TRUE
numberOfWars <<- numberOfWars + 1
comparisonCount <<- comparisonCount + 1
#remove 4 cards
for(x in c(1,2,3,4)){
if(length(playerOneHand) == 0 | length(playerTwoHand) == 0){
break
}
playerOneTable[[length(playerOneTable)+1]] <<- playerOneHand[[1]]
playerOneHand[[1]] <<- NULL
playerTwoTable[[length(playerTwoTable)+1]] <<- playerTwoHand[[1]]
playerTwoHand[[1]] <<- NULL
}
if(playerOneTable[[length(playerOneTable)]] > playerTwoTable[[length(playerTwoTable)]]){
if(numberOfWars == 1) {
firstWarWinner <<- "P1"
#print("P1")
}
comparisonCount <<- comparisonCount + 1
for(card in playerOneTable){
playerOneHand[[length(playerOneHand)+1]] <<- playerOneTable[[1]]
playerOneTable[[1]] <<- NULL
}
for(card in playerTwoTable){
playerOneHand[[length(playerOneHand)+1]] <<- playerTwoTable[[1]]
playerTwoTable[[1]] <<- NULL
}
break
} else if(playerOneTable[[length(playerOneTable)]] < playerTwoTable[[length(playerTwoTable)]]){
if(numberOfWars == 1) {
firstWarWinner <<- "P2"
#print("P2")
}
comparisonCount <<- comparisonCount + 1
for(card in playerTwoTable){
playerTwoHand[[length(playerTwoHand)+1]] <<- playerTwoTable[[1]]
playerTwoTable[[1]] <<- NULL
}
for(card in playerOneTable){
playerTwoHand[[length(playerTwoHand)+1]] <<- playerOneTable[[1]]
playerOneTable[[1]] <<- NULL
}
break
}
}
if(cardsTheSame == FALSE){
comparisonCount <<- comparisonCount + 1
}
}
if(length(playerOneHand)==0) {
gameWinner <<-"P2"
#print("P2")
}
if(length(playerTwoHand)==0) {
gameWinner <<- "P1"
#print("P1")
}
#print(comparisonCount)
gameFinished <<- TRUE
}
runSimulation <- function() {
numberOfGames <<- 0
numberOfSames <<- 0
totalComparisons <<- 0
avgComparisons <<- 0
firstRoundAndGameWinnerSame <<- list()
gameWinners <<- list()
firstWarWinners <<- list()
ComparisonsResults <<- list()
repeat {
setUpGame()
withTimeout({
runGame()
}, timeout = 2, onTimeout = "warning")
if(!firstWarWinner == 0 && gameFinished == TRUE){
firstWarWinners[[length(firstWarWinners)+1]] <<- firstWarWinner
gameWinners[[length(gameWinners)+1]] <<- gameWinner
ComparisonsResults[[length(ComparisonsResults)+1]] <<- comparisonCount
totalComparisons <<- totalComparisons + comparisonCount
numberOfGames <<- numberOfGames + 1
if(firstWarWinner == gameWinner) {
firstRoundAndGameWinnerSame[[length(firstRoundAndGameWinnerSame)+1]] <<- 1
numberOfSames <<- numberOfSames + 1
} else {
firstRoundAndGameWinnerSame[[length(firstRoundAndGameWinnerSame)+1]] <<- 0
}
}
if(numberOfGames == numberOfGamesToBePlayed) {
break
}
#Sys.sleep(1)
}
data <- data.frame(
"id" = c (1:numberOfGames),
"comparison_counts" = unlist(ComparisonsResults, recursive = TRUE, use.names = TRUE),
"first_war_winner" = unlist(firstWarWinners, recursive = TRUE, use.names = TRUE),
"game_winner" = unlist(gameWinners, recursive = TRUE, use.names = TRUE),
"same_winner" = unlist(firstRoundAndGameWinnerSame, recursive = TRUE, use.names = TRUE),
stringsAsFactors = FALSE
)
avgComparisons <<- totalComparisons/numberOfGamesToBePlayed
print(data)
print("number of games with same first war winner and game winner: ")
print(numberOfSames)
print("games with same first war winner and game winner per total: ")
print(numberOfSames/numberOfGamesToBePlayed)
print("average card comparisons per game: ")
print(avgComparisons)
}
runSimulation()