package game;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FiveChess implements Chess{
private static List<Point> StorBlack;
private static List<Point> StorWhite;
private static List<Point> Stor;
int color = 0 ; //0是白色
private int size ;
public FiveChess(int max){
if(max > 9)
size = max ;
else
size = 9 ;
Stor = new ArrayList<Point>();
StorBlack = new ArrayList<Point>();
StorWhite = new ArrayList<Point>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FiveChess c = new FiveChess(0);
while(true){
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
String sentence;
try {
sentence = inFromUser.readLine();
String[] in = sentence.split(" ");
if(in.length<2) continue ;
int x = Integer.parseInt(in[0]);
int y = Integer.parseInt(in[1]);
c.insert(x, y);
c.print();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
continue ;
}
}
}
@Override
public void insert(int x, int y) {
// TODO Auto-generated method stub
if( x <= size && y <= size ){//判断是否是合法输入
if(!Stor.contains(new Point(x, y))){
if(check(x, y, 0)) win() ;
else {
inter(x , y);
System.out.println("insert" + x + "," + y);
convert() ;
}//换边
}
else System.out.println("重复子!");
}
else System.out.println("错误的位置!");
}
public void convert(){
if(color == 0) color = 1;
else color = 0;
}
public void inter(int x , int y){
if(color == 0 ){
Stor.add(new Point(x ,y));
StorWhite.add(new Point(x ,y));
}
else{
Stor.add(new Point(x ,y));
StorBlack.add(new Point(x ,y));
}
}
@Override
public boolean check(int x, int y , int flag) {
// TODO Auto-generated method stub
if(checkUp(x, y, color, flag)||checkDown(x, y, color, flag)||checkLeft(x, y, color, flag)||checkRight(x, y, color, flag)
||checkLD(x, y, color, flag) || checkLU(x, y, color, flag)||checkRD(x, y, color, flag)||checkRU(x, y, color, flag)){
return true ;
}
return false;
}
@Override
public boolean checkUp(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x, y+1))){
return checkUp(x , y + 1 , color , flag++);
}
else{
return false;
}
}
}
public List<Point> choose(){
List<Point> lis ;
if(color == 0) lis = StorWhite ;
else lis = StorBlack ;
return lis;
}
@Override
public void win() {
// TODO Auto-generated method stub
if(color == 0){
System.out.println("白子获胜!");
}
else{
System.out.println("黑子获胜!");
}
}
@Override
public boolean checkRight(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x+1 , y))){
return checkRight(x + 1 , y , color , flag++);
}
else{
return false;
}
}
}
@Override
public boolean checkLeft(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x - 1 , y))){
return checkLeft(x - 1 , y , color , flag++);
}
else{
return false;
}
}
}
@Override
public boolean checkDown(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x , y -1))){
return checkDown(x , y - 1 , color , flag++);
}
else{
return false;
}
}
}
@Override
public boolean checkRU(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x+1 , y +1))){
return checkRU(x + 1 , y + 1 , color , flag++);
}
else{
return false;
}
}
}
@Override
public boolean checkRD(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x +1 , y -1))){
return checkRD(x + 1 , y - 1 , color , flag++);
}
else{
return false;
}
}
}
@Override
public boolean checkLU(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x -1 , y +1))){
return checkLU(x - 1 , y + 1 , color , flag++);
}
else{
return false;
}
}
}
@Override
public boolean checkLD(int x, int y, int color, int flag) {
// TODO Auto-generated method stub
if(flag == 4){
return true;
}
else{
List<Point> stor = choose();
if(stor.contains(new Point(x-1 , y -1))){
int fl = flag + 1;
return checkLD(x - 1 , y - 1 , color , fl);
}
else{
return false;
}
}
}
@Override
public void print() {
// TODO Auto-generated method stub
for(int i = 1 ; i <= size ; i++){
for(int j = 1 ; j <= size ; j ++){
if(Stor.contains(new Point(i , j))){//存在
if(StorWhite.contains(new Point(i , j))){
System.out.print("|o");
}else
System.out.print("|*");
}
else{
System.out.print("|-");
}
}
System.out.print("|");
System.out.print("\n");
}
}
}
chess接口
package game;
public interface Chess {
public void insert(int x , int y );
public boolean checkUp(int x , int y , int color , int flag);
public boolean checkRight(int x , int y , int color , int flag);
public boolean checkLeft(int x , int y , int color , int flag);
public boolean checkDown(int x , int y , int color , int flag);
public boolean checkRU(int x , int y , int color , int flag);
public boolean checkRD(int x , int y , int color , int flag);
public boolean checkLU(int x , int y , int color , int flag);
public boolean checkLD(int x , int y , int color , int flag);
public void win();
public boolean check(int x, int y , int flag);
public void print();
}
Point类
package game;
public class Point {
private int x ;
private int y ;
public Point(int x , int y){
this.x = x ;
this.y = y ;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj == null){
return false;
}
else{
Point o = (Point) obj;
if(o.x == this.x && o.y == this.y){
return true ;
}
else{
return false ;
}
}
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return x * 419 + y * 115;
}
}
完成输出
1 1
insert1,1
|o|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
2 3
insert2,3
|o|-|-|-|-|-|-|-|-|
|-|-|*|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|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