public static HerugolfGameState CreateRandomGolfGrid(int width, int height) { HerugolfGameState game = new HerugolfGameState(width, height); int numberOfHoles = 0; for (int xi = 0; xi < width; xi++) { for (int yi = 0; yi < height; yi++) { int r = Random.Range(0, 10); if (r < 1) { game.gridArray[xi, yi] = new GolfCell(xi, yi, CellType.HOLE_EMPTY); numberOfHoles++; } else if (r < 3) { game.gridArray[xi, yi] = new GolfCell(xi, yi, CellType.BUNKER); } else { game.gridArray[xi, yi] = new GolfCell(xi, yi, CellType.GREEN); } } } int numberOfBalls = 0; while (numberOfBalls < numberOfHoles) { int ballPosX = Random.Range(0, Mathf.RoundToInt(game.gridDimensions.x)); int ballPosY = Random.Range(0, Mathf.RoundToInt(game.gridDimensions.y)); Ball ball = new Ball(ballPosX, ballPosY, Random.Range(1, 6)); if (game.GetCell(ball.positionOnGridX, ball.positionOnGridY).type.Equals(CellType.GREEN)) { bool alreadyABallAtThisPosition = false; foreach (Ball otherBall in game.balls) { alreadyABallAtThisPosition |= ((ballPosX == otherBall.positionOnGridX) && (ballPosY == otherBall.positionOnGridY)); } if (!alreadyABallAtThisPosition) { game.balls.Add(ball); numberOfBalls++; } } } return(game); }
public void BuildGrid(HerugolfGameState gridState) { gridWidth = Mathf.RoundToInt(gridState.gridDimensions.x); gridHeight = Mathf.RoundToInt(gridState.gridDimensions.y); displayedGrid = new GameObject[gridWidth, gridHeight]; for (int xi = 0; xi < gridState.gridDimensions.x; xi++) { for (int yi = 0; yi < gridState.gridDimensions.y; yi++) { GolfCell cellData = gridState.GetCell(xi, yi); displayedGrid[xi, yi] = BuildSquare(xi, yi, cellData); } } foreach (Ball ball in gridState.balls) { AddBall(ball); } }