Winning SET game with python (part2)
Set with Friends ← สามารถลองเล่นเกมนี้ก่อนได้ที่นี่ (single/ multiplayer)
🥑table of contents
- part 1: introduction + solve set game with python cli
- part 2: using bot to solve set game
🍏preparation
- python 3
- ***selenium + chrome web driver (part2)
นี่ก็เป็น part ที่ 2 แล้วนะครับสำหรับซีรียเกม set พาร์ทนี้จะลงรายละเอียดค่อนข้างเยอะหากมีข้อสงสัย หรือแนะนำสามารถ comment ได้นะครับ
Installation🍒
สำหรับคนที่เคยลงแล้วสามารถข้ามส่วนนี้ไปได้เลยนะครับ
chrome web driver🍋
- checking chrome version <… → settings → about chrome>
- download web driver here ตาม version ข้างต้น(เอาแค่เลขตัวหน้าเหมือนๆกันก็พอ)
- วาง chromedriver.exe ไว้ที่ไหนก็ได้แต่ copy path มาแปะด้วยใน code ด้วย
driver = webdriver.Chrome('C:/Users/w.manothumsatit/Learning/Python/chromedriver.exe')
selenium 🍌
สามารถ download ได้จาก cli หรือ ตาม document
pip install selenium
START🍓
เช่นเคย source code available here (don’t forget to change web driver path) แนะนำให้ load source code มาดูประกอบนะครับ
python autoplay.py
Breakdown elements
right click on game and then inspect elements with icon below
- finding new private game button: get element by XPath
new_private_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/div/div[3]/div[1]/button[2]')))new_private_button.click()
2. finding start game button: get element by XPath
start_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/div/div[2]/div/div[2]/button')))start_button.click()
3. finding element in main game: get element by Class Name
จากภาพด้านบนจะเห็นว่า รูปที่ visible จะมี class jss82 อยู่ (only enter game first time) เราจะ get pieces เหล่านี้มา เป็น list
visibility_cards = driver.find_elements_by_class_name('jss82')
4. transform each piece into our Piece class
จากภาพจะเห็นว่า
- piece แรกจะมี element svg(class jss80)อยู่ 3 ตัว = number
- มีสีคือ ม่วง = fill:”#800080"
- รูปร่างคือ href:”#squiggle”
- pattern ต้องดูจาก mask=”” และ fill=”transparent” หรือไม่
จากนั้นแปลงเป็น function
5. solve pieces list with logic form part1
6. apply loop while to replay each turn
Result 🍈
To be imporve🍇
- speed up code: get_attribute ของ selenium ช้ามากทำให้แต่ละเทิร์นใช้เวลานาน, แต่ละเทิร์น breakdown แค่ piece ที่เพิ่มมาใหม่
- this code using for only normal mode ลองสร้างสำหรับ set-chain and ultra-set mode
- apply to play in online mode
ขอบคุณทุกคนที่อ่านมาถึงตรงนี้ หากมีข้อสงสัยเพิ่มเติมหรือมีข้อแนะนำสามารถ comment ได้นะครับ
`💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘💘