Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var height = 10;
- var width = 10;
- var winLenght = 5;
- var player = 'X';
- var winCounter = 0; // pocitadlo viteznych policek po sobe jdoucich
- function start() {
- createGameArena();
- }
- // --------------- vytvoření herního pole ----------------- //
- function createGameArena() {
- var gameDiv = document.getElementById('game');
- var table = document.createElement('table');
- for (var i = 0; i < height; i++) {
- var tr = document.createElement('tr');
- for (var j = 0; j < width; j++) {
- var td = document.createElement('td');
- td.id = j + 'x' + i;
- td.X = j;
- td.Y = i;
- td.addEventListener('click', clickToNode);
- tr.appendChild(td);
- }
- table.appendChild(tr);
- }
- gameDiv.appendChild(table);
- }
- function clickToNode(e) {
- /**
- *
- * @type Element element
- */
- var element = e.toElement;
- element.textContent = player;
- // funkce na kontrlu smeru
- checkLeftRight(element);
- checkUpBottom(element);
- checkDiagonalLeftUpToRightBottom(element);
- checkDiagonalRightUpToLeftBottom(element);
- switchPlayer();
- }
- function checkLeftRight(rootElement) {
- var rightElement = getRightElement(rootElement)
- while(rightElement != null && rightElement.textContent == player){
- rightElement = getRightElement(rightElement);
- winCounter++;
- }
- var leftElement = getLeftElement(rootElement);
- while(leftElement != null && leftElement.textContent == player){
- leftElement = getLeftElement(leftElement);
- winCounter++;
- }
- if(winCounter + 1 == winLenght){
- gameOver();
- }
- winCounter = 0;
- }
- function checkUpBottom(rootElement) {
- var topElement = getTopElement(rootElement)
- while(topElement != null && topElement.textContent == player){
- topElement = getTopElement(topElement);
- winCounter++;
- }
- var bottomElement = getBottomElement(rootElement);
- while(bottomElement != null && bottomElement.textContent == player){
- bottomElement = getBottomElement(bottomElement);
- winCounter++;
- }
- if(winCounter + 1 == winLenght){
- gameOver();
- }
- winCounter = 0;
- }
- function checkDiagonalLeftUpToRightBottom(rootElement) {
- var topLeftElement = getTopLeftElement(rootElement)
- while(topLeftElement != null && topLeftElement.textContent == player){
- topLeftElement = getTopLeftElement(topLeftElement);
- winCounter++;
- }
- var bottomRightElement = getBottomRightElement(rootElement);
- while(bottomRightElement != null && bottomRightElement.textContent == player){
- bottomRightElement = getBottomRightElement(bottomRightElement);
- winCounter++;
- }
- if(winCounter + 1 == winLenght){
- gameOver();
- }
- winCounter = 0;
- }
- function checkDiagonalRightUpToLeftBottom(rootElement) {
- var topRightElement = getTopRightElement(rootElement)
- while(topRightElement != null && topRightElement.textContent == player){
- topRightElement = getTopRightElement(topRightElement);
- winCounter++;
- }
- var bottomLeftElement = getBottomLeftElement(rootElement);
- while(bottomLeftElement != null && bottomLeftElement.textContent == player){
- bottomLeftElement = getbottomLeftElement(bottomLeftElement);
- winCounter++;
- }
- if(winCounter + 1 == winLenght){
- gameOver();
- }
- winCounter = 0;
- }
- function gameOver() {
- alert("game over, vyhrál " + player);
- }
- // ------------------------------------------------------------------
- function switchPlayer() {
- if (player == 'X')
- player = 'O';
- else
- player = 'X';
- }
- function getTopElement(rootElement) {
- var topElementId = rootElement.X + 'x' + (rootElement.Y - 1);
- return document.getElementById(topElementId);
- }
- function getBottomElement(rootElement) {
- var bottomElementId = rootElement.X + 'x' + (rootElement.Y + 1);
- return document.getElementById(bottomElementId);
- }
- function getLeftElement(rootElement) {
- var leftElementId = (rootElement.X - 1) + 'x' + rootElement.Y;
- return document.getElementById(leftElementId);
- }
- function getRightElement(rootElement) {
- var rightElementId = (rootElement.X + 1) + 'x' + rootElement.Y;
- return document.getElementById(rightElementId);
- }
- function getTopLeftElement(rootElement) {
- var topLeftElementId = (rootElement.X - 1) + 'x' + (rootElement.Y - 1);
- return document.getElementById(topLeftElementId);
- }
- function getTopRightElement(rootElement) {
- var topRitghtElementId = (rootElement.X + 1) + 'x' + (rootElement.Y - 1);
- return document.getElementById(topRitghtElementId);
- }
- function getBottomLeftElement(rootElement) {
- var bottomLeftElementId = (rootElement.X - 1) + 'x' + (rootElement.Y + 1);
- return document.getElementById(bottomLeftElementId);
- }
- function getBottomRightElement(rootElement) {
- var bottomRightElementId = (rootElement.X + 1) + 'x' + (rootElement.Y + 1);
- return document.getElementById(bottomRightElementId);
- }
- // ----------- debugovací funkce ---------------- //
- function lookAround(element) {
- console.log('left:');
- console.log(getLeftElement(element));
- console.log('right:');
- console.log(getRightElement(element));
- console.log('top:');
- console.log(getTopElement(element));
- console.log('bottom:');
- console.log(getBottomElement(element));
- console.log('top left:');
- console.log(getTopLeftElement(element));
- console.log('top right:');
- console.log(getTopRightElement(element));
- console.log('bottom left:');
- console.log(getBottomLeftElement(element));
- console.log('bottom right:');
- console.log(getBottomRightElement(element));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement