# [Community Puzzle] Counting TicTacToe

Coding Games and Programming Challenges to Code Better

Created by @RezaSi,validated by @Wontonimo,@darkhorse64 and @trictrac.
If you have any issues, feel free to ping them.

There is one very important thing missing from the statement. During a game, each player plays two matches with reversed colors in order to avoid the first player advantage and keep the game balanced. The final score is the sum of the points scored by each player during the two matches.

Therefore, a bot should be able to restart with a fresh state. For instance, if the number of available moves is 100 (resp. 99), you have a new game and you are the first (resp. second) player. I found this was the easiest way for a proper restart detection.

Edit: the statement has been updated

6 Likes

When a player has a row of 6 in his color, how many scores should be counted?
2 or 4?
It should be stated in the rule of game.

Yes, 4 points (every combination of 3 tiles counts)
The instructions are quite unclear indeedâ€¦
Also, letâ€™s use this answer to clarify that the 100th move, if itâ€™s an opponent move, is not printed (you canâ€™t stdin it). The reasons for omitting all these details are very unclear to me.

2 Likes

Welp, the mirrored games were added later and everyone (including approvers :v) forgot about the statement.

Apparently, using specific strategy you can force a draw.

1 Like

yup, the gameâ€™s a draw. any even-sized board can be drawn trivially by either player. Either player can mirror the other playerâ€™s moves or make any other move if they canâ€™t. Changing the board size to 9x9 should fix this since thereâ€™s an extra spot in the middle. It gives an advantage to player 1 since they get an extra turn, but the advantage is lost in round 2 where the other player goes first, and whoever gets the most points in total across both rounds wins.

2 Likes

Approvers these daysâ€¦

2 Likes

can you 100% force it when you have to start?
I tried it out and I donâ€™t get it 100%. However when I am not allowed to play for draw, I most of the times win.
Still I only get placed in the middle of the ranking board (which makes sense, as I rarely win), so I donâ€™t find that strategy concerning

Most likely your implementation is incorrect. We can mathematically prove that the strategy described by @Waffle3z for O forces a draw. The argument roughly goes like this:

1. After O plays, the board is symmetric, save for an extra O.
2. Therefore, when the game is over, the board is perfectly symmetric.
3. Therefore, the number of 3-in-a-rows for both players are the same.
2 Likes

I have encountered a crash

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at com.codingame.game.Player.getAction(Player.java:16)
at com.codingame.game.Referee.gameTurn(Referee.java:174)
at com.codingame.gameengine.core.GameManager.start(GameManager.java:122)
at com.codingame.gameengine.core.RefereeMain.start(RefereeMain.java:69)
at com.codingame.gameengine.core.RefereeMain.main(RefereeMain.java:52)`

1 Like

I dont see the problem since your score wont be good : you gonna be stuck in the middle of your current league

Actually a very nice ideaâ€¦ I wrote a Monte Carlo Tree Search for it, but thatâ€™s where the problems seem to start. Each search has such an extreme variable length that it can be anything from 0ms to >100ms. Even reading the input sometimes takes ~50ms, which extremely reduces the search time, which itself also constantly takes far too long.

I made all sorts of adjustments and set a buffer time of 20ms, but no chance. Since I donâ€™t have this variable runtime locally myself, I simply canâ€™t understand it.

what a shame

50ms to read the input sounds like something is terribly wrong.
Also, are you allocating memory for new nodes every time you create children? Itâ€™s better to use a pool of pre-allocated nodes and maintain indices to those instead.

Are you starting your timer after reading the first input of each turn?

Iâ€™ve got the same error.
After looking at the referee, itâ€™s when you print only one number instead of the 2 coordinates.
@RezaSi the referee should be improved to handle this possibility more cleanly.

Unfortunately, the drawing programs donâ€™t get stuck in the middle. Because strong players have to always start from the bottom, they have to cross the drawing programs, and give them some points. The weak programs, however, will stay at the bottom and will never get to play against them.

After entering my program a few times, @MSmits has climbed from rank 12 to rank 5. And he will climb again when I enter again.

It would be nice if they could be forced to re-enter the arena from the bottom from time to time. The current pairing algorithm will allow them to climb the ratings forever as long as they do not enter again.

Iâ€™ll resubmit, no problem. Let me know when you are done with the multi. Then I will resubmit again.

By the way: It is not exactly a drawing program. Just mostly. What I do every turn is try to solve the game. If i solve it, I play the best move and if not, I mirror the opponent.

So itâ€™s a drawing program with somewhat of a brain

1 Like

Hi. Thanks for your reply. Please donâ€™t worry about resubmitting. It is not a big deal. And you will be protecting my first place from now on

1 Like