1

Connect Four をプレイするためのプログラムを作成しています。すべてのメソッドが完了し、実際にゲームをプレイするためのメイン メソッドを構築しているところです。私のように手動でコードを入力するよりも、私の Play クラスをより効率的に使用する方法はありますか? この方法で勝者を見つける方法がわからないことに気付くまで、私はこれを行っても問題ありませんでした。メソッドを適切に処理し、メイン クラスをより整然と効率的にするために使用できるループはありますか?

これが私のコードです:

import java.util.Scanner;
public class Play {
   public static void main(String[]args){

   Scanner keyboard = new Scanner(System.in);
   System.out.print("Welcome to Connect 4. Please enter your names. \nPlayer 1 Name: ");
   String name1 = keyboard.nextLine();
   System.out.print("Player 2 Name: ");
   String name2 = keyboard.nextLine();
   System.out.println(name1 + " - you have red chips \"R\" and you go first.");
   Connect4Board game = new Connect4Board(6,7);
   System.out.print(game);
   System.out.println("Enter a column between 1-7: ");
   int column = keyboard.nextInt();
   game.add(column, "R");
   System.out.print(game);
   System.out.print(name2 + " - you have yellow chips \"Y\" and you go next.");
   System.out.println("Enter a column between 1-7: ");
   column = keyboard.nextInt();
   game.add(column, "Y");
   System.out.print(game);





  }
}

class Chip { //Chip class determines the color of the chip
   //Instance Variables
   private String colour;

public Chip (String c) { //Constructor 
   colour = c;
}

public String getColour(){ //Get Method
   return colour;
}

public void setColour(String c){ //Set Method
   c = colour;
}

public boolean equals(Chip c){ //Checks if two colors are the same
   return this.colour == c.getColour();
}

public String toString(){ //Converts to String
   return colour; 
    }
 }

class Board { //Creates a board with x rows and x columns.
  //Instance Variables
   private int rows;
   private int cols;

public Board (int r, int c){ //Constructor
   rows = r;
   cols = c;
}

public int getRows(){ //Get Methods
    return rows;
}

public int getCols(){
    return cols;
}

 public void setRows(int r){ //Set Methods
    r = rows;
}

 public void setCols(int c){
   c = cols;
  }
}

class Connect4Board extends Board {
   //Instance Variable
   private Chip [][] board;
   private String colourOfWin = "";


 public Connect4Board(int r, int c){
    super(r, c);
    board = new Chip[r][c];

}

public Chip[][] getBoard(){
        return board;
}

public void setBoard(int r,int c){
        setRows(r);
        setCols(c);
 }

public boolean add(int cols, String colour){
    boolean result = true;
    int counter = 0;
    boolean testPlacement = false;
    if(cols>=0 && cols<=6){
    }
    else
      result = false;
   if (board[5][cols] != null)
      result = false;
    for(int row = 5; row >= 0 && testPlacement == false; row--){
       if(board[row][cols]==null){
         counter += 1;
      }
          else
         testPlacement = true;   
       }
       if(result == true){
          board[6-counter][cols] = new Chip(colour);
         // 
      }
      return result;      
}

 public String winType(){
   String result;

   if(rowWinner()){
 result = "Row Winner";
    }
    else if(columnWinner()){
      result = "Column Winner";
   }   
   else if(rightDiagonalWinner() || leftDiagonalWinner()){
      result = "Diagonal Winner";
   }
   else{
      result = "No winner yet."; 
   }
      return result;
}
public boolean rowWinner(){
   boolean result = false;
   boolean result1 = true; //Test the equals to see if true

   for(int row=0; row<6 && result == false; row++){ //Nested for loops to proccess array. Row
      for(int cols=0; cols<4 && result == false; cols++){  //Column
         for(int win=0; win<4; win++){ //Check win 
                if(board[row][cols] != null && board[row][cols+win] != null){
                    if(board[row][cols+win].equals(board[row][cols])&& result1 == true ){ //result1 tests to see if the equals is true
               }
                   else
                        result1 = false;
                    }
                    else 
                        result1 = false;
                }
            if(result == true)
                colourOfWin = board[row][cols].getColour();
               result = result1;
               }
        }
        return result;
}

 public boolean columnWinner(){
    boolean result = false;
    boolean result1 = true; //Test the equals to see if true

    for(int row=0; row<3 && result == false; row++){ //Nested for loops to proccess array. Row
       for(int cols=0; cols<7 && result == false; cols++){  //Column
         for(int win=0; win<4; win++){ //Check win 
                if(board[row][cols] != null && board[row+win][cols] != null){
                    if(board[row+win][cols].equals(board[row][cols])&& result1 == true ){ //result1 tests to see if the equals is true
               }
                    else
                         result1 = false;
                    }
                   else 
                        result1 = false;
                }
                 if(result == true)
                      colourOfWin = board[row][cols].getColour();
                     result = result1;
                }
         }
           return result;
  }


public boolean rightDiagonalWinner(){
    boolean result = false;
    boolean result1 = true; //Test the equals to see if true

    for(int row=0; row<3 && result == false; row++){ //Nested for loops to proccess array. Row
      for(int cols=0; cols<4 && result == false; cols++){  //Column
         for(int win=0; win<4; win++){ //Check win 
                if(board[row][cols] != null && board[row+win][cols+win] != null){
                     if(board[row+win][cols+win].equals(board[row][cols])&& result1 == true ){ //result1 tests to see if the equals is true
               }
                   else
                        result1 = false;
                    }
                    else 
                        result1 = false;
                }
                 if(result == true)
                     colourOfWin = board[row][cols].getColour();
                    result = result1;
                }
          }
         return result;
 }

  public boolean leftDiagonalWinner(){
     boolean result = false;
     boolean result1 = true; //Test the equals to see if true

     for(int row=0; row<3 && result == false; row++){ //Nested for loops to proccess array. Row
      for(int cols=6; cols<2 && result == false; cols--){  //Column
          for(int win=0; win<4; win++){ //Check win 
                if(board[row][cols] != null && board[row+win][cols-win] != null){
                    if(board[row+win][cols-win].equals(board[row][cols])&& result1 == true ){ //result1 tests to see if the equals is true
                }
                   else
                        result1 = false;
                   }
                    else 
                        result1 = false;
               }
                if(result == true)
                     colourOfWin = board[row][cols].getColour();
                    result = result1;
               }
        }
         return result;
 }

 public boolean winner(){
    return rowWinner() || columnWinner() || rightDiagonalWinner() || leftDiagonalWinner();
 }

  public String toString(){
    String b;
         b="\tCol.\n";
         for (int j=0;j<7;j++){
             b=b+"\t"+(j+1);
         }
         b=b+"\n";
         for (int i=0;i<6;i++){
                b=b+(i+1)+"\t";
             for(int j=0;j<7;j++){
                 if(board[5-i][j]==null){
                    b=b+" \t";
                }
                else{
                    b=b+board[5-i][j].getColour()+"\t";
                }
              }
             b=b+"\n\n";
         }
         return b;
    }
}
4

0 に答える 0