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. Dismiss Notice

Additions to the Tic Tac Toe-tutorial

Discussion in 'Scripting' started by Flidrip, Nov 26, 2020.

  1. Flidrip

    Flidrip

    Joined:
    Nov 18, 2020
    Posts:
    2
    Hello friends!

    I've just done the Tic Tac Toe-tutorial (Creating a Tic-Tac-Toe Game Using Only UI Components - Unity Learn).
    Managed to simplify the code a little bit #feelsgoodman
    Disclosure: I am very much of a beginner, please bare with me!

    I would like to add some extras.

    1) - using the numpad in addition to clicking (7 would be top left game-tile).
    2) - the computer is not such a challenging opponent. What is the easiest way to introduce some AI?
    ideally, add different levels of difficulty?

    Kind of open-ended questions here, whatever anyone can contribute would be greatly appreciated.

    How would I begin to implement these two new functions?

    Here's the code:

    https://bit.ly/39ej353 - GameController.cs
    https://bit.ly/2JhA6bb - GridSpace.cs

    Thanks!

    -me
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Hey there!

    To make the whole thing work with "Numbers" is actually no problem,
    your Links doesn't really work so I went to the "Tutorial" and checked it out,

    you have a reference from all the "Grid - Buttons" you can also create an
    "InputHandler" and based on your Input you would call the Function you call on Button Click,

    In Case of making an "A.I", you can make something very random (not AI), in case you Click
    on any Field, you will also open for the "enemy" a random Field,

    if you don't want to do that, in case of making an "AI" you would need to create a State Machine
    to keep track of who's turn is it now, and also to implement some decision making for the AI,
    what would be the Best for the AI (Counter/Am I close to lose? / Am I the first one?) etc.
     
    Flidrip likes this.
  3. Flidrip

    Flidrip

    Joined:
    Nov 18, 2020
    Posts:
    2
    Terraya; thank you for your quick response!
    I'm trying to implement the InputHandler, but, well... I don't know how to.
    Would you give me an example how this code might look, and where to put it?

    I wasn't able to derive from the examples I found online as to where to put my code.

    I am to use an if statement as such?

    Code (CSharp):
    1.     public void HandleInput()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.[7]))
    4.         {
    5.  
    6.         }
    7.     }
    but im not sure where to put this if statement, and what to put in the brackets in order to link it to the buttons.

    Grateful for any help you or any other can offer! :)
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Hey,

    you could do this even with "UnityEvents" would make it nicer and more flexible in case you want any special occasions,

    you can start like you did yes, even if it is not "so clean" code, for the start its fine,
    you also need to say what should happen in case the btn gets pressed x)