Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/21-game/R/21-game-1.r
Normal file
49
Task/21-game/R/21-game-1.r
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
game21<-function(first = c("player","ai","random"),sleep=.5){
|
||||
state = 0
|
||||
finished = F
|
||||
turn = 1
|
||||
if(length(first)==1 && startsWith(tolower(first),"r")){
|
||||
first = rbinom(1,1,.5)
|
||||
}else{
|
||||
first = (length(first)>1 || startsWith(tolower(first),"p"))
|
||||
}
|
||||
while(!finished){
|
||||
if(turn>1 || first){
|
||||
cat("The total is now",state,"\n");Sys.sleep(sleep)
|
||||
while(T){
|
||||
player.move = readline(prompt = "Enter move: ")
|
||||
if((player.move=="1"||player.move=="2"||player.move=="3") && state+as.numeric(player.move)<=21){
|
||||
player.move = as.numeric(player.move)
|
||||
state = state + player.move
|
||||
break
|
||||
}else if(tolower(player.move)=="exit"|tolower(player.move)=="quit"|tolower(player.move)=="end"){
|
||||
cat("Goodbye.\n")
|
||||
finished = T
|
||||
break
|
||||
}else{
|
||||
cat("Error: invaid entry.\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
if(state == 21){
|
||||
cat("You win!\n")
|
||||
finished = T
|
||||
}
|
||||
if(!finished){
|
||||
cat("The total is now",state,"\n");Sys.sleep(sleep)
|
||||
while(T){
|
||||
ai.move = sample(1:3,1)
|
||||
if(state+ai.move<=21){
|
||||
break
|
||||
}
|
||||
}
|
||||
state = state + ai.move
|
||||
cat("The AI chooses",ai.move,"\n");Sys.sleep(sleep)
|
||||
if(state == 21){
|
||||
cat("The AI wins!\n")
|
||||
finished = T
|
||||
}
|
||||
}
|
||||
turn = turn + 1
|
||||
}
|
||||
}
|
||||
1
Task/21-game/R/21-game-2.r
Normal file
1
Task/21-game/R/21-game-2.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
game21()
|
||||
1
Task/21-game/R/21-game-3.r
Normal file
1
Task/21-game/R/21-game-3.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
game21(first = "ai")
|
||||
Loading…
Add table
Add a link
Reference in a new issue