Closed jfbogusz closed 7 years ago
What was the problem with the Scanner ?
When I used Scanner I received sometimes (1 on 6-10 games) following error: 22:54:49,996 ERROR [com.magusgeek.brutaltester.GameThread] Exception in game 1 java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) ~[?:1.8.0_121] at com.magusgeek.brutaltester.GameThread.run(GameThread.java:113) [cg-brutaltester-0.0.1-SNAPSHOT.jar:?]
When I changed to BufferedReader - I never received such error again (in more then 600 games). So I do not understund why it helps - but it solved my problam. You do not have such Exceptions?
I don't have the problem. Using a BufferedReader can cause some weird behavior when an AI crash. A scanner will throw a NoSuchElementException
when the process is dead (AI has crashsed). A BufferedReader will just wait indefinitely.
It's a little weird that you have this exceptions with a Scanner.
Maybe Scanner has timer for waiting and it is some kind of timeout? My AI is not very fast (around 40 ms from input to output) but do not crash. So I will check it once again today evening. :-)
Scanner has no timeout. If a scanner throw a NoSuchElementException
, it is because the stream is closed (mostly because the AI process crashed) or because the stream is not opened when the scanner try to read it. I think you are in the second case. Your AI is maybe too slow to start so the scanner is plugged on a non existent stream. It is a interesting case and it would be useful to fix this bug.
What language and commandline do you use to start your AI ?
My AI is written in C++. I use simple bash command on Ubuntu. Problem is not on begining (starting of the AI) - it is in midle of the game (for example after 10 - 20 turns). As I understund if it is problem with crash of my AI - BufferedReader will wait indefinitly - but it is not the case - BufferedReader reads as many lines as Referee need.
How do you print your output in your AI ? Do you flush ? Codingame use a bash script to run our codes to perform an autoflush. But in real life, you have to flush.
Maybe it is the problem. I do not flush. I will check it today evening. If so I will pull request with only changes in log. Thanks :-)
It looks that problem is connected rather with my AI. So I close the pull.
1) Changing Scanner to BufferedReader solved my problem with exeptions when waiting for player input. 2) Moving one line of logging to loop and adding logging player's stderr stream just after reading player's input give me log more readable and complete