📄 framework_game.java
字号:
package Framework;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class framework_Game {
static int player1=1;
static int player2=2;
int role;
IGame ch;
int size;
public framework_Game(IGame ch){
this.ch=ch;
}
public int changeTurn(){ //change player
if(role==player1)
role=player2;
else
role=player1;
return role;
}
void setBoardSize(int size){
this.size=size;
ch.setBoardSize(size);
}
public void playChess(IGame ch){
this.startGame();
int x=0;
int y=0;
boolean valid;
while(true){//read valid coordinates
String[] str;
do{
valid=true;
try {
str=this.readLine();
x=Integer.parseInt(str[0])-1;
y=Integer.parseInt(str[1])-1;
if(!ch.isValidStep(x,y)){
System.out.println("Not valid,please write a valid step..");
valid=false;
}
} catch (Exception e) {
System.out.println("Not valid input,please give valid number");
e.printStackTrace();
}
}while(!valid);
ch.playChess(role, x, y);//playChess
this.ch.drawChessBoard();//draw
if(this.ch.isWon(this.role)){
System.out.println("Player"+this.role+" has WON the Game");
break;
}
if(this.ch.isFini()){
System.out.println("No way to go,c'est fini");
break;
}
this.changeTurn();
}
}
public String[] readLine() throws Exception{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
String str []=new String[10];
str=line.split(",");
while(str.length!=2){//must be x,and y two parameters
System.out.println("Must be 2 variable");
line=in.readLine();
str=line.split(",");
}
return str;
}
public void startGame(){
System.out.println("Welcome to Game:"+ch.getGameName());
role=player1;
ch.drawChessBoard();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -