Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- export default {
- name: 'ScoreBoard',
- data() {
- return {
- state: 'gameStarted',
- addPlayerName: '',
- gameStarted: false,
- gameEnded: false,
- turnCounter: 0,
- selectedScore: null,
- winningPlayer: null,
- players: [
- {
- name: 'Rainbird',
- score: 0,
- strikes: 0,
- id: 0,
- },
- {
- name: 'George',
- score: 0,
- strikes: 0,
- id: 1,
- },
- {
- name: 'Balloo',
- score: 0,
- strikes: 0,
- id: 2,
- },
- {
- name: 'Greg',
- score: 0,
- strikes: 0,
- id: 3,
- },
- {
- name: 'Peter',
- score: 0,
- strikes: 0,
- id: 4,
- },
- {
- name: 'Captain',
- score: 0,
- strikes: 0,
- id: 5,
- }
- ],
- addPlayerCounter: 0,
- };
- },
- methods: {
- updateSelectedScore: function(value) {
- this.selectedScore = value;
- },
- addPlayer: function() {
- var playerName = this.addPlayerName;
- this.players.push({
- name: playerName,
- score: 0,
- strikes: 0,
- id: this.addPlayerCounter,
- })
- this.addPlayerName = '';
- this.addPlayerCounter++;
- },
- initAddPlayers: function() {
- this.state = 'addPlayers';
- },
- startGame: function() {
- this.state = 'gameStarted';
- },
- removePlayer: function(player) {
- for (let i = 0; i < this.players.length; i++) {
- if(this.players[i].id === player.id) {
- this.players.splice(i, 1);
- }
- }
- },
- shufflePlayers: function() {
- var players = this.players;
- function shuffle(array) {
- let counter = array.length;
- // While there are elements in the array
- while (counter > 0) {
- // Pick a random index
- let index = Math.floor(Math.random() * counter);
- // Decrease counter by 1
- counter--;
- // And swap the last element with it
- let temp = array[counter];
- array[counter] = array[index];
- array[index] = temp;
- }
- return array;
- }
- shuffle(players);
- for (let i = 0; i < players.length; i++) {
- players[i].id = i;
- }
- this.players = players.splice(0);
- },
- selectThis(val, index) {
- this.activeScoreButton = index;
- },
- endGame: function() {
- this.state = 'gameEnded';
- for (let i = 0; i < this.players.length; i++) {
- this.players[i].score = '-';
- this.players[i].strikes = 0;
- this.players[i].eliminated = false;
- }
- },
- saveScore: function() {
- var currentPlayer = this.current_player;
- //reset dash as we are going to get a result
- if(currentPlayer.score === '-') {
- currentPlayer.score = 0;
- }
- //if the score is 0, update the player's strike
- if(this.selectedScore === 0) {
- currentPlayer.strikes += 1;
- if(currentPlayer.strikes === 3) {
- currentPlayer.eliminated = true;
- }
- } else {
- //reset the players strike since they have scored
- currentPlayer.strikes = 0;
- //add the selected score to the current player, then reset it
- currentPlayer.score += this.selectedScore;
- }
- //test if player has gone over 50
- if(currentPlayer.score > 50) {
- currentPlayer.score = 25;
- }
- //reset score
- this.selectedScore = null;
- //check if player has won, otherwise next player
- if(currentPlayer.score === 50) {
- this.winningPlayer = currentPlayer;
- this.endGame();
- } else {
- //set the turnCounter to the next player;
- this.turnCounter = this.next_player.id;
- }
- }
- },
- computed : {
- current_player() {
- var currPlayer;
- var currTurn= this.turnCounter;
- for (let i = 0; i < this.players.length; i++) {
- if(this.players[i].id === currTurn) {
- currPlayer = this.players[i];
- }
- }
- return currPlayer;
- },
- next_player() {
- var currPlayer;
- var players = this.players;
- function getNextPlayer(index) {
- if (!players[index].eliminated) {
- return players[index];
- }
- index++;
- var newIndex = (index) % players.length;
- return getNextPlayer(newIndex);
- }
- var index = (this.turnCounter + 1) % players.length;
- return getNextPlayer(index);
- },
- sorted_players() {
- var clone = this.players.slice(0);
- return clone.sort((a, b) => {
- return a.eliminated - b.eliminated || b.score - a.score;
- });
- }
- }
- };
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement