首先要说明一下,UI做的有点丑,到时候可以自己改一下哈
python版可以移步到我的博客寻找哦!!!
可以在安卓真机上安装,apk自取链接👇
链接: https://pan.baidu.com/s/1Wsiq8NNDOSAViGOXVtU4gw?pwd=5pup
提取码: 5pup
复制这段内容后打开百度网盘手机App,操作更方便哦
游戏设计思路:
1.游戏的答案由系统时间选择
2.由玩家选择游戏难度,分为地狱模式,一般模式和简单模式,以此来限定游戏的次数
3.每次猜测结束后系统会给出玩家判断,猜测的数字与答案偏大还是偏小
4.结束游戏后可以供玩家选择是否继续游戏,查看历史答题记录,结束游戏
MainActivity.java 代码如下:
package cn.itcast.test;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
//存放历史记录
List<String> history = new ArrayList<String>();
//产生随机数
Random r = new Random();
int answer = r.nextInt(1024);
//已经尝试的次数
int try_num = 0;
//剩余尝试次数
int rest_try_num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_game);
TextView introduce = (TextView) findViewById(R.id.introduce_game);
//初始化游戏
initgame();
//开始游戏
start.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
introduce.setText("请输入相应的数字代码选择游戏难度(默认为一般模式):\n" +
"1.地狱模式(3次机会)\n" +
"2.一般模式(10次机会)\n" +
"3.简单模式(100次机会)");
choose_grade();
}
});
}
//初始化游戏
public void initgame() {
TextView introduce = (TextView) findViewById(R.id.introduce_game);
TextView result = (TextView) findViewById(R.id.result_game);
introduce.setText("游戏说明:若要开始游戏请点击开始游戏,开始游戏后每次在输入框输入相应内容后请点击提交按钮");
result.setText("");
}
//选择模式方法
public void choose_grade() {
Button submit = (Button) findViewById(R.id.submit_game);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView result = (TextView) findViewById(R.id.result_game);
EditText input = (EditText) findViewById(R.id.input_game);
String input_result = input.getText().toString();
if (input_result.equals("1")) {
rest_try_num = 3;
result.setText("当前为地狱模式,您共有3次机会,祝您好运");
start_play();
} else if (input_result.equals("2")) {
rest_try_num = 10;
result.setText("当前为一般模式,您共有10次机会,祝您好运");
start_play();
} else if (input_result.equals("3")) {
rest_try_num = 100;
result.setText("当前为简单模式,您共有100次机会,祝您好运");
start_play();
} else {
Toast.makeText(MainActivity.this, "错误代码", Toast.LENGTH_SHORT).show();
}
}
});
}
//正式开始游戏
@SuppressLint("SetTextI18n")
public void start_play() {
TextView introduce = (TextView) findViewById(R.id.introduce_game);
TextView result = (TextView) findViewById(R.id.result_game);
Button submit = (Button) findViewById(R.id.submit_game);
while (try_num < (rest_try_num + try_num)) {
introduce.setText("请输入您猜的数字");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = (EditText) findViewById(R.id.input_game);
String guess_answer = input.getText().toString();
if ((Integer.parseInt(guess_answer) < answer) && rest_try_num >= 0) {
history.add(guess_answer);
result.setText("您输入的数字比答案小!\n" + "您还剩下" + rest_try_num + "次机会,祝您好运");
} else if ((Integer.parseInt(guess_answer) > answer) && rest_try_num >= 0) {
history.add(guess_answer);
result.setText("您输入的数字比答案大!\n" + "您还剩下" + rest_try_num + "次机会,祝您好运");
} else if ((Integer.parseInt(guess_answer) == answer) && rest_try_num >= 0) {
history.add(guess_answer);
result.setText("恭喜您,回答正确!\n" + "您还剩下" + rest_try_num + "次机会,祝您好运");
history.add("成功");
choosePro();
}
try_num += 1;
rest_try_num -= 1;
if (rest_try_num == 1) {
result.setText("您只剩下1次机会啦!!!");
} else if (rest_try_num == 0) {
result.setText("很遗憾!您猜错的次数太多,游戏结束!");
history.add("失败");
choosePro();
}
}
});
break;
}
//此时可以结束游戏
Button over = (Button) findViewById(R.id.over_game);
over.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
overgame();
}
});
}
//游戏结束后选择是否继续游戏或者查看历史记录
@SuppressLint("SetTextI18n")
public void choosePro() {
TextView introduce = (TextView) findViewById(R.id.introduce_game);
TextView result = (TextView) findViewById(R.id.result_game);
Button submit = (Button) findViewById(R.id.submit_game);
introduce.setText("请输入相应的数字代码选择你想要进行的项目:\n" +
" 1.历史记录\n" +
" 2.继续游戏\n" +
" 3.结束游戏");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = (EditText) findViewById(R.id.input_game);
String input_result = input.getText().toString();
if (input_result.equals("1")) {
for (String s : history) {
result.setText(s);
}
} else if (input_result.equals("2")) {
choose_grade();
} else if (input_result.equals("3")) {
//此时可以结束游戏
Button submit = findViewById(R.id.submit_game);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView introduce = (TextView) findViewById(R.id.introduce_game);
TextView result = (TextView) findViewById(R.id.result_game);
introduce.setText("请输入相应的数字代码选择游戏难度(默认为一般模式):\n" +
"1.地狱模式(3次机会)\n" +
"2.一般模式(10次机会)\n" +
"3.简单模式(100次机会)");
result.setText("");
input.setText("");
}
});
}
}
});
}
//结束游戏方法
@SuppressLint("SetTextI18n")
public void overgame() {
Button over = (Button) findViewById(R.id.over_game);
EditText input = (EditText) findViewById(R.id.input_game);
over.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView introduce = (TextView) findViewById(R.id.introduce_game);
TextView result = (TextView) findViewById(R.id.result_game);
introduce.setText("请输入相应的数字代码选择游戏难度(默认为一般模式):\n" +
"1.地狱模式(3次机会)\n" +
"2.一般模式(10次机会)\n" +
"3.简单模式(100次机会)");
result.setText("");
input.setText("");
}
});
}
}
下面是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#686767"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- 标题栏-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#03A9F4"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center"
android:text="游戏"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
<!-- 规则-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3F51B5"
android:gravity="center"
android:text="猜数字"
android:textColor="#03A9F4"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="游戏规则"
android:textColor="@color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/introduce_game"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="top"
android:background="@color/white"
android:text=""
android:textColor="@color/black"
android:textSize="20dp"
tools:ignore="HardcodedText" />
</LinearLayout>
<!-- 结果-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结果"
android:textColor="@color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/result_game"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@color/white"
android:textColor="@color/black" />
</LinearLayout>
<!-- 输入-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="输入"
android:textColor="@color/black"
android:textSize="20dp" />
<EditText
android:id="@+id/input_game"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="@color/white"
android:hint="请输入内容"
android:textColor="@color/black"
android:textColorHint="#03A9F4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/submit_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#B7DAB4"
android:text="提交" />
<Button
android:id="@+id/start_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#B7DAB4"
android:text="开始游戏" />
<Button
android:id="@+id/over_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B7DAB4"
android:text="结束游戏" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
具体实现就是这样啦,技术很菜,希望大佬指点🙏