科大讯飞 ai算法挑战赛_为井字游戏挑战构建AI算法

科大讯飞 ai算法挑战赛

by Ben Carp

通过本·卡尔普

为井字游戏挑战构建AI算法 (Building an AI algorithm for the Tic-Tac-Toe challenge)

As part of the freeCodeCamp curriculum, I was challenged build a Tic-Tac-Toe web app. It was a real pleasure.

作为freeCodeCamp课程的一部分,我遇到了构建Tic-Tac-Toe网络应用程序的挑战。 真的很高兴。

The app includes an ultimate computer player. It can optimize any given situation on the Tic-Tac-Toe board. The outcome surprised me.

该应用程序包括一个终极的计算机播放器。 它可以优化井字游戏板上的任何给定情况。 结果使我感到惊讶。

Even in such a simple game, the computer player taught me some new moves. As for the code I wrote, it is somewhat unique and interesting to explore.

即使在这样简单的游戏中,计算机玩家也会教给我一些新的动作。 至于我编写的代码,它有些独特且有趣。

看看这个 (Check it out)

Visit this link and choose to play against the computer. I challenge you to win. You might find…that you can’t.

访问此链接,然后选择与计算机对战。 我挑战你赢 。 您可能会发现……您做不到。

Yet, if you are hard on the defense, you might find out that the computer is not able to win either. I learned by experience that Tic-Tac-Toe has a simple non-lose strategy.

但是,如果您在防御上很努力,则可能会发现计算机也无法获胜。 我从经验中学到,井字游戏有一个简单的不输球策略。

This means that if you manage to get a tie you are making the right defensive choices. The computer still optimizes its’ moves. So, the best result it can achieve against a player such as yourself might only be a tie.

这意味着,如果您设法获得平局,那么您将做出正确的防守选择。 电脑仍在优化其动作。 因此,对您这样的玩家所能达到的最佳结果可能只是平局。

主要解决方案步骤 (Main Solution steps)

1.板子数据结构 (1. board data structure)

_gameBoard: [[“”, “”, “”],[“”, “”, “”],[“”, “”, “”]]

The board Array contains 3 arrays, each representing a row.Each row array contains 3 character or string elements.

板子数组包含3个数组,每个数组代表一行。每个行数组包含3个字符或字符串元素。

These elements are either:

这些元素是:

  • “ ” as an empty string, representing an empty cell

    “”为空字符串,表示一个空单元格
  • “X” representing the X player

    代表X播放器的“ X”
  • “O” representing the O player

    代表O玩家的“ O”

2. getResult函数 (2. getResult function)

Begins at Line 59

从第59行开始

At any given state, the board will be in one and one only of these possible states:

在任何给定状态下,董事会将处于以下一种或一种可能的状态:

  • Incomplete

    不完整
  • player X won

    玩家X赢了
  • Player O won

    玩家O赢了
  • or a tie

    或领带

The getResult function receives a board array, iterates over all the rows, through all the columns and across both diagonals. It checks the succession of symbols. Then it lets us know the current state of that board.

getResult函数接收一个board数组,遍历所有行,所有列以及两个对角线。 它检查符号的连续性。 然后,它让我们知道该板的当前状态。

3. getBestMove函数 (3. getBestMove Function)

Here it gets more difficult. When the board is empty it is very difficult to identify the best possible move. Take a look at this board.

在这里变得更加困难。 当木板为空时,很难确定最佳移动方式。 看一下这个板子。

Which is the best possible possible move?

哪个可能是最好的举动?

When the board becomes populated, the best possible move pops out to our eyes.

当木板装满时,最好的动作突然出现在我们眼前。

Let’s use this populated board as our starting point. Lets decide that the next move is ours, and that our symbol is an “X”.

让我们以填充的木板为起点。 让我们决定下一步是我们的行动,我们的符号是“ X”。

Let’s try to identify the best possible move with the tools we already have. There are 3 empty cells that correspond with 3 possible moves. Lets check the result for each of these options.

让我们尝试使用我们现有的工具来确定最佳的移动方式。 有3个空单元格,它们对应3种可能的移动。 让我们检查每个选项的结果。

We can do this by iterating over the possible moves, and for each of them:

我们可以通过迭代可能的移动来实现此目的,对于每个移动:

  • Create a new board

    创建一个新板
  • Add our symbol to the corresponding empty cell

    将我们的符号添加到相应的空单元格中
  • Send this board to the getResult function

    将该板发送到getResult函数

From the 3 boards in the figure above, when we send the second board to the getResult function, we will receive our trophy.

从上图中的3个板中,当我们将第二个板发送到getResult函数时,我们将获得奖杯。

Please concentrate for the next essential steps:

请集中精力进行以下基本步骤:

  1. We need to grade the possible moves so we can compare them. Let’s decide that if a move yields a winning board we will grade it 1. If it yields a losing board it will receive the grade of -1. A tie will receive a grade of 0.

    我们需要对可能的移动进行分级,以便可以对其进行比较。 让我们决定,如果一个举动产生一个获胜的棋盘,我们将其评分为1。如果它产生一个失败的棋盘,则其评分将为-1。 平局得分为0。
  2. Move 2 will receive a grade of 1. When we find a move graded with 1 we can ignore all other possible moves. There is no other better possible move than a definite victory.

    动作2的等级为1。当我们找到等级为1的动作时,我们可以忽略所有其他可能的动作。 没有比确定的胜利更好的举动了。
  3. But for the sake of understanding, how would we grade moves 1 or 3, or any other move with an incomplete result?

    但是为了理解,我们将如何对第1或第3步或任何其他结果不完整的步进行评分?

Let’s Focus on move 3. The solution is to send the corresponding board recursively to the getBestMove function.

让我们关注移动3。解决方案是将相应的板递归发送到getBestMove函数。

You might be thinking, “But wait! Our opponent plays the next move.” That’s right. Let’s find out what grade our opponent gets for his best future move.

您可能会想,“但是等等! 我们的对手下一个动作。” 那就对了。 让我们找出对手最好的未来举动所获得的等级。

Our opponent has only two possible moves:

我们的对手只有两个可能的举动:

Move 3–1 will win the game in favor of our opponent. Since we are using the exact same getBestMove function, Move 3–1 will receive a grade of 1.

3–1的举动将赢得我们对手的胜利。 由于我们使用的是完全相同的getBestMove函数,因此Move 3–1的等级为1。

This might be a bit confusing as both our victory and our loss will receive grades of 1. We need to remember that this function call belongs to our opponent, and his victory is our loss and vice versa.

这可能有点令人困惑,因为我们的胜利和失败都将得到1级。我们需要记住,此函数调用属于我们的对手,而他的胜利就是我们的失败,反之亦然。

We must negate any grade returned to the getBestMove function by the getBestMove function.

我们必须取消由getBestMove函数返回给getBestMove函数的任何成绩。

Move 3–1 receives a grade of 1. The getBestMove function returns a grade of 1, and we can grade Move 3 with a -1.

移动3-1的等级为getBestMove函数返回的等级为1,我们可以将移动3的等级getBestMove -1。

In this manner, the getBestMove function continues to explore moves and consequent moves. This process will continue until:

以这种方式, getBestMove函数继续探索移动以及随后的移动。 该过程将持续到:

  1. It finds a move graded with 1, in which case it will return the move immediately

    它会找到等级为1的移动,在这种情况下,它将立即返回该移动
  2. It will continue until each possible move has a grade. The possible moves (with grades 0 and -1) are stored in an array

    它将继续,直到每个可能的动作都得到评分。 可能的移动(等级0和-1)存储在数组中
  3. The array will then be:

    该数组将是:

    [a] randomized

    [a]随机

    [b] sorted from high to low

    [b]从高到低排序

    [c] the first element will be returned

    [c]第一个元素将被返回

These steps guarantee that:

这些步骤保证:

  1. A losing move will be avoided unless it’s the only option

    除非是唯一的选择,否则将避免失败。
  2. The computer player can play diversely

    电脑播放器可以玩多种游戏

尾注: (End Notes:)

  1. There are strong legitimate concerns over the risks Artificial Intelligence (AI) brings with it.

    人们对人工智能(AI)带来的风险有强烈的正当担忧。

    Lets use AI for the benefit of all.

    让AI造福所有人。

    The best possible AI software is that which can prevent us from misusing AI.

    最好的AI软件是可以防止我们滥用AI的软件。

  2. I consulted Assaf Weinberg in the process of writing the app

    在编写应用程序的过程中,我咨询了阿萨夫·温伯格 ( Assaf Weinberg)

See my code on GitHub.

在GitHub上查看我的代码 。

翻译自: https://www.freecodecamp.org/news/building-an-ai-algorithm-for-the-tic-tac-toe-challenge-29d4d5adee07/

科大讯飞 ai算法挑战赛

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394926.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

js serialize php 解,[转]JavaScript 版本的 PHP serialize/unserialize 完整实现

下载: phpserializer.js/* phpserializer.js - JavaScript to PHP serialize / unserialize class.** This class is designed to convert php variables to javascript* and javascript variables to php with a php serialize unserialize* compatible way.** Copyright (C) …

Git 的 .gitignore 配置

.gitignore 配置文件用于配置不需要加入版本管理的文件,配置好该文件可以为我们的版本管理带来很大的便利,以下是个人对于配置 .gitignore 的一些心得。 1、配置语法: 以斜杠“/”开头表示目录; 以星号“*”通配多个字符&#xff…

wsdl文件是怎么生成的_C++ 动态库.dll的生成---超级详细!!!

怎么将建好的工程生成.dll工程?1、在C中打开工程2、运行结果:输出Print修改开始:1、打开属性。2、修改以下内容:目标文件扩展名,由.exe--》.dll,直接删除修改即可配置类型,由.exe--》.dll,下拉菜单可选择最…

时钟设置

date --set"05/31/16 18:16" 时钟设置 设置系统时间# date --set“07/07/06 10:19" (月/日/年 时:分:秒)2、hwclock/clock查看硬件时# hwclock --show# clock --show设置硬件时间# hwclock --set --date"07/07/06 10:19" &…

《成为一名机器学习工程师》_成为机器学习的拉斐尔·纳达尔

《成为一名机器学习工程师》by Sudharsan Asaithambi通过Sudharsan Asaithambi 成为机器学习的拉斐尔纳达尔 (Become the Rafael Nadal of Machine Learning) One year back, I was a newbie to the world of Machine Learning. I used to get overwhelmed by small decisions…

HTTP基本认证(Basic Authentication)的JAVA示例

大家在登录网站的时候,大部分时候是通过一个表单提交登录信息。但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证。下面来看看一看这个认证的工作过程:第一步: 客户端发送http request 给服务器,服务器验证该用户…

php-fpm 内存 facebook,【百家号】脸书百科,安装php-fpm-5.4.16-42.遇到的小问题 Web程序 - 贪吃蛇学院-专业IT技术平台...

环境:redhat 7.2版本 yum源也是7.2的iso[[email protected] lnmp_soft]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm已加载插件:langpacks, product-id, search-disabled-repos, subscription-managerThis system is not registered to Red Hat S…

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

昨晚的没来得及打,最近错过好几场CF了,这场应该不算太难 A. Unimodal Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputArray of integers is unimodal, if: it is strictly increasing in…

python能print中文吗_python怎么print汉字

今天就为大家分享一篇python中使用print输出中文的方法,具有很好的参考价值,希望对大家有所帮助。看Python简明教程,学习使用print打印字符串,试了下打印中文,不行。(推荐学习:Python视频教程&a…

ajax的一些相关

1、AJAX Asynchronous(异步的) JavaScript and XML AJAX是能不刷新整个网页的前提下,更新内容。通过少量的数据交换,达成局部页面刷新的效果。 而form表单提交经常是刷新整个页面,很繁琐 2、AJAX是基于现有的Internet…

select ...as_一起使用.select .map和.reduce方法可充分利用Ruby

select ...asby Declan Meehan由Declan Meehan 一起使用.select .map和.reduce方法可充分利用Ruby (Get the most out of Ruby by using the .select .map and .reduce methods together) You should absolutely never ever repeat yourself when writing code. In other word…

一些书单

仅对近来的学习做些回顾吧 学习永无止境--> 2015年已完成书单: 文学: 硅谷之火浪潮之巅天才在左疯子在右从0到1生命咖啡馆黑客与画家奇思妙想:15位计算机天才及其重大发现乔布斯传平凡的世界(三部全)一只iphone的全…

oracle 11gogg,【OGG】Oracle GoldenGate 11g (二) GoldenGate 11g 单向同步配置 上

Oracle GoldenGate 11g (二)GoldenGate 11g 单向同步配置 上ItemSource SystemTarget SystemPlatformRHEL6.4 - 64bitRHEL6.4 - 64bitHostnamerhel64.oracle.comora11g.oracle.comDatabaseOracle 11.2.0.3Oracle 11.2.0.3Character SetAL32UTF8AL32UTF8ORACLE_SIDPRODEMREPList…

今天听说了一个压缩解压整型的方式-group-varint

group varint https://github.com/facebook/folly/blob/master/folly/docs/GroupVarint.md 这个是facebook的实现 https://www.slideshare.net/parallellabs/building-software-systems-at-google-and-lessons-learned/48-Group_Varint_Encoding_Idea_encode

Centos7-卸载自带的jdk 安装jdk8

卸载JDK Centos7一般都会带有自己的openjdk,我们一般都回用oracle的jdk,所以要卸载 步骤一:查询系统是否以安装jdk #rpm -qa|grep java 或 #rpm -qa|grep jdk 或 #rpm -qa|grep gcj 步骤二:卸载已安装的jdk #rpm -e --nodeps java-1.8.0-openjdk…

小猪佩奇python_python画个小猪佩奇

#!/usr/bin/python #-*- coding: utf-8 -*-import turtleast def nose(x,y):#鼻子 t.pu() t.goto(x,y) t.pd() t.seth(-30) t.begin_fill() a0.4 for i in range(120):if 0<i<30 or 60<i<90: aa0.08t.lt(3) #向左转3度 t.fd(a) #向前走a的步长else: aa-0.08t.lt(3)…

javascript 符号_理解JavaScript中“ =”符号的直观指南

javascript 符号by Kevin Kononenko凯文科诺年科(Kevin Kononenko) 理解JavaScript中“ ”符号的直观指南 (A Visual Guide to Understanding the “” Sign in JavaScript) 实际上&#xff0c;对于第一次学习编码的人来说&#xff0c;赋值运算符(或“ ”符号)实际上会产生误导…

iOS开发UIScrollView的底层实现

起始 做开发也有一段时间了&#xff0c;经历了第一次完成项目的激动&#xff0c;也经历了天天调用系统的API的枯燥&#xff0c;于是就有了探索底层实现的想法。 关于scrollView的思考 在iOS开发中我们会大量用到scrollView这个控件&#xff0c;我们使用的tableView/collectionv…

oracle查看登录时间黑屏,oracle 11g默认用户名、密码解锁 以及安装后重启黑屏问题.doc...

oracle 11g默认用户名、密码解锁 以及安装后重启黑屏问题.doc还剩3页未读&#xff0c;继续阅读下载文档到电脑&#xff0c;马上远离加班熬夜&#xff01;亲&#xff0c;喜欢就下载吧&#xff0c;价低环保&#xff01;内容要点&#xff1a;遇的同学&#xff0c;参考一下解决办法…

第六十二节,html分组元素

html分组元素 学习要点&#xff1a; 1.分组元素总汇 2.分组元素解析 本章主要探讨HTML5中分组元素的用法。所谓分组&#xff0c;就是用来组织相关内容的HTML5元素&#xff0c;清晰有效的进行归类。 一&#xff0e;分组元素总汇 为了页面的排版需要&#xff0c;HTML5提供了几种语…