# Tic Tac Toe

So I decided to challenge myself as a programmer. I am designing a code in RobotC the can play tic-tac-toe, and also uses whether or not it one with certain moves to improve itself, hopefully to the point where it learns the optimal play strategy. I have the gameplay down, and I think I have the learning down, but there is still one area I want to improve. I want it to be able to tell when it wins or loses. I was wondering if there was a code that could take a bunch of variable, and continually check if any combination of those variable equals a certain set of values
Something like:

Cause currently if not, I have to write out every single possible combination of move # and winning move value, which upon doing some quick rough calculations quickly shot into the billions. Which even if each combo only takes 5 lines, well my program is already 2000 lines, and the cortex can only hold 384 kb of program.

So if there is a way, please let me know. Other wise I will be wasting hours getting this done. And maybe asking vex for a special cortex with extra space for my insane projects.

-5689D Captain

So I don’t use robotC, but I used to program in Scratch. I don’t know if this is possible, but if you could somehow add each move value to a list, and then test if that list contains all three items, then that would work.
However, if you have your values stored in such a way that you know whether each square is X, O, or neutral, (which wouldn’t be that hard I don’t think) then you can test each combination of winning squares to see if they are all X’s or all O’s, or a combination.
These are the best ways I can think of and I hope they help!

There are only 8 ways to win in TicTacToe. I don’t think that checking all the possibilities after each move would be burdensome. I’m assuming there is an array that stores the states of the 9 cells.

You could have it just check the possibilities that go through with the last move. That would be more efficient, but more code.

I don’t know how your game logic works since its not posted, but you could try some sort of raycasting technique. You can also just iterate using nested for loops. Honestly though im with @sankeydd here that 8 logic checks is no strain whatsoever, even at like 1 MHz (which your cortex likely will not hit and you don’t need to check on every iteration of game loop also if you want). Raycasting is super overkill for tic tac toe LOL.

The best way i see you doing this is having a two dimensional array, which is basically an array of arrays where the number of arrays in the array is the number of columns and the length of the arrays in the array is the length of the rows.

This way, you’ll have a grid like
x x x
x x x
Then, you use a for loop to loop through the first array, and then use another for loop inside that for loop to loop through every “row” array.

When doing so, you can detect when there is a space and then see whether there are o’s or x’s. That way, you can decide what move to make (let’s say there is a risky square where the enemy player has put two x’s, put an o there.)

Dear Sir: would you mind sharing your entire code as we are a bit lost with the VEX RobotC programming environment. Thanks

Ur a bit late buddy…

Yes, I know… still, can you help?

I think the person who posted this (2 years ago) has probably already solved the problem or moved onto bigger things. If u want to make the program yourself, I recommend creating a new thread.

However, ur post seems like you want help coding in robots. In which case, there are tons of programs on github that you can learn from

Thanks for your reply - I have coded the algorithm with a recursive loop but am lost in RobotC as it seems rather limited in functionality and no one gives you the full syntax… but you are probably right the the person moved on… do you know maybe how to program this in RobotC (tic tac toe)…? Thanks…