在Android Studio中写一个石头剪刀布的完整代码,要求人和电脑比拼,并且判断胜负情况
时间: 2024-11-04 18:11:08 浏览: 20
使用安卓实现的石头剪刀布小游戏
在Android Studio中编写一个石头、剪刀、布游戏的基本流程包括创建UI界面、处理用户输入、生成电脑随机选择以及判断胜负。下面是一个简单的示例,我们将使用Java编写:
首先,在Activity的布局文件(如activity_main.xml)中添加按钮和结果展示区域:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/player_choice_button"
android:text="玩家出拳"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/computer_choice_button"
android:text="电脑出拳"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/result_text_view"
android:text="结果:"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
```
然后在MainActivity.java中实现逻辑:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String[] choices = {"石头", "剪刀", "布"};
private TextView resultTextView;
private Button playerButton, computerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.result_text_view);
playerButton = findViewById(R.id.player_choice_button);
computerButton = findViewById(R.id.computer_choice_button);
// 设置点击事件监听器
playerButton.setOnClickListener(playerListener);
computerButton.setOnClickListener(computerListener);
}
private View.OnClickListener playerListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int playerChoice = getChoiceIndexFromUserInput(playerButton.getText().toString());
resultTextView.setText("玩家选择了: " + choices[playerChoice]);
int computerChoice = chooseRandom();
showResult(playerChoice, computerChoice);
}
};
private View.OnClickListener computerListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int computerChoice = chooseRandom();
resultTextView.setText("电脑选择了: " + choices[computerChoice]);
int playerChoice = getChoiceIndexFromUserInput(playerButton.getText().toString());
showResult(playerChoice, computerChoice);
}
};
private int chooseRandom() {
return (int) (Math.random() * choices.length);
}
private int getChoiceIndexFromUserInput(String input) {
if (input.equals("石头")) return 0;
else if (input.equals("剪刀")) return 1;
else if (input.equals("布")) return 2;
else throw new IllegalArgumentException("无效的选择");
}
private void showResult(int player, int computer) {
if (player == computer) {
resultTextView.setText("平局!");
} else if ((player == 0 && computer == 2) || (player == 1 && computer == 0) || (player == 2 && computer == 1)) {
resultTextView.setText("玩家胜利!");
} else {
resultTextView.setText("电脑胜利!");
}
}
}
```
这个示例只是一个基础版本,实际应用中可能还需要处理更多细节,比如循环游戏直到用户退出等。
阅读全文