本篇我們實作CreateJoinRoom的介面及程式
這裡我們主要實作3個Button,分別為:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ab7557"
tools:context=".createjoinroom.CreateJoinRoom">
<Button
android:id="@+id/btn_create_room"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="創建房間"
android:textColor="@color/white"
android:textSize="17dp"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline13"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline12" />
<Button
android:id="@+id/btn_join_room"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="加入房間"
android:textColor="@color/white"
android:textSize="17dp"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline15"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline14" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.45" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
<TextView
android:id="@+id/textView9"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
app:layout_constraintBottom_toTopOf="@+id/guideline17"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_back"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="返回"
android:textColor="@color/white"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline17"
app:layout_constraintEnd_toStartOf="@+id/guideline10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline114"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.65" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline115"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<Button
android:id="@+id/btn_ai"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="電腦對弈"
android:textColor="@color/white"
android:textSize="17dp"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline115"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline114" />
</androidx.constraintlayout.widget.ConstraintLayout>
創建及加入房間的原理架構,如下圖所示
使用者選擇創建房間,則新增一筆資料到firebase,此人即視作player1
使用者選擇加入房間,則輸入房間號碼後,根據此號碼可修改指定的資料,此人即視作player2
public class CreateJoinRoom extends AppCompatActivity implements DatabaseContract.view{
private Button btn_create_room,btn_join_room,btn_back,btn_ai;
private Intent intent;
private String date,player1message="",player2message="",winPlayer="",roomKey="",gameMode="";
private Random random = new Random();
private int random_room_number,turn=1;
private CreateBoardData createBoard = new CreateBoardData(this,"");
private DatabasePresenter dbp = new DatabasePresenter(this);
private ArrayList musicList = new ArrayList();
private ArrayList colorList = new ArrayList();
private DialogData dialogData = new DialogData(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_join_room);
btn_create_room = findViewById(R.id.btn_create_room);
btn_join_room = findViewById(R.id.btn_join_room);
btn_ai = findViewById(R.id.btn_ai);
btn_back = findViewById(R.id.btn_back);
getLastPageData();
btn_back.setOnClickListener(v -> {
finish();
});
btn_create_room.setOnClickListener(v -> {
btn_create_room.setEnabled(false);
btn_join_room.setEnabled(false);
random_room_number = random.nextInt(10000);
gameMode="link";
CreateRoom();
});
btn_join_room.setOnClickListener(v -> {
btn_create_room.setEnabled(false);
btn_join_room.setEnabled(false);
intent = new Intent(getApplicationContext(), JoinRoom.class);
intent.putIntegerArrayListExtra("musicData",musicList);
intent.putIntegerArrayListExtra("colorData",colorList);
startActivity(intent);
});
btn_ai.setOnClickListener(v -> {
random_room_number = random.nextInt(10000);
gameMode="ai";
CreateRoom();
});
}
private void CreateRoom(){
dialogData.showBackDialog("房間創建中...");
date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
dbp.addData(String.valueOf(random_room_number),
false,
false,
"white",
createBoard.CreateChessboard(),
createBoard.CreateChessmanMove(),
String.valueOf(turn),
createBoard.CreatePawnMove1(),
createBoard.CreatePawnMove2(),
createBoard.CreateCastlingMove1(),
createBoard.CreateCastlingMove2(),
date,
player1message,
player2message,
winPlayer);
}
public void addSuccess(){
Log.d("TAG", "資料上傳成功");
dbp.getData(String.valueOf(random_room_number));
}
public void addError(){
Log.d("TAG", "資料上傳失敗");
}
public void getSuccess(String getId,
String getRoomNumber,
Boolean getPlayer1OnlineState,
Boolean getPlayer2OnlineState,
String getMoveState,
HashMap<String,Object> getChessboardData,
HashMap<String,Object> getChessmanMoveData,
String getTurn,
HashMap<String,Object> getPawnMove1,
HashMap<String,Object> getPawnMove2,
HashMap<String,Object> getCastlingMove1,
HashMap<String,Object> getCastlingMove2,
String getDate,
String getPlayer1message,
String getPlayer2message,
String getWinPlayer){
Log.d("TAG", "資料獲取成功");
roomKey = getId;
dialogData.dismissBackDialog();
intent = new Intent(getApplicationContext(), Player1ChessGame.class);
intent.putExtra("RoomNumber",String.valueOf(random_room_number));
intent.putIntegerArrayListExtra("musicData",musicList);
intent.putIntegerArrayListExtra("colorData",colorList);
intent.putExtra("gameMode",gameMode);
intent.putExtra("roomKey",roomKey);
startActivity(intent);
}
public void getError(){
Log.d("TAG", "資料獲取失敗");
}
public void uploadSuccess(){
Log.d("TAG", "資料修改成功");
}
public void uploadError(){
Log.d("TAG", "資料修改失敗");
}
public void deleteSuccess(){
Log.d("TAG", "資料刪除成功");
}
public void deleteError(){
Log.d("TAG", "資料刪除失敗");
}
public void getLastPageData(){
musicList = getIntent().getIntegerArrayListExtra("musicData");
colorList = getIntent().getIntegerArrayListExtra("colorData");
}
@Override
protected void onResume() {
super.onResume();
Log.d("TAG", "當前頁面已開啟");
btn_create_room.setEnabled(true);
btn_join_room.setEnabled(true);
}
@Override
protected void onStop() {
super.onStop();
Log.d("TAG", "當前頁面已關閉");
}
}
至此,我們成功實作出創建及加入房間的介面及程式