检查玩家是否在连接四场比赛中获胜

时间:2015-11-14 05:06:24

标签: java

我无法开发一种方法来检查这两个玩家中的一个是否赢了。我试图写一些逻辑来在代码的“public void PlayerWinner”部分找到胜利者。所以这些仅仅是例子,它们甚至都不起作用。任何人都有我可以使用的想法,或简单的修复?这是我的java类介绍。感谢。

package assignment.pkg7;

import java.util.Scanner;

public class Assignment7 {

  public Assignment7() {
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 7; j++) {
        grid[i][j] = ' ';
      }
    }
    currentPlayer = 0;
    stdIn = new Scanner(System.in);

  }

  public void printBoard() {

    System.out.println("+---+---+---+---+---+---+---+");
    for (int row = 0; row < 6; row++) {
      System.out.print("| ");
      for (int i = 0; i < 7; i++) {
        System.out.print(grid[row][i] + " | ");
      }
      System.out.print("  |");
      System.out.println();
      System.out.println("+---+---+---+---+---+---+---+");
    }
  }

  public void playerTurn() {
    System.out.print("Player " + (currentPlayer + 1) + " please enter a column (0-6): ");
    int c = stdIn.nextInt();

    if (currentPlayer == 0) {
      for (int i = 5; i >= 0; i--) {
        if (grid[i][c] == ' ') {
          grid[i][c] = 'Y';
          break;
        }
      }
    }
    if (currentPlayer == 1) {
      for (int i = 5; i >= 0; i--) {
        if (grid[i][c] == ' ') {
          grid[i][c] = 'R';
          break;
        }
      }
    }
    if (currentPlayer == 0) {
      currentPlayer = 1;
    } else {
      currentPlayer = 0;
    }
  }

  public void PlayerWinner() {
    for (int i = 0; i > 1; i++) {
      if (grid[1][6] == 'Y' && grid[1][5] == 'Y' && grid[1][4] == 'Y' && grid[1][3] == 'Y' || grid[1][5] == 'Y' && grid[1][4] == 'Y' && grid[1][3] == 'Y' && grid[1][2] == 'Y' || grid[1][4] == 'Y' && grid[1][3] == 'Y' && grid[1][2] == 'Y' && grid[1][1] == 'Y') {
        System.out.println("Seeing if this works.");
      }
      if (grid[5][6] == 'Y' && grid[5][5] == 'Y') {
        System.out.println("Seeing if this works.");
      }
    }
  }

  private Scanner stdIn;
  private int currentPlayer;
  public char[][] grid = new char[6][7];

  public static void main(String[] args) {

    Assignment7 gameboard = new Assignment7();
    boolean gamecheck = true;
    do {
      gameboard.printBoard();

      gameboard.playerTurn();

      gameboard.PlayerWinner();
    } while (gamecheck = true);

  }
}

0 个答案:

没有答案