javascript - 如何强制唯一单元格在单击时删除其他单元格

标签 javascript html css

请运行代码片段,它可以让您更好地理解我的问题

  1. 如您所见,我有一个带有数字的单元格和一个带有符号 (S) 和红色的单元格。
  2. 如果我点击任何其他单元格,我的 (S)“UniqueCell”将会移动到那里。
  3. 假设我单击编号为 55 的单元格,我的 UniqueCell 将移动到那里,用 (S) 替换 55,现在我单击其他单元格,假设编号为 320 的单元格,我的 UniqueCell 从单元格 55 移动到单元格320,现在我的 UniqueCell 取代了 320,它是(S),但是 55 号细胞又恢复了它的数量。

我怎样才能防止细胞恢复其数量?一旦我点击它,我怎样才能让它永久失去它的号码?

注意:我正在尝试制作一个游戏,其中玩家 A 选择垂直方向,玩家 B 选择水平方向,因此每次点击绿色都会垂直和水平移动,如果可能的话,我希望每次我点击绿色所在的单元格,如果是垂直的,玩家获得这些分数,如果是水平的,玩家 b 获得分数

var isCol = 0;
var board = [];
for (r = 0; r < 7; r++) {
  var line = [];
  for (c = 0; c < 7; c++) {
    line.push(RandomGenerator(50, 500));
  }
  board.push(line);
}

function prs(curr, c, r) {
  showTable(curr, c, r);
  isCol = (isCol + 1) % 2;
}

function toColor(col, row, chosen_col, chosen_row) {
  var ret = false;
  switch (isCol) {
    case 0:
      if (row == chosen_row) {
        ret = true;
      }
      break;
    case 1:
      if (col == chosen_col) {
        ret = true;
      }
      break;
  }
  return ret;
}

function showTable(c, chosen_col, chosen_row) {
  var str = "";
  str += "<table border=1>";
  for (row = 0; row < 7; row++) {
    str += "<tr>";
    for (let col = 0; col < 7; col++) {
      str += "<td onclick='prs(this, " + col + "," + row + ")'";
      if (toColor(col, row, chosen_col, chosen_row)) {
        if(c.textContent == board[row][col]){
          str += " class=uniqueCell";
        }
        else str += " class='grn' ";
      }
      str += ">";
      if(c.textContent == board[row][col]){
        str += 'S';
      }
      else str += board[row][col];
      str += "</td>";
    }
    str += "</tr>";
  }
  str += "</table>";

  document.getElementById("ff").innerHTML = str;
}

function RandomGenerator(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

showTable(-1);

var getUnique = function(){
  var tdElements = document.querySelectorAll('#ff td');
  tdElements[
    RandomGenerator(0, tdElements.length)
  ].classList.add('uniqueCell');
  // update the text of the cell using the class
  document.querySelector('.uniqueCell').textContent = 'S';
};
getUnique();
td{
  border:2px solid black;
  width:10px;
  height:10px;
  text-align: center;
}
td:hover{background-color:lightgreen;}
.grn{
  background-color:green;
  color:white;
}

.uniqueCell {
  background-color: tomato;
}
<div id="ff"></div>

最佳答案

我们将收集的点保留在对象 P1 和 P2 中,它目前具有点属性 P1.pointsP2.points

我添加了在 Onclick 中调用的内部 prs() 函数

  $('#turn').text(`Player ${(isCol+1)} turn`);
  if (CellPoint) {
    if (isCol) {P1.points+=CellPoint;}else{P2.points+= CellPoint;}
    $('#p1').text(`Player 1: ${P1.points}`);
    $('#p2').text(`Player 2: ${P2.points}`);
  } else {
    console.log('selected S');
  }

var isCol = 0;
var CellPoint = 0;
var board = [];
var P1 = {
  points: 0
};
var P2 = {
  points: 0
};
for (r = 0; r < 7; r++) {
  var line = [];
  for (c = 0; c < 7; c++) {
    line.push(RandomGenerator(50, 500));
  }
  board.push(line);
}

function prs(curr, c, r) {

  CellPoint = parseInt($(curr).text());

  showTable(curr, c, r);
  isCol = (isCol + 1) % 2;
  clr = isCol ? 'blue' : 'red';
  $(curr).text('S');
  $('#turn').css("color", clr)
    .text(`Player ${(isCol+1)} turn`);
  if (CellPoint) {
    if (isCol) {
      P1.points += CellPoint;
    } else {
      P2.points += CellPoint;
    }
    $('#p1').text(`Player 1: ${P1.points}`);
    $('#p2').text(`Player 2: ${P2.points}`);

  } else {
    console.log('selected S');
  }


}

function toColor(col, row, chosen_col, chosen_row) {
  var ret = false;
  switch (isCol) {
    case 0:
      if (row == chosen_row) {
        ret = true;
      }
      break;
    case 1:
      if (col == chosen_col) {
        ret = true;
      }
      break;
  }
  return ret;
}

function showTable(c, chosen_col, chosen_row) {
  if(c!==-1){board[chosen_row][chosen_col] = 'S';}
  var str = "";
  str += "<table border=1>";
  for (row = 0; row < 7; row++) {
    str += "<tr>";
    for (let col = 0; col < 7; col++) {
      str += "<td onclick='prs(this, " + col + "," + row + ")'";
      
        if(board[row][col]=='S'){
          str += " class=uniqueCell";
        } else{
        if (toColor(col, row, chosen_col, chosen_row)) {
        str += " class='grn' ";} }
     
      str += ">";
      if(board[row][col]=='S') {
        str += 'S';
      } else str += board[row][col];
      str += "</td>";
    }
    str += "</tr>";
  }
  str += "</table>";

  document.getElementById("ff").innerHTML = str;
}

function RandomGenerator(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

showTable(-1);

var getUnique = function() {
  var tdElements = document.querySelectorAll('#ff td');
  tdElements[
    RandomGenerator(0, tdElements.length)
  ].classList.add('uniqueCell');
  // update the text of the cell using the class
  document.querySelector('.uniqueCell').textContent = 'S';
};
getUnique();
td {
  border: 2px solid black;
  width: 10px;
  height: 10px;
  text-align: center;
}

td:hover {
  background-color: lightgreen;
}

.grn {
  background-color: green;
  color: white;
}

.turn1 {
  background-color: green;
  color: red;
}

.turn0 {
  background-color: green;
  color: blue;
}

.uniqueCell {
  background-color: tomato;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div><span id='p1' style='color:red;'>Player1: </span>&nbsp; X &nbsp;<span style='color:blue;' id='p2'>Player2: </span></div>
<p id='turn'>Player 1 turn</p>
<div id="ff"></div>

关于javascript - 如何强制唯一单元格在单击时删除其他单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60394748/

相关文章:

javascript - 如何减少纯客户端站点上的 html 重复

javascript - 使用正则表达式操作带 html 标签的字符串

css - 覆盖容器

javascript - 聪明的 JavaScript 绕过 eval 方法

javascript - Js追加和删除特定的div

javascript - Protractor - 从 Bootstrap 下拉列表中选择

javascript - 保存在运行 html 时所做的更改(在浏览器中)

html - 按钮不在内容的末尾

html - bulma 中相同大小的标签

css - 如何将 .svg 文件转换为字体?