示例#1
0
    /*
    public void NewGame(Size size)
    {
        width = size.Width;
        height = size.Height;
        state = new State[width, height];
        left = new int[width, height];
        right = new int[width, height];
        below = new int[width, height];
        above = new int[width, height];
        space = new int[width, height];
        hits = new int[width, height];
        remainingShips = new List<int>();
        mustExplore = new List<Point>();
    }

    // nothing fancy, just random placement.  Also note which ships we have available.
    public void PlaceShips(ReadOnlyCollection<Ship> ships)
    {
        remainingShips.Clear();
        foreach (Ship s in ships)
        {
            remainingShips.Add(s.Length);
            s.Place(
                new Point(
                    rand.Next(width),
                    rand.Next(height)),
                (ShipOrientation)rand.Next(2),new Size(10,10));
        }
    }*/
    public System.Collections.IEnumerator PlaceShip(Ship ship)
    {
        yield return null;// return new WaitForSeconds(0.5f);

        if (ship != null)
        {
            int tries = 0;
            bool success = false;
            while (!success)
            {
                if (tries > 100) break; // no infinite loops

                if (!ship.Place(new Point(rand.Next(this.gameSize.Width), rand.Next(this.gameSize.Height)),
                            (ShipOrientation)rand.Next(2), gameSize))
                    continue;

                remainingShips.Add(ship.Length);

                success = true;
                foreach (Ship s in mShips)
                {
                    if(s != ship)
                        success &= !ship.ConflictsWith(s);
                }

                tries++;
            }

            if (!success) throw new OperationCanceledException("PlaceShip");

            if (mPlayer)
            {
                /*foreach (Point p in ship.GetAllLocations())
                {
                    Init_board.gameGrid[p.X, p.Y].renderer.material.color = Color.green;
                }*/
                ship.LoadModel();
                ship.ShowModel();
            }

        }

        waitingForRoutine = false;
        curship++;
    }
    public System.Collections.IEnumerator PlaceShip(Ship ship)
    {
        bool placed = false;
        bool valid = false;

        if (ship != null)
        {
            ship.LoadModel();
            ship.HideModel();

            while (!placed)
            {
                yield return null;

                if (Input.GetKeyDown("r"))
                {
                    if(placeOrientation == ShipOrientation.Horizontal)
                        placeOrientation = ShipOrientation.Vertical;
                    else placeOrientation = ShipOrientation.Horizontal;
                }

                if (Input.GetMouseButtonDown(0) && valid)
                {
                    placed = true;
                    break;
                }

                Point cast = getBoardRayCast(false, Color.white);

                if (ship.Place(cast, placeOrientation, gameSize))
                {
                    bool collides = false;
                    foreach (Ship s in mShips)
                    {
                        if (s != ship)
                            collides |= ship.ConflictsWith(s);
                    }

                    if (!collides)
                    {
                        ship.ShowModel();
                        valid = true;
                    }
                    else
                    {
                        ship.HideModel();
                        valid = false;
                    }
                }
                else
                {
                    ship.HideModel();
                    valid = false;
                }
            }
        }

        waitingForRoutine = false;
        curship++;
    }