Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating Artificial Intelligence

Discussion in 'Scripting' started by jasoncrate, Jul 2, 2015.

  1. jasoncrate

    jasoncrate

    Joined:
    Jul 2, 2015
    Posts:
    2
    Good afternoon,

    I'll try to be as brief as possible. I know that one can create artificial intelligence in this manner : https://unity3d.com/learn/tutorials/projects/stealth/enemy-ai but I'm looking to approach it in a different manner.

    Let's say I have a Tic Tac Toe game that is playable by two people (the application controls who's turn it is). I'm looking for the ability to get the board state at any moment in time and have some logic make a decision based on the information at hand. I'm not looking to do this in real time when others are playing the game, just in the sense that I can simulate 1000 games very quickly in order to perform genetic algorithms.

    Is there any way I can take our application and check the board state outside of Unity?

    Thanks very much for your time!
    Jason
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I'm not really sure what you mean by "Is there any way I can take our application and check the board state outside of Unity?", but I can tell you how turn-based games are usually run. With games as simple as tic-tac-toe and reversi, developers usually come up with a function that "scores" the board, using an algorithm that tries to give each player a better score based on the current state of the game. AI will try several random moves (or every possible move in some cases) and see which move gives them the best score increase (or close to the best for easier AI). The best AI will actually simulate several turns ahead because sometimes you need to take a step back to take two steps forward.

    Careful how much you simulate per turn, simulating too many moves will make the computer's turn long (and will freeze the game for the duration unless you calculate on another thread). In some games, it is acceptable for the AI to take some time to "think".
     
  3. jasoncrate

    jasoncrate

    Joined:
    Jul 2, 2015
    Posts:
    2
    Absolutely, that's exactly what I'm looking for. How do I simulate several moves ahead inside (our outside) of Unity? I'm unfamiliar with how to take things that have listeners and triggers on them and move it a few steps ahead (without having to watch or process any animations). I was hoping to do this without having to run the Unity engine (so that I can make these calculations much quicker) but I think that may be out.

    I was hoping for the Unity application to be a black box solution where I could feed it a move and it would return to me the board state. Maybe I need to attack this in a different manner.

    Thanks for your reply!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Your board state should be stored and handled separately from your visualisation. (Think MVC). The actual board state for tic tac toe is pretty simple, its simply a series of 9 variables. Each variable has three states: empty, nought or cross. You could record this as 0, 1, -1.