2013-10-21 39 views
0

好的,在我的程序中,我必须制作一个叫做Opoly的专卖版。它的基本程序有一些简单的规则需要通过什么步骤将字符数组中的程序更改为字符串中的字符串?

  1. 如果是整除 7板细胞的板块的土地,你的奖励加倍。

  2. 如果你在最后的棋盘单元格上,你必须返回3个空格。因此 如果棋盘大小为20,最后一个位置是位置19,并且如果您登陆那里 ,则应该返回到位置16.(如果最后一个单元的位置 可以被7整除,则没有额外的点数 增加,但如果新的位置,3放回,均匀 除以7,然后加分)。

  3. 如果你一直围绕着董事会,你会得到100分。 请注意,如果你恰好降落在位置0,您第一次收到100 加分(制造这一切的周围),然后你的分数是 一倍,因为0是7整除,

  4. 每10个移动(即,旋转器的每旋转10次,移动 数字10,20,30,等等),奖励减少50点。即使在该时刻的其他行动也适用,在第10次或第20次或第30次行动之后立即应用此处罚。 请注意,通过此规则,奖励金额可能为 为负值。

我已经成功完成了这项工作。但是,现在我的老师希望我们使用Strings而不是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); 
    } 
} 
+0

有无你还尝试过什么,或者你只是想让我们为你做? – Dom

+0

你不能。当字符串不是时,数组是可变的。每次更改单个字母时,都需要重新创建字符串。 – njzk2

+0

啊,好的!谢谢@ njzk2 – Robert

回答

1

没有真正回答这个问题,所以如果需要的话可以删除。

您的board包含两种类型的值:o*。玩家,而不是玩家。

当你在板阵列上测试并测试if (board[k] == 'o'){时,你真正在做的是找到k,比如k是玩家的位置。

当您修改板阵列时,您真正在做的是将k - 3影响到玩家的位置。

玩家的位置可以表示为一个整数,默认值为0,应保持在0和size - 1之间。

通常情况下,你会简单地使用playerPosition代替k取代那些

for (int k = 0; k < size; k++){ 
    if (board[k] == 'o') { 
     // ... 
    } 
} 

块和

board[k] = '*'; 
board[<someValue>] = 'o'; 

通过简单playerPosition = <someValue>;哪里是k - 3k + spin ...

相关问题