Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need to implement AI in TicTacToe

Need to implement AI in TicTacToe

Scheduled Pinned Locked Moved C#
game-devalgorithmsquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haroon1980
    wrote on last edited by
    #1

    Hi, I am developing TicTacToe in which i finished working functionlity where 2 people will play. Now i want to add AI to my game what can be the best algorithm to do that. One more thing any piece of algorithm or any starting code would be very nice. Now just a real question lets say Player selected one box. Now its Computer turn. It has 8 possible moves how would you implement that can you give any idea in algorithm shape or Pseudocode please. I would probably not write code to evaluate each state individually, I'd write a state evaluator that checked for winning moves, blocking moves, and then potential moves based on difficulty. :confused: thanks

    C A D J 4 Replies Last reply
    0
    • H haroon1980

      Hi, I am developing TicTacToe in which i finished working functionlity where 2 people will play. Now i want to add AI to my game what can be the best algorithm to do that. One more thing any piece of algorithm or any starting code would be very nice. Now just a real question lets say Player selected one box. Now its Computer turn. It has 8 possible moves how would you implement that can you give any idea in algorithm shape or Pseudocode please. I would probably not write code to evaluate each state individually, I'd write a state evaluator that checked for winning moves, blocking moves, and then potential moves based on difficulty. :confused: thanks

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      An obvious way is to write a system where it chooses moves randomly or semi randomly ( prefer the middle, then prefer the corners, etc, or whatever you think ) and remembers which moves lead to it winning. Obviously, some basic 'finish the row if I can win this turn' code also makes sense to have.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • H haroon1980

        Hi, I am developing TicTacToe in which i finished working functionlity where 2 people will play. Now i want to add AI to my game what can be the best algorithm to do that. One more thing any piece of algorithm or any starting code would be very nice. Now just a real question lets say Player selected one box. Now its Computer turn. It has 8 possible moves how would you implement that can you give any idea in algorithm shape or Pseudocode please. I would probably not write code to evaluate each state individually, I'd write a state evaluator that checked for winning moves, blocking moves, and then potential moves based on difficulty. :confused: thanks

        A Offline
        A Offline
        andre_swnpl
        wrote on last edited by
        #3

        I dont think you will need much in the line of AI. you could simply calculate all available moves and assign a weight to each move and choose the best one. You could even use some kind of probability to "see ahead" to try to guess what the opponents best moves may be given a certain move.

        1 Reply Last reply
        0
        • H haroon1980

          Hi, I am developing TicTacToe in which i finished working functionlity where 2 people will play. Now i want to add AI to my game what can be the best algorithm to do that. One more thing any piece of algorithm or any starting code would be very nice. Now just a real question lets say Player selected one box. Now its Computer turn. It has 8 possible moves how would you implement that can you give any idea in algorithm shape or Pseudocode please. I would probably not write code to evaluate each state individually, I'd write a state evaluator that checked for winning moves, blocking moves, and then potential moves based on difficulty. :confused: thanks

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          there're only 9! (363k) potential games. Brute force is a viable option.

          -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

          A 1 Reply Last reply
          0
          • D Dan Neely

            there're only 9! (363k) potential games. Brute force is a viable option.

            -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

            A Offline
            A Offline
            andre_swnpl
            wrote on last edited by
            #5

            Agreed! Not very elegant but certainly viable.

            1 Reply Last reply
            0
            • H haroon1980

              Hi, I am developing TicTacToe in which i finished working functionlity where 2 people will play. Now i want to add AI to my game what can be the best algorithm to do that. One more thing any piece of algorithm or any starting code would be very nice. Now just a real question lets say Player selected one box. Now its Computer turn. It has 8 possible moves how would you implement that can you give any idea in algorithm shape or Pseudocode please. I would probably not write code to evaluate each state individually, I'd write a state evaluator that checked for winning moves, blocking moves, and then potential moves based on difficulty. :confused: thanks

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              I got to the same point a couple of years ago. As already stated, brute force (check every move possible) is viable in TTT as the number of moves is quite small. You might also like to read up on AI algo's like AlphaBeta - there is LOADS of reading online about AI. My head got a bit swallowed up by it all and thats why I never got any further with my TTT game. But I did learn a hell of alot anyway.

              --- How to get answers to your questions[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups