所以我正在开发一个简单的 RPG 游戏。我想添加一个选项来加载整个游戏的进度,但我想我对 Java 不太熟悉,所以没有问题。这个想法是在检查点上保存游戏,并在游戏继续时加载保存的数据。我在使用 loadGame
方法时遇到错误,我想这与我尝试加载的数据都是不同的数据类型这一事实有关。健康和等级是整数,而装备物品是一个字符串。
这里只介绍两个方法,saveGame
和loadGame
,因为代码有点太长,无法简单地将其粘贴到整体中。当然,如果需要,我会添加额外的代码。
public static void saveGame() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("savedGame.txt"));
bw.write("Your health is " + player.getHealth());
bw.newLine();
bw.write("Your level is " + player.getLevel());
bw.newLine();
bw.write("" + player.equippedItem);
bw.close();
}
catch (Exception e) {
System.out.println("There's an error.");
}
}
saveGame()
运行良好,它将数据存储在文件中。问题在于 loadGame()
方法。
public void loadGame() {
try {
BufferedReader br = new BufferedReader(new FileReader("savedGame.txt"));
player.getHealth();
player.getLevel();
player.equippedItem;
br.close();
} catch (Exception e) {
System.out.println("There's an error.");
}
}
player.getHealth()
和 player.getLevel()
的结果被忽略了,可能是我刚才提到的数据类型问题。
答案 0 :(得分:1)
您正在做的是在 saveGame() 中创建一个文本文件,在 loadGame() 中“打开”该保存文件,但不访问其中的数据。< /p>
您已经正确地创建了一个 BufferedReader,但您没有正确地使用它。您将需要使用 Codeception version: 4.1.20
PHP Version: 7.4.3
Operating System: Ubuntu 20
Installation type: Phar || Composer : Installed via Composer and Wp Browser
List of installed packages (composer show)
antecedent/patchwork 2.1.12 Method redefinition (monkey-pa...
behat/gherkin v4.8.0 Gherkin DSL parser for PHP
clue/stream-filter v1.5.0 A simple and modern approach t...
codeception/codeception 4.1.20 BDD-style testing framework
codeception/lib-asserts 1.13.2 Assertion methods used by Code...
codeception/lib-innerbrowser 1.5.0 Parent library for all Codecep...
codeception/module-asserts 1.3.1 Codeception module containing ...
codeception/module-cli 1.1.1 Codeception module for testing...
codeception/module-db 1.1.0 DB module for Codeception
codeception/module-filesystem 1.0.3 Codeception module for testing...
codeception/module-phpbrowser 1.0.2 Codeception module for testing...
codeception/module-webdriver 1.2.1 WebDriver module for Codeception
codeception/phpunit-wrapper 9.0.6 PHPUnit classes used by Codece...
codeception/stub 3.7.0 Flexible Stub wrapper for PHPU...
codeception/util-universalframework 1.0.0 Mock framework module used in ...
dg/mysql-dump v1.5.1 MySQL database dump.
doctrine/inflector 2.0.3 PHP Doctrine Inflector is a sm...
doctrine/instantiator 1.4.0 A small, lightweight utility t...
guzzlehttp/guzzle 7.3.0 Guzzle is a PHP HTTP client li...
guzzlehttp/promises 1.4.1 Guzzle promises library
guzzlehttp/psr7 1.8.1 PSR-7 message implementation t...
hautelook/phpass 0.3.5 Portable PHP password hashing ...
http-interop/http-factory-guzzle 1.0.0 An HTTP Factory using Guzzle PSR7
illuminate/collections v8.40.0 The Illuminate Collections pac...
illuminate/contracts v8.40.0 The Illuminate Contracts package.
illuminate/macroable v8.40.0 The Illuminate Macroable package.
illuminate/support v8.40.0 The Illuminate Support package.
jean85/pretty-package-versions 2.0.3 A library to get pretty versio...
lucatume/wp-browser 3.0.6 WordPress extension of the Php...
mikehaertl/php-shellcommand 1.6.4 An object oriented interface t...
mikemclin/laravel-wp-password 2.0.1 Laravel package that checks an...
mustache/mustache v2.13.0 A Mustache implementation in PHP.
myclabs/deep-copy 1.10.2 Create deep copies (clones) of...
nesbot/carbon 2.46.0 An API extension for DateTime ...
nikic/php-parser v4.10.4 A PHP parser written in PHP
phar-io/manifest 2.0.1 Component for reading phar.io ...
phar-io/version 3.1.0 Library for handling version i...
php-http/client-common 2.3.0 Common HTTP Client implementat...
php-http/discovery 1.13.0 Finds installed HTTPlug implem...
php-http/httplug 2.2.0 HTTPlug, the HTTP client abstr...
php-http/message 1.11.0 HTTP Message related tools
php-http/message-factory v1.0.2 Factory interfaces for PSR-7 H...
php-http/promise 1.1.0 Promise used for asynchronous ...
php-webdriver/webdriver 1.10.0 A PHP client for Selenium WebD...
phpdocumentor/reflection-common 2.2.0 Common reflection classes used...
phpdocumentor/reflection-docblock 5.2.2 With this component, a library...
phpdocumentor/type-resolver 1.4.0 A PSR-5 based resolver of Clas...
phpspec/prophecy 1.13.0 Highly opinionated mocking fra...
phpunit/php-code-coverage 9.2.6 Library that provides collecti...
phpunit/php-file-iterator 3.0.5 FilterIterator implementation ...
phpunit/php-invoker 3.1.1 Invoke callables with a timeout
phpunit/php-text-template 2.0.4 Simple template engine.
phpunit/php-timer 5.0.3 Utility class for timing
phpunit/phpunit 9.5.4 The PHP Unit Testing framework.
psr/container 1.1.1 Common Container Interface (PH...
psr/event-dispatcher 1.0.0 Standard interfaces for event ...
psr/http-client 1.0.1 Common interface for HTTP clients
psr/http-factory 1.0.1 Common interfaces for PSR-7 HT...
psr/http-message 1.0.1 Common interface for HTTP mess...
psr/log 1.1.3 Common interface for logging l...
psr/simple-cache 1.0.1 Common interfaces for simple c...
ralouphie/getallheaders 3.0.3 A polyfill for getallheaders.
rmccue/requests v1.8.0 A HTTP library written in PHP,...
sebastian/cli-parser 1.0.1 Library for parsing CLI options
sebastian/code-unit 1.0.8 Collection of value objects th...
sebastian/code-unit-reverse-lookup 2.0.3 Looks up which function or met...
sebastian/comparator 4.0.6 Provides the functionality to ...
sebastian/complexity 2.0.2 Library for calculating the co...
sebastian/diff 4.0.4 Diff implementation
sebastian/environment 5.1.3 Provides functionality to hand...
sebastian/exporter 4.0.3 Provides the functionality to ...
sebastian/global-state 5.0.2 Snapshotting of global state
sebastian/lines-of-code 1.0.3 Library for counting the lines...
sebastian/object-enumerator 4.0.4 Traverses array structures and...
sebastian/object-reflector 2.0.4 Allows reflection of object at...
sebastian/recursion-context 4.0.4 Provides functionality to recu...
sebastian/resource-operations 3.0.3 Provides a list of PHP built-i...
sebastian/type 2.3.1 Collection of value objects th...
sebastian/version 3.0.2 Library that helps with managi...
sentry/sdk 3.1.0 This is a metapackage shipping...
sentry/sentry 3.2.1 A PHP SDK for Sentry (http://s...
symfony/browser-kit v5.2.4 Simulates the behavior of a we...
symfony/console v5.2.6 Eases the creation of beautifu...
symfony/css-selector v5.2.4 Converts CSS selectors to XPat...
symfony/deprecation-contracts v2.4.0 A generic function and convent...
symfony/dom-crawler v5.2.4 Eases DOM navigation for HTML ...
symfony/event-dispatcher v5.2.4 Provides tools that allow your...
symfony/event-dispatcher-contracts v2.4.0 Generic abstractions related t...
symfony/finder v5.2.4 Finds files and directories vi...
symfony/http-client v5.2.6 Provides powerful methods to f...
symfony/http-client-contracts v2.4.0 Generic abstractions related t...
symfony/options-resolver v5.2.4 Provides an improved replaceme...
symfony/polyfill v1.22.1 Symfony polyfills backporting ...
symfony/process v5.2.4 Executes commands in sub-proce...
symfony/service-contracts v2.4.0 Generic abstractions related t...
symfony/string v5.2.6 Provides an object-oriented AP...
symfony/translation v5.2.6 Provides tools to internationa...
symfony/translation-contracts v2.4.0 Generic abstractions related t...
symfony/var-dumper v5.2.6 Provides mechanisms for walkin...
symfony/yaml v5.2.5 Loads and dumps YAML files
theseer/tokenizer 1.2.0 A small library for converting...
voku/portable-ascii 1.5.6 Portable ASCII library - perfo...
vria/nodiacritic 0.1.2 Tiny helper function that remo...
webmozart/assert 1.10.0 Assertions to validate method ...
wp-cli/mustangostang-spyc 0.6.3 A simple YAML loader/dumper cl...
wp-cli/php-cli-tools v0.11.12 Console utilities for PHP
wp-cli/wp-cli v2.4.1 WP-CLI framework
读取文本文件,该文件将返回类似“您的健康状况为 123”这样的字符串。
要使用此数据更新玩家的状态,您可以在 loadGame() 中添加如下内容:
actor: AcceptanceTester
modules:
enabled:
- WPDb
- WPBrowser
- \Helper\Acceptance
config:
WPDb:
dsn: '%TEST_SITE_DB_DSN%'
user: '%TEST_SITE_DB_USER%'
password: '%TEST_SITE_DB_PASSWORD%'
dump: 'tests/_data/lasso_test.sql'
#import the dump before the tests; this means the test site database will be repopulated before the tests.
populate: false
# re-import the dump between tests; this means the test site database will be repopulated between the tests.
cleanup: true
waitlock: 10
url: '%TEST_SITE_WP_URL%'
urlReplacement: true #replace the hardcoded dump URL with the one above
tablePrefix: '%TEST_SITE_TABLE_PREFIX%'
WPBrowser:
url: '%TEST_SITE_WP_URL%'
adminUsername: '%TEST_SITE_ADMIN_USERNAME%'
adminPassword: '%TEST_SITE_ADMIN_PASSWORD%'
adminPath: '%TEST_SITE_WP_ADMIN_PATH%'
headers:
X_TEST_REQUEST: 1
X_WPBROWSER_REQUEST: 1
解析方法将采用字符串输入,例如“您的健康状况为 123”并返回整数 123。