0

the html code is this

<input type="button" value="Throw the Dice" onmouseup="throwTwoDice(6,6)"> 
            <div id="dice1" style="padding:10px;font-style:bold;font-size:xx-large">0 - 0</div>
        </div> 

the javascript code is this

function dice(sides){
        var rand = Math.floor(Math.random()*sides) + 1;
        return rand;
    };


function throwTwoDice(dice1Sides, dice2Sides){
        var rand1 = dice(dice1Sides);
        var rand2 = dice(dice2Sides);
        document.getElementById('dice1').innerHTML = rand1 + ' - ' + rand2;
        return true;
    };

I have to randoms numbers rand1 and rand2. I want to be rand1 = x1 and rand2 = x2. I have a table with 2 lines and 12 columns,the line 1 is x1 and line 2 is x2. I have imagesenter image description here on [1,1] and in [12,2]. The code is this of javascript

let colors={black:"https://media2.giphy.com/media/htSM5MPkwlKaSYE7yN/giphy.gif?cid=ecf05e47pftg0oul854g6z0mypnv4564axlqcaxsrlttxl5o&rid=giphy.gif"
,red:"https://media3.giphy.com/media/fxhEoiBJo7NNXc7ozy/giphy.gif?cid=ecf05e474lq8uatdu2wyyun0cpf096zqy78hsopz0pxzvete&rid=giphy.gif"}

let items = [{
    "x": 1,
    "y": 1,
    "piece": "p",
    "piece_color": "black",
    "b_color": "black"
  },
  {
    "x": 12,
    "y": 2,
    "piece": "p",
    "piece_color": "red",
    "b_color": "red"
  }
];
$(function() {
  draw_empty_board();
  fill_board();
});





function fill_board() {
  fill_board_by_data(items);
}

function fill_board_by_data(data) {
    
    
    
    for (var i = 0; i < data.length; i++) {
    var o = data[i];
    var id = '#square_' + o.x + '_' + o.y;
    var c = (o.piece != null) ? o.piece_color : '';
    var im = (o.piece != null) ? '<img class="piece" src="' + colors[c] + '.png">' : '';
    var htmls = ''; //delcare this
    //check color if black 14 or 15(red)
    var count = (o.piece_color == "black") && (o.piece != null) ? 15 : 15
    //loop till count
    for (var j = 0; j < count; j++) {
      //append img
      htmls += '<img class="piece" src="' + colors[c] + '.png">';
    }
    //finally add img inside td
    $(id).addClass(o.b_color + '_square').html(htmls)


  }
  
  
  
  
  
  
  
    var t = '<table id="chess_table">';
        for (var i = rowCount; i > 0; i--) {

            t += '<tr>';
            t += '<td class="line_label">' + i + '</td>';
            for (var j = 1; j < columnCount; j++) {


                t += '<td class="chess_square" id="square_' + j + '_' + i + '">' + j + ',' + i + '</td>';

            }
            t += '</tr>';
        }
        t += '<tr><td class="column_label line_label"></td>';
        for (var j = 1; j < 13; j++) {

            t += '<td class="column_label">' + j + '</td>';
        }
        t += '</tr>';
        t += '</table>';
  
  
  
  
  
  
  
  
  
  
  
  
}
  

I want when the random numbers come example 3 - 6 . Then the first,the (3 in that case) will be position [3,1].Then 1image from black will go to that slot. Also,about the (6 in that case) will be the position [6,2] and one of the red image will go on that slot. As a result,to sum up 3 is the x1 and 6 is the x2.More detailed x1 line is have 1 and x2 line has the 2.So numbers will be like this ( ,1) ( ,1) etc and for the x2 ( ,x2) ( ,x2) etc.I replace the empty place with a random number.I am trying so far to make my images moved or to do that ,but I losing the images or the values are not going well. hint: black images are moving only in the x1 line. Red images move only in x2 line.

  • 1
    I can't really follow what you are trying to do. You could insert a working snippet in your question, to make it easier to understand. – Josef Wittmann Dec 23 '20 at 16:53
  • I have a table 2 lines 12 columns.The lines 1 and 2. I name it x=1 and x=2. As you see I have upload a picture . With a table similar of what I am creating. –  Dec 23 '20 at 17:39
  • I have a table 2 lines 12 columns.The lines 1 and 2. I name it x=1 and x=2. As you see I have upload a picture . With a table similar of what I show you here. I have pictures 15 (images ) . I have black ,color and red color. 15 each . Now, Black (15 images ) are in [1,1] and Red(15 images ) are in [12,2]. I have created a dice (that it has 2 random values). It gives me example when I press fhe button 2 numbers example 7 - 9. The 7 will go for blacks and it will be for example [7,1] so 1 image out of 15 of blacks will go that slot as you see the image. On the other hand , 9 will be the [9,2] –  Dec 23 '20 at 17:54
  • With other way ,if you still not get it. See the image what I want to do is from [1,1] there I have images (the blacks) I want to move them until [8,1]. As you can sss the 1 is standar what is changing is the first number . So when I press the button I will take a random number until 8. That random will be the first number which is unknown [ ,1]. And the same thing I am doing for the slots [,2] –  Dec 23 '20 at 18:00
  • @josef wittmann do you understand what I am trying to do ? . I cant make a snippet because is the same thing as the image I uploaded. Except that i have images on [1,1] and in [12,2] (in the picture i upload it is [8,2]) –  Dec 23 '20 at 18:03
  • 1
    I can't follow. You seem to have a clear picture in your head, but there is no way to follow, unless you focus on what your problem really is (forget about the details). *What* are you trying to achive, one step at a time? – Josef Wittmann Dec 23 '20 at 18:11
  • Move the jmages the black on x =1 from 1,1 to move it with a random number until 12,1. The same with red ,but I want to move the red only in x=2. Thats it. I want it to do it with a random numbers . Lets say xx is unknow and x1=1 then [xx,1],where xx= a random number . Then my image (the black) goes on that –  Dec 23 '20 at 18:26
  • @Josef wittmann I think now you will understand. I have a table. I have two lines. In each line I have different images. I throw a dice and I am creating two random numbers . Those random numbers I am using those to move 1 of the blacks and 1 of red. Red moves in their lines .(thats means x=2) and black moves in their lines(x=1). The random numbers from dice are the [ random number,1] , [random number,2] . –  Dec 23 '20 at 18:37
  • It's getting clearer. A good approach would be to have a game state (maybe an array of pieces like your `items`) and then have a function `render(items)`, that draws things according to the `items` state. Then you don't have to worry about drawing, when handling game logic. And then just create another function that handles the game logic (find a black piece and move it according to the first dice, find a red piece and move it according to the second piece). Is this where you're struggling? – Josef Wittmann Dec 23 '20 at 19:51
  • @JosefWittmann exactly that.That I am trying to do so many hours but still can't.I have tried a lot of things and no much informations about it on internet –  Dec 23 '20 at 19:59
  • Please add all relevant details to the question, then remove all of the comments that provide question relevant details. Researchers and volunteers should not need to read comments to understand the question. Why is there a php tag on this question? – mickmackusa Dec 27 '20 at 12:06

1 Answers1

0

What do you think about this:

// basic game settings
const gameSettings = {
  length: 12,
  die: {
    min: 1,
    max: 8,
  }
}

// define "actors"
let gameItems = [{
    name: "R",
    bgColor: "red",
    color: "white",
    position: 11,
    direction: -1,
    startingPosition: 11,
    imgUrl: `https://picsum.photos/seed/red/40/20`,
  },
  {
    name: "B",
    bgColor: "black",
    color: "white",
    position: 0,
    direction: 1,
    startingPosition: 0,
    imgUrl: `https://picsum.photos/seed/black/40/20`,
  },
]

// the random function
// source: https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript
function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}

// -----------------

// creating a row & cells
const rowHtml = (gameItem, cols) => {
  let html = `<div class="row" style="grid-template-columns: repeat(${ cols }, 1fr)">`
  for (let i = 0; i < cols; i++) {
    let content = "-"
    let background = "white"
    let color = "black"

    if (gameItem.position === i) {
      marker = gameItem.name
      background = gameItem.bgColor
      color = gameItem.color
      content = `<img src="${ gameItem.imgUrl }" />`
    }

    html += `<div class="cell" style="background: ${ background }; color: ${ color }">${ content }</div>`
  }
  html += `</div>`
  return html
}

// updating rows
const updateRows = (container, gameItems, cols) => {
  let html = ''
  for (let i = 0; i < gameItems.length; i++) {
    html += rowHtml(gameItems[i], cols)
  }
  container.innerHTML = html
}

// setting container
const container = document.getElementById('container')

// set up board for first time
updateRows(container, gameItems, gameSettings.length)

// -----------------
// action buttons
const btnRoll = document.getElementById("rollTheDice")
const btnResetBoard = document.getElementById("resetBoard")

// roll action
btnRoll.addEventListener('click', function() {
  const {
    length,
    die: {
      min
    },
    die: {
      max
    }
  } = gameSettings

  gameItems = gameItems.map(item => ({
    ...item,
    position: item.position + (randomIntFromInterval(min, max) * item.direction),
  }))
  updateRows(container, gameItems, length)
})

// reset action
btnResetBoard.addEventListener('click', function() {
  const {
    length
  } = gameSettings
  gameItems = gameItems.map(item => ({
    ...item,
    position: item.startingPosition,
  }))
  updateRows(container, gameItems, length)
})
.row {
  display: grid;
  grid-template-rows: 1fr;
}

.cell {
  box-sizing: border-box;
  border: 1px solid black;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 32px;
}

.cell img {
  max-width: 100%;
  min-width: 100%;
}
<div id="container"></div>
<hr />
<button id="rollTheDice">ROLL THE DICE</button><br />
<button id="resetBoard">RESET BOARD</button>

In this snippet, you can set the number of players, the length of the board, the range of the dice (min-max), and some settings of the "players".

EDIT

Added three new attributes to the gameItems:

  • direction controls the direction of steps of a gameItem (1 is left to right, -1 is right to left
  • startingPosition sets where a gameItem starts after resetting the board
  • imgUrl is the URL of an image that is shown as the gameItem on the board
muka.gergely
  • 8,063
  • 2
  • 17
  • 34
  • gergely after that } html += `` return html } // updating rows const updateRows = (container, gameItems, cols) => { let html = '' for (let i = 0; i < gameItems.length; i++) { html += rowHtml(gameItems[i], cols) } container.innerHTML = html –  Dec 23 '20 at 21:39
  • until the end .I put it in the javascript script and it doesn't show me as javascript code,lol –  Dec 23 '20 at 21:39
  • So a) maybe it is because this is not html and it is .php ? that's why I think.Second I want R to start from the end. not from the left to right ,from right to left.The idea is right but it needs some changes..if you could edited –  Dec 23 '20 at 21:43
  • also I can't add on that code images..It is perfect that if it could be worked with images and instead of html to be on php this code –  Dec 23 '20 at 22:21
  • @mainklain Hi, sorry for this answer taking so much time - it's been XMas time. – muka.gergely Dec 27 '20 at 11:30