Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVitaly Khe
7,160 PointsPls check out my solution
I did try the represented code and found that it doesn't work for diagonals. Then i decided to go through the idea that looks a bit more clear for me.
- For a dropped token(T) we use a function to find the down left(L) and down right(R) space, which we start to check the diagonal from:
s s s s T s L s R
- there are 2 cycles starting from L and R and rising up by diagonals
``javascript
function downLeftEdge(x, y, columns, rows) {
return ( x < rows - 1 - y ) ? [0, y + x] : [ x - (rows - y - 1) , rows - 1 ];
}
function downRightEdge(x, y, columns, rows) {
return (columns - x < rows - y) ? [ columns - 1, y + (columns - x - 1)] : [rows - y - 1 + x, rows - 1];
}
const rows = this.board.rows;
const columns = this.board.columns;
let x = downLeftEdge(targetSpace.x, targetSpace.y, columns, rows)[0];
let y = downLeftEdge(targetSpace.x, targetSpace.y, columns, rows)[1];
let counter = 1;
while( x < columns - 1 && y > 0 ){
x++;
y--;
(spaces[x][y].owner === owner) ? counter++ : counter = 0;
if( counter > 3) {
win = true;
}
}
x = downRightEdge(targetSpace.x, targetSpace.y, columns, rows)[0];
y = downRightEdge(targetSpace.x, targetSpace.y, columns, rows)[1];
counter = 1;
while( x > 0 && y > 0 ){
x--;
y--;
(spaces[x][y].owner === owner) ? counter++ : counter = 0;
if( counter > 3) {
win = true;
}
}
``
- Pls feel free to ask or say something. Maybe a bug found..))
3 Answers
Steven Parker
231,269 PointsThe solution given in the course should work for all diagonals, and the strategy it uses will detect wins in any position on boards of any size. Perhaps a typo was introduced when you transferred it into your project.
I don't understand what the code above does, partly because the "downXxxEdge" functions both return arrays, but then the "x" and "y" values that are assigned are handled as scalar values. I would expect this would generate "NaN" results.
But if I understand the basic strategy you were trying to implement, you wanted to check only diagonals that the target would be part of, and not just check for any win on the entire board like the course sample code does.
It's a good idea, and more efficient than the original. The implementation still needs some work, but it's a nice practice exercise. I hope you give it another shot.
Vitaly Khe
7,160 PointsThanks for you answer!
Seems, i have copy-pasted the code not really correctly. It really does work :)
And you are right, in my solution there is a check of diagonals for just dropped token(target space). About arrays and scalar values i did not understand, sorry.
Do you mean the better ways is using a function that returns and array to be pointed to a variable first ? ``javascript
let coordinates = downLeftEdge([args]); x = coordinates[0]; y = coordinates[1];
``
Steven Parker
231,269 PointsYou can disregard the array/scalar comment. I must have overlooked the index applied to the function on first glance.
But it still appears to count all tokens of the same type on the diagonal, but I think the game rules require 4 contiguous ones to qualify as a win.
Vitaly Khe
7,160 Pointstrue it seems that i minded a classic lines game instead))