2017-01-24 4 views
0

私は4行ゲームで作業しています。 しかし、私はそれに問題を抱えています。私はゲームの仕事をすることができました。しかし、私は私の公の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/how-many-lines-per-many-in-java-in- –

+0

[fillboard#]がコードから抜けています –

答えて

1

はい、あなたが別のクラスを作成し、現在のクラスGameMechanicsでそれを拡張し、別のクラスの内部で使用すると、関数を定義することができます。

注:これは非常に簡単な方法です。 それ以外の方が、mvcモデルに似たクラスとインターフェイスを管理していると、YouTubeのmvc構造javaを検索できます。

+0

[https:// en。 m.wikipedia.org/wiki/Composition_over_inheritance)? –

2

次のようにこれを行う簡単な方法は次のとおりです。

  1. は、例えば、独自のクラスにあなたのchar[][] boardを抽出Board サイードクラスは、例えばBoardPresenter。サイードクラスは内部BoardクラスのgetField(int index)を使用する関数presentBoard(Board board)を持っている必要があり、別のクラスに「あなたのコードの一部を提示,, char getField(int index)
  2. が抽出機能を公開することができます。

GameMechanicsクラスが持つ責任の数を減らしながら、内部ボードストレージメカニズムを抽象化します(https://en.wikipedia.org/wiki/Single_responsibility_principle参照)

関連する問題