Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向窗体添加图像_Javascript_Html_Image_Forms_Dom - Fatal编程技术网

Javascript 向窗体添加图像

Javascript 向窗体添加图像,javascript,html,image,forms,dom,Javascript,Html,Image,Forms,Dom,我目前正在尝试为noughts和Cross的Javascript版本设置一些简单的视觉效果,但在向表单添加图像时遇到了困难。更具体地说,我正在尝试将背景图像设置为电路板,然后用图像版本替换X和O。基本上,在我开始整理onclick之前,我正在尝试实现一个更好的GUI。如有任何建议,将不胜感激 谢谢 HTML JavaScript var turn = 0 // A counter for the number of turns that have passed var win =

我目前正在尝试为noughts和Cross的Javascript版本设置一些简单的视觉效果,但在向表单添加图像时遇到了困难。更具体地说,我正在尝试将背景图像设置为电路板,然后用图像版本替换X和O。基本上,在我开始整理onclick之前,我正在尝试实现一个更好的GUI。如有任何建议,将不胜感激

谢谢

HTML




JavaScript

  var turn = 0 // A counter for the number of turns that have passed
  var win = false // Tracking whether the main game loop should continue

  var space = ' ' // The symbol we will use for an empty space on the board
  var player1 = 'O' // The symbol we will use for player 1
  var player2 = 'X' // The symbol we will use for player 2



   // The board will be represented by a 2D list
   // (a list of rows, with each row being itself a list)
   board = []
   board[0] = [space, space, space]
   board[1] = [space, space, space]
   board[2] = [space, space, space]

   // Take a board variable called b and print out a nice text
   // representation of the board.
   // The board variable should be a 2D list with each element containing space, 'X' or 'O'
   function printboard(b) {
      var position1 = board[0][0]
      var position2 = board[0][1]
      var position3 = board[0][2]
      var position4 = board[1][0]
      var position5 = board[1][1]
      var position6 = board[1][2]
      var position7 = board[2][0]
      var position8 = board[2][1]
      var position9 = board[2][2]

      document.getElementById("position1").innerHTML = position1;
      document.getElementById("position2").innerHTML = position2;
      document.getElementById("position3").innerHTML = position3;
      document.getElementById("position4").innerHTML = position4;
      document.getElementById("position5").innerHTML = position5;
      document.getElementById("position6").innerHTML = position6;
      document.getElementById("position7").innerHTML = position7;
      document.getElementById("position8").innerHTML = position8;
      document.getElementById("position9").innerHTML = position9;
  }

   // Take a player type (X or O) and play a single move on the board
   // for that player. 
   // At the moment this just prompts the player for their move and 
   // then makes the move if possible. The move is lost if they try
   // an already occupied position. At the moment no attempt it made
   // to validate the input
   function playMove(player) {
      ("Your turn player", player)
      row = parseInt(prompt("enter row:"))
      col = parseInt(prompt("enter column:"))
      if (board[row][col] === player1 || board[row][col] === player2) {
          ("Already taken! Move skipped")
      } else {
          board[row][col] = player
      }
  }



   // Take a board (2D list of space, 'X' or 'O') and a player marker
   // Returns True if that player has won on the board, and False if they
   // have not
   function checkWin(b, player) {

      // Check each row
      for (var r = 0; r < b.length; r = r + 1) {
          row = b[r];
          if (row[0] === player && row[1] === player && row[2] === player) {
              return true
          }
      }

      // Check diagonals
      if (b[0][0] === player && b[1][1] === player && b[2][2] === player) {
          return true
      }
      // Todo: second diagonal


      // Check columns
      for (var col = 0; col < 3; col = col + 1) {
          if (b[0][col] === player && b[1][col] === player && b[2][col] === player) {
              return true
          }
      }

      // If we have not already returned by now then there was no win position.
      return false
  }

   function play_game() {
      printboard(board)

      while (win === false) {


          playMove(player1)
          printboard(board)
          win = checkWin(board, player1)

          if (!win) {
              playMove(player2)
              printboard(board)
              win = checkWin(board, player2)
          }
      }


      document.write("Game over")
  }
var turn=0//已通过的圈数计数器
var win=false//跟踪主游戏循环是否应继续
var space='''//我们将在电路板上用作空白的符号
var player1='O'//我们将用于播放器1的符号
var player2='X'//我们将用于播放器2的符号
//董事会将由2D列表表示
//(行列表,每行本身就是一个列表)
董事会=[]
板[0]=[空间,空间,空间]
板[1]=[空间,空间,空间]
板[2]=[空间,空间,空间]
//取一个名为b的板变量,打印出一个漂亮的文本
//董事会的代表。
//board变量应该是一个2D列表,每个元素都包含空格“X”或“O”
功能印制板(b){
变量位置1=板[0][0]
变量位置2=板[0][1]
变量位置3=板[0][2]
变量位置4=板[1][0]
变量位置5=董事会[1][1]
var位置6=董事会[1][2]
变量位置7=板[2][0]
var position8=董事会[2][1]
var position9=董事会[2][2]
document.getElementById(“position1”).innerHTML=position1;
document.getElementById(“position2”).innerHTML=position2;
document.getElementById(“position3”).innerHTML=position3;
document.getElementById(“position4”).innerHTML=position4;
document.getElementById(“position5”).innerHTML=position5;
document.getElementById(“position6”).innerHTML=position6;
document.getElementById(“position7”).innerHTML=position7;
document.getElementById(“position8”).innerHTML=position8;
document.getElementById(“position9”).innerHTML=position9;
}
//选择一个玩家类型(X或O),在棋盘上玩一个单步
//为了那个球员。
//目前,这只是提示玩家移动和移动
//然后尽可能采取行动。如果他们尝试,就会失败
//已经占据的位置。目前它没有作出任何尝试
//验证输入
功能playMove(播放器){
(“轮到你的球员”,球员)
行=parseInt(提示(“输入行:”)
col=parseInt(提示(“输入列:”)
如果(棋盘[行][col]==player1 | |棋盘[行][col]==player2){
(“已采取行动!跳过移动”)
}否则{
棋盘[行][列]=玩家
}
}
//拿一块棋盘(2D空格列表,'X'或'O')和一个玩家标记
//如果该玩家在棋盘上获胜,则返回True;如果该玩家在棋盘上获胜,则返回False
//没有
函数checkWin(b,播放器){
//检查每一行
对于(var r=0;r
@Tom感谢您的编辑!如果你做了一把小提琴,那会很有帮助
  var turn = 0 // A counter for the number of turns that have passed
  var win = false // Tracking whether the main game loop should continue

  var space = ' ' // The symbol we will use for an empty space on the board
  var player1 = 'O' // The symbol we will use for player 1
  var player2 = 'X' // The symbol we will use for player 2



   // The board will be represented by a 2D list
   // (a list of rows, with each row being itself a list)
   board = []
   board[0] = [space, space, space]
   board[1] = [space, space, space]
   board[2] = [space, space, space]

   // Take a board variable called b and print out a nice text
   // representation of the board.
   // The board variable should be a 2D list with each element containing space, 'X' or 'O'
   function printboard(b) {
      var position1 = board[0][0]
      var position2 = board[0][1]
      var position3 = board[0][2]
      var position4 = board[1][0]
      var position5 = board[1][1]
      var position6 = board[1][2]
      var position7 = board[2][0]
      var position8 = board[2][1]
      var position9 = board[2][2]

      document.getElementById("position1").innerHTML = position1;
      document.getElementById("position2").innerHTML = position2;
      document.getElementById("position3").innerHTML = position3;
      document.getElementById("position4").innerHTML = position4;
      document.getElementById("position5").innerHTML = position5;
      document.getElementById("position6").innerHTML = position6;
      document.getElementById("position7").innerHTML = position7;
      document.getElementById("position8").innerHTML = position8;
      document.getElementById("position9").innerHTML = position9;
  }

   // Take a player type (X or O) and play a single move on the board
   // for that player. 
   // At the moment this just prompts the player for their move and 
   // then makes the move if possible. The move is lost if they try
   // an already occupied position. At the moment no attempt it made
   // to validate the input
   function playMove(player) {
      ("Your turn player", player)
      row = parseInt(prompt("enter row:"))
      col = parseInt(prompt("enter column:"))
      if (board[row][col] === player1 || board[row][col] === player2) {
          ("Already taken! Move skipped")
      } else {
          board[row][col] = player
      }
  }



   // Take a board (2D list of space, 'X' or 'O') and a player marker
   // Returns True if that player has won on the board, and False if they
   // have not
   function checkWin(b, player) {

      // Check each row
      for (var r = 0; r < b.length; r = r + 1) {
          row = b[r];
          if (row[0] === player && row[1] === player && row[2] === player) {
              return true
          }
      }

      // Check diagonals
      if (b[0][0] === player && b[1][1] === player && b[2][2] === player) {
          return true
      }
      // Todo: second diagonal


      // Check columns
      for (var col = 0; col < 3; col = col + 1) {
          if (b[0][col] === player && b[1][col] === player && b[2][col] === player) {
              return true
          }
      }

      // If we have not already returned by now then there was no win position.
      return false
  }

   function play_game() {
      printboard(board)

      while (win === false) {


          playMove(player1)
          printboard(board)
          win = checkWin(board, player1)

          if (!win) {
              playMove(player2)
              printboard(board)
              win = checkWin(board, player2)
          }
      }


      document.write("Game over")
  }