Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
2048使用python selenium获取板_Python_Python 3.x_Selenium_Selenium Webdriver - Fatal编程技术网

2048使用python selenium获取板

2048使用python selenium获取板,python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,我正在尝试创建一个可以玩游戏的AI。我的问题是我不能得到我想要的格式的电路板 我的代码(获得董事会): 我想让它把瓷砖放在字典的正确位置,例如: 如果磁贴1-1是4,那么它应该执行board['1']=“4” 我不懂Python,但这是Java,希望你能翻译它 我以board[1][2]=16的形式将电路板放入int的二维数组中,其中该磁贴上的类将是tile-16 tile-position-1-2 tile new。我知道这不是你要求的,但你没有提供足够的细节,说明你是如何储存各种职位的 Li

我正在尝试创建一个可以玩游戏的AI。我的问题是我不能得到我想要的格式的电路板

我的代码(获得董事会):

我想让它把瓷砖放在字典的正确位置,例如: 如果磁贴1-1是4,那么它应该执行
board['1']=“4”


我不懂Python,但这是Java,希望你能翻译它

我以
board[1][2]=16
的形式将电路板放入
int
的二维数组中,其中该磁贴上的类将是
tile-16 tile-position-1-2 tile new
。我知道这不是你要求的,但你没有提供足够的细节,说明你是如何储存各种职位的

List<WebElement> tiles = driver.findElements(By.cssSelector("div.tile"));
int[][] board = new int[5][5]; // need 5 instead of 4 because we are going to use indices 1-4 and not 0-3
for (WebElement tile : tiles)
{
    String className = tile.getAttribute("className");
    String regex = "tile tile-(\\d*) tile-position-(\\d)-(\\d) tile-new";
    Matcher matcher = Pattern.compile(regex).matcher(className);
    matcher.matches();
    int x = Integer.parseInt(matcher.group(2));
    int y = Integer.parseInt(matcher.group(3));
    int value = Integer.parseInt(matcher.group(1));
    board[x][y] = value;
}

希望这能有所帮助。

列表不是比字典更合适吗?不,因为人工智能的工作方式……试着把这个问题重新表述为一个编程问题,用一个最简单的例子说明什么不起作用。您是否对如何使用
selenium
阅读网页内容感到困惑,或者对如何更新字典感到困惑,或者在存储游戏板时寻找数据结构建议感到困惑?tile 1-1如何转换为['1']?1-2会去哪里?2-1?你需要将你提供的东西简化为你所要求的,通过提供一些代码显示你已经做了一些工作,并解释为什么它不起作用。1-1就像跳线…这就是它在页面中的方式,但我想让它像1-1是1,2-1是2,3-1是3,4-1是4,然后1-2是5,2-2是6…4-4是16(问题是我对编程非常陌生,这是我第一次使用selenium,所以我不知道它为什么不起作用:/)我会尽力理解和翻译它(我不懂Java)谢谢:DIt不应该太难…基于selenium的方法应该很容易翻译。
int[]
是一个二维数组。
for(WebElement tile:tiles)
类似于
foreach()
在其他语言中。我认为正则表达式会很简单,因为正则表达式字符串很明显,您只需提取特定的匹配组,将它们转换为
int
,并将它们分配到2d数组中。如果您需要更多帮助,请告诉我,我会看看我能做些什么。我仍然无法理解,但我认为我正在执行process…..问题是我不知道出了什么问题……我得到了一个AttributeError:'非类型'对象没有属性'组'….当我打印(电路板)时,我得到了类似:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,2'],[0,0,0,0,0,0,0]]它还有一个数字和一行…所以我设置它的方式有一点是,我不使用
board
的第0个索引…只使用第1-4行。这样它与类名匹配。所以从x=1到4,y=1到4循环,你应该很好。或者你可以花时间-1'处理所有索引…:)是的,我现在知道了,我只做了范围(1,5)…但我还有另一个问题…在做了一次之后,如果我尝试循环它,它会说是回溯(最近一次调用):文件“C:\Users\panag\OneDrive\Python\Finished\Uncompiled\2048\browser auto play\as.py”,第24行,在game\u board=get\u board()文件中“C:\Users\panag\OneDrive\Python\Finished\Uncompiled\2048\browser auto play\as.py”,get\u board x=int(m.group(3))AttributeError中的第16行:“非类型”对象没有属性“组”
List<WebElement> tiles = driver.findElements(By.cssSelector("div.tile"));
int[][] board = new int[5][5]; // need 5 instead of 4 because we are going to use indices 1-4 and not 0-3
for (WebElement tile : tiles)
{
    String className = tile.getAttribute("className");
    String regex = "tile tile-(\\d*) tile-position-(\\d)-(\\d) tile-new";
    Matcher matcher = Pattern.compile(regex).matcher(className);
    matcher.matches();
    int x = Integer.parseInt(matcher.group(2));
    int y = Integer.parseInt(matcher.group(3));
    int value = Integer.parseInt(matcher.group(1));
    board[x][y] = value;
}
0000
0020
0000
0020