Esempio n. 1
0
    private void place_Battleship(BattleShip btshp,int ship_Size)
    {
        Vector2 vc;

        int random_x_value=0;
        int random_y_value=0;

        //int last_placed_x = 0;
        //int last_placed_y = 0;

        int initial_y = 0;
        int initial_x = 0;

        int i=0;
        int temp_calc;

        //placing first loc of AI ship
        random_y_value = Random.Range (0, 10);
        random_x_value = Random.Range (0, 10);

        vc = new Vector2 (random_x_value, random_y_value);

        if (btshp.Set_Loc(vc))
        {
            //location ok - increase counter
            initial_y = random_y_value;
            initial_x = random_x_value;
            //last_placed_y = initial_y;
        //		last_placed_x = random_x_value;
        //	Debug.Log("Placed AI ship at X" + vc.x + " Y" + vc.y);
            i++;
        }

        //Debug.Log ("continuing creation of AI's ship");

        //placing ship with 4 squars
        while (i<ship_Size)
        {
            // generating location for ship
            // if condition occurs, then Y axis is contant and X values will be randomly generated.
            if (initial_y>initial_x)
            {
                temp_calc = initial_x - (ship_Size - (ship_Size - i));
                do {
                    random_x_value = Random.Range (temp_calc, ((initial_x+(ship_Size-1))-(i-1)));
                } while ((random_x_value>=10)||(random_x_value<0));

                vc = new Vector2 (random_x_value, initial_y);

                if (btshp.Set_Loc(vc))
                {
                    //location ok - increase counter
                    //last_placed_x = random_x_value;
            //		Debug.Log("Placed AI ship at X" + vc.x + " Y" + vc.y);
                    i++;
                }
            }
            //X axis is contant and Y values will be randomly generated.
            else
            {
                temp_calc = initial_y - (ship_Size - (ship_Size - i));
                do {
                        random_y_value = Random.Range (temp_calc, ((initial_y+(ship_Size-1))-(i-1)));
                } while ((random_y_value>=10)||(random_y_value<0));

                vc = new Vector2 (initial_x, random_y_value);

                if (btshp.Set_Loc(vc))
                {
                    //location ok - increase counter
                    //last_placed_y = random_y_value;
                //	Debug.Log("Placed AI ship at X" + vc.x + " Y" + vc.y);
                    i++;
                }
            }
        }
    }