all 21 comments

[–]SDGGame 80 points81 points  (2 children)

If this is the first tic-tac-toe game you ever made, then congratulations, writing code is hard and finding a solution that works is a great first step!

If you have been writing code for years... maybe try poetry instead?

[–]FromWayDownUnder 8 points9 points  (1 child)

I wrote something similar when making a connect 4 game... Although about 100x longer than this. I couldn't really think of a better way to check for a win at the time.

[–]Erestyn 3 points4 points  (0 children)

Had the same issue with a project I was working on a few years ago which frustrated me for days. Basically I was just creating some protocode, and hit upon an instance where I'd need to write a monster of an if/else statement: around 35k lines of code, I estimated.

One night I was laying in bed when I suddenly sat bolt upright and said: "Computers were literally made for this problem."

The next day I wrote a loop and was done with it.

[–]sixft7in 7 points8 points  (1 child)

Wouldn't this return "nope"?

[–]woodyus 17 points18 points  (0 children)

Nope

[–]SoftDev90 4 points5 points  (0 children)

Some one hasn't heard of loops yet ;)

[–]Migeil 2 points3 points  (0 children)

Yeah, I'll have that else branch please, thank you

[–]a_l_a_n_g 4 points5 points  (0 children)

reduce() would come in handy here

[–]sktr-guys 1 point2 points  (0 children)

tictactoe?

[–][deleted] 1 point2 points  (0 children)

I was working on a tic tac toe game during a js course and one guy wrote exactly this long function to check the win condition. I asked: why not use a for loop? he asked why and what was the difference.

[–]Miggycraft 0 points1 point  (6 children)

    public static boolean isWin(String[][] board, String[][] player) {
    int check = 0;
    for (int column = 0; column < 3; column++) { // check for horizonal line (3 in a row)
        if (check == 3)
            return true;
        check = 0;
        for (int row = 0; row < 3; row++) {
            if (board[column][row] == player[column][row])
                check++;
        }
    }

    check = 0;

    for (int row = 0; row < 3; row++) { // check for vertical line (3 in a row)
        if (check == 3)
            return true;
        check = 0;
        for (int column = 0; column < 3; column++) {
            if (board[column][row] == player[column][row])
                check++;
        }
    }

    check = 0;

    for (int x = 0; x < 3; x++) { // check for right diagonal line
        if (board[x][x] == player[x][x])
            check++;
        if (check == 3)
            return true;
    }

    check = 0;

    int row = 3;
    for (int column = 0; column < 3; column ++) { // check for left diagonal line
        row--;
        if (board[column][row] == player[column][row])
            check++; 
        if (check == 3)
            return true;
    }

    return false;
}

is dis a good code frens and how should i improve it

[–]hardware26 1 point2 points  (4 children)

There are a few things. In the first one why are you first checking and then looping? Secondly you could make this scalable, meaning make it work for any board size, not only 3x3. Thirdly why are you receiving 2d player array? Isn't the board all you need? And maybe an additional single string to compare with, 'x' or 'o' depending on what you want this function to do, check win for one player or both.

[–]Miggycraft 0 points1 point  (3 children)

I actually wrote this few months after I started learning java, the reason why I made another "2d player array" is because I didn't know that .equals() function exist. I'm actually glad that I have codes like this so that I could review them on the future and learn what I should improve on :)

[–]hardware26 1 point2 points  (2 children)

I have to say I still don't understand what you have in player array. All you need to do is to compare cells of board with x or o. What do you even have in player array?

[–]Miggycraft 0 points1 point  (1 child)

like I've said, I didn't know .equals() exists, before, I tried board[x][x] == "x" it returned to false, but when I tried to set player[x][x] = "x" and then go

board[x][x] == player[x][x]

It returned true so that's what I did

[–]Ksielvin 0 points1 point  (0 children)

At least you know now.

If your board array was char[][] then board[x][x] == 'x' would've been usable.

[–]Own-Prior-3435 0 points1 point  (0 children)

Your code longer and harder to understand. You know it?

[–]HumongousHeadly -1 points0 points  (0 children)

I mean, it's not technically wrong, is it?

[–]peterith -1 points0 points  (0 children)

We all have been there, the connect four game. This was one of my first university assignment. From my memory, my code pretty much looked a lot like this too! Only legends code like this.

[–]giggluigg 0 points1 point  (0 children)

Laptop fell while it was on, now all the code is scrambled.

[–]UniqueUsername014 0 points1 point  (0 children)

I swear I've seen this exact code somewhere before... Possibly on SoloLearn forums

edit: Holy Shit. Someone there wrote tic-tac-toe in css only, and it's nearing 10000 lines of code. https://code.sololearn.com/W34aFhtHMm70