javascript - How can I implement tic tac toe win condition with my current setup? -


i working on tic tac toe multiplayer game built in node.js.

my main issue figuring out how check win condition. know how works using array want program game way....

var mongoose = require('mongoose');  var gameschema = new mongoose.schema({      player1: {         type: mongoose.schema.types.objectid,         ref: "user"     },      player2: {         type: mongoose.schema.types.objectid,         ref: "user"     },      // array of selected spaces 'x' or 'o'     placements: [{         type: mongoose.schema.types.objectid,         ref: "placement"     }],      // username of current player.     currentplayer: string });  module.export = mongoose.model('game', gameschema); 

placement schema:

var mongoose = require('mongoose');  var placementschema = new mongoose.schema({     marker: string, //x or o      selectedspace: number // 0-8 });  module.export = mongoose.model('placement', placementschema); 

i want use placements array of model objects....

what best way check win condition way?

or should rethink way model setup?

if understand setup correctly, whenever add placement, need check if there winner or not.

// req.params.game = id of board game // req.body = { marker: 'x' or 'o', selectedspace: 0 8 }  app.post('/game/:game/placement', (req, res, next) => {     // find board game     game.findbyid(req.params.game, (err, game) => {         if (err) return next(err);          // create placement (!)         placement.create(req.body, (err, placement) => {             if (err) return next(err);              // add placement game             game.placements.push(placement._id);              // might want swap game.currentplayer @ point too; i'll leave              game.save(err => {                 if (err) return next(err);                  // placements b/c we'll need check of them                 game.populate('placements').execpopulate().then(() => {                     let winner = game.getwinner();                       if (winner) {                         // send response declare winner , stop game                     } else {                         // send response nothing                     }                 });             });         });     }); }); 

in schema, have

// add logic check if there winner or not;  // winner has 0 1 2 (horizontal 1st row), 3 4 5 (horizontal 2nd row), 6 7 8 (horizontal 3rd row), // 0 3 6 (vertical 1st column), 1 4 7 (vertical 2nd column), 2 5 8 (vertical 3rd column), 0 4 8 (diagonal), 2 4 6 (diagonal);  // has not been tested should give pointers @ least; // there might simpler logic gameschema.methods.getwinner = function () {     let strikes = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]];     let dict = {         x: [],         o: []     };     // group placements marker (!)     let winningplacement = this.placements.find(placement => {         dict[placement.marker].push(placement.selectedspace);         // check if of strikes contained in marker's list of selected spaces         return strikes.some(strike => issuper(dict[placement.marker], strike));     });     // assuming player1 'x'     if (winningplacement) {         return winningplacement.marker == 'x' ? this.player1 : this.player2;     }     return false; }; 

outside scope of question, see answer issuper() logic.

imo though think should rethink design. example, using array of references bit unneccessary. save doing (!) there's no way distinguish between player x or o (unless assume player1 starts first x).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -