好的,所以在我的程序中,我必须制作一个名为 Opoly 的 Monopoly 的简化版本。它是一个带有一些简单规则的基本程序
如果您的棋子落在可以被 7 整除的棋盘格上,则您的奖励翻倍。
如果您降落在最后一个棋盘格上,则必须返回 3 个空格。因此,如果棋盘大小为 20,则最后一个位置是位置 19,如果你降落在那里,你应该回到位置 16。(如果最后一个单元格的位置可以被 7 整除,则不加分,但是如果新的棋子位置,后退 3 个位置,可以被 7 整除,则加分)。
如果你在棋盘上一直走下去,你会得到 100 分。请注意,如果您准确地降落在位置 0,您首先会获得 100 额外积分(用于四处走动),然后您的分数会加倍,因为 0 可以被 7 整除,
每十次移动(即旋转器每旋转十次,移动编号 10、20、30 等),奖励减少 50 分。这个惩罚在第 10 步、第 20 步或第 30 步移动后立即生效,即使该瞬间的其他动作也适用。请注意,使用此规则,奖励金额可能会变为负数。
我已经成功地做到了。但是,现在我的老师希望我们使用字符串而不是字符数组来做同样的事情。所以我知道 String 实际上是一个 char 数组,所以我必须采取哪些步骤才能使这变得非常轻松。
这是我的代码,您需要一些视觉效果。我评论了大部分代码以帮助理解它。
import java.util.*;
public class OpolyDriver{
public static void main(String[] args){
System.out.println("Enter an int > 3 - the size of the board");
Scanner s = new Scanner(System.in);
int boardSize = s.nextInt();
System.out.println("Board Size: " + boardSize);
Opoly g = new Opoly(boardSize);
g.playGame();
}
}
public class Opoly{
private static int size; //how big the board is
private static int spin; //value of the spinner
private static int reward; //total points
private static int turnNumber; //how many turns have passed
private static char[] board; //the array of the board and holding the position of the player
private static boolean first; //temp variable to create the array with *'s and o
public Opoly(int s){ //constructor
size = s; //sets the size passed by the main method defined by the user
reward = 100; //startes player with 100 points
turnNumber = 0; //sets turn number to 0
board = new char[size]; //creates the array
first = true; //temp variable to create the array with *'s and o
}
public void playGame(){
Opoly.drawBoard(); //prints out board for the first time
while (Opoly.isGameOver()){ //checks when the player has recieved 1000 points or more
Opoly.spinAndMove(); //spins, moves, and adds points
Opoly.drawBoard(); //prints out the updated board and reward
}
Opoly.displayReport(); //displays the stats when the game is over
}
public static void spin(){
spin = (1 + (int)(Math.random() * 5)); //generates a number from 1 to 5
}
public static void move(){
if (turnNumber % 10 == 0) //RULE #4 - Every tenth move, reduces the reward by 50 points.
reward = reward - 50;
for (int k = 0; k < size; k++){ //finds the position of the player
if (board[k] == 'o'){
board[k] = '*';
if (k == (size - 1)){ //RULE #2 (condition 1) - If you land on the final board cell, you must go back 3 spaces.
board[k] = '*';
board[k - 3] = 'o';
if (((k - 3) % 7 == 0) && (k - 3 != 0)) //RULE #2 (condition 2 & 3) - If the position of the last cell is evenly divisible by 7, no extra points are added. If the new piece location, 3 places back, IS evenly divisible by 7, then extra points ARE doubled
reward = reward * 2;
if (((k - 3) + spin) >= size){ //brings the array back in bounds to cirlce the position of the player
board[k - 3] = '*';
reward = reward + 100; //RULE #3 - If you make it all the way around the board, you get 100 points.
board[((k - 3) + spin) - size] = 'o';
}
else if (((k - 3) + spin) <= size){ //moves the position when player is in bounds of array
board[k - 3] = '*';
board[(k - 3) + spin] = 'o';
}
}
else if ((k + spin) >= size){ //brings the array back in bounds to cirlce the position of the player
reward = reward + 100; //RULE #3 - If you make it all the way around the board, you get 100 points.
board[(k + spin) - size] = 'o';
}
else if ((k + spin) <= size) //moves the position when player is in bounds of array
board[k + spin] = 'o';
k = size; //resets k
}
}
}
public static void spinAndMove(){
turnNumber++; //adds a turn
Opoly.spin(); //sets a number to the spin variable
Opoly.move(); //moves the position
for (int k = 0; k < size; k++){ //adds points
if (board[k] == 'o'){
if (k == 0) //RULE #1 - Score is doubled, since 0 is evenly divisible by 7,
reward = reward * 2;
else if ((k % 7 == 0) && (k != (size - 1))) //RULE #1 - Score is doubled when it is evenly divisible by 7,
reward = reward * 2;
}
}
}
public static boolean isGameOver(){
boolean isOver = true; //checks if game is over
if (reward >= 1000) //if the reward is 1000 points or over than the game is over
isOver = false;
return isOver;
}
public static void drawBoard(){
if (first){ //temp variable is used to create the board for the first time
board[0] = 'o';
for(int i = 1; i < size; i++)
board[i] = '*';
}
for(int i = 0; i < size; i++) //for loop that prints out the updated board
System.out.print(board[i]);
System.out.println(" " + reward); //prints out the reward
first = false; //temp variable set to flase so it wont recreate the board again
}
public static void displayReport(){ //displays stats
System.out.println("game over");
System.out.println("rounds of play: " + turnNumber);
System.out.println("final reward: " + reward);
}
}