android 象棋游戏开发
摘要:这是一款基于GitHub项目修改的中国象棋Android应用,主要功能包括悔棋、重开局、AI等级设置和先手选择等。项目采用Android Studio开发,compileSdkVersion为33,支持Android 5.0(API 24)及以上系统。应用采用SoundPool实现音效播放,SharedPreferences保存游戏配置和进度,并实现了完整的象棋逻辑和UI交互。主要功能模块包括游戏主界面(MainActivity)、棋盘视图(GameBoardView)和游戏逻辑(GameLogic)等,支持加载上次游戏进度,提供多种棋子样式选择。
1、效果展示
基于github项目修改
2、功能支持悔棋、重开、设置等级、优先出棋等
a.工程build代码情况
模块build
compileSdk 33defaultConfig {applicationId "com.hzy.chinese.jchess"minSdk 24targetSdk 32versionCode 5versionName "1.0.4"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}
工程build文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.plugins {id 'com.android.application' version '7.2.2' apply falseid 'com.android.library' version '7.2.2' apply falseid 'io.github.wurensen.android-aspectjx' version '3.3.2' apply false
}task clean(type: Delete) {delete rootProject.buildDir
}
b.chess主页面代码:
package com.hzy.chinese.jchess.activity;import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import com.hzy.chinese.jchess.R;
import com.hzy.chinese.jchess.game.GameConfig;
import com.hzy.chinese.jchess.game.GameLogic;
import com.hzy.chinese.jchess.game.IGameCallback;
import com.hzy.chinese.jchess.view.GameBoardView;import java.util.LinkedList;import butterknife.BindView;
import butterknife.ButterKnife;public class MainActivity extends AppCompatActivityimplements IGameCallback {@BindView(R.id.game_board)GameBoardView mGameBoard;@BindView(R.id.game_progress)ProgressBar mGameProgress;private SoundPool mSoundPool;private LinkedList<Integer> mSoundList;private GameLogic mGameLogic;private SharedPreferences mPreference;private boolean mSoundEnable;private int mHandicapIndex;private boolean mComputerFlip;private int mPieceStyle;private int mAILevel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_chess);ButterKnife.bind(this);mPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());loadDefaultConfig();initSoundPool();initGameLogic();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main_activity, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.main_menu_exit:// 退出finish();break;case R.id.main_menu_retract:// 悔棋mGameLogic.retract();break;case R.id.main_menu_restart:// 重开mGameLogic.restart(mComputerFlip, mHandicapIndex);showMessage(getString(R.string.new_game_started));break;case R.id.main_menu_settings:// 设置startActivity(new Intent(this, SettingsActivity.class));break;}return super.onOptionsItemSelected(item);}@Overrideprotected void onResume() {super.onResume();loadDefaultConfig();mGameLogic.setLevel(mAILevel);mGameBoard.setPieceTheme(mPieceStyle);mGameBoard.invalidate();}@Overrideprotected void onDestroy() {if (mSoundPool != null) {mSoundPool.release();}mPreference.edit().putString(GameConfig.PREF_LAST_FEN, mGameLogic.getCurrentFen()).apply();super.onDestroy();}private void loadDefaultConfig() {// 音效开启状态 默认 开启 true 关闭 falsemSoundEnable = mPreference.getBoolean(getString(R.string.pref_sound_key), true);// 先走让子 0 1 2 3mHandicapIndex = Integer.parseInt(mPreference.getString(getString(R.string.pref_handicap_key), "0"));// 电脑先走 false true 则先走mComputerFlip = mPreference.getBoolean(getString(R.string.pref_who_first_key), false);// 棋子样式 0 1mPieceStyle = Integer.parseInt(mPreference.getString(getString(R.string.pref_piece_style_key), "0"));// 电脑棋力 0 1 2 3 4mAILevel = Integer.parseInt(mPreference.getString(getString(R.string.pref_level_key), "0"));}private void initSoundPool() {mSoundList = new LinkedList<>();int poolSize = GameConfig.SOUND_RES_ARRAY.length;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {mSoundPool = new SoundPool.Builder().setMaxStreams(poolSize).build();} else {mSoundPool = new SoundPool(poolSize, AudioManager.STREAM_MUSIC, 0);}for (int res : GameConfig.SOUND_RES_ARRAY) {mSoundList.add(mSoundPool.load(this, res, 1));}}private void initGameLogic() {mGameLogic = mGameBoard.getGameLogic();mGameLogic.setCallback(this);mGameLogic.setLevel(mAILevel);mGameBoard.setPieceTheme(mPieceStyle);// load last saved gameString lastFen = mPreference.getString(GameConfig.PREF_LAST_FEN, "");if (lastFen.isEmpty()) {mGameLogic.restart(mComputerFlip, mHandicapIndex);} else {showMessage(getString(R.string.load_last_game_finish));mGameLogic.restart(mComputerFlip, lastFen);}}@Overridepublic void postPlaySound(final int soundIndex) {if (mSoundPool != null && mSoundEnable) {int soundId = mSoundList.get(soundIndex);mSoundPool.play(soundId, 1, 1, 0, 0, 1);}}@Overridepublic void postShowMessage(final String message) {runOnUiThread(() -> showMessage(message));}private void showMessage(String message) {Toast.makeText(this, ""+message, Toast.LENGTH_LONG).show();}@Overridepublic void postShowMessage(int messageId) {postShowMessage(getString(messageId));}@Overridepublic void postStartThink() {runOnUiThread(() -> mGameProgress.setVisibility(View.VISIBLE));}@Overridepublic void postEndThink() {runOnUiThread(() -> mGameProgress.setVisibility(View.GONE));}
}
3、源码获取
https://download.csdn.net/download/shi450561200/91070234?spm=1001.2014.3001.5501