5 kyu
Tic-Tac-Toe Checker
11,294 of 26,298eugene-bulkin
Loading description...
Arrays
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This kata would've been a lot better, and a bit tougher, if draws also counted games that had empty spaces but were unwinnable.
not as simple as i thought
[[1,1,1],[0,2,2],[0,0,0]]: expected -1 to equal 1.
The test expects -1, although the correct answer is 1.
It's
expected your answer to equal expected answer
, so the expected value in the error message is 1 not -1. -1 is the value your function returned. Not a kata issue.Thank you for your answer.
Yes, it was my fault.
I need to be attentive to details.
would it not also be a cat's game(no win con) before all tiles has been filled?
eg: [[1, 2, 2], [2, 1, 1], [0, 0, 2]]
Nice kata!
Haskell translation
approved
This comment has been hidden.
ok i fixed it, somehow my for(let x in array) got "function" instead of array element on the last iteration, idk why, maybe it would help someone
That's how
for...in
works, and this is the reason why there isfor...of
.thanks, i will look into it
Failures in the attempt here complete with correct results in vs code.
The odds are extremely high that your
vs code
has incorrect testing.Can you give an example?
For the following board: [[2,2,2],[0,1,1],[1,0,0]] - Expected: 2, instead got: 0
On my local machine, when I enter the given array
isSolved([[2,2,2],[0,1,1],[1,0,0]]);
I receive a 2 locally, but on the site, it returns a 0. The console log is also wrong online, but correct on my machine.
I've checked every failed test, with the same results.
This comment has been hidden.
For the test example you provided, your code actually returns
-1
.Furthermore, the notion that
Console.log()
is not functioning properly on Codewars is also extremely unlikely.The console log agrees with the result in both cases (on codewars and on my local computer). Where are you getting the -1 return? Have you tried running it in vs code? I don't see why they would not agree.
I ran your code on Codewars.
You can do exactly what I did, which was add the test you provided above to the
Sample Tests
. When you run the code, you will see-1
for that test.If codewars is the problem, how are you going to duplicate finding the correct answer by using codewars?
The code runs fine in vs code. The test arrays I've been using have come directly from codewars' attempt to run the code. Each time, the test produces correct answers locally, in vs code, and incorrect answers on codewars. It's weird, but I have not been changing the code; I've been copying and pasting completely. So either something is working that shouldn't be in vs code, or something isn't working that should be on codewars. Or I'm missing something, completely.
I don't see how adding a new sample test would be the solution, if I understand what you mean. It's possible I don't.
Your code uses global vars and they keep their value between tests, that's a problam with your code, not with Codewars. Read the docs: https://docs.codewars.com/training/troubleshooting#your-solution-seems-to-work-or-is-close-to-working-but-still-fails-tests
In this case, you keep adding elements to the array and your code only checks the first 3. So, if the first test is giving you a 0, all the other tests will give you the same result.
You know, I hate to say it, but after trying to paste in the tests into the sample test section, seeing them work, I thought it might be something like that.
Thanks for your help, guys. I'll take another look at the docs!
In my opinion this cata not contain enough tests (I go it on Python), it's too easy to hardcode and pass it. For example there have place lack of tests for 'O', i make very simple one check for 'O' in row, and successful pass it. It's not check on columns or diagonal for 'O', or diagonal from right to left for 'X'
There are no tests in Python for antidiagonal(counterdiagonal) - board[0][2] - board[1][1] - board[2][0]
Finally, first cata, who give me real good brainstorm
this is such a whatever challenge lmao
There is no vertical check for X or O, but it was a good challenge.
python new test framework is required. updated in this fork
Merged
Sould be 6 or 7 kyu to my mind :)
Ranks of approved katas cannot be changed.
This comment has been hidden.
The code above is invalid. Please use markdown formatting when asking questions related to coding.
Also, OP solved it, closing...
This comment has been hidden.
I like this problem
Don't undestand how i can get error "Failed asserting that 0 is identical to -1." if my code return -1 ONLY if there is no win AND there is zeroes(at least one) in array. If array has zero how correct answer can be 0?
This is not the proper way to raise an issue. You must give clear elements and prove there is an issue, not make free assumptions about your code, that may be buggy. Please follow carefully this or your issue may be closed for being considered inconsistent: https://docs.codewars.com/training/troubleshooting/
Ok, got it
something is not working in the COBOL kata. All my results are correct but it says no
I've gotten it to work by removing the vertical possibilities, but that's not the right thing to do because the solution should work vertically as well. Check the code please
Please give clear examples of failed tests to help fixing the issue.
Should be fixed in the current version, please check it.
Yes. now, It works perfectly. Thaaaaaanks
Why flat is not a function? This method i use for array
Why are you asking the same question as the comment just below yours instead of simply reading the answer?
TypeError: board.flat is not a function. What? Array.prototype.flat() The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Array.prototype.flat
requires Node 11, which this kata doesn't support. Not a kata issue.This comment has been hidden.
This comment has been hidden.
For the following board: [['[0,1,1]', '[2,0,2]', '[2,1,0]']]: 0 should equal -1
why is this in string not in integer. All of the test is pass but not this one because it's a string
Yeah, i'm also confused about that, should at least be mentioned in the task.
there is no tests for first line is {0,0,0}
Why is that an issue? Not all possible boards are checked.
Not an issue apparently.
C translation (author gone)
Approved
This comment has been hidden.
This kata only supports Node v8.
Array.prototype.flat
was added in Node v11. Not really a kata issue.Okay, thank you, for explanation
JavaScript: when I run the test cases [[2,1,2],[2,1,1],[1,2,1]] and [[1,2,1],[1,1,2],[2,1,2]] on my device it runs just fine andreturns the expected 0 but onthe Kata it returns -1. Anyone know how to fix that or what the problem might be?
That's a problem with your code, not a kata issue, print
count_val
and debug your code.yes buddy rightly said it should be 0 as there is no empty cell and thus it is an draw
This comment has been hidden.
Solidity: constantly getting erorr Expected: -1, instead got: 0, even if all cases with same error from comments works fine with my code
I think the test case here is the line of
0
's; it should not return anything, for this is just an empty line.pretty fun, would like to revisit to see if i can do it better
D translation
I guess undefined beheviour in random tests should be tested. For instance if board [[2 1 2] [2 1 1] [2 1 1]] is generated, where both "X" and "O" won should not exist. Or at least this should be some other case and function should be expected to return 3. Also task explicitly states that input board would be valid tick-tack-toe board. P.S. I am talking about task in Golang, tests for other languages may be written correctly
Do you mean that board is generated in random tests? In which language? If not, then
Is ok.
Random portion of golang tests
Thanks for reporting it. It should be fixed now.
nice kata! imagine the horror if we would have to validate the board too haha
Go translation https://www.codewars.com/kumite/6254e53c4cad28003669cc12?sel=6254e53c4cad28003669cc12
Approved
Could you check if random tests produce invalid boards?
Yes I'll take a look, thanks
edit: It was producing boards where both X and O could win, I've updated the random board generator to generate another new board when this occurs, it just needs to be approved again
Update approved.
This was fun! I think there's a missing test case to look for a winning column though, i.e. [[1,2,0],[1,0,2],[1,2,0]]. I completed the task and only noticed when reviewing my solution.
Is there a problem with this test case? For the following board: [[0,0,2],[0,0,0],[1,0,1]] - Expected: -1, instead got: 0 I change the output to an array to see what was going on, but the result still output 0, even though other cases output the array properly?
Why would there be a problem? The correct answer (expected) is obviously -1, but your code returns 0.
Because when I changed my return to return my array, it still said "instead got: 0"? My array should have showed up instead?
note you have nultiple
return
statementsThis is kata is crashed on php. board: [ [1,2,1], [1,1,2], [2,1,2] ] my function return 0; test required -1;
Test messsage: Failed asserting that 1 matches expected -1.
According to the tests I see here this case expects 0. But
Failed asserting that 1 matches expected -1.
suggests your code is failing in another testcase.Indeed.
Why not just show all tests?)
What do you mean?
When I complete the task, I see only one test case. When I click the "Attempt" button, the solution runs through tests that I can't see.
Have you tried to print the output? I'm not familiar with PHP's tests framework, but from what I see tests logs don't give ever the input, and it seems tests in each series (fixed and random) stop as soon as one test fails.
COBOL translation (author is inactive).
Approved
Ruby: random tests generate incorrect boards, for example:
[[0, 2, 2], [1, 1, 0], [0, 2, 2]]
.fixed, the boards can no longer have two winners or players skipping their turns
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
The flat() method is not working!
Because it depends on Node's version!
This comment has been hidden.
nice kata!
Hello all, when I test my code throught testRandomized I got errors
for example
or next situation
Please have a look at this TL;DR : The likeliest problem is that you're misinterpreting the tests output : the logs are ABOVE the assertion message, NOT below it. If you need more help, please tag it as
question
and specify your programming language ;-)According to the random tests, this should have given a -1
[[2,1,2], [2,1,1], [1,2,1]]
I am extremely confused by this. The above board is finished (no blank spaces) and nobody has won. Why wouldn't 0 be the correct response?
Javascript test cases need's to check for column winners, my solution passes withouth check it , despites than that, great kata!
Same as python
This comment has been hidden.
The kata says:
so no issue!.
There is still an issue. Random tests must be created as well.
there are other issues about lack of random tests
done for Ruby, Python done here
i love how many different solutions are there!
This comment has been hidden.
This comment has been hidden.
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
With more than 3k solutions in Python, it's highly probable your code is wrong. So, post your code following the directions in the link so we can check it.
Looks like u alr solved it so closing~~
The log is called after the user's function in javascript tests so when they mutated it, the error message seems wrong. Move the user's function call to the bottom.
fixed
javascript: Problem with test. Result should be -1, not 1.
Are you mutating the input (there is no test with that input in javascript)? The user's function is called before the log.
Indeed, my mistake. I used sort method. Thanks for fast reply.
Hi, I'm in a bind and I need to see the code.
Can someone share it?
thanks
You could click
UNLOCK SOLUTIONS
and forfeit the points if you want to give up, although I've never done that. I either just taken a break on the kata and come back to it later, when I'm better at programming, lol, or I do a lot of research and crank out a solution. if you have a specific question, you could post that as aquestion
and if you post any code, use proper code markdown, and don't forget to use the spoiler tag.other than that, if someone "shares" code with you so you can pass a kata, that would just be cheating, and you could be banned
Fun to revisit something I've done before, but the ambiguity of the -1 condition description soured it for me.
In case this helps: You are being asked to comment on the snapshot of the game you are given. It could be finished, it could be unfinished. Your return is just commenting on the current state available.
This comment has been hidden.
Why do I so rarely see a neatly organized object-oriented solution? Always procedural, procedural, procedural, and never a custom data structure in the mix. Makes me sad.
What custom data structure would you add?
fair enough, but for a problem like that it feels like OOP just adds overhead for no practical gain, since the kata just asks for a return value, not a modelization im not a good programmer but that's what it feelks like for me your solution looks cool tho i'll admit
Problem statement is self-contradictory.
It says that -1 should be returned if there are empty cells, but it also says that 1 or 2 should be returned if there's a winner.
It is possible to win before running out of cells.
Fixed.
What is the expected output if
board
is[[1,2,1], [1,2,2], [1,2,2]]
? The description is not clear!That's not a legit input, so you don't need to solve for it. Tic-tac-toe can't have a row (or in your case column) of 3 Xs and 3 Os since players alternate adding values. So you'd never get your board state.
This comment has been hidden.
Hi! One important test case is missing from the test suite: the one, where the winning combination on the diagonal runnung from the top right to the bottom left corner. I missed this check in my code initially and tests passed anyway. Example board:
func should return 0 if there is zero value anywhere in the array, am i right?
No, read the instructions again:
A game can be solved without filling all the board with
X
andO
.I know that example is a little braindead for player 2 but it's a valid example ;)
sorry, i meant should return -1 if anywhere 0 finded...
No, for the same reason, you have to check if there is no winner yet too.
I enjoyed this kata, just a minor edge case issue:
C# could do with an extra test - my first solution passes when it shouldn't, for example it would return this board as a win for player 1:
int[,] board = new int[,] { { 0, 0, 1 }, { 0, 0, 0 }, { 1, 0, 0 } };
(ie I forgot to check the centre square when checking for a bottom-left to top-right diagonal)
ruby: the random generator needs some constraints:
And I don't get this type of case (last fixed test and happens in the random ones too):
resolved.
No, still generates stuff like:
[[0, 2, 2], [1, 1, 0], [0, 2, 2]]
, which is impossible.This comment has been hidden.
Player 2 has won the game because they got three in a row on the on the bottom row. As soon as someone gets three, the game is over and that player wins. Player 1 doesn't get another turn.
Test cases dont match the description of the Kata. It shall be assumed that any 0 on the board will mean that the game is unsolved, so returning -1.
I fail 2 further tests all including remaining 0`s, the others all pass. Whats the matter here?
The game can be won/solved whilst still having zeros present on the board.
Well the katas description clearly states to return -1 "if the board is not yet finished (there are empty spots)", hence the confusion.
The problem is that the game can be won while there are still empty spaces. If the game isn't won, and there are empty spaces, then return -1. Otherwise, return the winner.
the question remains : which 3 result do we start first?
example: in array [[1, 1, 1], [1, 0, 2], [2, 0, 2]]
if
rows
are considered first,returns 1
elsereturns -1
in array: [[1, 0, 1], [1, 2, 2], [1, 0, 2]]
if
columns
are considered first,returns 1
elsereturns -1
going on, pattern can be alternating
row
andcolumn
ordiagonal
etc... So the constraints mentioned in the kata is either incomplete or wrong test case is used.Suggested correction:
-1 if the board is not yet finished (there are empty spots)
Add phrase (or something that suggests):
if
none have won and
the board ...Solidity Translation Kumited - please carefully review and approve :D
PHP Translation Kumited - please carefully review and approve :D
This comment has been hidden.
Hi,
You're new at cw... ;) We have a problem here, with old katas: when they reach the 500 completions, tests are locked. So... For this type of kata, it isn't anymore doable to add new tests (and so, it isn't possible either to correct wrong tests when there are... You'll discover that soon enough... ;-/ )
happy coding anyway! ;)
Oh! thank you
Can we use NumPy or pandas libraires for this? List of lists are very annoying to handle in Python, so it would help a lot.
I'm not sure if i'ts an anti-pattern or not, but I often flatten the list into one dimension and instead calculate a row length. In this case since the list lengths are constant, it is very easy to work with
Would be good to mention in the description that input list may go in the form of list of strings, like this:
[['[0,0,2]', '[0,0,0]', '[1,0,1]']]
exactly, but what do if input is inn form of list of string, is slicing is only option we have?
I loved the exercise :)! Keep up the good work.
I've noticed a potential border case alluded to but not responded to by other commenters. Consider the cases a = [[1, 2, 1], [1, 2, 2], [2, 1, 0]] b = [[2, 1, 1], [1, 2, 2], [2, 1, 0]] My code returns isSolved(a) = 0, isSolved(b) = -1 Other successful code returns isSolved(a) = -1, isSolved(b) = -1
The distinction is whether you can call a cat's game before the last square is occupied if those moves wouldn't matter anyway. Perhaps this is a matter of opinion, but that should be clarified in the instructions and test cases. As is, code producing differing results is accepted.
Honestly, I don't feel that my code does all I wish it to. In some cases(i think, havent fully investigated), the answer depends on whose turn it is, but since this information isn't included in the game state, I have had to pretend that either player might go next.
Anyway, I had fun, thanks.
It seems that I've caught the similar error as you.
https://pp.vk.me/c633929/v633929334/118c2/x6M87yS7dN0.jpg
Isn't it?
@goodwin64
This is not what Just David is referring to. The boardstate in the screenshot you are linking to clearly hasx
as a winner (1s on the diagonal).@Just David
an example of a cat's game according to the definition is a draw - so your code should be correct in the case that the moves do not matter for the outcome (which in your case would be a draw, or cat's game, hence 0)Your answer shouldn't depend on whose turn it is, the description says you have to determine whether the
current state
is a final one (x wins, o wins or it's a draw) or not (undeterminable, you do not know who's move it is)This comment has been hidden.
This comment has been hidden.
Well, we don't know either when you don't post your code or even tell us what language you're having this issue with.
Help us help you!
Closing.
I think it would be best to clarify if an unwinnable state counts as a draw.
alr added
It seems to have a wrong test case: For the following board: [['[2,1,2]', '[2,1,1]', '[1,2,1]']]: 0 should equal -1. How that board could be 'not solved'? (maybe I just don't know tic-tac-toe rules)
Ur example is a draw, -1 is some
0
left on the board.Yeah. "For the following board: [['[2,1,2]', '[2,1,1]', '[1,2,1]']]: 0 should equal -1" was the response the tester was giving me. My solution for that board was 0, but the 'correct' answer for the test suite was -1. It seems to be fixed now, tho
For the following board: '[[1,2,1],[1,1,2],[2,1,2]]' (and mirror like [[2,1,2],[2,1,1],[1,2,1]]) - Expected: 0, instead got: -1 >>> but test returns 0.... Issue is not solved (JavaScript+Node v6.11.0)
At least in Python, it seems that you're missing test cases where there is a winning column. My previous submission was wrong and yet accepted. I think there should be winning triples (both of 1 and 2) in all six possible places.
This comment has been hidden.
The it is a -1 cause it is not solved yet, your method should only evaluate a random board. It does not plays the tic tac toe, nor predict a winner nor a next move, only evaluate the current state.
I wrote some test-cases for my code, perhaps they're useful?
Let me know if this should be a spoiler. I could make them more concise, but meh.
Link: http://pastebin.com/gRxefvTk
Example:
This comment has been hidden.
It's certainly more intricate than it needs to be ;)
This comment has been hidden.
The supplied test case currently has a // comment, which is valid Javascript but invalid Python. Should be a # comment instead. Trivial to change, but a Python newbie might not know it.
Fixed, thanks!
This comment has been hidden.
I'll add it to the test cases, thanks!
This comment has been hidden.
That board is unsolved because it isn't full. Once X goes again then it's a draw. It's all semantics, though.
And uhh I'd have to look at your code? for-in loops loop over keys, whereas the other one loops over numerical values, so I'd have to see the code to make a statement.
This was a fun kata!
I can't believe that no one tried to create a solution to solve any board size (considering the rows and columns in equal length). My solution is far from the best, but any board size is supported :).
In my opinion, supporting any size was a lot of fun to work out.
Yeah, I tried to make sure my solution supported any size. All I'd have to do is replace some of the 2s and 3s.
When kata forks are implemented I will try to extend this one to NxN but I have to watch out for timeouts. What I will probably do is write a Tic-Tac-Toe class since a board that preserves state allows for O(n) solve checking.
Unbelievable how many approaches there are on this relatively simple kata!
yes, because your solution looks totally obvious and simple!
Lol! I admit your's may seem much cleaner, but it has it's own intricacy :-)! (can't say more without spoiler) Sometimes I take the extra challenge to solve within a single return statement, but it makes code look a bit messy.
This comment has been hidden.
Oh, nevermind that my solution would fail with that — I did demarc between the rows and between the columns. But you should still add a test case for it, if you haven't already. :)
This comment has been hidden.
Yep — in theory, if someone just concatenates the whole board without thinking through delimiting rows or columns, that would come up as a solution.
Good call! I'll add some of those in!
The code that is automatically populated in the test fixture does not actually call the isSolved method.
should be
Thanks! Fixed!