![]() ![]() ![]() Print " %s | %s | %s" % (pc],pc],pc])Īs one commenter has pointed out, there is one more restriction. Assuming only one of them has a win, that's a legal, winning game.īelow is a proof of concept in Python, but first the output of time when run on the process sending output to /dev/null to show how fast it is: real 0m0.169s So I would simply use brute force and, for each position where the difference is zero or one between the counts, check the eight winning possibilities for both sides. But we can actually ignore that since there's no way to win two ways without a common cell without having already violated the "maximum difference of one" rule, since you need six cells for that, with the opponent only having three. There's actually another limitation in that it's impossible for one side to have won in two different ways without a common cell (again, they would have won in a previous move), meaning that: XXXĬan be. If both have three in a row, then one of them would have won in the previous move. In addition, it's impossible to have a state where both sides have three in a row, so they can be discounted as well. There are only 3 9, or 19,683 possible combinations of placing x, o or in the grid, and not all of those are valid.įirst, a valid game position is one where the difference between x and o counts is no more than one, since they have to alternate moves. ![]() This has win logic, loss logic, draw logic along with scores, reset and new game! package is one of those problems that's actually simple enough for brute force and, while you could use combinatorics, graph theory, or many other complex tools to solve it, I'd actually be impressed by applicants that recognise the fact there's an easier way (at least for this problem). Public boolean placeXorO(char player, int row, int col) ("Would you like to play against the computer?") Any help to fixing this problem is greatly appreciated! What I tried to put in is: win += (board + board + board) I have finished the code, but I am not sure how I would check to see if there is a winner in the diagonals. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |