License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

/*
 * Board.fx
 *
 */

package sl;

import javafx.fxd.FXDLoader;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.util.Math;
import sl.Board.Squ;
import sl.Main;
import sl.Player;
import javafx.scene.transform.Scale;


/**
 * @author JavaFX Samples Team
 */


public var dim  = bind Math.min(Main.sceneheight - Main.sceneheight /10 - Main.sceneheight/20, Main.scenewidth - Main.scenewidth /10 - Main.scenewidth/20);//600;
public var sqSide =  bind dim / 10;

var fxdContent:Node ;

public var shiftwidth = bind Math.max(0, (Main.scenewidth - dim - sqSide )/2);

public var shifty = bind (sqSide * 3/4) + Math.max(0, (Main.sceneheight - dim - sqSide )/2);

//Class Board
public class Board extends CustomNode {
    
    
    public var blocks: Squ[];
    public var players: Player[];
    
    public var g: Group;
    public override function create(): Node {
        
            fxdContent = FXDLoader.load(
                "{__DIR__}Snakes_Ladders_Game_Board.fxz",
                //"{__DIR__}SnakesLaddersGameTitleCloseControl.fxz"
                
                ); // loads the content

            fxdContent.cache= true;
          
            doLayout1();

            var g1:Group = Group{
                transforms: [
                    Scale {
                        x : bind dim/653 
                        y : bind dim/684
                        pivotX:0
                        pivotY:0
                    }
                ]
                cache:true
                translateX:bind sqSide + shiftwidth;
                translateY:bind shifty;
                content:[
                    fxdContent
                ]
            };
       g = Group {
            content: [
                g1,
                //add the sqaures of the board
                //now this is only for logic, no GUI
                blocks,
                //add the player coins to the board
                players
            ]
        };
            return g;
    }//end create
    //doing the board layout with the sqaures 1 - 100
    function doLayout1(){
            var count = 0;
            var sq;
            sq = Squ{
                xPos: bind 0  + shiftwidth
                yPos: bind  dim - sqSide + shifty
                i: count

            };
            insert sq into blocks;
            count = count + 1;

            for(row in [0..9]) {
                
                var y =  bind dim - (sqSide + row * sqSide) + shifty;
                
                for (col in [0..9]){
                    //rows going forward LEFT -> RIGHT
                    if (row mod 2 == 0) {
                        sq = Squ{
                            xPos: bind col * sqSide  + sqSide  + shiftwidth;
                            yPos: bind y
                            i: count
                        }
                    }
                    else {
                        //rows going backward LEFT <- RIGHT
                        sq = Squ{
                            xPos: bind dim - ( col * sqSide)  + shiftwidth;
                            yPos: bind y
                            i: count
                        }
                    }//end else
                    insert sq into blocks;
                    count = count + 1
            }//end each col for loop
            }//end each row for loop
            for (cs:Integer in [ 0..99 ]) {
                //check if there is a ladder or snake here
                var moveStep:Integer = Main.gd.moves[ cs ];
                if (moveStep != cs){
                    var sq1:Squ = blocks[ cs ];
                    sq1.start = true;
                    sq1.stop = moveStep;
                }//end if
            }//end for creating snakes and ladders
            //make the player coins
            var player1 = Player{
                idd: 0;
                name: "Player 1";
                col: Color.RED;
                currentsquare: 0;
            };//end player 1

            insert player1 into players;

            var player2 = Player{

                idd: 1;
                name: "Player 2";
                col: Color.DARKORANGE;
                currentsquare: 0;
            };//end player 2
            insert player2 into players;
        }
}//end board
//Class Squ for each square
public class Squ extends CustomNode {
    //global square number on the board
    var i:Integer;
    //indicate the number of the square
    var sqno = String.valueOf(i);
    //graphical pixel coordinates important to move the player coin around
    public var xPos:Number;
    public var yPos:Number;
    //indicate start of either ladder or snake
    public var start:Boolean;
    //indicate the end of the above, is valid only if start is true
    public var stop:Integer;
    public override function create():Node {
        return Group {
            content: [
                ]//end content
        }//end Group
    }//end create function
}//end class Squ