捕捉两者之间的颜色?

时间:2014-04-06 23:09:29

标签: java arrays

我需要帮助完成我的java任务。作业是:

创建一个游戏,其中: 有两个游戏片,黑色和白色。 当一块或一条线在其左右两侧或前后两侧都被其对手包围时,据说它被捕获并且其颜色变为包围它的部分的颜色。 在你的回合中,你必须捕获至少一个对手的棋子。 当用户没有更多有效动作时,游戏结束。 获胜者将分配给捕获更多作品的玩家。

棋盘是6x6,你只需要用文字显示结果,而不是实际游戏。我只知道如何使用数组创建一个板。我不知道如何编写实际游戏的代码。一些帮助将不胜感激!!我知道我必须使用很多“if”语句和循环,但我不知道该怎么做。这就是我到目前为止所做的:

boardA[0] = "A1";
boardA[1] = "A2";
boardA[2] = "A3";
boardA[3] = "A4";
boardA[4] = "A5";
boardA[5] = "A6";

boardB[0] = "B1";
boardB[1] = "B2";
boardB[2] = "B3";
boardB[3] = "B4";
boardB[4] = "B5";
boardB[5] = "B6";

boardC[0] = "C1";
boardC[1] = "C2";
boardC[2] = "C3";
boardC[3] = "C4";
boardC[4] = "C5";
boardC[5] = "C6";

boardD[0] = "D1";
boardD[1] = "D2";
boardD[2] = "D3";
boardD[3] = "D4";
boardD[4] = "D5";
boardD[5] = "D6";

boardE[0] = "E1";
boardE[1] = "E2";
boardE[2] = "E3";
boardE[3] = "E4";
boardE[4] = "E5";
boardE[5] = "E6";

boardF[0] = "F1";
boardF[1] = "F2";
boardF[2] = "F3";
boardF[3] = "F4";
boardF[4] = "F5";
boardF[5] = "F6";
Scanner keyboard = new Scanner(System.in);
System.out.println("This is how the board looks like:" + "\n" + Arrays.toString(boardA) + 
                   "\n" + Arrays.toString(boardB)+ "\n" + Arrays.toString(boardC) 
                     +"\n"+ Arrays.toString(boardD) +"\n"+Arrays.toString(boardE)
                  + "\n"+Arrays.toString(boardF));
System.out.println("The board has 6 blocks, starting with 4 people in the middle, 2 black and 2 white facing each other.");
boardC[2] = "white";
boardC[3] = "black";
boardD[2] = "black";
boardD[3] = "white";
System.out.println("The board starts with: " + "\n" + Arrays.toString(boardA) + 
                   "\n" + Arrays.toString(boardB)+ "\n" + Arrays.toString(boardC) 
                     +"\n"+ Arrays.toString(boardD) +"\n"+Arrays.toString(boardE)
                  + "\n"+Arrays.toString(boardF));
System.out.println("You are white. Where do you want to move?");
String input = keyboard.nextLine();

1 个答案:

答案 0 :(得分:1)

你肯定要求......很多。我们无法为您完成任务!

我相信你应该从一点一点开始写一些东西。 例如,您可以:

  1. 首先让它适用于某个特定情况,
  2. 然后是另一个,
  3. 然后看一些模式并使其成为通用的......

  4. 但是,我会让你开始 我认为您与用户的互动应该是这样的:

    boolean gameEnded = false;
    boolean isWhitesTurn = true;
    while(!gameEnded) {
       System.out.println("You are" + (isWhitesTurn)?"white":"black" + ". Where do you want to move?");
       final String input = keyboard.nextLine();
    
       // process the input here
    
       isWhitesTurn = !isWhitesTurn; // every time, you toggle between the players.
    }
    System.out.println("Game over.");
    

    现在,为了处理输入,我们首先假设用户将输入有效的String 。也就是说,一些实际的板箱的坐标。 稍后,您必须检查并处理错误的输入。

    以下是你对这个输入的看法:

    1. String翻译成某些Object例如"E5"应该是boardE[4]。我想您可以创建一些方法,例如getValueAt(final String coordinates)setValueAt(final String coordinates)
    2. 检查这些坐标是否已有玩家(使用getValueAt())。

      2.1如果有,请求新输入。 2.2如果没有,请将玩家的颜色放在正确的坐标上(使用setValueAt()),然后执行改变被包围的玩家的颜色的算法"。你知道我的意思。

    3. 必须有某种条件停止游戏(例如:棋盘上不再有空案例)。核实。如果游戏应该结束,请设置gameEnded = true;

    4. 由于我对此,我建议您的主板看起来更像是一块实际的主板,即...... 矩阵

      例如,我会用这个:

      private static final int BOARD_WIDTH = 6;
      private static final int BOARD_HEIGHT = 6;
      final String[][] board = new String[BOARD_WIDTH][BOARD_HEIGHT];
      

      并将其初始化为:

      for (int i = 0; i < BOARD_HEIGHT; i++) {
          final String letter = new Character((char) ('A' + i)).toString();
          for (int j = 0; j < BOARD_WIDTH; j++) {
              this.board[i][j] = letter + String.valueOf(j);
          }
      }
      

      然后打印出来:

      for (int i = 0; i < BOARD_HEIGHT; i++) {
          for (int j = 0; j < BOARD_WIDTH; j++) {
              System.out.print(this.board[i][j] + "\t");
          }
      
          System.out.println();
      }
      

      我希望这一切能让你开始正常;)