ในเมื่อเราผ่าน Basic Knowledge ของ R Language มาแล้วเรามาลองทำเกม
เป่ายิ้งฉุบกันดีกว่า
เย้
โจทย์คือ
- ทำเกม เป่า ยิ้ง ฉุบ
- ถามเรื่อยๆจนกว่าจะตอบว่า no
- ถ้าไม่เล่นแล้วให้มีผลสรุปด้วย
เรามา scope ให้เล็กลงก่อน คือเขียนโปรแกรมให้มันทำครั้งเดียวก่อน
อันดับแรกเรามาถาม User กันก่อน
wanna_play <- tolower(readline("Wanna play Pao Yin Chub? ([y]es/[n]o): "))
if (wanna_play == "y") {
...
}else if (wanna_play == "n"){
...
}else {
...
}
จาก code ข้างบน จะเป็นการถาม user และ ใช้ if-else ในการทำ condition ที่ user จะเลือก
ซึ่งจะมี 3 อย่างเท่านั้นคือ y,n และ นอกเหนือจากนั้น
ต่อไป ใน condition ที่ wanna_play เป็น y เราจะใส่ logic ค้อน กรรไกร กระดาษ เข้าไป
# Ask "Choose Rock, Paper or Scissor"
input_player <- tolower(readline("Choose [R]ock, [P]aper, or [S]cissor: "))
# Define valid choices
rps <- c("r", "p", "s")
# Randomly select for the computer
input_com <- sample(rps, 1)
# Display choices
print(paste("Player:", input_player, "| Com:", input_com))
# Determine winner
if ((input_player == "r" && input_com == "s") ||
(input_player == "p" && input_com == "r") ||
(input_player == "s" && input_com == "p")) {
player_scores <- player_scores + 1
print("You Win!!!")
} else if ((input_com == "r" && input_player == "s") ||
(input_com == "p" && input_player == "r") ||
(input_com == "s" && input_player == "p")) {
com_scores <- com_scores + 1
print("You Lose!!!")
} else {
print("It's a tie!")
}
จากนั้นจะเป็นการใส่ logic สรุปผลเข้าไป ใน condition ที่ wanna_play เป็น n
# Summarize results and exit
if (player_scores > com_scores) {
print("....You Win Overall....")
} else if (player_scores < com_scores) {
print("....You Lose Overall....")
} else {
print("It's a Draw Overall!!")
}
break # Exit the loop
สุดท้ายแต่ไม่ท้ายสุด เราจะ Print คำเตือน ให้กับ else สำหรับคนที่ไม่ได้ตอบ y or n
print("Invalid input! Please enter 'y' or 'n'.")
สุดท้าย แล้วอย่าลืมที่จะ run function เพื่อจะเล่นด้วยล่ะ!
# Run the function
pao_yin_chub()
และนี่คือ Full Script Code นะ เอาไปลองกันได้เลย
pao_yin_chub <- function(){
player_scores <- 0
com_scores <- 0
# loop infinite until the answer is "no"
while (TRUE){
wanna_play <- tolower(readline("Wanna play Pao Yin Chub? ([y]es/[n]o): "))
if (wanna_play == "y") {
# Ask "Choose Rock, Paper or Scissor"
input_player <- tolower(readline("Choose [R]ock, [P]aper, or [S]cissor: "))
# Define valid choices
rps <- c("r", "p", "s")
# Randomly select for the computer
input_com <- sample(rps, 1)
# Display choices
print(paste("Player:", input_player, "| Com:", input_com))
# Determine winner
if ((input_player == "r" && input_com == "s") ||
(input_player == "p" && input_com == "r") ||
(input_player == "s" && input_com == "p")) {
player_scores <- player_scores + 1
print("You Win!!!")
} else if ((input_com == "r" && input_player == "s") ||
(input_com == "p" && input_player == "r") ||
(input_com == "s" && input_player == "p")) {
com_scores <- com_scores + 1
print("You Lose!!!")
} else {
print("It's a tie!")
}
} else if (wanna_play == "n") {
# Summarize results and exit
if (player_scores > com_scores) {
print("....You Win Overall....")
} else if (player_scores < com_scores) {
print("....You Lose Overall....")
} else {
print("It's a Draw Overall!!")
}
break # Exit the loop
} else {
print("Invalid input! Please enter 'y' or 'n'.")
}
}
}
# Run the function
pao_yin_chub()




