PowerPlay: Tic Tak Toe Precision Vs Language Context
Tags: powerfuldevs, chatgpt, powerplatform, gamedev
Published: 2023-07-31 06:34:33

**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,"")
```

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

```
Set(XIconBottomMiddlevar,"true")
```

**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))
```


Demo
![Tic Tac Toe - Precision Code](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExMTQ5a2RpZGoxdTMxcWUya3VrZGw1MTZ3N2VyOWlpczg1YXg5NXZuZSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/w8jGTlwviRCieEJtSq/giphy.gif)



## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/854182oa9vyadp4u1kiv.png)

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

```


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9ggbs354ekr0qdv5ugl.png)

![Tic Tac Toe -  LLM](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExc21zMGd1MG40cW04ZDU2anllcWcwdWc3aW95eGx2bGY1YzB2d2l3eSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/Qc7DTodi8VtJyaRJ6B/giphy.gif)

## 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](https://dev.to/balagmadhu/tic-tac-toe-html-scripting-4c5c)

**Inspiration**:
[Power Automate Documentation](https://dev.to/wyattdave/power-automate-documentation-by-gpt-3ba9) by @wyattdave