AI Gaming with Azure & Python
Tags: azure, python, cloud, gamedev
Published: 2021-04-03 17:36:28

AI has several applications but Gaming might be the most fascinating one! So how about we talk a little about AI Gaming & all the amazing things it brings to the table when industry-leading cloud computing service Azure and my favorite programming language Python comes together.

# What is AI Gaming?

In classic gaming, we bring together two players and they play the game accordingly to score maximum points and the one with maximum points, well as you've guessed, wins the game. 

AI Gaming is the same thing, but here you bring two programmers and they code their bots (or scripts). Both bots are programmed to play the same game and the bot which is faster, more accurate & can score more points will win the game.

Sounds fun right!

# AI Gaming Platform

What if I tell you, there's an entire platform dedicated to AI Gaming where you can learn, code & compete with your bots? Well, I am not kidding, you can see yourself here [aigaming.com](https://www.aigaming.com/). 

![2021-04-03 17_01_34-Home Page - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4vgv50p42z3a1r72bpj.png) 

Throughout this article, we are going to explore the platform and see how it works plus how you can create bots and power them with Azure's cutting-edge Cognitive Services.

## Setting up your AI Gaming Account

Alright so before we do anything, let's quickly create your AI Gaming Account.

![2021-04-03 17_03_23-Registration - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bddkad3kl8aavxhllkn6.png)
 

Go ahead and click on the Register on the top right corner and fill in your email, a strong password, a fancy username, and click on register. I'll leave the rest on you.

Now once you've successfully registered and logged in to your account, we can proceed to further steps.

## Getting familiar with the Interface

Let's quickly gaze over the essential components the AI Gaming so we can quickly get started coding out bots.

![2021-04-03 17_15_25-Home Page - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hit0k465jmhoakpeaao9.png)
 

**In the top header, we have a few options:**

- **Games:** Under this menu, you can browse through all the games you can play on the AI Gaming platform. Every game is different, hence the strategies & code for each will be different as well. You can also register for tournaments  & compete with other players.

![2021-04-03 17_26_04-Home Page - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25ad7c4k0xcs8z1nolkt.png)
 

- **Editor:** The editor is where you will be writing all your code. This is where you'll probably spend most of your time.

 ![2021-04-03 17_31_01-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nooz5yt040guoh5iou6n.png) 

- **My Account:** Here you can find all the account and bot management options. Nothing much to worry about here.

![2021-04-03 20_08_35-Home Page - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e92krqvohw26l15jmups.png) 

- **Help:** Help section is full of useful & beginner-friendly tutorials to help you get started with AI Gaming.

![2021-04-03 20_11_23-Help - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtverhsz964itju3vkpy.png) 

For now, you can ignore the About us & News Section.

# Let's Get Coding!

Alright, so the fun part begins here! Let's do it!

## Creating a Bot

So the first thing is to create a bot before we start coding it. Here's how to do it -

1. Head to the Bot Management Section in the My Account Dropdown.

    ![2021-04-03 20_17_53-Help - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r8x9g3sof9hdc4998ub.png) 

2. Now create a new bot from the top right corner

    ![2021-04-03 20_41_55-Bot Management - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ps2asfplz6h2gub2vk9.png) 

3. Give your Bot a name and click on OK

    ![2021-04-03 20_42_57-Bot Management - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58s0zk7bl9zxc0ysfqqn.png)
 

## Getting your Azure Computer Vision API Key

Although there are many games available for you to play on the AI Gaming Platform, for the sake of this tutorial we are going to play the good old Match Game. Now in Match Game, you have match titles with similar content. The catch is, to match the content, you have to turn the tiles to see what content it has and remember it, then you have to locate a tile with similar content to lock them and score the point. The bot with higher points and minimum turns will win the game.

To increase our odds of winning, we can make use of Azure's Computer Vision API Key to give our bot the power of AI.

**To create an Azure Computer Vision API Key, follow these steps:**

1. Create an account on [Azure](https://portal.azure.com/#home). If you are a student, then you can [get the free student credits](https://azure.microsoft.com/en-us/free/students/) to get started.
2. Once done, head on to the [Azure Portal](https://portal.azure.com/#home).
3. Now click on **Create a Resource**

    ![2021-04-03 21_09_32-Home - Microsoft Azure - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dpupfijof54qfmnjg9dl.png) 

4. Now from the left-hand category list, select **AI + Machine Learning category**

    ![2021-04-03 21_10_48-New - Microsoft Azure - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2z0irjfpwn2frrurh7ox.png)
 
5. Now from the right-hand list, click on **Computer Vision**

    ![2021-04-03 21_12_35-New - Microsoft Azure - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ed7spra428r7oax78rv2.png)
 

6. Now here are a few things you need to fill in to create the API

    ![2021-04-03 21_14_38-Create Computer Vision - Microsoft Azure - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kiwglc4bm8erwzl8vm1.png)
 

    - Subscription: Select the subscription you would like to continue with. Most probably it will be your student subscription.
    - Resource Group: Resource Groups are nothing but just a way to group certain resources to help you become organized. You can choose any of your previous resource groups or if you don't have any yet, you can create a new one easily.
    - Region: Region is where your resource in the world will be located. You should make sure that your resource region is close to your current location so that latency can be minimized. If you're in India, then you are highly recommended to go for the West Europe region.
    - Name: You have to give some name to your resource.
    - Pricing tier: Here you have to select what tier you prefer. If you have the subscription then go for, Standard S1 tier since you can make more calls per minute with this tier, hence making it an ideal option for the AI Gaming tournaments.

    Once done, click on **Review + Create**

7. Once your resource is successfully deployed, you will see this message. Simply click on **Go to Resource**

    ![2021-04-03 21_43_14-Microsoft.CognitiveServicesComputerVision-20210403211402 - Microsoft Azure - Bra](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ct4fgizutg16pxnyq0zq.png)
 

8. Now under the Resource Management in the left-hand sidebar, go to **Keys and Endpoint**

    ![2021-04-03 21_50_02-AIGaming-tutorial - Microsoft Azure - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0a72ak3q7crnc9cmzv8.png)
 

9. Now you will see the two API Keys that have been generated for you, copy any one of these & finally let's get to coding.

    ![2021-04-03 21_51_38-AIGaming-tutorial - Microsoft Azure - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hq8la9kgyt77huogdq3.png)
 
    ## Writing some code

    Now since your API keys are ready, head back to [AI Gaming Platform](https://www.aigaming.com/) & jump to the editor.

    1. Click on the New & select **Microsoft API Template.py**

        ![2021-04-03 22_00_35-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghgi4o4jgchejvwva65s.png)
 

    2. Give the file a name & click on **YES**

        ![2021-04-03 22_02_08-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qld7pny7wtmt31zih45r.png) 

    3. Now you will see a lot of readymade code over here. This will be helpful since we don't have to type everything manually.

        ![2021-04-03 22_03_42-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8ysx5l4994o1zjv0hur.png) 

    4. Online 7, make sure the **`botName`** variable is set to the Bot you created. In my case it was **`botName='mindninjaX-defbot'`.**

        ![2021-04-03 22_06_22-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhdjlv05w064ab4yukpm.png)
 

    5. One last thing, on line 16, we have to add our API key, which we generated in the last step.

        ![2021-04-03 22_10_00-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zotf3op6310lvje52da2.png)
 
        ```python
        headers_vision = {'Ocp-Apim-Subscription-Key': 'YOUR-WESTEUROPE-MICROSOFT-COMPUTER-VISION-API-KEY', 'Content-Type': 'application/octet-stream'}
        ```

        Here we simply have to replace **`YOUR-WESTEUROPE-MICROSOFT-COMPUTER-VISION-API-KEY`** with our API Key.

        For example if your API Key is **`2f6c3c7d02db4490a6ac00a10a7af82e`**, then the line 16 will look like this:

        ```python
        headers_vision = {'Ocp-Apim-Subscription-Key': '2f6c3c7d02db4490a6ac00a10a7af82e', 'Content-Type': 'application/octet-stream'}
        ```

    6. Save your code.

        ![2021-04-03 22_18_40-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufa4usx9jvyt4fbxpbbe.png)
 

    7. Now make sure that you have selected the **Match Game** in the Game Type dropdown since we are going to play Match Game. And also make sure that you have selected the **housebot-practise** bot in the Opponents dropdown.

        ![2021-04-03 22_19_12-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d39gycze7vy7ra2ld54k.png)
 

    8. Now finally, let's run our code!

        ![2021-04-03 22_21_15-Online Code Editor - aigaming.com - Brave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xwhqeix8cj7p7y7j0as.png)
 

    Now you will see the game has started.

    

    And since we are using Azure Computer Vision API Key, our Bot easily won the game with a large margin in the score.

## That's it!

That was a lot! But honestly speaking we only scratched the surface of what's possible with Azure & AI Gaming. I highly encourage you to go ahead and play an AI Gaming Game & invite your friends to see who can score more points. With the power of Azure, the sky is the limit.

## Thank you!

Thank you so much for reading! I hope you found this beginner tutorial useful. 

Also if you have any questions or doubts feel free to contact me on [Twitter](https://twitter.com/mindninjaX), [LinkedIn](https://www.linkedin.com/in/mindninjax/) & [GitHub](https://github.com/mindninjaX). Or you can also post a comment/discussion & I will try my best to help you :D