为基本问题提前道歉。我已经写了一些代码,增强而不是编程一个连接4游戏,而一个球员是对两个球员。我的问题是,我似乎无法打成平局,除非r(即玩家)获胜,否则它将继续运行。另外,我注意到的另一件事是,代码中的以下内容似乎总是正确的:
if (player=='R')
请你建议,我怎样才能克服这一点,使赢家可以是三个球员(r,b或y)中的任何一个。以下是完整的代码:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[][] board = new char[6][7];
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length; col++) {
board[row][col] = ' ';
}
}
int go = 1;
char player = 'R';
char cpu1 = 'B';
char cpu2 = 'Y';
boolean winner = false;
while (winner == false && go <= 42) {
boolean validMove;
int play;
int cpu1play;
int cpu2play;
do {
display(board);
System.out.println("Player " + player + " choose a column to put your token into");
play = input.nextInt();
Random rand = new Random();
cpu1play = rand.nextInt(6);
cpu2play = rand.nextInt(6);
validMove = validate(play, board);
validMove = validate(cpu1play, board);
validMove = validate(cpu2play, board);
} while (validMove == false);
///Player
for (int row = board.length - 1; row >= 0; row--) {
if (board[row][play] == ' ') {
board[row][play] = player;
break;
}
}
// CPU1
for (int row = board.length - 1; row >= 0; row--) {
if (board[row][cpu1play] == ' ') {
board[row][cpu1play] = cpu1;
break;
}
}
// CPU2
for (int row = board.length - 1; row >= 0; row--) {
if (board[row][cpu2play] == ' ') {
board[row][cpu2play] = cpu2;
break;
}
}
winner = isWinner(player, board);
go++;
}
display(board);
if (winner){
if (player=='R'){
System.out.println("You won");
}else if (player == 'B'){
System.out.println("Cpu1 won");
}else if (player == 'Y'){
System.out.println("Cpu2 won");
}
}else{
System.out.println("Tie game");
}
}
public static boolean validate(int column, char[][] board){
//valid column?
if (column < 0 || column > board[0].length){
return false;
}
//full column?
if (board[0][column] != ' '){
return false;
}
return true;
}
public static void display ( char[][] board){
System.out.println("---------------");
for (int row = 0; row < board.length; row++) {
System.out.print("|");
for (int col = 0; col < board[0].length; col++) {
System.out.print(board[row][col]);
System.out.print("|");
}
System.out.println();
System.out.println("---------------");
}
System.out.println(" 0 1 2 3 4 5 6");
System.out.println();
}
public static boolean isWinner(char player, char[][] board){
//check for 4 across
for(int row = 0; row<board.length; row++){
for (int col = 0;col < board[0].length - 3;col++){
if (board[row][col] == player &&
board[row][col+1] == player &&
board[row][col+2] == player &&
board[row][col+3] == player){
return true;
}
}
}
//check for 4 up and down
for(int row = 0; row < board.length - 3; row++){
for(int col = 0; col < board[0].length; col++){
if (board[row][col] == player &&
board[row+1][col] == player &&
board[row+2][col] == player &&
board[row+3][col] == player){
return true;
}
}
}
//check upward diagonal
for(int row = 3; row < board.length; row++){
for(int col = 0; col < board[0].length - 3; col++){
if (board[row][col] == player &&
board[row-1][col+1] == player &&
board[row-2][col+2] == player &&
board[row-3][col+3] == player){
return true;
}
}
}
//check downward diagonal
for(int row = 0; row < board.length - 3; row++){
for(int col = 0; col < board[0].length - 3; col++){
if (board[row][col] == player &&
board[row+1][col+1] == player &&
board[row+2][col+2] == player &&
board[row+3][col+3] == player){
return true;
}
}
}
return false;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!