2017-01-24 41 views
0

我正在研究四連勝的遊戲。 但我遇到了一個問題。我已經能夠使遊戲工作。但我想知道是否可以將我的public void fillBoard()和public void presentBoard()移動到另一個類中。這是因爲我想讓代碼更有條理。我該如何將其轉移到其他職業

package com.company; 

public class Main { 

    public static void main(String[] args) 
    { 
     GameMechanics game = new GameMechanics(); 
     game.play(); 
    } 
} 

package com.company; 

import java.util.Scanner; 

public class GameMechanics 
{ 

    /* 
    This is my local variables 
    */ 

    public Scanner scanner = new Scanner(System.in); 
    public char token; 
    public int column; 
    public int player = 2; 
    public int turn = 2; 
    public int count = 0; 
    public boolean gameRunning = true; 

    public void play() 
    { 
     this.createBoard(); 
     //While gameRunning is true, the methods inside the { } will run, and that's the 4InARow game 
     while (gameRunning) 
     { 
      this.presentBoard(); 
      this.changeTurn(); 
      this.dropToken(); 
      this.gameWon(); 
     } 
     presentBoard(); 

    } 

    public void gameWon() 
    { 
     this.winConHorizontal(); 
     this.winConVertical(); 
    } 

    private char[][] board = new char[6][7]; 

    //Creating my board and assign "space" to all the fields in the array. 
    public void createBoard() { 
     for (int i = 0; i < 6; i++) { 
      for (int j = 0; j < 7; j++) { 
       board[i][j] = ' '; 
      } 
     } 
    } 

    //Presents the board, it prints the board with |"space"| so it looks more like a gameboard. 
    public void presentBoard() { 
     for (int i = 0; i < 6; i++) { 
      for (int j = 0; j < 7; j++) { 
       if (j == 0) { 
        System.out.print("|"); 
       } 
       System.out.print(board[i][j] + "|"); 
      } 
      System.out.println(); 
     } 
    } 

    public void changeTurn() { 
     if (this.turn == this.player) { 
      this.turn = 1; 
      this.token = 'X'; 
     } else { 
      this.turn++; 
      this.token = 'O'; 
     } 
    } 

    public void dropToken() { 
     System.out.println("player " + turn + ": press 1-7 to drop the token"); 
     column = scanner.nextInt() - 1; 
     //If pressed any intValue outside the board, it will tell you to try again. 
     if (column >= 7 || column <= -1) 
     { 
      System.out.println("place the token inside the bord"); 
      changeTurn(); 
     } else { 
      //Drops the token and replace it with playerChar. 
      for (int i = 5; i > -1; i--) { 
       if (board[i][column] == ' ') 
       { 
        board[i][column] = token; 
        break; 
       } 
      } 
     } 
    } 

    public boolean winConHorizontal() { 

     while (gameRunning) { 
      for (int i = 0; 6 > i; i ++) { 
       for (int j = 0; 7 > j; j ++) { 
        if (board[i][j] == this.token) { 
         count ++; 
        } else { 
         count = 0; 
        } 
        if (count >= 4) { 
         System.out.println("player " + (turn) + " Wins!!!!"); 
         gameRunning = false; 
        } 
       } 
      } 
      break; 
     } 
     return gameRunning; 
    } 

    public boolean winConVertical() { 

     while (gameRunning) { 
      for (int i = 0; 7 > i; i ++) { 
       for (int j = 0; 6 > j; j ++) { 
        if (board[j][i] == this.token) { 
         count ++; 
        } else { 
         count = 0; 
        } 
        if (count >= 4) { 
         System.out.println("player " + (turn) + " Wins!!!!"); 
         gameRunning = false; 
        } 
       } 
      } 
      break; 
     } 
     return gameRunning; 
    } 
} 
+0

看看這個:http://softwareengineering.stackexchange.com/questions/ 66523 /每個類中的多少行數太多-ja-java –

+0

順便說一句,在代碼中缺少'fillBoard#' –

回答

1

是的,你可以讓另一個類,並與當前類GameMechanics擴展它,並在另一個類中可以定義的功能。

注意:這是一個非常簡單的平易近人的方式。 否則更好,如果你管理你的類和接口類似於mvc模型,你可以搜索youtube爲mvc結構java。

+0

您可以,但[想要](https:// en。 m.wikipedia.org/wiki/Composition_over_inheritance)? –

2

一個簡單的方法來做到這一點如下:

  1. 提取您char[][] board到自己的類,例如Board 說類可以將該功能char getField(int index)
  2. 提取,,呈現」你的代碼的一部分到另一個類,如BoardPresenter。所述類應該具有的功能presentBoard(Board board)其內部使用Board類的getField(int index)

通過這樣做,您可以抽象出您的內部電路板存儲機制,同時減少類別所具有的職責數量(請參閱https://en.wikipedia.org/wiki/Single_responsibility_principle