Java基礎実習項目————五子棋
90570 ワード
package com.bzu.test;
import java.util.Scanner;
/*
:FiveChessGame.java
: ( )
:
:2018.11.11
:2018.11.19
:
*/
//
public class FiveChessGame
{
//
static char[][] chess={
{
' ','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}, // 0
{
'0','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}, // 1
{
'1','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'2','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'3','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}, // 4
{
'4','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'5','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'6','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'7','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'8','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'9','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'a','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}, // 11
{
'b','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'c','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'd','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'e','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{
'f','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'} // 16
};
// main
public static void main(String[] args)
{
//
// for(int i=0;i
// for(int j=0;j
// System.out.print(chess[i][j]+" ");
// }
// System.out.println(); //
// }
// printChess ,
printChess();
//
boolean flag=true;// flag true, ; flag false,
// Scanner
Scanner sc=new Scanner(System.in);
while(true){
//
if(flag){
//
System.out.println(" :");
String blackStr=sc.next(); //
// System.out.println(" :"+blackStr);
//charAt( ) "34" "af" "be"
char cB1=blackStr.charAt(0);
char cB2=blackStr.charAt(1);
// System.out.println(" :"+cB1);
// System.out.println(" :"+cB2);
// ,
// isHaveChess , :1 0
int numB=isHaveChess(cB1,cB2);
if(numB==1){
// if , ,
System.out.println(" , ");
flag=true; // flag true,
}else{
// else , , ,
// a ,f , 11 16
//chess[11][16]='@';
// updateBlackChess , , * @
updateBlackChess(cB1,cB2);
// , @ ,
//for(int i=0;i
//for(int j=0;j
//System.out.print(chess[i][j]+" ");
//}
//System.out.println();//
//}
// printChess ,
printChess();
// , , ; ,
// isBlackWin ,
boolean bWin=isBlackWin();
if(bWin){
// ,
System.out.println(" , ");
break; // ,while ,
}else{
//
flag=false; // ,flag false,
}
}
}
//
if(!flag){
//
System.out.println(" :");
String whiteStr=sc.next(); //
// System.out.println(" :"+whiteStr);
// ,
char cW1=whiteStr.charAt(0);
char cW2=whiteStr.charAt(1);
// System.out.println(" :"+cW1);
// System.out.println(" :"+cW2);
// isHaveChess ,
int numW=isHaveChess(cW1,cW2);
if(numW==1){
//
System.out.println(" , ");
flag=false; //
}else{
//
// 3 6 , 4 7
//chess[4][7]='0';
// updateWhilteChess
updateWhiteChess(cW1,cW2);
// , 0,
//for(int i=0;i
//for(int j=0;j
//System.out.print(chess[i][j]+" ");
//}
//System.out.println();//
//}
// printChess ,
printChess();
boolean TWin=isWhiteWin();
if(TWin){
// ,
System.out.println(" , ");
break; // ,while ,
}else{
//
flag=true; // ,flag false,
}
}
}
}
} //main
// --
public static void printChess(){
for(int i=0;i<chess.length;i++){
// i , 0-16
for(int j=0;j<chess[i].length;j++){
// j , 0-16
System.out.print(chess[i][j]+" ");
}
System.out.println();//
}
}
// -- , * @
//c1 c2 a f 3 5
public static void updateBlackChess(char c1,char c2){
// : a f c1=a c2=f chess[11][16]='@'
// 3 5 c1=3 c2=5 chess[4][6]='@'
// 1 2 2 3 chess[2][3]='@'
// : , , * @
char[] cr={
' ','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int x=0;
int y=0;
//c1 c2 x y
//c1=a c2=f ---> x=11 y=16
for(int i=0;i<cr.length;i++){
// i cr , 0-16
if(cr[i]==c1){
// if , i
x=i;
}
if(cr[i]==c2){
// if , i
y=i;
}
}
// x y, , * @
chess[x][y]='@';
}
// -- , * 0
public static void updateWhiteChess(char c1,char c2){
int x=0;
int y=0;
//c1 c2 x y
char[] cr={
' ','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for(int i=0;i<cr.length;i++){
// i cr
if(cr[i]==c1){
x=i;
}
if(cr[i]==c2){
y=i;
}
}
// * 0
chess[x][y]='0';
}
// -- ,
//c1 c2 1 0
public static int isHaveChess(char c1,char c2){
// : c1 c2, , 0 @
int x=0;
int y=0; //x y
char[] cr={
' ','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for(int i=0;i<cr.length;i++){
// i cr , 0-16
if(cr[i]==c1){
//
x=i;
}
if(cr[i]==c2){
//
y=i;
}
}
// x y,
// , 1; , 0
if(chess[x][y]=='0' || chess[x][y]=='@'){
//if ,
return 1;
}else{
//else ,
return 0;
}
}
// --
// true, ; false,
public static boolean isBlackWin(){
// - 5 @
for(int i=1;i<chess.length;i++){
// i , 1-16
//i=1 j=1 2 3 4 5 6 .... 12
//i=2 j=1 2 3 4 5 6 .... 12
//...
//i=16 j=1 2 3 4 5 6 .... 12
for(int j=1;j<chess[i].length-4;j++){
// j , 1-12
if(chess[i][j]=='@'&&chess[i][j+1]=='@'&&chess[i][j+2]=='@'&&chess[i][j+3]=='@'&&chess[i][j+4]=='@'){
// , 5 @
return true;
}
}
}
// | 5 @
for(int j=1;j<chess[0].length;j++){
// j chess[0].length 0 17 j 1-16
//j=1 i=1 2 3 4 5 6 7 ...12
//j=2 i=1 2 3 4 5 6 7 ...12
//...
//j=16 i=1 2 3 4 5 6 7... 12
for(int i=1;i<chess.length-4;i++){
// i , 1 , 12
if(chess[i][j]=='@'&&chess[i+1][j]=='@'&&chess[i+2][j]=='@'&&chess[i+3][j]=='@'&&chess[i+4][j]=='@'){
// , 5 @
return true;
}
}
}
// ‘@’
for(int i = 1; i< chess.length-4;i++){
for(int j = 5;j<chess[i].length;j++){
if(chess[i][j] == '@' && chess[i+1][j-1]=='@'&& chess[i+2][j-2]=='&' && chess[i+3][j-3]=='@'&&chess[i+4][j-4]=='@'){
return true;
}
}
}
// ‘@’
for(int i =1 ; i<chess.length-4;i++){
// i , 1 , 12
for(int j = 1;j<chess[i].length-4; j++) {
// j , 1 , 12
if (chess[i][j] == '@' && chess[i+1][j+1] == '@' && chess[i+2][j+2] == '@' && chess[i+3][j+3] == '@' && chess[i+4][j+4]=='@') {
// ,
return true;
}
}
}
return false;
}
// --
// true, , false,
public static boolean isWhiteWin() {
// 5 0
for (int i = 1 ;i < chess.length;i++ )
{
for (int j = 1 ;j <chess[i].length-4;j++) {
if (chess[i][j]=='0'&&chess[i][j+1]=='0'&& chess[i][j+2]=='0'&& chess[i][j+3]=='0'&&chess[i][j+4]=='0') {
return true;
}
}
}
// 5 0
for(int j = 1 ; j <chess[0].length ;j++) {
for(int i = 1 ;i < chess.length-4;i++) {
if (chess[i][j]=='0'&&chess[i+1][j]=='0'&& chess[i+2][j]=='0'&& chess[i+3][j]=='0'&&chess[i+4][j]=='0') {
return true;
}
}
}
// / 5 0
for(int i = 1 ;i <chess.length-4;i++)
{
for(int j =5 ;j<chess[i].length;j++) {
if(chess[i][j]== '0'&&chess[i+1][j-1]== '0'&&chess[i+2][j-2]== '0'&&chess[i+3][j-3]== '0'&&chess[i+4][j-4]== '0'){
return true;
}
}
}
// \ 5 0
for(int i = 1 ;i <chess.length-4;i++){
for (int j = 1; j <chess[i].length-4;j++ ) {
if(chess[i][j]== '0'&&chess[i+1][j+1]== '0'&&chess[i+2][j+2]== '0'&&chess[i+3][j+3]== '0'&&chess[i+4][j+4]== '0') {
return true;
}
}
}
return false;
}
}