单击后如何强制唯一单元格删除其他单元格 [英] How to force unique Cell to delete other cells upon clicking

查看:58
本文介绍了单击后如何强制唯一单元格删除其他单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 如您所见,我有一个带数字的单元格和一个带符号(S)和红色的单元格.
  2. 如果我单击任何其他单元格,则我的(S)"UniqueCell"将移到那里.
  3. 让我说我单击编号为55的单元格,我的UniqueCell将会移动到那里,用(S)替换为55,现在我单击其他单元格,比方说编号为320的单元格,我的UniqueCell从单元格55移动到单元格320,现在我的UniqueCell用(S)代替了320,但是单元格55重新获得了它的编号.

我如何防止细胞重新获得其编号?单击后如何使它永久丢失其编号?

How i can prevent the cells from gaining back its numbers? how i can make it lose its numbers permanently once i clicked on it?

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

note: I'm trying to make a game where player A pick vertical and player B to pick horizontal, hence the green moving vertical and horizontal every click, if possible, i want each time i click on cell where the green is, if it was Vertical, player gain those points, if it was horizontal, player b gain the points

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.points& P2.points

we keep points collected in object P1 & P2 it has for now points property P1.points & P2.points

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

I added inside prs() funtion called in Onclick

  $('#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>

这篇关于单击后如何强制唯一单元格删除其他单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆