如何在Java游戏Domineering中防止英镑符号重叠

时间:2013-02-20 04:47:06

标签: java

我是一个相当新的java和我应该修改游戏,以便不允许重叠#符号放在板上。例如:如果第一个玩家水平选择0,0,则第二个玩家不能再垂直选择0,0

我插了

(if(HORIZONTAL == VERTICAL) { System.out.println("illegal move try again"))

但它不会起作用。我做错了什么?

这是程序

    public class Domineering
    { 
     public static final java.util.Scanner INPUT
     = new java.util.Scanner(System.in);

      public static final boolean HORIZONTAL = false;
      public static final boolean VERTICAL = true;

      private boolean [] [] squares;

      public Domineering()
      { squares = new boolean [8][8];
       }

       public boolean hasLegalMoveFor(boolean player)
       {
        int rowOffset = 0;
        int columnOffset=0;
        if (player == HORIZONTAL)
        {
         columnOffset =1;
         }else{
          rowOffset =1;
          }
          for(int row =0; row <(8-rowOffset); row++)
          {
           for(int column = 0; column <(8-columnOffset); column++)
           {
            if(!(squares[row][column]
             || squares[row + rowOffset][column + columnOffset])){
              return true;
               }
              }
             }
            return false;
           }
    public void play
    {
    boolean player = HORIZONTAL;
    while (true)
    {
     System.out.println("\n" + this);
     if (player == HORIZONTAL)
     {
      System.out.println("Horizontal to play");
     }else{
     System.out.println("Vertical to play");
     }
     if(!(hasLegalMoveFor(player)))
     {
      System.out.println)"No legal move--Sorry you lose!");
      return;
      }
      System.out.print("Row:");
      int row = INPUT.nextInt();
      System.out.print("Column");
      int column = INPUT.nextInt();
      playAt(row, column, player);
      player =!player;
      {
      if (HORIZONTAL == VERTICAL)  //this is what i added but it doesnt seem to
                                   //work right..what am i doing wrong?
      {
       System.out.println("Sorry Illegal Move try again")
       return;
        }
       }
      }
    public void playAT (int row, int column, boolean player)
       {
       squares [row][column +1]=true;
        }
       else
        {
        squares[row +1][column] = true;
        }
       }
    public string toString()
     {
     String result = " 0 1 2 3 4 5 6 7";
     for (int row =0; row < 8; row++)
     {
     result += "\n" + row;
      for (int column = 0; column < 8; column++)
     {
     if(squares[row][column]){
     result += " #";
     }else{
     result += " .";
       }
      } 
     }
    return result;
   }

    public static void main(String[] args){
     System.out.println("Welcome to Domineering.");
     Domineering game = new Domineering();
     game.play();
     }
    }

0 个答案:

没有答案