|
| 1 | +var gameBoard = [ |
| 2 | + 0, 6, 6, 6, 6, 6, 6, |
| 3 | + 0, 6, 6, 6, 6, 6, 6 |
| 4 | +]; |
| 5 | + |
| 6 | +var currentPlayer = 'one'; |
| 7 | +var readOut = document.querySelector('div.info'); |
| 8 | + |
| 9 | +function renderBoard() { |
| 10 | + var gameContainer = document.querySelector('.container'); |
| 11 | + gameContainer.innerHTML = ''; // Clear previous content |
| 12 | + |
| 13 | + for (var i = 0; i < gameBoard.length; i++) { |
| 14 | + if (i % 7 === 0) { |
| 15 | + var row = document.createElement('div'); |
| 16 | + gameContainer.appendChild(row); |
| 17 | + } |
| 18 | + |
| 19 | + var button = document.createElement('button'); |
| 20 | + button.setAttribute('class', 'pit' + i); |
| 21 | + button.setAttribute('id', i); |
| 22 | + button.innerHTML = gameBoard[i]; |
| 23 | + row.appendChild(button); |
| 24 | + } |
| 25 | + |
| 26 | + var playerClass = document.querySelectorAll('.container div'); |
| 27 | + playerClass[0].setAttribute('class', 'playerTwo'); |
| 28 | + playerClass[1].setAttribute('class', 'playerOne'); |
| 29 | + |
| 30 | + setListeners(); |
| 31 | +} |
| 32 | + |
| 33 | +function moveStones(pitIndex) { |
| 34 | + var stonesInHand = gameBoard[pitIndex]; |
| 35 | + var pitNextIndex = pitIndex + 1; |
| 36 | + gameBoard[pitIndex] = 0; |
| 37 | + var lastIndex; |
| 38 | + if (stonesInHand > 0) { |
| 39 | + for (var i = pitNextIndex; i < stonesInHand + pitNextIndex; i++) { |
| 40 | + lastIndex = i % gameBoard.length; |
| 41 | + gameBoard[lastIndex] += 1; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + var nextTurn = bankStones(lastIndex); |
| 46 | + if (!nextTurn) { |
| 47 | + setPlayer(); |
| 48 | + } |
| 49 | + updateBoard(); |
| 50 | +} |
| 51 | + |
| 52 | +function bankStones(lastIndex) { |
| 53 | + var inverse = gameBoard.length - lastIndex; |
| 54 | + if ((lastIndex !== 0) && (lastIndex !== 7) && (gameBoard[lastIndex] === 1)) { |
| 55 | + if (currentPlayer === 'two') { |
| 56 | + if (1 <= lastIndex && lastIndex <= 6) { |
| 57 | + gameBoard[7] = gameBoard[7] + gameBoard[inverse] + 1; |
| 58 | + gameBoard[lastIndex] = 0; |
| 59 | + gameBoard[inverse] = 0; |
| 60 | + } |
| 61 | + } else { |
| 62 | + if (8 <= lastIndex && lastIndex <= 13) { |
| 63 | + gameBoard[0] = gameBoard[0] + gameBoard[inverse] + 1; |
| 64 | + gameBoard[lastIndex] = 0; |
| 65 | + gameBoard[inverse] = 0; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Check if the last stone landed in the player's bank |
| 71 | + if ((currentPlayer === 'one' && lastIndex === 0) || (currentPlayer === 'two' && lastIndex === 7)) { |
| 72 | + return true; // The player gets another turn |
| 73 | + } |
| 74 | + return false; |
| 75 | +} |
| 76 | + |
| 77 | +function updateBoard() { |
| 78 | + for (var i = 0; i < gameBoard.length; i++) { |
| 79 | + var pit = document.querySelectorAll('button'); |
| 80 | + pit[i].textContent = gameBoard[i]; |
| 81 | + } |
| 82 | + |
| 83 | + checkWin(); |
| 84 | +} |
| 85 | + |
| 86 | +function checkWin() { |
| 87 | + var playerOneStones = gameBoard[1] + gameBoard[2] + gameBoard[3] + gameBoard[4] + gameBoard[5] + gameBoard[6]; |
| 88 | + var playerTwoStones = gameBoard[8] + gameBoard[9] + gameBoard[10] + gameBoard[11] + gameBoard[12] + gameBoard[13]; |
| 89 | + if (playerOneStones === 0 || playerTwoStones === 0) { |
| 90 | + var playerOneTotal = playerOneStones + gameBoard[0]; |
| 91 | + var playerTwoTotal = playerTwoStones + gameBoard[7]; |
| 92 | + |
| 93 | + if (playerOneTotal > playerTwoTotal) { |
| 94 | + readOut.textContent = 'Player One Wins!'; |
| 95 | + } else { |
| 96 | + readOut.textContent = 'Player Two Wins!'; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function setPlayer() { |
| 102 | + var gameContainer = document.querySelector('.container'); |
| 103 | + |
| 104 | + if (currentPlayer === 'one') { |
| 105 | + currentPlayer = 'two'; |
| 106 | + gameContainer.classList.add('rotated'); |
| 107 | + readOut.textContent = 'It is player ' + currentPlayer + '\'s turn'; |
| 108 | + } else { |
| 109 | + currentPlayer = 'one'; |
| 110 | + gameContainer.classList.remove('rotated'); |
| 111 | + readOut.textContent = 'It is player ' + currentPlayer + '\'s turn'; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function setListeners() { |
| 116 | + for (var i = 0; i < gameBoard.length; i++) { |
| 117 | + var pit = document.querySelectorAll('button'); |
| 118 | + pit[i].addEventListener('click', function (eventObject) { |
| 119 | + moveStones(Number(eventObject.target.id)); |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +renderBoard(); |
0 commit comments