Problem 58. Tic Tac Toe FTW
Given a tic tac toe board:
- 1 represents X
- 0 represents empty.
- -1 represents O
It is X's move. If there is an immediate win possibility, choose a square for an immediate win. Otherwise return 0.
Return absolute index of the square of choice. If multiple square are valid, return them in order.
Example:
Input a = [ 1 0 1 -1 1 0 0 -1 -1] Output wins is [3 4]
Solution Stats
Problem Comments
-
15 Comments
Should example Output wins should be [3 4]?
I guess so.
The example should definitely be [3 4]. I can't understand why the no-wins output has to be 0 rather than [] - the empty matrix would be more consistent with the requirement to list all squares. I wonder also if the list is supposed to have unique entries? The test cases don't check this.
Test 2: b=7 ?
Sorry. OK. It is X's move
Bad problem. Example solution should be [3 4].
Test suite problem.
The first test calls my function twice, with different values for (a). To test this
y function starts and endes with diagnostics:
----
function wins = ticTacToe(a)
disp('a=')
disp( a )
...
wins = unique( wins)
end
------
The first test result is:
------
%%
a = [ 1 0 0
0 -1 0
-1 0 1];
b = [0];
out = ticTacToe(a);
assert(isequal(out(:), b(:)))
a=
1 0 1
0 -1 0
-1 -1 1
wins =
4 8
a=
1 0 0
0 -1 0
-1 0 1
wins =
Empty matrix: 0-by-1
Assertion failed.
-----
I keep getting an error while submitting my solution: "There was an issue submitting your solution. Remove inappropriate content. For guidelines, see About Cody." There is no inappropriate content in my solution.
Brendan, this may be the side effect of anti-spam filters. Try to change variable names, etc. Sometimes even adding a space here or there works.
Brendan, Jan is right. Dealing with spam is giving us a hard time. If we set the barrier too low, we get terrible inflows of spam. If we set it too high, we annoy good people like you. Sorry about the hassle. We are working on it now.
Please fix test case 1. It's somehow creating problems for the other test cases because 'a = [ ' doesn't start on a new line.
I agree with yurenchu. The test cases are broken at the moment and need to be fixed - otherwise correct solutions don't work.
, = ,
'qx' = 'qx'
@yurenchu - I fixed the test case 1 to start in a new line.
Thank you for fixing the test case!
Solution Comments
Show commentsProblem Recent Solvers1756
Suggested Problems
-
Calculate the Levenshtein distance between two strings
1420 Solvers
-
Determine Whether an array is empty
769 Solvers
-
Return a list sorted by number of consecutive occurrences
358 Solvers
-
723 Solvers
-
4096 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!