科大讯飞 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算法挑战赛

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

  1. 机器学习算法 拟合曲线_制定学习曲线以检测机器学习算法中的错误

    机器学习算法 拟合曲线 机器学习 (Machine Learning) The learning curve is very useful to determine how to improve th ...

  2. 聚类算法 距离矩阵_论文阅读9——AP聚类算法

    Affinity Learning for Mixed Data Clustering 论文提出了基于混合对数据进行聚类的学习框架,具体研究内容包括:1)如何处理具有混合类型属性的数据.2)如何学习数 ...

  3. 三维图形几何变换算法实验_基于深度学习的三维重建算法综述

    点击上方"计算机视觉life",选择"星标" 快速获得最新干货 00 前言 目前,三维重建技术已在游戏.电影.测绘.定位.导航.自动驾驶.VR/AR.工业制造以 ...

  4. otsu阈值分割算法原理_大津二值化算法OTSU的理解

    otsu 大津算法介绍: OTSU算法是由日本学者OTSU于1979年提出的一种对图像进行二值化的高效算法. 利用阈值将原图像分成前景,背景两个图象. 前景:用n1,csum,m1来表示在当前阈值下的 ...

  5. 3算法全称_全网最通俗的KMP算法图解

    导语 本篇内容研究字符串匹配问题,首先介绍字符串匹配问题,引出Brute-Force算法及其优化方法,最后深入详解KMP算法.文章结构如下(全文阅读需要30分钟左右): 字符串匹配问题 1字符串匹配问 ...

  6. fp算法例题_机器学习(九)—FP-growth算法

    本来老师是想让我学Hadoop的,也装了Ubuntu,配置了Hadoop,一时间却不知从何学起,加之自己还是想先看点自己喜欢的算法,学习Hadoop也就暂且搁置了,不过还是想问一下园子里的朋友有什么学 ...

  7. python路径规划算法可视化_路径规划问题:DIJKSTRA算法 以及Python实现

    一. DJKSTRA算法概述 我们可以将地图抽象为Graph的数据结构,然后利用Graph的广度优先遍历算法(Breadth-First Search, BFS)解决无权重的High-Level的地图 ...

  8. rrt算法流程图_基于RRT的运动规划算法综述

    基于 RRT 的运动规划算法综述 1. 介绍 在过去的十多年中, 机器人的运动规划问题已经收到了大量的关注, 因为机器人开始成 为现代工业和日常生活的重要组成部分. 最早的运动规划的问题只是考虑如何移 ...

  9. java五子棋的重要算法讲解_[Java五子棋小游戏-Ai算法精讲以及实现]-02--高级算法初步...

    高级算法初步 走对自己利益最大的路 它有难了不要慌,抛弃它,以这盘棋局的胜利为他报仇 碰撞检测与跨步算法原理图 反向计算权重 每次计算完权重,都要进行一次反向权重计算 权重值 : 两侧的权重值相加 权 ...

最新文章

  1. python3 asyncio 协程模块
  2. 无监督学习:异常检测与剔除(局部异常因子法 SVM异常检测器)
  3. 【矩阵乘法】Quad Tiling(poj 3420)
  4. mysql中独立表空间与共享表空间之前如何切换
  5. rdlc tablix_SQL Server中的报表–通过分组功能(由Tablix控件提供)处理数据
  6. 大学计算机专业绩点在3.5算好,大学绩点3.5算什么水平 绩点低有哪些补救方法...
  7. Django图书商城项目/图书管理/毕业设计
  8. 信息安全工程师(中级)—重要知识点总结
  9. 加班熬夜一个月,终于把所有Python库整理齐了,祝你早日学成
  10. mysql 主键 默认索引_mysql 主键和默认 设为索引的规则
  11. 金融业务-美港股和A股的区别
  12. 匹配问题: 匈牙利算法 、最优指派、相等子图、库恩—曼克莱斯 (Kuhn-Munkres) 算法
  13. 一千万条数据去重_DB2千万级数据去重
  14. linux lp0设备,P1108打印机插入Linux设备USB口后自动reset
  15. 阿里大鱼进行发短信业务---使用详细步骤-Java操作
  16. 区块链编程_区块链编程的10个最佳工具包
  17. set | map | multiset | multimap 快速上手
  18. 用JS实现图片切换、定时器、轮播图
  19. 自制工具:Cocos2d-x Android.mk文件自动修改器
  20. java clover_clover记录数据库的生成方法

热门文章

  1. Java-进阶:多线程1
  2. 51nod1584加权约数和
  3. Python 2.7 cython cythonize py 编译成 pyd 谈谈那些坑(转载)
  4. 897. 递增顺序查找树-未解决
  5. Maven+eclipse快速入门
  6. Github 简明教程 - 添加远程库
  7. OTL翻译(9) --常量的SQL语句
  8. attr与prop的区别
  9. 中小企业ERP实施的项目管理
  10. 计算机中的数(一):数在计算机中的表示