Brayzure's recent activity

  1. Comment on Programming Mini-Challenge: TicTacToeBot in ~comp

    Brayzure
    Link
    Figured I'd post a solution in Javascript, was fun to see how I would approach it. I wanted to improve on the "check all eight lines and see if any match" solution, so I decided to modify it...

    Figured I'd post a solution in Javascript, was fun to see how I would approach it. I wanted to improve on the "check all eight lines and see if any match" solution, so I decided to modify it slightly. I reasoned that, if a winning row exists, it must pass through one of three spots. The center, or either of two opposite corners. The script checks those three spots and, if they have a mark in them, checks if a row exists that passes throw it. If a valid row exists, it's immediately returned.

    const valid = {
        // Each key represents a starting spot
        4: [ // Each item represents the remaining two spots in a row
            [0, 8],
            [1, 7],
            [2, 6],
            [3, 5]
        ],
        0: [
            [1, 2],
            [3, 6]
        ],
        8: [
            [2, 5],
            [6, 7]
        ]
    }
    
    function checkForWinner(board) {
        for(const testSpot of Object.keys(valid)) {
            // Continue check if test spot is not empty
            if(board[testSpot] !== "#") {
                const player = board[testSpot];
                // Check each row that passes through test spot
                for(const checkSpots of valid[testSpot]) {
                    if(board[checkSpots[0]] === player && board[checkSpots[1]] === player) {
                        return player;
                    }
                }
            }
        }
        // Alternatively, return "#" and forgo the check later on
        return false;
    }
    
    const boardString = process.argv[2];
    const board = boardString.split("");
    const winner = checkForWinner(board);
    console.log(winner ? winner : "#")
    

    The script can be run by calling
    node check "O#XXXOO##"

    It's not exactly short, but I'm happy with how I solved it.

    3 votes
  2. Comment on What upcoming games or updates are you excited about? in ~games

    Brayzure
    Link Parent
    I'm so excited for it. It's going to be a day 1 purchase from me. If it's at all as expansive as TW3, I'll be set for a long time.

    I'm so excited for it. It's going to be a day 1 purchase from me. If it's at all as expansive as TW3, I'll be set for a long time.

    6 votes