DEV Community

Cover image for AI Gaming with Azure & Python
Rishabh Singh ⚡
Rishabh Singh ⚡

Posted on

AI Gaming with Azure & Python

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

2021-04-03 17_01_34-Home Page - - Brave

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 - - Brave

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 - - Brave

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 - - Brave

  • 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 - - Brave

  • 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 - - Brave

  • 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 - - Brave

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 - - Brave

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

    2021-04-03 20_41_55-Bot Management - - Brave

  3. Give your Bot a name and click on OK

    2021-04-03 20_42_57-Bot Management - - Brave

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. If you are a student, then you can get the free student credits to get started.
  2. Once done, head on to the Azure Portal.
  3. Now click on Create a Resource

    2021-04-03 21_09_32-Home - Microsoft Azure - Brave

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

    2021-04-03 21_10_48-New - Microsoft Azure - Brave

  5. Now from the right-hand list, click on Computer Vision

    2021-04-03 21_12_35-New - Microsoft Azure - Brave

  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

- 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**
Enter fullscreen mode Exit fullscreen mode
  1. 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

  2. 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

  3. 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

    Writing some code

    Now since your API keys are ready, head back to AI Gaming Platform & jump to the editor.

    1. Click on the New & select Microsoft API

      2021-04-03 22_00_35-Online Code Editor - - Brave

    2. Give the file a name & click on YES

      2021-04-03 22_02_08-Online Code Editor - - Brave

    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 - - Brave

    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 - - Brave

    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 - - Brave

      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:

      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 - - Brave

    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 - - Brave

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

      2021-04-03 22_21_15-Online Code Editor - - Brave

    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, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Top comments (1)

nemanajay profile image
Ajay Neman

Detailed and neat article.
Thanks for this 🔥 🔥