logic 相关问题

逻辑指的是代码的最终流程以及您如何到达所需的解决方案。问题应该与为特定问题寻找编码解决方案(或改进现有编码逻辑)有关。请使用适当的语言标记,逻辑的详尽描述以及您正在处理的相关代码。一般逻辑问题是偏离主题的。如果您只是需要代码审查,请考虑https://codereview.stackexchange.com

c# 使用 Array.FindIndex 时索引超出范围

我目前正在尝试解决 Leetcode 问题(我是一名初学者编码器)。 这就是问题: 给定一个非空整数数组 nums,除了一个之外,每个元素都出现两次。找到那首单曲...

回答 1 投票 0

如何使用 SFML 和 C++ 渲染贪吃蛇游戏的精灵

游戏运行良好,但它使用矩形形状,我想使用精灵渲染蛇部分。这是代码(是的,它不是那么干净,我只是想让它工作): #包括 游戏运行良好,但它使用矩形形状,我想使用精灵渲染蛇部分。这是代码(是的,它不是那么干净,我只是想让它工作): #include <SFML/Graphics.hpp> // Graphics module #include <SFML/Audio.hpp> // Sound module #include <iostream> // Basic input/output (for errors) #include <vector> // Dynamic arrays (for snake body) #include <random> // For random food positions class Game { private: sf::Vector2u windowSize; // Size of the game window int a, b; // Length and width of a block sf::RenderWindow window; // Window object for drawing sf::Font font; // Text font sf::Clock clock; // For time measurement std::vector<sf::Vector2i> body; // Snake body (segments as coordinates) sf::Vector2i food; // Food position sf::Vector2i direction; // Snake direction int score; // Player's score bool gameOver; // Game state: finished or not int n, m; // Number of rows and columns float delay, timer; // Update delay and elapsed time sf::Music eat; // Sound effect for eating public: Game(unsigned short x, unsigned short y); // Constructor void start(); // Start the game private: void loop(); // Main game loop void events(); // Process events (keyboard, etc.) void update(); // Update game logic void render(); // Render elements on screen void gameOverScreen(); // Show "Game Over" screen sf::Vector2i getFoodPosition(); // Generate new food position }; int WinMain() { Game game(800, 600); game.start(); } int main() { Game game(800, 600); // Create object with window size 800x600 game.start(); // Call the start function } Game::Game(uint16_t x, uint16_t y) { windowSize = { x, y }; // Save window width and height window.create(sf::VideoMode(windowSize.x, windowSize.y, 1), "Snake"); // Create the window a = 50; // Set block width b = 50; // Set block height n = windowSize.x / a; // Calculate number of horizontal blocks m = windowSize.y / b; // Calculate number of vertical blocks font.loadFromFile("resources/Fonts/sfpro_bold.OTF"); // Load font for text eat.openFromFile("resources/Audio/eating_apple.mp3"); } void Game::start() { body.clear(); // Clear the snake body body.push_back({ 5,3 }); // Head body.push_back({ 4,3 }); // Body segment body.push_back({ 3,3 }); // Tail direction = { 0, 0 }; // Direction food = { getFoodPosition() }; // Initial food position gameOver = false; // Game not over (false) score = 0; // Start score from 0 loop(); // Main loop } void Game::loop() { timer = 0.f; // Accumulated time delay = 0.125f; // Game update delay while (window.isOpen()) { events(); // Handle user inputs (keyboard and mouse) timer += clock.getElapsedTime().asSeconds(); // Add elapsed time in seconds to the timer if (timer > delay) { update(); // Update the game (move snake, etc.) render(); // Render the screen (blocks, snake, food, text, etc.) timer = 0; // Reset timer } clock.restart(); // Restart the clock for the next cycle } } void Game::events() { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Escape) window.close(); else if (event.key.code == sf::Keyboard::Up && (direction.y != 1)) direction = { 0, -1 }; else if (event.key.code == sf::Keyboard::Down && (direction.y != -1)) direction = { 0, 1 }; else if (event.key.code == sf::Keyboard::Left && (direction.x != 1)) direction = { -1, 0 }; else if (event.key.code == sf::Keyboard::Right && (direction.x != -1)) direction = { 1, 0 }; else if (event.key.code == sf::Keyboard::R) /*if (gameOver)*/ start(); } } } void Game::update() { if (gameOver || direction == sf::Vector2i{ 0,0 }) return; sf::Vector2i newHead = body[0] + direction; for (size_t i = 1; i < body.size(); i++) { if (newHead == body[i]) { gameOver = true; return; } } if (newHead.x > n - 1 || newHead.x < 0 || newHead.y > m - 1 || newHead.y < 0) { gameOver = true; return; } if (newHead == food) { body.insert(body.begin(), newHead); food = getFoodPosition(); score++; eat.play(); } else { body.insert(body.begin(), newHead); body.pop_back(); } } void Game::render() { window.clear(); sf::RectangleShape block(sf::Vector2f(a, b)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { block.setPosition(i * a, j * b); int p = (i + j) % 2; block.setFillColor(sf::Color(172 - p * 7, 214 - p * 7, 67 - p * 7, 255)); window.draw(block); } } block.setPosition(food.x * a, food.y * b); block.setFillColor(sf::Color::Red); window.draw(block); for (int i = 0; i < body.size(); i++) { if (i == 0) block.setFillColor(sf::Color::Magenta); else block.setFillColor(sf::Color(0, 71, 181)); block.setPosition(body[i].x * a, body[i].y * b); window.draw(block); } sf::Text scr("Score: " + std::to_string(score), font, 32); scr.setFillColor(sf::Color::White); scr.setPosition(4, 0); window.draw(scr); if (gameOver) gameOverScreen(); window.display(); } void Game::gameOverScreen() { eat.stop(); sf::RectangleShape screen({ float(windowSize.x), float(windowSize.y) }); screen.setPosition({ 0,0 }); screen.setFillColor(sf::Color(0, 0, 0, 100)); sf::Text gameOverText(" Game over!\nPress R to restart", font, 38); gameOverText.setFillColor(sf::Color::White); gameOverText.setPosition((windowSize.x / 2) - 150, (windowSize.y / 2) - 20); window.draw(screen); window.draw(gameOverText); } sf::Vector2i Game::getFoodPosition() { std::random_device randomDevice; std::mt19937 randomNumberGenerator(randomDevice()); std::uniform_int_distribution<int> distX(0, n - 1); std::uniform_int_distribution<int> distY(0, m - 1); sf::Vector2i position; do { position.x = distX(randomNumberGenerator); position.y = distY(randomNumberGenerator); if (std::find(body.begin(), body.end(), position) == body.end()) { return position; } } while (true); } 上面的代码目前没有精灵功能,但这是我尝试过的一种方法(适用于渲染精灵):创建纹理和精灵的地图,从文件加载纹理,将它们映射到精灵,然后我准备好了一张精灵地图。这部分并不难,我的渲染逻辑很可能存在缺陷。代码是从我拥有精灵逻辑时获取的,我基本上使用字符串来确定我应该加载哪个精灵。 sf::Vector2i prev = body[i + 1]; // sf::Vector2i next = body[i - 1]; //because i pop the tail and insert new part at the beginning if (next.x == prev.x) { spriteName = "body_vertical"; //vertical sprite | } else if (next.y == prev.y) { spriteName = "body_horizontal"; // horizontal -- } else { if (prev.x < next.x) { if (prev.y > next.y) spriteName = "body_topright"; // start top curve right downwards else spriteName = "body_bottomleft"; // bottom -> left } else if (prev.y < next.y) { spriteName = "body_topleft"; // top -> left else spriteName = "body_bottomright"; // bottom -> right } } else{ if (prev.y > next.y) { spriteName = "body_topleft"; // top -> left else spriteName = "body_bottomright"; // bottom -> right } else if (prev.y < next.y) { spriteName = "body_topright"; // top -> right else spriteName = "body_bottomleft"; // bottom -> left } } } 精灵链接:opengameart.org/content/snake-game-assets。基本上,我想要的是一些关于如何选择要加载的正确精灵以及在哪种情况下加载的反馈(头部和尾部也将受到赞赏🥹)。谢谢! 这是一个稍微干净一点的代码, // choosing is based on the assumption // that the head is the last element in the body array // if not, you can simply reverse the conditions if (next == /*last in body*/) { spriteName == "head_sprite"; } else if (next == /*first in body*/) { spriteName = "tail_sprite"; } else if (next.x == prev.x || next.y == prev.y) { spriteName == "body_sprite"; } else { spriteName = "curved_sprite"; } // now rotate and reverse if (prev.x == next.x) { // rotate 90deg } else if (prev.x > next.x) { // reverse horizontally } if (prev.y > next.y) { // reverse vertically } 旋转/反转精灵比根据每种情况选择单独的精灵要方便得多。 我从您附加的那些精灵中选择了snake_graphics.zip [“head_down”,“tail_down”,“body_vertical”,“body_bottomright”] 并将它们重命名为 [“head_sprite”、“tail_sprite”、“body_sprite”、“curved_sprite”] 分别。 我可能错误地旋转/反转了精灵 因为我不确定上一个还是下一个是当前的(“假设上一个是”),但这就是基本思想

回答 1 投票 0

有人知道哪里可以学习 AIMA python 库吗?

我正在上一门“人工智能简介”课程,我们被分配做一个关于使用Python中的AIMA库的简单专家系统的迷你项目(来自《人工智能:现代方法》一书)。我是哈...

回答 1 投票 0

是什么导致我的河内逻辑塔无限循环?

所以我一直在使用堆栈而不是递归来编写河内塔代码,并根据维基百科关于使用迭代解决 ToH 的部分提出了以下逻辑。 如果(拖...

回答 1 投票 0

用于审查改进大查询的示例查询

查看代码并解释修复它所需的内容。 sql 新手,这是一项复习作业。所以试图理解正在执行的逻辑是什么 这将在大型查询平台上运行 那个...

回答 1 投票 0

Redux 业务逻辑最佳实践

我正在使用 React 和 Redux 构建购物车,但无法理解最佳实践流程。 我的购物车操作: 导出 const addToCart = (产品) => (调度, getState) => { 让{亲...

回答 2 投票 0

如何返回一个数组以从后方向显示

嘿,我是java新手,还没有弄清楚如何向后打印数组的逻辑。我只是不明白它是如何使用 forloop 进行流动的 这相当简单,chatgpt 可以轻松做到...

回答 1 投票 0

我可以使用 Sumproduct 数组并检查两个单独的标准来计算 TRUE 语句吗?

我有一个表格,我引用该表格来根据下拉框计算符合特定条件(2024 年 2 月、2022 年 8 月等)的日期。目前我正在通过 SUMPRODUCT 进行计数,检查...

回答 1 投票 0

比较两个向量之间的 NA 的 R 方法

我正在尝试比较两个向量的相同元素,例如下面的向量 a 和 b。 # 向量 a 和 b 一个<- c(1, 2, 3, NA) b <- c(1, 3, NA, NA) # compare for identity a == b ## [1] TRUE

回答 2 投票 0

在 C 编程中,我可以在不使用循环的情况下打印 '1' n 次吗?

当我们使用Python时,我们可以使用print("1"*n)多次打印相同的字符串。这有助于减少执行时间。如果我们有一种方法可以在 C 语言中做到这一点,那将会很有帮助。 预计...

回答 1 投票 0

网格中最大的产品

我被这个问题困扰了。我确实认为我已经找到了正确的解决方案,但是当将其提交到网站时,它不接受。 我尝试通过打印所有可能的组合来调试它......

回答 3 投票 0

在农民宇航员中形成逻辑

电影中有这样的逻辑,我无法完全形式化(例如在 Coq 中)。 有人想在他的农场发射一枚火箭,监视该地点的联邦调查局人员正在互相讨论为什么......

回答 3 投票 0

项目欧拉解决方案 8,需要解释

我已经解决了Project Euler的问题8; 1000 位数字中乘积最大的四个相邻数字是 9 × 9 × 8 × 9 = 5832。

回答 1 投票 0

在Prolog中制作连续数字的嵌套列表

任何人都可以检查这是否是一个完整的BS代码。显然它不起作用,因为我的答案是错误的,但我只是想知道这是在正确的轨道上还是完全不......

回答 1 投票 0

先行词中的条件顺序导致堆栈溢出

作为一个简单的练习,我编写了自己的排列。 该堆栈溢出: 没有(_, [], [])。 没有(A,[A|T],T)。 没有(A,[H | T],[H | G]):-没有(A,T,G)。 my_permutation([], []). 我的_排列...

回答 1 投票 0

如何在现有列上创建具有分组值计数和尊重的列[已关闭]

我有以下数据表,我想通过在现有列上添加一些条件来获取计数,如果我能得到相同的解决方案,那将是非常有帮助的。 输入: 钥匙1...

回答 3 投票 0

如何不依赖于用户的类型顺序

我正在尝试用c编写一个程序,当用户输入3个不同的数字时,程序返回最高的、中间的和最低的,但只有当用户输入a时它才有效

回答 1 投票 0

Javascript 自增和自减运算符

令i = 1; i = i++ - --i + ++i - i--;控制台.log(i); 结果显示 0 i++ 为 1、--i 为 1、++i 为 2、i-- 为 2 时如何工作。 据我了解,i++ 应该是 1,--i 应该是 0,++i 应该...

回答 1 投票 0

如何在 Dart 中逐个字母地书写?

我是 Dart 的初学者,我正在尝试编写一个函数,逐个字母地打印字符串,每个字母之间有延迟。它在大多数情况下都有效,但是,它给了我...

回答 1 投票 0

我的程序存在逻辑问题 - 初级

我正在尝试在 SWIFTUI 中的以下代码中将视图从第一个视图更改为最终视图。我希望在单击按钮 C 后进入最终页面。除非我单击 C,否则不会发生这种情况...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.