PowerPlay: Tic Tak Toe Precision Vs Language Context

Bala Madhusoodhanan - Jul 31 '23 - - Dev Community

Intro:
As a curious enthusiast, wanted to explore the problem solving with both traditional algorithms and solve the same with LLM. While both are powerful tools for problem-solving, they operate on different principles and exhibit unique strengths. The problem scoped to explore was to build the timeless game of Tic Tac Toe.

Method 1: Traditional Rule based Algorithm

Initializing variables:
XVar's These variables represent the positions on the game board where "X" icons are placed. When a player selects a position, the corresponding variable will be set to "true" to indicate that an "X" has been placed there.

CircleVar's These variables represent the positions on the game board where "O" icons are placed. When a player selects a position, the corresponding variable will be set to "true" to indicate that an "O" has been placed there.

Winnervar This variable is used to keep track of whether there is a winner in the game. It is initially set to "false" and will be updated to "true" when a player wins the game.

WinnerPersonvar This variable is used to store the identity of the player who wins the game (either "X" or "O"). It is initialized as an empty string and will be updated when a player wins the game.

Set(XIconTopLeftvar,"false");Set(XIconTopMiddlevar,"false");Set(XIconTopRightvar,"false");Set(XIconMiddleLeftvar,"false");Set(XIconMiddleMiddlevar,"false");Set(XIconMiddleRightvar,"false");Set(XIconBottomLeftvar,"false");Set(XIconBottomMiddlevar,"false");Set(XIconBottomRightvar,"false");Set(TopLeftCircleVar,"false");Set(TopMiddleCircleVar,"false");Set(TopRightCircleVar,"false");Set(MiddleLeftCircleVar,"false");Set(MiddleMiddleCircleVar,"false");Set(MiddleRightCircleVar,"false");Set(BottomLeftCircleVar,"false");Set(BottomMiddleCircleVar,"false");Set(BottomRightCircleVar,"false");Set(Winnervar,"false");Set(WinnerPersonvar,"")
Enter fullscreen mode Exit fullscreen mode

Selection Action:
When ever a player selects a grid you set the variable

Set(XIconBottomMiddlevar,"true")
Enter fullscreen mode Exit fullscreen mode

Rule Engine:
The below code is responsible for handling player moves, checking for a winning condition, and updating the game board and scores accordingly. Clone the same condition for all the 9 grids

If(ButtonX=true,Set(XIconBottomRightvar,"true"),ButtonO=true,Set(BottomRightCircleVar,"true"));If(ButtonX=true,Set(ButtonO,true)&&Set(ButtonX,false),ButtonO=true,Set(ButtonX,true)&&Set(ButtonO,false));If(TopLeftCircleVar&&TopMiddleCircleVar&&TopRightCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),MiddleLeftCircleVar&&MiddleMiddleCircleVar&&MiddleRightCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),BottomLeftCircleVar&&BottomMiddleCircleVar&&BottomRightCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),TopLeftCircleVar&&MiddleLeftCircleVar&&BottomLeftCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),TopMiddleCircleVar&&MiddleMiddleCircleVar&&BottomMiddleCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),TopRightCircleVar&&MiddleRightCircleVar&&BottomRightCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),TopLeftCircleVar&&MiddleMiddleCircleVar&&BottomRightCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1),TopRightCircleVar&&MiddleMiddleCircleVar&&BottomLeftCircleVar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,OButton.Text)&&Set(OScorevar,OScorevar+1));If(XIconTopLeftvar&&XIconTopMiddlevar&&XIconTopRightvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconMiddleLeftvar&&XIconMiddleMiddlevar&&XIconMiddleRightvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconBottomLeftvar&&XIconBottomMiddlevar&&XIconBottomRightvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconTopLeftvar&&XIconMiddleLeftvar&&XIconBottomLeftvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconTopMiddlevar&&XIconMiddleMiddlevar&&XIconBottomMiddlevar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconTopRightvar&&XIconMiddleRightvar&&XIconBottomRightvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconTopLeftvar&&XIconMiddleMiddlevar&&XIconBottomRightvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1),XIconTopRightvar&&XIconMiddleMiddlevar&&XIconBottomLeftvar="true",Set(Winnervar,"true")&&Set(WinnerPersonvar,XButton.Text)&&Set(XScorevar,XScorevar+1))
Enter fullscreen mode Exit fullscreen mode

Demo
Tic Tac Toe - Precision Code

Method 2: Play with a LLM engine

The idea was to leverage Power Automate flow to be using various actions and operations to process JSON data and interact with AI Builder to perform predictions based on a Tic Tac Toe board. The logic is as below:

Power Automate Logic

The Prompt to check the status of the game board.

can you check if we have a winner with the below tic tac toe board. the letter b indicates that those cells are blank and available for move. 

@{body('Parse_JSON')[0]?['value']}|@{body('Parse_JSON')[3]?['value']}|@{body('Parse_JSON')[6]?['value']}
-|-|-
@{body('Parse_JSON')[1]?['value']}|@{body('Parse_JSON')[4]?['value']}|@{body('Parse_JSON')[7]?['value']}
-|-|-
@{body('Parse_JSON')[2]?['value']}|@{body('Parse_JSON')[5]?['value']}|@{body('Parse_JSON')[8]?['value']}

If the result is player has won 
Set stop

Enter fullscreen mode Exit fullscreen mode

While LLMs have made remarkable strides in language processing, they are not infallible and have limitations in logical thinking

Example where no logic was factored

Tic Tac Toe -  LLM

Summary:

Themes Method 1: Rules Engine Method 2: ChatGPT Engine
Functional Fitness Works always as and always consistent with outcome and correct outcome Inconsistent outcome. Got confused and replaced the previous moves as well
Implementation Complexity Needed writing the game logic, managing user inputs, and displaying the game board Simple Implementation as , you can generate responses to user inputs and create an interactive text-based Tic Tac
Performance Excellent Involves an API call for the logic so latency

Conclusion:
Power Automate with the AI builder is not designed for interactive games like Tic Tac Toe, and implementing the entire game logic may be quite complex. A better approach was for a rule based one.

PS - JavaScript would have been much more easier - HTML Version

Inspiration:
Power Automate Documentation by @wyattdave

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .