Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I'm trying to write a bot in Python to play a Web-based game of tic-tac-toe. I've found a tutorial written in Java that closely fits my own purposes, but I'm having trouble understanding how to duplicate the Java code in Python.

I want a structure to represent the status of each of the squares on the board. The tutorial calls for a Map which holds each square and a Boolean value indicating whether it's been marked already.

Java:
private Map<Space, Boolean> spaces = new HashMap(); 


I know Python's hash maps are called dictionaries, but how do I go about declaring one and populating it with each of the squares from my Square enum and set occupancy to false for all of them? (The game has not begun, so the board is empty.)

Java:

Arrays.stream(Space.values()).forEach(space -> spaces.put(space, false)); 


What is the Python equivalent of these two segments? Also, what is "stream" in the code immediately above?

What I have tried:

Square.py

import enum
from selenium.webdriver.common.by import By

class Squares(enum.Enum):
    TOP_LEFT = "top left square"
    TOP_CENTER = "top middle square"
    TOP_RIGHT = "top right square"
    CENTER_LEFT = "center left square"
    CENTER_CENTER = "center middle square"
    CENTER_RIGHT = "square right square"
    BOTTOM_LEFT = "bottom left square"
    BOTTOM_CENTER = "bottom middle square"
    BOTTOM_RIGHT = "bottom right square"

    def __init__(self, className: str):
        self.className = className
        self.locator = By.XPATH(format("//div[@class='%s']", className))

    def getClassName(self):
        return self.className

    def getLocator(self):
        return self.locator


Board.py

from selenium import webdriver
from Square import Squares

class Board():

    driver = webdriver.Firefox()
    driver.get("http://localhost:3000")
Posted
Updated 30-Oct-22 6:04am

This question and your very related one have a huge amount in common: Enum in java versus Python[^]
You are making a mistake, and a pretty big one.

You are assuming that code in Language A just needs to be translated to Language B in order to get a good app in the target language. That doesn't happen - it produces bad code in the target and worse, it teaches you nothing about writing code, designing an app, or pretty much anything else.

The two languages use different frameworks, so what works well in Java doesn't necessarily translate well to Python (very little does).

So what you are doing is making your life harder in the long run: you don't learn how to develop an app, you don't learn Python particularly well, and you don't get a good grade when you hand it in!

So why do it?

Instead, read the Java code and extract the design, the overall way it works - not the specifics of the language it uses - and use that as a specification for your own Python app. Then design an app based on that specification, and start coding that. You'll get a better app, quicker, and you'll learn stuff that will help you with the next assignment. And probably get a better grade to boot.
 
Share this answer
 
to avoid Enum and Map
get list of empty squares from webpage
(where elements are of RemoteWebElement type)

use random element
and do .click()
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900