android--Chinese Chess

Source: Internet
Author: User
Tags bit set gety savepoint

Chess, a lot of people contact, Scholars wrote a, the great God can point out ~ directly on the code:

Post the main code, want to download the demo: Chinese Chess demo

Package Wyf.ytl;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.media.mediaplayer;import Android.view.motionevent;import Android.view.surfaceholder;import android.view.surfaceview;/** * This class is the main class of the whole program, is the main game interface * The interface inherits from Surfaceview and implements the Surfaceholder.callback interface * which contains a brush frame for the thread class * */public class Gameview extends Surfaceview implements S Urfaceholder.callback{private tutorialthread thread;//Brush frame thread Timethread timethread; Chessactivity activity;//declaring activity references bitmap qipan;//checkerboard bitmap qizibackground;//chess background picture bitmap win;//victory picture Bitmap lost ;//Failed picture Bitmap ok;//OK button Bitmap vs;//black Red Square vs picture Bitmap right;//right pointer Bitmap left;//left pointer Bitmap current;//"current" text Bitmap exit2;//exit button picture Bitmap sound2;//sound button picture Bitmap sound3;//is currently playing sound bitmap time;//colon bitmap redtime;//red colon bitmap background ;//Background picture MediaPlayer go;//Chess sound Paint paint;//Brush Boolean caipan = true;//is player moves boolean focus = false;//Whether there is currently a selected piece int selectqizi = 0; Of course the selected pieces int Starti, startj;//record the starting position of the current pawn int endi, endj;//record the target position of the current piece bitmap[] Heizi = new bitmap[7];//The picture array of sunspots bitmap[] Hongzi = new bitmap[7];//The picture array of the red child bitmap[] Number = new bitmap[10];//An array of images, used to display time bitmap[] rednumber = new bitmap[10];//Red A picture of a number used to display the time guize guize;//rule class int status = 0;//game state. 0 games, 1 victories, 2 defeats int heitime = 0;//Black total think time int hongtime = 0;//Red total think time int[][] Qizi = new int[][]{//Chessboard {2,3,6,5,1,5,6,3,2},{0,0 , 0,0,0,0,0,0,0},{0,4,0,0,0,0,0,4,0},{7,0,7,0,7,0,7,0,7},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{ 14,0,14,0,14,0,14,0,14},{0,11,0,0,0,0,0,11,0},{0,0,0,0,0,0,0,0,0},{9,10,13,12,8,12,13,10,9},};p ublic GameView ( Context context,chessactivity activity) {//Constructor super (context); this.activity = activity;//Gets the activity's reference Getholder (). Addcallback (this); go = Mediaplayer.create (This.getcontext (), r.raw.go);//load chess sound This.thread = new Tutorialthread ( Getholder (), this);//Initialize the brush frame thread this.timethread = new Timethread (this);//Initialize the think Time thread init ();//Initialize the required resources guize = new GUize ();//Initialization rule class}public void init () {//Initialize Method paint = new paint ();//initialization Brush Qipan = Bitmapfactory.decoderesource ( Getresources (), r.drawable.qipan);//Checkerboard Picture Qizibackground = Bitmapfactory.decoderesource (Getresources (), R.drawable.qizi);//The background of the pawn win = Bitmapfactory.decoderesource (Getresources (), r.drawable.win);//Victory picture lost = Bitmapfactory.decoderesource (Getresources (), r.drawable.lost);//failed picture OK = Bitmapfactory.decoderesource ( Getresources (), r.drawable.ok);//OK button picture vs = Bitmapfactory.decoderesource (Getresources (), R.drawable.vs);// vs typeface Picture right = Bitmapfactory.decoderesource (Getresources (), r.drawable.right);//rightwards Pointer left = Bitmapfactory.decoderesource (Getresources (), r.drawable.left);//left pointer current = Bitmapfactory.decoderesource ( Getresources (), r.drawable.current);//text "current" Exit2 = Bitmapfactory.decoderesource (Getresources (), r.drawable.exit2) ;//exit button Picture Sound2 = Bitmapfactory.decoderesource (Getresources (), r.drawable.sound2);//Voice button picture time = Bitmapfactory.decoderesource (Getresources (), r.drawable.time);//BlackColon redtime = Bitmapfactory.decoderesource (Getresources (), r.drawable.redtime);//red colon Sound3 = Bitmapfactory.decoderesource (Getresources (), r.drawable.sound3); heizi[0] = Bitmapfactory.decoderesource ( Getresources (), R.drawable.heishuai);//Hei Jing heizi[1] = Bitmapfactory.decoderesource (Getresources (), R.drawable.heiju) ;//Black heizi[2] = Bitmapfactory.decoderesource (Getresources (), R.drawable.heima);//Dark horse heizi[3] = Bitmapfactory.decoderesource (Getresources (), r.drawable.heipao);//Black cannon heizi[4] = Bitmapfactory.decoderesource ( Getresources (), R.drawable.heishi);//Hass heizi[5] = Bitmapfactory.decoderesource (Getresources (), R.drawable.heixiang );//black elephant heizi[6] = Bitmapfactory.decoderesource (Getresources (), r.drawable.heibing);//Black soldier hongzi[0] = Bitmapfactory.decoderesource (Getresources (), R.drawable.hongjiang);//red will hongzi[1] = Bitmapfactory.decoderesource ( Getresources (), R.drawable.hongju);//red car hongzi[2] = Bitmapfactory.decoderesource (Getresources (), R.drawable.hongma) ;//red horse hongzi[3] = Bitmapfactory.decoderesource (getrEsources (), r.drawable.hongpao);//Red cannon hongzi[4] = Bitmapfactory.decoderesource (Getresources (), R.drawable.hongshi); /Red shi hongzi[5] = Bitmapfactory.decoderesource (Getresources (), R.drawable.hongxiang);//red phase hongzi[6] = Bitmapfactory.decoderesource (Getresources (), r.drawable.hongzu);//red stroke number[0] = Bitmapfactory.decoderesource ( Getresources (), R.DRAWABLE.NUMBER0);//black number 0number[1] = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.NUMBER1)///black number 1number[2] = Bitmapfactory.decoderesource (Getresources (), r.drawable.number2);// Black number 2number[3] = Bitmapfactory.decoderesource (Getresources (), r.drawable.number3);//black number 3number[4] = Bitmapfactory.decoderesource (Getresources (), r.drawable.number4);//black number 4number[5] = Bitmapfactory.decoderesource ( Getresources (), r.drawable.number5);//black number 5number[6] = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.NUMBER6)///black number 6number[7] = Bitmapfactory.decoderesource (Getresources (), r.drawable.number7);// Black number 7number[8] = Bitmapfactory.decoderesource (getresoUrces (), r.drawable.number8);//black digit 8number[9] = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.NUMBER9); /black digit 9rednumber[0] = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.REDNUMBER0);//red number 0rednumber[1] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber1);//red number 1rednumber[2] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber2);//red number 2rednumber[3] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber3);//red number 3rednumber[4] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber4);//red number 4rednumber[5] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber5);//red number 5rednumber[6] = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.REDNUMBER6);//red number 6rednumber[7] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber7);//red number 7rednumber[8] = Bitmapfactory.decoderesource (Getresources (), r.drawable.rednumber8);//red number 8rednumber[9] = Bitmapfactory.decoderesource (Getresources (), r.drawable. Rednumber9);//Red number 9background = Bitmapfactory.decoderesource (Getresources (), r.drawable.bacnground);} /** * This method is defined by itself is not rewritten * The method is dead, only based on the data drawn on the screen */public void OnDraw (canvas canvas) {//self-written drawing method Canvas.drawcolor (Color.White); Canvas.drawbitmap (background, 0,0, NULL);//Clear Background canvas.drawbitmap (Qipan, ten, ten, NULL);//Draw checkerboard for (int i=0; i< Qizi.length; i++) {for (int j=0; j<qizi[i].length; J + +) {//Draw pawn if (qizi[i][j]! = 0) {canvas.drawbitmap (Qizibackground, 9+j*34, 10+i* (Qizi[i][j] = = 1) {//For Black Canvas.drawbitmap (heizi[0], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 2) {//For Black Cars canvas.drawbitmap (heizi[1], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 3) {//For Dark Horse canvas.drawbitmap (heizi[2], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 4) {//For Black Cannon Canvas.drawbitmap (heizi[3], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 5) {//For Hass when Canvas.drawbitmap (Heizi[4], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 6) {//For Black Elephant canvas.drawbitmap (heizi[5], 12+j*34, 13+i*35, paint);} else if (Qizi[i][j]= = 7) {//For Black Soldier Canvas.drawbitmap (Heizi[6], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 8) {//For Red will when Canvas.drawbitmap (Hongzi[0], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 9) {//For Red Car canvas.drawbitmap (hongzi[1], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 10) {//For Red Horse canvas.drawbitmap (hongzi[2], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 11) {//For Red Cannon when Canvas.drawbitmap (Hongzi[3], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 12) {//For Red Canvas.drawbitmap (Hongzi[4], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 13) {//For Red Phase canvas.drawbitmap (Hongzi[5], 12+j*34, 13+i*35, paint);} else if (qizi[i][j] = = 14) {//For Red Stroke Canvas.drawbitmap (hongzi[6], 12+j*34, 13+i*35, Paint);}}} Canvas.drawbitmap (VS, Ten, Max, paint);//Draw vs Background map//Draw Black side time canvas.drawbitmap (Times, Bayi, 411, paint);//Draw colon int temp =    this.heitime/60;//conversion Time String timestr = temp+ "";//Convert to String if (Timestr.length () <2) {////when less than two bits in front of 0timeStr = "0" + timestr;}    for (int i=0;i<2;i++) {//cycle draw time int Tempscore=timestr.charat (i)-' 0 '; Canvas.drawbiTMap (Number[tempscore], 65+i*7, 412, paint); }//Draw minutes temp = This.heitime%60;timestr = temp+ "";//Convert to String if (Timestr.length () <2) {timestr = "0" + timestr;//when length is small    At 2 o'clock add a 0} for (int i=0;i<2;i++) {//loop int Tempscore=timestr.charat (i)-' 0 '; Canvas.drawbitmap (Number[tempscore], 85+i*7, 412, paint);//Draw}//start draw Red Square time Canvas.drawbitmap (This.redtime, 262, 410, paint);//red side colon int temp2 = this.hongtime/60;//conversion time string timeStr2 = temp2+ "";//Convert to String if (Timestr2.length () <2) {//    When less than two bits are filled in front 0timestr2 = "0" + timeStr2;}    for (int i=0;i<2;i++) {//cycle draw time int Tempscore=timestr2.charat (i)-' 0 '; Canvas.drawbitmap (Rednumber[tempscore], 247+i*7, 411, paint);//Draw}//Draw minutes Temp2 = this.hongtime%60;//Find the current number of seconds times    TR2 = temp2+ "";//Convert to String if (Timestr2.length () <2) {//less than two bits when preceded with 0 fill timeStr2 = "0" + timeStr2;}    for (int i=0;i<2;i++) {//loop draw int Tempscore=timestr2.charat (i)-' 0 '; Canvas.drawbitmap (Rednumber[tempscore], 267+i*7, 411, paint);//Draw Time number}if (Caipan = True) {//when the playerMoves, the Red Square moves Canvas.drawbitmap (right, 155, 420, paint);//Draw rightwards pointer}else{//black moves, i.e. moves when computer canvas.drawbitmap (left, 120, 420, paint);//Draw the pointer to the left}CANVAS.DRAWBITMAP (current, 138, 445, paint);//Draw the present text Canvas.drawbitmap (Sound2, Ten, A, and paint);// Draw sound if (activity.issound) {//If you are playing a sound canvas.drawbitmap (Sound3, 452, paint);//Draw}canvas.drawbitmap (Exit2, 250, 440 , paint);//Draw Exit button if (status = = 1) {//When winning Canvas.drawbitmap (win, Max, Max, paint);//Draw victory picture Canvas.drawbitmap (OK, 113, 240, paint);} if (status = = 2) {//Failed Canvas.drawbitmap (lost, Max, Max, paint);//Draw Failed interface Canvas.drawbitmap (OK, 113, 236, paint);}} /** * The method is the main logical interface of the game * Accept player Input * According to the location of the click and the current game state to make the corresponding processing * and when the need to switch view, by sending the activity handler message to handle * Note that only the screen is pressed events * * @Override public boolean ontouchevent (Motionevent event) {//Override screen listener if (event.getaction () = = Motionevent.action_down) {// Only mouse-pressed events if (Event.getx () >10&&event.getx () <10+sound2.getwidth () && event.gety () >440 & & Event.gety () <440+sound2.getheight ()) {//Press the sound button activity.issound =!activity.issound;//Sound takes the reverse if (activity.issound) {//When a sound needs to be placed if (activity.gamesound! = null) {//gamesound is not empty if (! Activity.gamesound.isPlaying ()) {//Activity.gamesound.start ();//Play Music}}}else{if (activity.gamesound! = null) {//gamesound is not empty if (activity.gamesound.isPlaying ()) {//is currently having music activity.gamesound.pause ();//Stop Music}}}} End presses the sound button if (Event.getx () >250&&event.getx () <250+exit2.getwidth () && event.gety () >440 && event.gety () <440+exit2.getheight ()) {//Press the Exit button Activity.myHandler.sendEmptyMessage (1);//Send a message, Switch to menuview}if (status = = 1) {//After victory if (Event.getx () >135&&event.getx () <190&& event.gety () > 249 && event.gety () <269) {//clicked OK button activity.myHandler.sendEmptyMessage (1);//Send message, switch to Menuview}}else if ( Status = = 2) {//after failure if (Event.getx () >135&&event.getx () <190&& event.gety () >245 && Event.gety () <265) {//clicked OK button activity.myHandler.sendEmptyMessage (1);//Send message, switch to menuview}}/** * During game logic processing * When clicking on the board , first determine whether the player is currently moves, * and thenJudge of course whether the player has selected pieces, if not selected * If there are selected pieces, then determine the location of the click is open space, the other pieces or their own chess pieces * is the space to determine whether to walk * is the other pieces of the same judge whether you can walk, can walk naturally eat son * is his own chess pieces */else if (status = = 0) {//In-game if (Event.getx () >10&&event.getx () <310&& event.gety () >10 && Event.gety () <360) {///click the position within the checkerboard if (Caipan = = true) {///If it is the player moves int i =-1, j = -1;int[] pos = GetPos (event);//coordinates are converted to the row and Column I = Pos[0];j = pos[1];if (Focus = = False) {/////not previously checked checkers if (qizi[i][j]! = 0) {//Click on the position there is a pawn if (Qizi[i][j] > 7) {//click on your own pawn. That is, the following black piece Selectqizi = qizi[i][j];//to set the piece as the selected piece focus = true;//mark the currently selected piece Starti = I;STARTJ = J;}}} else{//Previously selected pawn if (qizi[i][j]! = 0) {//Click on the position of the pawn if (Qizi[i][j] > 7) {//If it is your own pawn. Selectqizi = qizi[i][j];// Set the piece as the selected piece Starti = I;STARTJ = j;} else{//if it is the other piece Endi = I;endj = j;//Save the point boolean Canmove = Guize.canmove (Qizi, Starti, STARTJ, Endi, ENDJ); if (canmove) {//IF Can move past Caipan = false;//do not let the player go if (qizi[endi][endj] = = 1 | | qizi[endi][endj] = = 8) {//If it is "handsome" or "will" this.success ();//Victory}else{ if (activity.issound) {Go.start ();//play chess sound}qizi[endi][endj] = qizi[starti][startj];//move piece QIZI[STARTI][STARTJ] = 0;//will be set empty Starti = -1;STARTJ = -1;endi = -1;endj = -1;//Restore savepoint focus = false;//tag currently does not have a pawn chessmove cm = Guize.searchagoodmove (qizi);//Check the current situation for the best way to go if (activity.issound) {Go.start ();// Play chess sound}qizi[cm.tox][cm.toy] = qizi[cm.fromx][cm.fromy];//move piece qizi[cm.fromx][cm.fromy] = 0;caipan = true;//restore Player Response}}}} End Click Position there is a pawn else{//if the location of the click does not have a pawn Endi = I;endj = J;boolean Canmove = Guize.canmove (Qizi, Starti, STARTJ, Endi, ENDJ);//See if can go if (canmove) {//If you can move Caipan = false;//do not let the player go if (activity.issound) {Go.start ();//play chess sound}qizi[endi][endj] = qizi[ starti][startj];//moving pieces QIZI[STARTI][STARTJ] = 0;//will be disposed of empty Starti = -1;STARTJ = -1;endi = -1;endj = -1;//Restore savepoint focus = false;// Flag bit set Falsechessmove cm = Guize.searchagoodmove (qizi);//Get one step away if (qizi[cm.tox][cm.toy] = = 8) {//Computer ate your will status = 2;// Toggle game status to Fail}if (Activity.issound) {//Play sound when Go.start ();//playing chess sound}qizi[cm.tox][cm.toy] = qizi[cm.fromx][cm.fromy];// Move a piece qizi[cm.fromx][cm.fromy] = 0;caipan = true;//The player has selected a pawn before responding}}}//end}}End click on the position within the board when}//end game}return super.ontouchevent (event);} Public int[] GetPos (motionevent e) {//convert coordinates to the number of dimensions of the array int[] pos = new Int[2];d ouble x = E.getx ();//Get the x-coordinate of the click position double y = e.gety () ;//Get the y-coordinate of the click Position if (x>10 && y>10 && x<10+qipan.getwidth () && y<10+qipan.getheight ()) {//Click on the board when pos[0] = Math.Round ((float) ((y-21)/36));//The line where the row was obtained pos[1] = Math.Round ((float) ((x-21)/35));//Gets the column}else{// Click the location is not the board when pos[0] = -1;//Set the position to not be available pos[1] = 1;} Return pos;//returns an array of coordinates}public void success () {//Victory status = 1;//Switch to victory State}public void Surfacechanged (Surfaceholder holder, int format, int width,int height) {}public void surfacecreated (Surfaceholder holder) {//overridden This.thread.setFlag (tru        e);        This.thread.start ();//start Brush frame thread Timethread.setflag (true); Timethread.start ();//Start think time thread}public void surfacedestroyed (Surfaceholder holder) {//view is released when the Boolean retry called = TR        Ue Thread.setflag (false);//Stop Brush frame thread Timethread.setflag (false);//Stop Thinking Time thread while (rEtry) {try {thread.join (); Timethread.join ();//wait for thread to end retry = false;//Set loop flag bit false} catch (Interruptedexcepti On e) {//loop continuously until the waiting thread ends}}}class tutorialthread extends thread{//brush frame thread private int span = 300;//number of milliseconds to sleep p Rivate surfaceholder surfaceholder;//surfaceholder Reference private Gameview Gameview;//gameview Reference private Boolean flag = false;//Loop Flag Public tutorialthread (Surfaceholder Surfaceholder, Gameview gameview) {//constructor THIS.SURFACEH older = surfaceholder;//Get surfaceholder Reference This.gameview = gameview;//get Gameview reference} public void        Setflag (Boolean flag) {//set loop flag This.flag = flag;                }public void Run () {//overridden method canvas c;//canvas while (this.flag) {//loop drawing c = null;                    try {c = This.surfaceHolder.lockCanvas (null); Synchronized (this.surfaceholder) {GamevIew.ondraw (c);//Call the Drawing method}} finally {//with the finally guarantee that the following code must be executed if (c! = Nu                    LL) {//Update screen display content This.surfaceHolder.unlockCanvasAndPost (c); }} try{Thread.Sleep (span);//Sleep span milliseconds}catch (Exception e) {//No Exception information e.printstacktrace ();//print exception stack Information}}}}}


android--Chinese Chess

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.