我的功能一直没有运行

时间:2013-12-27 12:29:47

标签: javascript

我想总是运行checkDouble(),因为我可以决定是否应该调用changeTurn()。它工作正常,直到我添加checkType()。我想总是运行checkDouble(),因为这样我可以决定是否应该调用changeTurn()。它工作正常,直到我添加了checkType()。

HTML

<div class="board">

    <div class="col1">
               <?php testdrawBoard("-1","40","29"); ?>                                     
    </div>

    <div class="col2">
        <div class="top">
                <?php testdrawBoard("1","1","10"); ?>
        </div>                      
        <div class="center">
        <div class="dicewrapper">
        <span class="dice1" id="dice1"></span>
        <span class="dice2" id="dice2"></span>
        </div>
        </div>
        <div class="bottom">
               <?php testdrawBoard("-1","29","20"); ?>
        </div>
    </div>       

    <div class="col3">
                <?php testdrawBoard("1","10","21"); ?>
    </div>

</div>

<button id="roll1" onclick="playgame()">Player 1 Roll</button><button id="roll2" onclick="playgame()">Player 2 Roll</button>
<div id="debug1"></div>
<div id="debug2"></div>
<div id="debug3"></div>
<div id="debug4"></div>  
<div id="debug5"></div>  
<div id="debug6"></div>

JS

        var doubleCount = 0;
        var poslocation=["","Cairo","Community Chest","Vienna","Income Tax","Schiphol Airport","Brussels","Chance","Oslo","Zurich","Jailhouse",
        "Amsterdam","Electro","Bangkok","Istanbul","de Gaulle Airport", "Hong Kong", "Chest", "Madrid", "Sydney","Coffee House",
        "Toronto","Chance","Mumbai","Rome","Heathrow Airport","Rio de Janeiro","Tokyo","Nuclear Power Plant","Paris",
        "Thuglife", "Berlin","Bejing","Community Chest","Moscow","John F Kennedy Airport","Chance","New York","Super Tax","London", "Go"];
        playerturn=1;  createPlayers(); createPositions();   
function playgame(){

        rollDice(); //two dice rolls

        drawDice(); //displays the dice

        setPosition(); //update player position 

        movePiece();

        checkType();
        alert("going to check double");

        checkDouble();
}
function rollDice()
{
        var d1 = Math.floor(Math.random() * ((6-1)+1) + 1);
        var d2 = Math.floor(Math.random() * ((6-1)+1) + 1);
        dice1=d1;
        dice2=d2;
        dicetotal=dice1+dice2;
        document.getElementById("debug1").innerHTML= "Dice roll was:" + dice1 + "," + dice2;
}
function drawDice()
{
        $('.dice1').attr('id',  "dice" + dice1);
        $('.dice2').attr('id', "dice" + dice2);
}
function setPosition()
{
        currentpos =  players['player'+playerturn].currentpos;
        startpos =    players['player'+playerturn].startpos;
        prevpos =       players['player'+playerturn].prevpos;
        currentpos = startpos + dicetotal;
        prevpos = currentpos - dicetotal;
        players['player'+playerturn].currentpos = currentpos;
        players['player'+playerturn].prevpos = prevpos; 

        if(players['player'+playerturn].currentpos>40){
        players['player'+playerturn].currentpos = players['player'+playerturn].currentpos - 40;
        players['player'+playerturn].balance = players['player'+playerturn].balance + 200;
        }
        players['player'+playerturn].startpos = players['player'+playerturn].currentpos;
}
function movePiece()
{      

         var z = players['player'+playerturn].currentpos - dicetotal;
         var x = "#piece"+playerturn+"pos"+players['player'+playerturn].currentpos; 
         var y = "#piece"+playerturn+"pos"+players['player'+playerturn].prevpos;
         $(x).slideToggle();
         $(y).slideToggle();
}
function checkType(){
        var currentpos = players['player'+playerturn].currentpos;
        var x = positions['position'+currentpos].forsale;
        if(x=="y"){
        checkOwner();      
        }

}
function checkOwner(){


        var currentpos = players['player'+playerturn].currentpos;
        var owner = positions['position'+currentpos].owner;
        if(owner=="unowned"){
        assignOwner();
        }
        else{
        payrent();
        }
}
function payTax(){

        type = positions['position'+currentpos].type;
        if(type=="tax"){

        tax = positions['position'+currentpos].tax;
        players['player'+playerturn].balance = players['player'+playerturn].balance - tax;

        }
}
function assignOwner()
{      
         var currentpos = players['player'+playerturn].currentpos;
         players['player'+playerturn].balance = players['player'+playerturn].balance - positions['position'+players['player'+playerturn].currentpos].purchaseprice;
         positions['position'+currentpos].owner = "player" + playerturn;

}
function payrent()
{      
         if ( positions['position'+currentpos].type=="utility"){

         price = dicetotal * 4;
         players['player'+playerturn].balance = players['player'+playerturn].balance - price;

         }

         var currentpos = players['player'+playerturn].currentpos;
         players['player'+playerturn].balance = players['player'+playerturn].balance - positions['position'+currentpos].rentprice;

}
function checkDouble()
{
        if (dice1 == dice2) {                                                                    
        doubleCount++;
        if (doubleCount == 3)                                                             
        {
           alert("Thuglife");
        }
        alert("Not going to change players because there is a double alright mate!");
        }
        else if (dice1 != dice2){
        alert("Going to change players now!")
        changeTurn();
        doubleCount = 0;
        }
}
function changeTurn()
{

      if(playerturn==1) { 
      playerturn = 2 ; 
      $("#roll1").toggle();
      $("#roll2").toggle();
      }
      else{ 
      playerturn = 1;
      $("#roll2").toggle(); 
      $("#roll1").toggle();
      }
}

function createPlayers(){

    players = {

    player1:{currentpos:  0,prevpos: 0,startpos: 0,balance: 1500},
    player2:{currentpos:  0,prevpos: 0,startpos: 0,balance: 1500}
              };
}
function createPositions(){
    positions = {
    position1:{title:  "Cairo",type: "brown",owner: "unowned",purchaseprice: 60,rentprice: 2,forsale: "y"},
      position2:{title:  "Community Chest",type: "card",forsale: "n"},
    position3:{title:  "Vienna",type: "brown",owner: "unowned",purchaseprice: 60,rentprice: 4,forsale: "y"},
      position4:{title:  "Income Tax",type: "tax",tax: 200},
    position5:{title:  "Schiphol Airport",type: "airport",owner: "",purchaseprice: 200,rentprice: 25,forsale: "y"},
    position6:{title:  "Brussels",type: "blue",owner: "unowned",purchaseprice: 100,rentprice: 6,forsale: "y"},
     position7:{title:  "Chance",type: "card",forsale: "n"},
    position8:{title:  "Oslo",type: "blue",owner: "unowned",purchaseprice: 100,rentprice: 6,forsale: "y"},
    position9:{title:  "Zurich",type: "blue",owner: "unowned",purchaseprice: 120,rentprice: 8,forsale: "y"},
      position10:{title:  "Jailhouse",type: "jail",forsale: "n"},
    position11:{title:  "Amsterdam",type: "pink",owner: "unowned",purchaseprice: 140,rentprice: 10,forsale: "y"},
      position12:{title:  "Electro",type: "utility",owner: "unowned",purchaseprice: 150,forsale: "y"},
    position13:{title:  "Bangkok",type: "pink",owner: "unowned",purchaseprice: 140,rentprice: 10,forsale: "y"},
    position14:{title:  "Istanbul",type: "pink",owner: "unowned",purchaseprice: 160,rentprice: 12,forsale: "y"},
    position15:{title:  "Charles de Gaulle Airport",type: "airport",owner: "",purchaseprice: 200,rentprice: 25,forsale: "y"},
    position16:{title:  "Hong Kong",type: "orange",owner: "unowned",purchaseprice: 180,rentprice: 14,forsale: "y"},
      position17:{title:  "Community Chest",type: "card",forsale: "n"},
    position18:{title:  "Madrid",type: "orange",owner: "unowned",purchaseprice: 180,rentprice: 14,forsale: "y"},
    position19:{title:  "Sydney",type: "orange",owner: "unowned",purchaseprice: 200,rentprice: 16,forsale: "y"},
      position20:{title:  "Coffee House",type: "coffee",forsale: "n"}, 
    position21:{title:  "Toronto",type: "red",owner: "unowned",purchaseprice: 220,rentprice: 18,forsale: "y"},
      position22:{title:  "Chance",type: "card",forsale: "n"},
    position23:{title:  "Mumbai",type: "red",owner: "unowned",purchaseprice: 220,rentprice: 18,forsale: "y"},
    position24:{title:  "Rome",type: "red",owner: "unowned",purchaseprice: 220,rentprice: 20,forsale: "y"},
      position25:{title:  "Heathrow Airport",type: "airport",owner: "",purchaseprice: 200,rentprice: 25,forsale: "y"},
    position26:{title:  "Rio de Janeiro",type: "yellow",owner: "",purchaseprice: 240,rentprice: 22,forsale: "y"},
    position27:{title:  "Tokyo",type: "yellow",owner: "unowned",purchaseprice: 240,rentprice: 22,forsale: "y"},
      position28:{title:  "Nuclear",type: "utility",owner: "unowned",purchaseprice: 150,forsale: "y"},
    position29:{title:  "Paris",type: "yellow",owner: "unowned",purchaseprice: 280,rentprice: 24,forsale: "y"},
      position30:{title:  "Go to Jail",type: "jail",forsale: "n"},
    position31:{title:  "Berlin",type: "green",owner: "unowned",purchaseprice: 300,rentprice: 26,forsale: "y"},
    position32:{title:  "Bejing",type: "green",owner: "unowned",purchaseprice: 300,rentprice: 26,forsale: "y"}, 
      position33:{title:  "Community Chest",type: "card",forsale: "n"},
    position34:{title:  "Moscow",type: "green",owner: "unowned",purchaseprice: 320,rentprice: 28,forsale: "y"},
    position35:{title:  "John F Kennedy Airport",type: "airport",owner: "unowned",purchaseprice: 200,rentprice: 25,forsale: "y"},
      position36:{title:  "Chance",type: "card",forsale: "n"},
    position37:{title:  "New York",type: "navy",owner: "unowned",purchaseprice: 350,rentprice: 35,forsale: "y"},
      position38:{title:  "Super Tax",type: "tax",tax: 100,forsale: "n"}, 
    position39:{title:  "London",type: "navy",owner: "unowned",purchaseprice: 350,rentprice: 50,forsale: "y"}
              };
} 
function debugview(){
        document.getElementById("debug1").innerHTML= "Currently player 1 is at " + poslocation[players['player1'].currentpos] + " which is owned by: " + positions['position'+players['player1'].currentpos].owner;
        document.getElementById("debug2").innerHTML= "Currently player 2 is at " + poslocation[players['player2'].currentpos]+ " which is owned by: " + positions['position'+players['player2'].currentpos].owner;
        document.getElementById("debug3").innerHTML= "Player 1 balance is: $" + players['player1'].balance;
        document.getElementById("debug4").innerHTML= "Player 2 balance is: $" + players['player2'].balance;
        //document.getElementById("debug6").innerHTML= "Dice roll was:" + dice1 + "," + dice2;
        }             

0 个答案:

没有答案