题目要求
代码示例
package ch08Q20 ;
import java. util. Scanner ;
public class ch08Q20 {
public static void main ( String [ ] args) {
final int ROW_CHESS = 6 ;
final int COL_CHESS = 7 ;
int counter = 0 ;
int winner;
int [ ] [ ] chess = new int [ ROW_CHESS] [ COL_CHESS] ;
while ( counter < ( ROW_CHESS * COL_CHESS) ) {
display ( chess) ;
chess = input ( chess, counter) ;
counter++ ;
winner = checkWinner ( chess) ;
if ( winner == 1 ) {
display ( chess) ;
System . out. println ( "The red player won!" ) ;
System . exit ( 0 ) ;
} else if ( winner == 2 ) {
display ( chess) ;
System . out. println ( "The yellow player won!" ) ;
System . exit ( 0 ) ;
}
}
display ( chess) ;
System . out. println ( "You are tied!" ) ;
System . exit ( 0 ) ;
}
public static void display ( int [ ] [ ] chess) {
for ( int i = ( chess. length - 1 ) ; i >= 0 ; i-- ) {
for ( int j = 0 ; j < ( chess[ i] . length * 2 + 1 ) ; j++ ) {
if ( j % 2 == 0 ) {
System . out. print ( "|" ) ;
} else {
if ( chess[ i] [ ( j - 1 ) / 2 ] == 1 ) {
System . out. print ( "R" ) ;
} else if ( chess[ i] [ ( j - 1 ) / 2 ] == 2 ) {
System . out. print ( "Y" ) ;
} else {
System . out. print ( " " ) ;
}
}
}
System . out. println ( ) ;
}
System . out. println ( "---------------" ) ;
}
public static int [ ] [ ] input ( int [ ] [ ] chess, int counter) {
int dropChess;
if ( ( counter % 2 ) == 0 ) {
System . out. print ( "Drop a red disk at column (0-6): " ) ;
} else if ( ( counter % 2 ) == 1 ) {
System . out. print ( "Drop a yellow disk at column (0-6): " ) ;
}
Scanner scanner = new Scanner ( System . in) ;
dropChess = scanner. nextInt ( ) ;
if ( dropChess >= 0 && dropChess <= 6 ) {
if ( ( counter % 2 ) == 0 ) {
for ( int i = 0 ; i <= 6 ; i++ ) {
if ( i < 6 && chess[ i] [ dropChess] == 0 ) {
chess[ i] [ dropChess] = 1 ;
break ;
} else if ( i == 6 ) {
System . out. println ( "You have drop a out-of-bounds disk!" ) ;
System . out. println ( "Please resume the game!" ) ;
System . exit ( 1 ) ;
}
}
} else if ( ( counter % 2 ) == 1 ) {
for ( int i = 0 ; i <= 6 ; i++ ) {
if ( i < 6 && chess[ i] [ dropChess] == 0 ) {
chess[ i] [ dropChess] = 2 ;
break ;
} else if ( i == 6 ) {
System . out. println ( "You have drop a out-of-bounds disk!" ) ;
System . out. println ( "Please resume the game!" ) ;
System . exit ( 1 ) ;
}
}
}
} else {
System . out. println ( "You have drop a out-of-bounds disk!" ) ;
System . out. println ( "Please resume the game!" ) ;
System . exit ( 1 ) ;
}
return chess;
}
public static int checkWinner ( int [ ] [ ] chess) {
int winnerRow, winnerCol, winnerDiag, winner = 0 ;
winnerRow = checkWinnerRow ( chess) ;
winnerCol = checkWinnerCol ( chess) ;
winnerDiag = checkWinnerDiag ( chess) ;
if ( winnerRow == 1 || winnerCol == 1 || winnerDiag == 1 ) {
winner = 1 ;
} else if ( winnerRow == 2 || winnerCol == 2 || winnerDiag == 2 ) {
winner = 2 ;
}
return winner;
}
public static int checkWinnerRow ( int [ ] [ ] chess) {
int winnerRow = 0 ;
for ( int i = 0 ; i < 6 ; i++ ) {
for ( int j = 0 ; j < 4 ; j++ ) {
if ( chess[ i] [ j] == chess[ i] [ j + 1 ] && chess[ i] [ j + 1 ] == chess[ i] [ j + 2 ] && chess[ i] [ j + 2 ] == chess[ i] [ j + 3 ] && chess[ i] [ j] == 1 ) {
winnerRow = 1 ;
} else if ( chess[ i] [ j] == chess[ i] [ j + 1 ] && chess[ i] [ j + 1 ] == chess[ i] [ j + 2 ] && chess[ i] [ j + 2 ] == chess[ i] [ j + 3 ] && chess[ i] [ j] == 2 ) {
winnerRow = 2 ;
}
}
}
return winnerRow;
}
public static int checkWinnerCol ( int [ ] [ ] chess) {
int winnerCol = 0 ;
for ( int i = 0 ; i < 7 ; i++ ) {
for ( int j = 0 ; j < 3 ; j++ ) {
if ( chess[ j] [ i] == chess[ j + 1 ] [ i] && chess[ j + 1 ] [ i] == chess[ j + 2 ] [ i] && chess[ j + 2 ] [ i] == chess[ j + 3 ] [ i] && chess[ j] [ i] == 1 ) {
winnerCol = 1 ;
} else if ( chess[ j] [ i] == chess[ j + 1 ] [ i] && chess[ j + 1 ] [ i] == chess[ j + 2 ] [ i] && chess[ j + 2 ] [ i] == chess[ j + 3 ] [ i] && chess[ j] [ i] == 2 ) {
winnerCol = 2 ;
}
}
}
return winnerCol;
}
public static int checkWinnerDiag ( int [ ] [ ] chess) {
int winnerDiag = 0 ;
for ( int i = 0 ; i < 6 ; i++ ) {
for ( int j = 0 ; j < 7 ; j++ ) {
if ( ( ( i + 3 ) < 6 ) && ( ( j + 3 ) < 7 ) ) {
if ( chess[ i] [ j] == chess[ i + 1 ] [ j + 1 ] && chess[ i + 1 ] [ j + 1 ] == chess[ i + 2 ] [ j + 2 ] && chess[ i + 2 ] [ j + 2 ] == chess[ i + 3 ] [ j + 3 ] && chess[ i] [ j] == 1 ) {
winnerDiag = 1 ;
break ;
} else if ( chess[ i] [ j] == chess[ i + 1 ] [ j + 1 ] && chess[ i + 1 ] [ j + 1 ] == chess[ i + 2 ] [ j + 2 ] && chess[ i + 2 ] [ j + 2 ] == chess[ i + 3 ] [ j + 3 ] && chess[ i] [ j] == 2 ) {
winnerDiag = 2 ;
break ;
}
} else if ( ( ( i - 3 ) >= 0 ) && ( ( j + 3 ) < 7 ) ) {
if ( chess[ i] [ j] == chess[ i - 1 ] [ j + 1 ] && chess[ i - 1 ] [ j + 1 ] == chess[ i - 2 ] [ j + 2 ] && chess[ i - 2 ] [ j + 2 ] == chess[ i - 3 ] [ j + 3 ] && chess[ i] [ j] == 1 ) {
winnerDiag = 1 ;
break ;
} else if ( chess[ i] [ j] == chess[ i - 1 ] [ j + 1 ] && chess[ i - 1 ] [ j + 1 ] == chess[ i - 2 ] [ j + 2 ] && chess[ i - 2 ] [ j + 2 ] == chess[ i - 3 ] [ j + 3 ] && chess[ i] [ j] == 2 ) {
winnerDiag = 2 ;
break ;
}
}
}
}
return winnerDiag;
}
}