Asignación de números a una matriz sin usar un bucle -- performance campo con ruby campo con array camp codereview Relacionados El problema

Assigning numbers to an array without using a loop


-1
vote

problema

Español

La tarea es asignar los números package com.thecodingwombat.screens; public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel; public TitleScreen(Game game, AssetManager assetManager) { this.game = game; this.assetManager = assetManager; this.titleScreen = this; stage = new Stage(new ScreenViewport()); font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class); Texture board3 = assetManager.get("board_preview_3.png", Texture.class); Texture board4 = assetManager.get("board_preview_4.png", Texture.class); Texture board5 = assetManager.get("board_preview_5.png", Texture.class); Texture board6 = assetManager.get("board_preview_6.png", Texture.class); Texture board8 = assetManager.get("board_preview_8.png", Texture.class); createTitle(); createButtons(); boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"}; boardSizes = new int[] {3, 4, 5, 6, 8}; gameScreens = new GameScreen[5]; boardPreviews = new Texture[] {board3, board4, board5, board6, board8}; board = new Image(boardPreviews[1]); board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300); stage.addActor(board); boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f))); boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight()); boardDescriptionLabel.setAlignment(Align.center); stage.addActor(boardDescriptionLabel); } private void createButtons() { TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle(); button1Style.font = font; button1Style.fontColor = new Color(.2f, .2f, .2f, 1f); layout.setText(font, "Start Game"); TextButton play = new TextButton("Start Game", button1Style); Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class)); Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class)); play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2); leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); stage.addActor(play); stage.addActor(leftButton); stage.addActor(rightButton); play.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (gameScreens[currentBoardIndex] == null) gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]); game.setScreen(gameScreens[currentBoardIndex]); gameScreens[currentBoardIndex].setFontSize(); return true; } }); leftButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 0) currentBoardIndex = 4; else currentBoardIndex--; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); rightButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 4) currentBoardIndex = 0; else currentBoardIndex++; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); } private void createTitle() { Label.LabelStyle label1Style = new Label.LabelStyle(); label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class); label1Style.fontColor = new Color(.2f, .2f, .2f, 1f); Label title = new Label("2048", label1Style); title.setAlignment(Align.center); title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0); stage.addActor(title); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } } 12 Dados como la entrada por el usuario en un package com.thecodingwombat.screens; public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel; public TitleScreen(Game game, AssetManager assetManager) { this.game = game; this.assetManager = assetManager; this.titleScreen = this; stage = new Stage(new ScreenViewport()); font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class); Texture board3 = assetManager.get("board_preview_3.png", Texture.class); Texture board4 = assetManager.get("board_preview_4.png", Texture.class); Texture board5 = assetManager.get("board_preview_5.png", Texture.class); Texture board6 = assetManager.get("board_preview_6.png", Texture.class); Texture board8 = assetManager.get("board_preview_8.png", Texture.class); createTitle(); createButtons(); boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"}; boardSizes = new int[] {3, 4, 5, 6, 8}; gameScreens = new GameScreen[5]; boardPreviews = new Texture[] {board3, board4, board5, board6, board8}; board = new Image(boardPreviews[1]); board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300); stage.addActor(board); boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f))); boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight()); boardDescriptionLabel.setAlignment(Align.center); stage.addActor(boardDescriptionLabel); } private void createButtons() { TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle(); button1Style.font = font; button1Style.fontColor = new Color(.2f, .2f, .2f, 1f); layout.setText(font, "Start Game"); TextButton play = new TextButton("Start Game", button1Style); Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class)); Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class)); play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2); leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); stage.addActor(play); stage.addActor(leftButton); stage.addActor(rightButton); play.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (gameScreens[currentBoardIndex] == null) gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]); game.setScreen(gameScreens[currentBoardIndex]); gameScreens[currentBoardIndex].setFontSize(); return true; } }); leftButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 0) currentBoardIndex = 4; else currentBoardIndex--; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); rightButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 4) currentBoardIndex = 0; else currentBoardIndex++; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); } private void createTitle() { Label.LabelStyle label1Style = new Label.LabelStyle(); label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class); label1Style.fontColor = new Color(.2f, .2f, .2f, 1f); Label title = new Label("2048", label1Style); title.setAlignment(Align.center); title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0); stage.addActor(title); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } } 3 . Para EX: Si el usuario da 10 como entrada, la matriz generada debe ser así,

  package com.thecodingwombat.screens;  public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel;  public TitleScreen(Game game, AssetManager assetManager) {     this.game = game;     this.assetManager = assetManager;     this.titleScreen = this;     stage = new Stage(new ScreenViewport());      font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class);     Texture board3 = assetManager.get("board_preview_3.png", Texture.class);     Texture board4 = assetManager.get("board_preview_4.png", Texture.class);     Texture board5 = assetManager.get("board_preview_5.png", Texture.class);     Texture board6 = assetManager.get("board_preview_6.png", Texture.class);     Texture board8 = assetManager.get("board_preview_8.png", Texture.class);      createTitle();     createButtons();      boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"};     boardSizes = new int[] {3, 4, 5, 6, 8};     gameScreens = new GameScreen[5];     boardPreviews = new Texture[] {board3, board4, board5, board6, board8};      board = new Image(boardPreviews[1]);     board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300);     stage.addActor(board);      boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f)));     boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight());     boardDescriptionLabel.setAlignment(Align.center);      stage.addActor(boardDescriptionLabel); }  private void createButtons() {     TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle();     button1Style.font = font;     button1Style.fontColor = new Color(.2f, .2f, .2f, 1f);      layout.setText(font, "Start Game");      TextButton play = new TextButton("Start Game", button1Style);     Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class));     Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class));      play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2);     leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f);     rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f);      stage.addActor(play);     stage.addActor(leftButton);     stage.addActor(rightButton);      play.addListener(new InputListener() {         @Override         public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {             if (gameScreens[currentBoardIndex] == null)                 gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]);             game.setScreen(gameScreens[currentBoardIndex]);             gameScreens[currentBoardIndex].setFontSize();             return true;         }     });      leftButton.addListener(new InputListener() {         @Override         public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {             if (currentBoardIndex == 0)                 currentBoardIndex = 4;             else                 currentBoardIndex--;             boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]);             board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex])));             return true;         }     });      rightButton.addListener(new InputListener() {         @Override         public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {             if (currentBoardIndex == 4)                 currentBoardIndex = 0;             else                 currentBoardIndex++;             boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]);             board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex])));             return true;         }     }); }  private void createTitle() {     Label.LabelStyle label1Style = new Label.LabelStyle();     label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class);     label1Style.fontColor = new Color(.2f, .2f, .2f, 1f);     Label title = new Label("2048", label1Style);     title.setAlignment(Align.center);     title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0);     stage.addActor(title); }  @Override public void show() {     Gdx.input.setInputProcessor(stage); }  @Override public void render(float delta) {     Gdx.gl.glClearColor(1, 1, 1, 1);     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);     stage.act();     stage.draw(); } } 4  

El código a continuación funciona bien, pero estoy usando un 99887766655443315 para asignar este package com.thecodingwombat.screens; public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel; public TitleScreen(Game game, AssetManager assetManager) { this.game = game; this.assetManager = assetManager; this.titleScreen = this; stage = new Stage(new ScreenViewport()); font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class); Texture board3 = assetManager.get("board_preview_3.png", Texture.class); Texture board4 = assetManager.get("board_preview_4.png", Texture.class); Texture board5 = assetManager.get("board_preview_5.png", Texture.class); Texture board6 = assetManager.get("board_preview_6.png", Texture.class); Texture board8 = assetManager.get("board_preview_8.png", Texture.class); createTitle(); createButtons(); boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"}; boardSizes = new int[] {3, 4, 5, 6, 8}; gameScreens = new GameScreen[5]; boardPreviews = new Texture[] {board3, board4, board5, board6, board8}; board = new Image(boardPreviews[1]); board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300); stage.addActor(board); boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f))); boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight()); boardDescriptionLabel.setAlignment(Align.center); stage.addActor(boardDescriptionLabel); } private void createButtons() { TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle(); button1Style.font = font; button1Style.fontColor = new Color(.2f, .2f, .2f, 1f); layout.setText(font, "Start Game"); TextButton play = new TextButton("Start Game", button1Style); Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class)); Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class)); play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2); leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); stage.addActor(play); stage.addActor(leftButton); stage.addActor(rightButton); play.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (gameScreens[currentBoardIndex] == null) gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]); game.setScreen(gameScreens[currentBoardIndex]); gameScreens[currentBoardIndex].setFontSize(); return true; } }); leftButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 0) currentBoardIndex = 4; else currentBoardIndex--; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); rightButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 4) currentBoardIndex = 0; else currentBoardIndex++; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); } private void createTitle() { Label.LabelStyle label1Style = new Label.LabelStyle(); label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class); label1Style.fontColor = new Color(.2f, .2f, .2f, 1f); Label title = new Label("2048", label1Style); title.setAlignment(Align.center); title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0); stage.addActor(title); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } } 6 a un package com.thecodingwombat.screens; public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel; public TitleScreen(Game game, AssetManager assetManager) { this.game = game; this.assetManager = assetManager; this.titleScreen = this; stage = new Stage(new ScreenViewport()); font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class); Texture board3 = assetManager.get("board_preview_3.png", Texture.class); Texture board4 = assetManager.get("board_preview_4.png", Texture.class); Texture board5 = assetManager.get("board_preview_5.png", Texture.class); Texture board6 = assetManager.get("board_preview_6.png", Texture.class); Texture board8 = assetManager.get("board_preview_8.png", Texture.class); createTitle(); createButtons(); boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"}; boardSizes = new int[] {3, 4, 5, 6, 8}; gameScreens = new GameScreen[5]; boardPreviews = new Texture[] {board3, board4, board5, board6, board8}; board = new Image(boardPreviews[1]); board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300); stage.addActor(board); boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f))); boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight()); boardDescriptionLabel.setAlignment(Align.center); stage.addActor(boardDescriptionLabel); } private void createButtons() { TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle(); button1Style.font = font; button1Style.fontColor = new Color(.2f, .2f, .2f, 1f); layout.setText(font, "Start Game"); TextButton play = new TextButton("Start Game", button1Style); Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class)); Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class)); play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2); leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); stage.addActor(play); stage.addActor(leftButton); stage.addActor(rightButton); play.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (gameScreens[currentBoardIndex] == null) gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]); game.setScreen(gameScreens[currentBoardIndex]); gameScreens[currentBoardIndex].setFontSize(); return true; } }); leftButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 0) currentBoardIndex = 4; else currentBoardIndex--; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); rightButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 4) currentBoardIndex = 0; else currentBoardIndex++; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); } private void createTitle() { Label.LabelStyle label1Style = new Label.LabelStyle(); label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class); label1Style.fontColor = new Color(.2f, .2f, .2f, 1f); Label title = new Label("2048", label1Style); title.setAlignment(Align.center); title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0); stage.addActor(title); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } } 7 que puede probar un montón de package com.thecodingwombat.screens; public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel; public TitleScreen(Game game, AssetManager assetManager) { this.game = game; this.assetManager = assetManager; this.titleScreen = this; stage = new Stage(new ScreenViewport()); font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class); Texture board3 = assetManager.get("board_preview_3.png", Texture.class); Texture board4 = assetManager.get("board_preview_4.png", Texture.class); Texture board5 = assetManager.get("board_preview_5.png", Texture.class); Texture board6 = assetManager.get("board_preview_6.png", Texture.class); Texture board8 = assetManager.get("board_preview_8.png", Texture.class); createTitle(); createButtons(); boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"}; boardSizes = new int[] {3, 4, 5, 6, 8}; gameScreens = new GameScreen[5]; boardPreviews = new Texture[] {board3, board4, board5, board6, board8}; board = new Image(boardPreviews[1]); board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300); stage.addActor(board); boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f))); boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight()); boardDescriptionLabel.setAlignment(Align.center); stage.addActor(boardDescriptionLabel); } private void createButtons() { TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle(); button1Style.font = font; button1Style.fontColor = new Color(.2f, .2f, .2f, 1f); layout.setText(font, "Start Game"); TextButton play = new TextButton("Start Game", button1Style); Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class)); Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class)); play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2); leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); stage.addActor(play); stage.addActor(leftButton); stage.addActor(rightButton); play.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (gameScreens[currentBoardIndex] == null) gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]); game.setScreen(gameScreens[currentBoardIndex]); gameScreens[currentBoardIndex].setFontSize(); return true; } }); leftButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 0) currentBoardIndex = 4; else currentBoardIndex--; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); rightButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 4) currentBoardIndex = 0; else currentBoardIndex++; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); } private void createTitle() { Label.LabelStyle label1Style = new Label.LabelStyle(); label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class); label1Style.fontColor = new Color(.2f, .2f, .2f, 1f); Label title = new Label("2048", label1Style); title.setAlignment(Align.center); title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0); stage.addActor(title); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } } 8 Durante la ejecución si el usuario le da una entrada como package com.thecodingwombat.screens; public class TitleScreen implements Screen { private Stage stage; private GameScreen[] gameScreens; private TitleScreen titleScreen; private Game game; private AssetManager assetManager; private Image board; private GlyphLayout layout = new GlyphLayout(); private String[] boardDescriptions; private Texture[] boardPreviews; private int currentBoardIndex = 1; private int[] boardSizes; private BitmapFont font; private Label boardDescriptionLabel; public TitleScreen(Game game, AssetManager assetManager) { this.game = game; this.assetManager = assetManager; this.titleScreen = this; stage = new Stage(new ScreenViewport()); font = assetManager.get("smallerRegularFont.ttf", BitmapFont.class); Texture board3 = assetManager.get("board_preview_3.png", Texture.class); Texture board4 = assetManager.get("board_preview_4.png", Texture.class); Texture board5 = assetManager.get("board_preview_5.png", Texture.class); Texture board6 = assetManager.get("board_preview_6.png", Texture.class); Texture board8 = assetManager.get("board_preview_8.png", Texture.class); createTitle(); createButtons(); boardDescriptions = new String[] {"Small - 3x3", "Classic - 4x4", "Big - 5x5", "Bigger - 6x6", "Huge - 8x8"}; boardSizes = new int[] {3, 4, 5, 6, 8}; gameScreens = new GameScreen[5]; boardPreviews = new Texture[] {board3, board4, board5, board6, board8}; board = new Image(boardPreviews[1]); board.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 + layout.height * 1.5f, 300, 300); stage.addActor(board); boardDescriptionLabel = new Label(boardDescriptions[1], new Label.LabelStyle(font, new Color(.2f, .2f, .2f, 1f))); boardDescriptionLabel.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 150, TotallyNot2048.SCREEN_HEIGHT / 3, 300, font.getCapHeight()); boardDescriptionLabel.setAlignment(Align.center); stage.addActor(boardDescriptionLabel); } private void createButtons() { TextButton.TextButtonStyle button1Style = new TextButton.TextButtonStyle(); button1Style.font = font; button1Style.fontColor = new Color(.2f, .2f, .2f, 1f); layout.setText(font, "Start Game"); TextButton play = new TextButton("Start Game", button1Style); Image leftButton = new Image(assetManager.get("leftButton.png", Texture.class)); Image rightButton = new Image(assetManager.get("rightButton.png", Texture.class)); play.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - layout.width * .75f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * 3, layout.width * 1.5f, layout.height * 2); leftButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 - 300 / 2 - layout.height * 1.5f, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); rightButton.setBounds(TotallyNot2048.SCREEN_WIDTH / 2 + 300 / 2, TotallyNot2048.SCREEN_HEIGHT / 3 - layout.height * .2f, layout.height * 1.5f, layout.height * 1.5f); stage.addActor(play); stage.addActor(leftButton); stage.addActor(rightButton); play.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (gameScreens[currentBoardIndex] == null) gameScreens[currentBoardIndex] = new GameScreen(game, titleScreen, assetManager, boardSizes[currentBoardIndex]); game.setScreen(gameScreens[currentBoardIndex]); gameScreens[currentBoardIndex].setFontSize(); return true; } }); leftButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 0) currentBoardIndex = 4; else currentBoardIndex--; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); rightButton.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentBoardIndex == 4) currentBoardIndex = 0; else currentBoardIndex++; boardDescriptionLabel.setText(boardDescriptions[currentBoardIndex]); board.setDrawable(new TextureRegionDrawable(new TextureRegion(boardPreviews[currentBoardIndex]))); return true; } }); } private void createTitle() { Label.LabelStyle label1Style = new Label.LabelStyle(); label1Style.font = assetManager.get("bigFont.ttf", BitmapFont.class); label1Style.fontColor = new Color(.2f, .2f, .2f, 1f); Label title = new Label("2048", label1Style); title.setAlignment(Align.center); title.setBounds(0, Gdx.graphics.getHeight() * .85f, Gdx.graphics.getWidth(), 0); stage.addActor(title); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } } 9 .

  degreeType0  

Entonces, ¿cómo debo optimizar este código para un mejor rendimiento?

Original en ingles

The task is to assign the n numbers given as input by the user in an array . For Ex : If user gives 10 as input then the generated array should be like this ,

arr[0] = 0 arr[1] = 1 arr[2] = 2 arr[3] = 3 . . . arr[10] = 10  

The code below works fine but i am using a loop to assign this numbers to an array which can prove a lot of run-time during execution if the user gives an input like 10^6 .

puts "enter the number of times you want to test" times = gets.chomp.to_i  1.upto times do |i|      puts "enter the total number elements in the array ."      no = gets.chomp.to_i      puts "total number of elements are #{no + 1}"      arr = []     sum = 0      0.upto no do |i|          arr[i] = i      end      arr.each_index { |index| print "#{index} " }      sum = arr.reduce(:+)     puts "#{sum}"     end 

So , how should i optimize this code for better performance ?

        
       
       

Lista de respuestas

2
 
vote

No hay necesidad de WHERE ... IN1 Antes de llamar WHERE ... IN2 .

El 99887766555443323 nunca se usa. De hecho, es confuso, porque introduce un 99887766555443324 más tarde. Si todo lo que desea hacer es repetir algún código, use WHERE ... IN5 en lugar de WHERE ... IN6 .

WHERE ... IN7 parece "Sí" / "No". Por convención, WHERE ... IN8 es un buen nombre para un número, si no tiene un nombre mejor para ello.

El código sería más eficiente si no usó ninguna matriz en absoluto. Simplemente use la serie Fórmula:

$$ sum_ {i = 0} ^ n i = dfrac {n (n - 1)} {2} $$

  WHERE ... IN9  
 

There's no need to #chomp before calling #to_i.

The outer i is never used. In fact, it's confusing, because you introduce an inner i later. If all you want to do is repeat some code, use #times instead of #upto.

no looks like "yes"/"no". By convention, n is a good name for a number, if you don't have any better name for it.

The code would be more efficient if you didn't use any array at all. Just use the series formula:

$$\sum_{i=0}^n i = \dfrac{n (n - 1)}{2}$$

puts "enter the number of times you want to test" gets.to_i.times do     puts "enter the total number elements in the array ."     n = gets.to_i     puts "total number of elements are #{n + 1}"      (0..n).each { |i| print "#{i} " }     puts n * (n - 1) / 2 end 
 
 
         
         

Relacionados problema

2  Mejora de la función que compara dos cadenas  ( Improving function that compares two strings ) 
Estoy aprendiendo C y he hecho esta función muy simple para comparar dos cuerdas. Me gustaría saber cómo se puede mejorar: int match(char* string1, char* s...

6  Buscar número de más en una matriz 2D  ( Find number of plus in a 2d array ) 
Problema Chargrid El CharGrid La clase encapsula una matriz de 2-D con una pareja de operaciones. INT APULUS () Busque un patrón '+' en la cuadrícu...

2  Devuelve verdadero si los elementos de una matriz no contienen uno u otro  ( Return true if the elements of an array do not contain one or the other ) 
Estoy completando gradualmente los ejercicios de codificación para Java. Aquí está el uno acabo de hacer: Dada una matriz de INTS, devuelva verdadera si ...

5  Encuentra el primer número único de la matriz  ( Find first unique number from array ) 
Se me pidió que creara una función que devuelva el primer entero único de la matriz de enteros. En un caso de éxito, la función devuelve el número único en un...

2  Cargando imágenes en el fondo para una vista de tabla  ( Loading images in the background for a table view ) 
Tengo fetchData2 MÉTODO QUE LLENA MI imagesArray : func fetchData(){ let imageQuery = PFUser.query() imageQuery?.whereKey("username", contained...

6  Palindrome más largo en una matriz  ( Longest palindrome in an array ) 
Soy nuevo en la programación, y creo que este código podría mejorarse. ¿Alguna sugerencia? 'done'0 ...

6  Clase de matriz multidimensional simple en C ++ 11  ( Simple multi dimensional array class in c11 ) 
La nueva versión del código se puede revisar en Clase de matriz multidimensional simple en C ++ 11 - Seguimiento . El siguiente código implementa una clas...

2  Importando datos en Excel  ( Importing data into excel ) 
¿Existe una forma más fácil de importar datos en una matriz de Excel u otra estructura de datos? He intentado investigar colecciones, pero he encontrado la D...

7  Colecciones vacías en caché  ( Cached empty collections ) 
A menudo necesito devolver las colecciones vacías. Uno de esos días, escribí lo siguiente para devolver una instancia en caché: public static class Array<...

6  Fusionando dos varias de clases  ( Merging two array of classes ) 
Tengo esta función que debe fusionar dos matriz de clases cuando id es igual a id de Array2. Para la simplicidad, convirtí una matriz en un arraylist pa...




© 2022 respuesta.top Reservados todos los derechos. Centro de preguntas y respuestas reservados todos los derechos