java - 寻宝 java 二维数组
问题描述
我正在尝试制作一个寻宝板程序,在这个游戏中,用户将探索一个 10 x 10 的板,其中包含宝藏、海盗和离岛逃生。目标是带着尽可能多的宝藏逃离岛屿。宝藏(T) - 随着更多宝藏的发现而增加(即第一个宝藏是 +100 金达布隆,第二个是 +200 金达布隆,依此类推) 海盗 (P) - 你遇到的海盗越多,他们能获得的金达布隆就越多拿。(即第一组是-100金币,第二组是-200金币,以此类推) 但是,如果玩家没有宝藏,就没有什么可失去的。两者 (B)- 如果有宝藏和海盗,玩家将获得一半可用的宝藏。Escape (X) - 当远处看到一艘船时,用户可以离开岛屿。
public static void main(String[] args) throws FileNotFoundException {
Scanner s = new Scanner(System.in);
// create 10x10 arrays
final int ROWS = 10, COLS = 10;
char[][] displayMap = new char[ROWS][COLS]; //stores the map
char[][] board = new char[ROWS][COLS];// this will display the board
//variales for how many pirates
char pirate = 'P';
//sets the location of the pirates on the board
int upperBound = 5, lowerBound = 0;
int pirateRow = (int) ((Math.random() * (upperBound - lowerBound + 1))
+ lowerArea); // the pirates location
int pirateColumn = (int) ((Math.random() * (upperBound - lowerBound + 1))
+ lowerBound); // pirates location
int treasures = 5;
//call loadMap to read in the map and return it
displayMap = loadMap();
//this will take the board and resets the board
clearBoard(board);
//passes board and pirate informations to displayBoard then display it
displayBoard(board, pirate, pirateRow, pirateColumn, treasures);
int selection = 0;
int playAgain = 0;
while (selection != 4) {
// displays the menu
System.out.println("WELCOME TO TREASURE HUNT");
System.out.println("1. Play Game ");
System.out.println("2. How to Play");
System.out.println("3. Legend");
System.out.println("4. Exit");
// take the input from menu
System.out.println("Enter your selection.");
selection = s.nextInt();
switch (selection) {
case 1:
PlayGame();
break;
case 2:
HowToPlay();
break;
case 100:
Legend();
break;
case 4:
System.out.println("See you again next time!");
default:
System.out.println("ERORR");
break;
}
// when user input = 1
if (selection == 1) {
// test for valid input
int rowNum = 0, columnNum = 0;
boolean isValid = false;
while ( !isValid) {
//prompt user
}
System.out.println("Pick a position to explore or enter 100 to"
+ "see the legend. ");
char row;
// check the row input
Scanner sc = new Scanner(System.in); // decalres scanner
row = sc.next().charAt(0); // this will store the character input
if (row >= 0&& row <= 99) { // this has to be a range of 0-99
rowNum = testRowInput(row); // this will test and return what is in the meethod
if (rowNum >= 0 && rowNum <= 9) { // tests if rowNum is within the range
// prompt user for column number / position
System.out.println("Pick a position to explore or enter 100 to"
+ "see the legend. ");
columnNum = getIntRange(1, 10);
columnNum --; // this will convert column range to 0-9
/**
* check if the spot has not yet been revealed, otherwise
* it has been explored already
*/
if ( testRevealed (board, rowNum, columnNum)) {
sc.nextLine(); // empty row input from buffer
System.out.println("You have already explored there.");
} else {
isValid = true;
}
} else {
sc.nextLine(); // empty row input
System.out.println("Nothing to see here.");
}
}
} // finished checking user input
// caught by pirate / losses gold doubloons
if ( rowNum == pirateRow ) && ( columnNum == pirateColumn)){
board[pirateRow][[pirateColumn]] = pirate;
}
}
}
/**
*
*/
public static void PlayGame() {
Scanner sc = new Scanner(System.in);
int pirates = 0;
int treasures = 0;
System.out.println("You chose option 1. Enter the number of pirates");
pirates = sc.nextInt();
System.out.println("Enter the number of treasures.");
treasures = sc.nextInt();
System.out.println(displayBoard);
}
/**
*
*/
public static void HowToPlay() {
System.out.println("You chose option 2");
System.out.println("For this game, the user will be going on a treasure"
+ " hunt. They will explore a 10 x 10 board that will contain "
+ "treasure, pirates and an escape off the island. The goal is "
+ "to escape the island with the most treasure possible. \n"
+ "Treasure(T) - increases as more treasure is found (ie, the first treasure is "
+ "+100 gold doubloons, the second is +200 gold doubloons and so on)\n"
+ "Pirates (P) - the more pirates you encounter the more gold doubloons they can take."
+ " (ie. the first crew is -100 gold doubloons, the second crew "
+ "is -200 gold doubloons and so on) BUT, if the player has no"
+ " treasure, there is nothing to be lost.\n"
+ "Both (B)- If there is treasure AND pirates, the player gets half the treasure "
+ "available.\n"
+ "Escape (X) - When a ship is seen in the distance, the user can leave the island.");
}
/**
*
*/
public static void Legend() {
System.out.println("****************************************");
System.out.println("%20s " + "LEGEND");
System.out.println("****************************************");
System.out.println("0 - indicates that spot has not been explored yet\n"
+ "T - indicates treasure, 100 gold doubloon\n"
+ "P - indicates pirates, lose 100 gold doubloon\n"
+ "B - indicates both treasure and pirates, 50 gold doubloon\n"
+ "X - escape the island");
}
These codes are not yet finished and I am still in process in doing it. There are
more methods to work on but what I am trying to work on right now, is how I can
display how much gold doubloons the user has every time a treasure is found and
every time they encounter a pirate or both.
The user's position entry must be between 0 and 99, inclusive. Should look like
this.
Positions [00 - 09]: 0 0 0 0 0 0 0 0 0 0
Positions [10 - 19]: 0 0 0 0 0 0 0 0 0 0
Positions [20 - 29]: 0 0 0 0 0 0 0 0 0 0
Positions [30 - 39]: 0 0 0 0 0 0 0 0 0 0
Positions [40 - 49]: 0 0 0 0 0 0 0 0 0 0
Positions [50 - 59]: 0 0 0 0 0 0 0 0 0 0
Positions [60 - 69]: 0 0 0 0 0 0 0 0 0 0
Positions [70 - 79]: 0 0 0 0 0 0 0 0 0 0
Positions [80 - 89]: 0 0 0 0 0 0 0 0 0 0
Positions [90 - 99]: 0 0 0 0 0 0 0 0 0 0
解决方案
在此示例中,让x
表示所选单元格的 x 坐标,并y
表示所选单元格的 y 坐标。您的代码似乎相当不完整,因此让我们doubloons
表示玩家在游戏期间持有的达布隆总数。
// We need two fields to track the number of treasures and pirates.
// These fields are effectively multipliers for the rewards and penalties
// for each treasure or pirate encountered as the game progresses
int doubloons = 0;
int treasureCount = 0;
int pirateCount = 0;
if(board[x][y] == 'T')
{
treasureCount++;
doubloons = doubloons + (treasureCount * 100);
}
else if(board[x][y] == 'P')
{
pirateCount++;
doubloons = doubloons - (pirateCount * 100);
}
else if(board[x][y] == 'B')
{
treasureCount++;
pirateCount++;
doubloons = doubloons + (treasureCount * 50);
}
else if(board[x][y] == 'X')
{
// whatever your end game scenario is supposed to be
}
else
{
// whatever is supposed to happen when there's no treasure, no
// pirates and no escape
}
请将任何后续问题限制在如何跟踪宝藏和海盗的具体问题上,因为这似乎是手头的问题。Stack Overflow 尝试坚持“一个问题,一个接受的答案”的格式,所以如果您有更多不相关(甚至半相关)的问题,请将它们作为单独的问题发布,以免混淆演示文稿。
推荐阅读
- javascript - Highcharts 树状图
- javascript - 生成器返回在 for-await-of 循环中不起作用
- php - 如何获取 next_post() 链接以使用手动排序而不是 Wordpress 中的默认页面 ID 排序?
- typescript - 如何使用 Typescript 对可能的空数组进行建模
- python - 捆绑提取具有地牢的每个链接将它们分开并将每个链接添加到列表中并删除重复项
- rest-assured-jsonpath - Jayway JSONPath 元素检索
- sql - 在自定义类型中过滤 DATE?
- php - 使用 pdflib 生成 pdf 所有图像都很模糊
- angular - Angular Reactive 表单将管道字符串值转换回整数
- mysql - 如何连接 2 个 sql 表,其中一个表在单个列中包含多个值