Solved error of not removing cups after the 7th cup. Winner function was reworked, draw funktion was removed.
This commit is contained in:
parent
3a30e2a597
commit
c2be377a92
@ -19,8 +19,7 @@ Board.events = {
|
|||||||
GAME_READY: 'gameReady',
|
GAME_READY: 'gameReady',
|
||||||
CUP_MARKED: 'cupMarked',
|
CUP_MARKED: 'cupMarked',
|
||||||
CHANGE_TURN: 'changeTurn',
|
CHANGE_TURN: 'changeTurn',
|
||||||
WINNER: 'winner',
|
WINNER: 'winner'
|
||||||
DRAW: 'draw'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(Board, EventEmitter);
|
util.inherits(Board, EventEmitter);
|
||||||
@ -67,9 +66,7 @@ Board.prototype.mark = function(cupId) {
|
|||||||
var res = this.checkWinner();
|
var res = this.checkWinner();
|
||||||
if (res.win) {
|
if (res.win) {
|
||||||
this.disableAll();
|
this.disableAll();
|
||||||
this.emit(Board.events.WINNER, {player: this.players[this.currentTurn], pos: res.pos});
|
this.emit(Board.events.WINNER, {player: this.players[this.currentTurn]});
|
||||||
} else if (this.checkDraw()) {
|
|
||||||
this.emit(Board.events.DRAW, {});
|
|
||||||
} else {
|
} else {
|
||||||
this.currentTurn = (this.currentTurn + 1) % 2;
|
this.currentTurn = (this.currentTurn + 1) % 2;
|
||||||
this.emit(Board.events.CHANGE_TURN, this.players[this.currentTurn]);
|
this.emit(Board.events.CHANGE_TURN, this.players[this.currentTurn]);
|
||||||
@ -83,7 +80,6 @@ Board.prototype.mark = function(cupId) {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
Board.prototype.checkTurn = function(playerId) {
|
Board.prototype.checkTurn = function(playerId) {
|
||||||
('checkTurn');
|
|
||||||
return this.players[this.currentTurn].id == playerId;
|
return this.players[this.currentTurn].id == playerId;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -91,52 +87,50 @@ Board.prototype.checkTurn = function(playerId) {
|
|||||||
*
|
*
|
||||||
* @returns {{win: boolean, pos: Array}}
|
* @returns {{win: boolean, pos: Array}}
|
||||||
*/
|
*/
|
||||||
Board.prototype.checkWinner = function() {
|
Board.prototype.checkWinner = function() {
|
||||||
|
var win = false;
|
||||||
|
var counter=0;
|
||||||
|
|
||||||
|
this.cups.forEach(function(cup) {
|
||||||
|
if(cup.active == false) {
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if(counter == 10) {
|
||||||
|
win = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
var winPosition = [
|
var winPosition = [
|
||||||
[0, 1, 2],
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
[3, 4, 5],
|
|
||||||
[6, 7, 8],
|
|
||||||
|
|
||||||
[0, 3, 6],
|
|
||||||
[1, 4, 7],
|
|
||||||
[2, 5, 8],
|
|
||||||
|
|
||||||
[0, 4, 8],
|
|
||||||
[6, 4, 2]
|
|
||||||
];
|
];
|
||||||
|
|
||||||
var player = this.players[this.currentTurn];
|
var player = this.players[this.currentTurn];
|
||||||
var pos = [];
|
var pos = [];
|
||||||
|
|
||||||
var win = winPosition.some(function(win) {
|
var win = winPosition.some(function(win) {
|
||||||
if (this.cups[win[0]].value === player.label) {
|
if (this.cups[win[0]].textContent === player.label) {
|
||||||
var res = this.cups[win[0]].value === this.cups[win[1]].value && this.cups[win[1]].value === this.cups[win[2]].value;
|
var res = this.cups[win[0]].textContent === this.cups[win[1]].textContent && this.cups[win[1]].textContent === this.cups[win[2]].textContent;
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
pos = win;
|
pos = win;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}, this);
|
}, this);*/
|
||||||
|
|
||||||
return {
|
return {
|
||||||
win: win,
|
win: win
|
||||||
pos: pos
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
Board.prototype.checkDraw = function() {
|
|
||||||
return this.cups.every(function(cup) {
|
|
||||||
return cup.value === this.players[0].label || cup.value === this.players[1].label;
|
|
||||||
}, this);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -62,12 +62,9 @@ Board.prototype.enableTurn = function() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Board.prototype.highlightcups = function(pos) {
|
Board.prototype.highlightcups = function() {
|
||||||
var cups = this.cups;
|
var cups = this.cups;
|
||||||
alert('highlightcups');
|
alert('highlightcups');
|
||||||
pos.forEach(function(i) {
|
|
||||||
// cups[i].classList.add('colorRed');
|
|
||||||
});
|
|
||||||
|
|
||||||
cups.forEach(function(cup) {
|
cups.forEach(function(cup) {
|
||||||
cup.setAttribute('marked', 'true');
|
cup.setAttribute('marked', 'true');
|
||||||
@ -208,18 +205,12 @@ Board.prototype.doMark = function(cupId, label) {
|
|||||||
*
|
*
|
||||||
* @param pos
|
* @param pos
|
||||||
*/
|
*/
|
||||||
Board.prototype.doWinner = function(pos) {
|
Board.prototype.doWinner = function() {
|
||||||
this.disableAll();
|
this.disableAll();
|
||||||
this.highlightcups(pos);
|
this.highlightcups();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
Board.prototype.doDraw = function() {
|
|
||||||
alert('doDraw');
|
|
||||||
this.lowlightcups();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -241,17 +232,23 @@ Board.prototype.highlightScoreboard = function(playerId) {
|
|||||||
* @returns {{win: boolean, pos: Array}}
|
* @returns {{win: boolean, pos: Array}}
|
||||||
*/
|
*/
|
||||||
Board.prototype.checkWinner = function() {
|
Board.prototype.checkWinner = function() {
|
||||||
|
var counter=0;
|
||||||
|
var win = false;
|
||||||
|
this.cups.forEach(function(cup) {
|
||||||
|
if (cup.getAttribute('marked') === 'true') {
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if(counter === 10) {
|
||||||
|
win = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
var winPosition = [
|
var winPosition = [
|
||||||
[0, 1, 2],
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
[3, 4, 5],
|
|
||||||
[6, 7, 8],
|
|
||||||
|
|
||||||
[0, 3, 6],
|
|
||||||
[1, 4, 7],
|
|
||||||
[2, 5, 8],
|
|
||||||
|
|
||||||
[0, 4, 8],
|
|
||||||
[6, 4, 2]
|
|
||||||
];
|
];
|
||||||
|
|
||||||
var player = this.players[this.currentTurn];
|
var player = this.players[this.currentTurn];
|
||||||
@ -268,7 +265,7 @@ Board.prototype.checkWinner = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}, this);
|
}, this);*/
|
||||||
|
|
||||||
return {
|
return {
|
||||||
win: win,
|
win: win,
|
||||||
@ -277,15 +274,7 @@ Board.prototype.checkWinner = function() {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
Board.prototype.checkDraw = function() {
|
|
||||||
return this.cups.every(function(cup) {
|
|
||||||
return cup.textContent === this.players[0].label || cup.textContent === this.players[1].label;
|
|
||||||
}, this);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
var socket = new WebSocket('ws://localhost:2667');
|
var socket = new WebSocket('ws://192.168.188.72:2667');
|
||||||
var events = {
|
var events = {
|
||||||
outgoing: {
|
outgoing: {
|
||||||
JOIN_GAME: 'csJoinGame',
|
JOIN_GAME: 'csJoinGame',
|
||||||
@ -114,9 +114,7 @@ socket.onmessage = function(event){
|
|||||||
|
|
||||||
case events.incoming.GAME_OVER:
|
case events.incoming.GAME_OVER:
|
||||||
if (msg.data.player) {
|
if (msg.data.player) {
|
||||||
board.doWinner(msg.data.pos);
|
board.doWinner();
|
||||||
} else {
|
|
||||||
board.doDraw();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.send(makeMessage(events.outgoing.QUIT, hero.id));
|
socket.send(makeMessage(events.outgoing.QUIT, hero.id));
|
||||||
|
@ -71,11 +71,6 @@ wss.on('connection', function connection(ws) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
board.on(Board.events.DRAW, function(event) {
|
|
||||||
wss.clients.forEach(function(client) {
|
|
||||||
client.send(makeMessage(events.outgoing.GAME_OVER, event));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('message', function incoming(msg) {
|
ws.on('message', function incoming(msg) {
|
||||||
|
|
||||||
|
@ -4,10 +4,17 @@ Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängige
|
|||||||
|
|
||||||
Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
|
Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
|
||||||
|
|
||||||
Voraussetzungen: Node.js wurde installiert.
|
Voraussetzungen: Node.js wurde installiert, so dass npm im Terminal verfügbar ist.
|
||||||
|
|
||||||
Erst den Web-server starten mit:
|
Erst den Web-server starten mit:
|
||||||
npm run-script web-server
|
npm run-script web-server
|
||||||
|
|
||||||
danach den Socket-server starten mit:
|
danach den Socket-server starten mit:
|
||||||
npm run-script socket-server
|
npm run-script socket-server
|
||||||
|
|
||||||
|
Becher über die Tasten:
|
||||||
|
qwer
|
||||||
|
asd
|
||||||
|
yx
|
||||||
|
c
|
||||||
|
verschwinden lassen.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user