2016-12-19 60 views
-3

我想創建一個方法/ while循環來檢查哪個播放器移動。現在我查看一下哪個遊戲在for循環中移動。我試圖讓我的代碼更加清晰和簡潔。這裏是主要的方法:如何查看哪個玩家在我的連線四場比賽中移動?

public static void main(String[] args) { 


      try (Scanner input = new Scanner(System.in)) { 
       int height = 6, width = 8, moves = height * width; 
       ConnectFour board = new ConnectFour(width, height); 
       System.out.println("Use 0-" + (width - 1) + " to choose a column."); 
       System.out.println(board); 



       for (int player = 0; moves-- > 0; player = 1 - player) { // Here is where i check to see who plays 
        char symbol = players[player]; 
        board.chooseAndDrop(symbol, input); 
        System.out.println(board); 
        if (board.isWinningPlay()) { 
         System.out.println("Player " + symbol + " wins!"); 
         return; 
        } 
       } 
       System.out.println("Game over, no winner."); 
      } 


     } 

我的線沿線的思考更多:

    int playerNb = 0; 
        while (thegamestarted) 
        { 
         if (playerNb == 0) 
          // Get user input 
         else 
          // Get second player input 

         // Process the input 
         // Change playerNb to 1 or 0 
        } 

以下是完整代碼:

import java.util.Arrays; 
    import java.util.Scanner; 
    import java.util.stream.Collectors; 
    import java.util.stream.IntStream; 

    public class ConnectFour { 
     private static final char[] players = new char[] { 'X', 'O' }; 

     private final int width, height; 
     private final char[][] grid; 
     private int lastCol = -1, lastTop = -1; 

     public ConnectFour(int width, int height) { 
      this.width = width; 
      this.height = height; 
      this.grid = new char[height][]; 
      for (int h = 0; h < height; h++) { 
       Arrays.fill(this.grid[h] = new char[width], '.'); 
      } 
     } 

     public String toString() { 
      return IntStream.range(0, this.width) 
          .mapToObj(Integer::toString) 
          .collect(Collectors.joining()) + "\n" + 
        Arrays.stream(this.grid) 
         .map(String::new) 
         .collect(Collectors.joining("\n")); 
     } 

     /** 
     * Prompts the user for a column, repeating until a valid 
     * choice is made. 
     */ 
     public void chooseAndDrop(char symbol, Scanner input) { 
      do { 
       System.out.print("\nPlayer " + symbol + " turn: "); 
       int col = input.nextInt(); 

       if (! (0 <= col && col < this.width)) { 
        System.out.println("Column must be between 0 and " + 
             (this.width - 1)); 
        continue; 
       } 
       for (int h = this.height - 1; h >= 0; h--) { 
        if (this.grid[h][col] == '.') { 
         this.grid[this.lastTop=h][this.lastCol=col] = symbol; 
         return; 
        } 
       } 

       System.out.println("Column " + col + " is full."); 
      } while (true); 
     } 

     /** 
     * Detects whether the last chip played was a winning move. 
     */ 
     public boolean isWinningPlay() { 
      if (this.lastCol == -1) { 
       throw new IllegalStateException("No move has been made yet"); 
      } 
      char sym = this.grid[this.lastTop][this.lastCol]; 
      String streak = String.format("%c%c%c%c", sym, sym, sym, sym); 
      return contains(this.horizontal(), streak) || 
        contains(this.vertical(), streak) || 
        contains(this.slashDiagonal(), streak) || 
        contains(this.backslashDiagonal(), streak); 
     } 

     /** 
     * The contents of the row containing the last played chip. 
     */ 
     private String horizontal() { 
      return new String(this.grid[this.lastTop]); 
     } 

     /** 
     * The contents of the column containing the last played chip. 
     */ 
     private String vertical() { 
      StringBuilder sb = new StringBuilder(this.height); 
      for (int h = 0; h < this.height; h++) { 
       sb.append(this.grid[h][this.lastCol]); 
      } 
      return sb.toString(); 
     } 

     /** 
     * The contents of the "/" diagonal containing the last played chip 
     * (coordinates have a constant sum). 
     */ 
     private String slashDiagonal() { 
      StringBuilder sb = new StringBuilder(this.height); 
      for (int h = 0; h < this.height; h++) { 
       int w = this.lastCol + this.lastTop - h; 
       if (0 <= w && w < this.width) { 
        sb.append(this.grid[h][w]); 
       } 
      } 
      return sb.toString(); 
     } 

     /** 
     * The contents of the "\" diagonal containing the last played chip 
     * (coordinates have a constant difference). 
     */ 
     private String backslashDiagonal() { 
      StringBuilder sb = new StringBuilder(this.height); 
      for (int h = 0; h < this.height; h++) { 
       int w = this.lastCol - this.lastTop + h; 
       if (0 <= w && w < this.width) { 
        sb.append(this.grid[h][w]); 
       } 
      } 
      return sb.toString(); 
     } 

     private static boolean contains(String haystack, String needle) { 
      return haystack.indexOf(needle) >= 0; 
     } 

     public static void main(String[] args) { 
      try (Scanner input = new Scanner(System.in)) { 
       int height = 6, width = 8, moves = height * width; 
       ConnectFour board = new ConnectFour(width, height); 
       System.out.println("Use 0-" + (width - 1) + " to choose a column."); 
       System.out.println(board); 

       for (int player = 0; moves-- > 0; player = 1 - player) { 
        char symbol = players[player]; 
        board.chooseAndDrop(symbol, input); 
        System.out.println(board); 
        if (board.isWinningPlay()) { 
         System.out.println("Player " + symbol + " wins!"); 
         return; 
        } 
       } 
       System.out.println("Game over, no winner."); 
      } 
     } 
    } 
+3

是什麼問題?你在調試器中運行這些嗎?結果是什麼?它有用嗎?如果沒有,你能找出行爲偏離你的期望的地方嗎? –

回答

2

它有點困難告訴你想要從您的代碼中獲取數據,但是跟蹤其播放器的絕對最簡單的方法是跟蹤轉數,然後檢查模數函數是否爲偶數或者奇數

這只是一段簡短的psuedocode,向您展示如何用簡單的數學知道轉向是什麼。您將不得不根據自己的需要進行調整。你可以看到它只會是「玩家2」打開一個轉數,其中轉數除以2沒有餘數。只記得在每一步之後都要增加轉身次數。

沒有「好」的答案。你是編寫代碼的人,你可以決定它是誰,你只需要跟蹤它。

int turn = 1; 

for () { 
    if (turn % 2 == 0) { 
     System.out.println("Player 2"); 
    } else { 
     System.out.println("Player 1"); 
    } 
    turn++; 
} 
+0

嘿,隊友感謝您的回覆。我試圖檢查看看它是誰。如果是玩家1,則玩家1進行移動。否則,如果它是玩家2玩家2進行移動。 –

+0

你在追蹤它是什麼樣的動作,你可以推斷我的例子來處理「移動」而不是「轉」數字。如果「移動1」總是屬於玩家1,那麼如果您知道移動,您應該始終知道誰在玩。 – mstorkson