Flex Mobile 4.6 TIC TAC TOE计算机移动 [英] Flex Mobile 4.6 TIC TAC TOE Computer Move

查看:71
本文介绍了Flex Mobile 4.6 TIC TAC TOE计算机移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flex 4.6移动问题.屏幕上有9个框.当我单击第一个框(使用ID名称命名)时,它应该放置文本 X .我想要的是计算机通过插入 O 在其余的一个框中生成下一步动作.我相信您应该使用math.random还是数组?这是一个Tic Tac Toe游戏.

I have a Flex 4.6 mobile problem. I have 9 boxes on the screen. When I click on the first box (named using an ID name), it should place the text X. What I want is for the computer to generate the next move in one of the remaining boxes by inserting an O. I believe you should use math.random or an array? It's for a Tic Tac Toe game.

任何帮助将不胜感激.

谢谢.

推荐答案

我在html/javascript中做到了这一点,是使用minimax AI的井字游戏的示例,希望它可以作为Flex的参考实现项目(要进行测试,只需将代码复制并粘贴到空白html文档中,然后使用您喜欢的浏览器运行它即可):

I made this in html/javascript, is an example of a tic-tac-toe game using minimax AI, hope it helps as a reference implementation for your flex project (to test it, just copy and paste the code in blank html document and run it with your favorite browser):

<html>
<head>
<script type="text/javascript">
<!-- 
 var board = new Array();
 var CPU = 1;
 var USER = 2;
 var EMPTY = 0;
 var lockBoard = false;
 var X = "X";
 var O = "O";
 var MAX_DEPTH = 10;

 // Updates the HTML table using the "board" array 
 function updateHTMLTable() {
    for (i = 0; i < 3; i++)
       for (j = 0; j < 3; j++) {
          var cell = document.getElementById(i + "," + j);
          switch (board[i * 3 + j]) {
             case USER: cell.innerHTML = X; break;
             case CPU:  cell.innerHTML = O; break;
             default: cell.innerHTML = " "; break;
          }
       }
 }

 function makeUserMove(x, y) {
    // Check either cpu is playing or game is over
    if (lockBoard) return;

    // Check if the cell is free to place the player movement
    if (board[x * 3 + y] == EMPTY) {
       board[x * 3 + y] = USER;
       c = document.getElementById(x + "," + y);
       c.innerHTML = X;
    } else {
       alert("Move not allowed, is already filled in");
    }
    if (wins(USER, board)) {
       alert("You Win");
       lockBoard = true;
       return;
    }
    cpuPlay();
 }

 function startGame() {
    for (i = 0; i < 9; i++)
        board[i] = EMPTY;
    updateHTMLTable();
    lockBoard = false;
 }

 // Check if there is no space left on the board to play 
 function isBoardFull(b) {
    for (var i = 0; i < 9; i++)
       if (b[i] == EMPTY) return false;
    return true;
 }

 function simulateUserMove(board, depth) {
    if (wins(CPU, board)) return {value: 1, board: board};
    if (wins(USER, board)) return {value: -1, board: board};
    if (isBoardFull(board) || depth == MAX_DEPTH) return {value: 0, board: board};

    var userMovement = {value: 999999, board: undefined};
    for (var i = 0; i < 9; i++) {
       if (board[i] == EMPTY) {
          var boardCopy = board.slice(0); // creates a copy of the array
          boardCopy[i] = USER;

          var cpuMovement = cpuMove(boardCopy, depth + 1);

          if (cpuMovement.value < userMovement.value) { 
             userMovement.value = cpuMovement.value;
             userMovement.board = boardCopy;
          } 
       }  
    }
    return userMovement;
 }

 function cpuMove(board, depth) { 
    if (wins(CPU, board)) return {value: 1, board: board};
    if (wins(USER, board)) return {value: -1, board: board};
    if (isBoardFull(board) || depth == MAX_DEPTH) return {value: 0, board: board};

    var cpuMovement = {value: -999999, board: undefined};
    for (var i = 0; i < 9; i++) {
       if (board[i] == EMPTY) {
          var boardCopy = board.slice(0); // creates a copy of the array
          boardCopy[i] = CPU;

          var userMovement = simulateUserMove(boardCopy, depth + 1);

          if (userMovement.value > cpuMovement.value) { 
             cpuMovement.value = userMovement.value;
             cpuMovement.board = boardCopy;
          } 
       }  
    }
    return cpuMovement;
 }

 // This function make the cpu movement using a "kind of" greedy approach, is not very smart 
 function cpuPlay() {
    lockBoard = true;
    var move = cpuMove(board, 0);
    board = move.board;
    updateHTMLTable();

    if (wins(CPU, board))
       alert("I win!"); 
    else 
       lockBoard = false;
 }

 function wins(player, board) {
    // Iterate the board to find if the player identified by the parameter "player" winned

    // First, check rows 
    var enemy = (player == USER) ? CPU : USER;
    var won = false;
    for (var i = 0; i < 3; i++) {
       won = true;
       for (var j = 0; j < 3; j++) {
          if (board[i * 3 + j] == enemy || board[i * 3 + j] == EMPTY) {
             won = false;
             break;
          }
       }
       if (won) return true;
    }
    // Then, check columns
    for (var i = 0; i < 3; i++) {
       won = true;
       for (var j = 0; j < 3; j++) {
          if (board[j * 3 + i] == enemy || board[j * 3 + i] == EMPTY) {
             won = false;
             break;
          }
       }
       if (won) return true;
    }
    // Last, the 2 diagonals
    for (var i = 0; i < 9; i += 4) {
       won = true;
       if (board[i] == enemy || board[i] == EMPTY) {
          won = false;
          break;
       }
    }
    if (won) return true;
    for (var i = 2; i < 7; i += 2) {
       won = true;
       if (board[i] == enemy || board[i] == EMPTY) {
          won = false;
          break;
       }
    }
    return won;
 }
//-->
</script>

</head>
<body>
  <table align="center" border="1">
    <tr>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(0, 0)" id="0,0">&nbsp;</td>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(0, 1)" id="0,1">&nbsp;</td>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(0, 2)" id="0,2">&nbsp;</td>
    </tr>
    <tr>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(1, 0)" id="1,0">&nbsp;</td>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(1, 1)" id="1,1">&nbsp;</td>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(1, 2)" id="1,2">&nbsp;</td>
    </tr>
    <tr>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(2, 0)" id="2,0">&nbsp;</td>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(2, 1)" id="2,1">&nbsp;</td>
       <td align="center" valign="middle" width="20" height="20" onclick="javascript: makeUserMove(2, 2)" id="2,2">&nbsp;</td>
    </tr>
  </table><br>
  <p align="center">
     <input type="button" value="Play Again!" onclick="javascript: startGame()">
  </p>

  <script type="text/javascript"> 
     // start game
     startGame();
  </script>
</body>
</html>

这篇关于Flex Mobile 4.6 TIC TAC TOE计算机移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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