科大訊飛 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.pswp.cn/news/394926.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394926.shtml
英文地址,請注明出處:http://en.pswp.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提供了幾種語…