2013-03-12 76 views
0

我正在嘗試爲Android創建一個TicTacToe遊戲。但我有以下問題。 首先我想實現線程我的應用程序,但我還沒有succesfull(幾乎嘗試過任何東西)。 其次我想知道如何應對這兩個錯誤(我不知道,如果他們被稱爲錯誤):成長堆(斷枝情況下),以65.24 MB爲2567824字節分配跳過118幀!該的applcation可能會做它的主線程太多的工作(雖然我認爲這將與線程執行來解決)自定義Android線程實現查看

public class DrawClass extends View { 

int index; 
int x, y, RectX, RectY; 
// it will hold the rectangles dimensions depending on the screen dimensions 
public Rect[] rectangles = new Rect[9]; 
// it will be our reference array as to what does each rectangle hold 
char[] isFilledWith = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; 
// bitmaps 
Bitmap empty, cross, circle; 

// constructor 
public DrawClass(Context context) { 

    super(context); 
    // setting bitmaps and calculating screen dimensions 
    setBitmaps(); 
    Calculations(context); 

} 

public void setBitmaps() {// set the 3 bitmaps empty , circle , cross 
    cross = BitmapFactory.decodeResource(getResources(), R.drawable.cross1); 
    circle = BitmapFactory.decodeResource(getResources(), 
      R.drawable.circle1); 
    empty = BitmapFactory.decodeResource(getResources(), R.drawable.empty1); 
} 

@Override 
public void onDraw(Canvas canvas) { 

    super.onDraw(canvas); 

    Paint paint = new Paint(); 
    // this for updates the canvas with the appropriate bitmaps cross , circle or empty 
    // depending on tyhe char array isFilledWith 
    for (int i = 0; i < 9; i++) { 
     if (isFilledWith[i] == ' ') { 
      canvas.drawBitmap(empty, null, rectangles[i], null); 
     } else if (isFilledWith[i] == 'X') { 
      canvas.drawBitmap(cross, null, rectangles[i], null); 
     } else if (isFilledWith[i] == 'O') { 
      canvas.drawBitmap(circle, null, rectangles[i], null); 
     } 
     paint.setColor(Color.WHITE); 
     canvas.drawLine(x, 0, x, 3 * y, paint);// 1h 
     canvas.drawLine(2 * x, 0, 2 * x, 3 * y, paint);// 2h 
     canvas.drawLine(0, y, 3 * x, y, paint);// 3h 
     canvas.drawLine(0, 2 * y, 3 * x, 2 * y, paint);// 4h 
    } 
} 

protected void Calculations(Context context) { 
    /* 
    * We calculate screen dimensions and fill the rectangles array with the 
    * appropriate dimensions 
    */ 
    DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 
    int width = metrics.widthPixels; 
    int height = metrics.heightPixels; 

    x = (width/3); 
    y = (height/3); 

    int rectanglescounter = 0; 
    for (int i = 0; i < 3; i++) { 
     for (int j = 0; j < 3; j++) { 
      rectangles[rectanglescounter] = new Rect((x * j), (y * i), 
        ((x * j) + x), ((y * i) + y)); 
      if (rectanglescounter < 8) 
       rectanglescounter++; 
      else 
       break; 
     } 
    } 
    Log.d("Checks", "rectangles calculated"); 
} 
// setter for the array 
public void setArray(int index, char character) { 
    this.isFilledWith[index] = character; 
} 
// getter for the array 
public char getArray(int index) { 
    return isFilledWith[index]; 
} 
// This method searches for tictactoe winning patterns . 
public char checkForWinner() { 
    if (isFilledWith[0] == isFilledWith[1] 
      && isFilledWith[1] == isFilledWith[2]) {// 1st horizontal 

     if (isFilledWith[0] == 'X') { 
      Log.d("WINNING CHECK", "1st row win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[0] == 'O') { 
      Log.d("WINNING CHECK", "1st row win detected"); 
      return 'O'; 
     } 
    } else if (isFilledWith[3] == isFilledWith[4] 
      && isFilledWith[4] == isFilledWith[5]) {// 2nd horizontal 

     if (isFilledWith[3] == 'X') { 
      ; 
      Log.d("WINNING CHECK", "2st row win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[3] == 'O') { 
      Log.d("WINNING CHECK", "2st row win detected"); 
      return 'O'; 
     } 
    } else if (isFilledWith[6] == isFilledWith[7] 
      && isFilledWith[7] == isFilledWith[8]) {// 3rd horizontal 

     if (isFilledWith[6] == 'X') { 
      Log.d("WINNING CHECK", "3st row win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[6] == 'O') { 
      Log.d("WINNING CHECK", "3st row win detected"); 
      return 'O'; 
     } 
    } else if (isFilledWith[0] == isFilledWith[3] 
      && isFilledWith[3] == isFilledWith[6]) {// 1st vertical 

     if (isFilledWith[0] == 'X') { 
      Log.d("WINNING CHECK", "1st column win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[0] == 'O') { 
      Log.d("WINNING CHECK", "1st column win detected"); 
      return 'O'; 
     } 
    } else if (isFilledWith[1] == isFilledWith[4] 
      && isFilledWith[4] == isFilledWith[7]) {// 2st vertical 

     if (isFilledWith[1] == 'X') { 

      Log.d("WINNING CHECK", "2st column win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[1] == 'O') { 
      Log.d("WINNING CHECK", "2st column win detected"); 
      return 'O'; 
     } 
    } else if (isFilledWith[2] == isFilledWith[5] 
      && isFilledWith[5] == isFilledWith[8]) {// 3rd vertical 

     if (isFilledWith[2] == 'X') { 
      Log.d("WINNING CHECK", "3st column win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[2] == 'O') { 
      Log.d("WINNING CHECK", "3st column win detected"); 
      return 'O'; 
     } 
    } else if (isFilledWith[0] == isFilledWith[4] 
      && isFilledWith[4] == isFilledWith[8]) {// diagonal 
     if (isFilledWith[0] == 'X') { 
      Log.d("WINNING CHECK", "1st diagonal win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[0] == 'O') { 
      Log.d("WINNING CHECK", "1st diagonal win detected"); 
      return 'O'; 
     } 
    } else if ((isFilledWith[2] == isFilledWith[4] && isFilledWith[4] == isFilledWith[6])) {// diagonal 

     if (isFilledWith[2] == 'X') { 
      Log.d("WINNING CHECK", "2nd diagonal win detected"); 
      return 'X'; 
     } 
     if (isFilledWith[2] == 'O') { 
      Log.d("WINNING CHECK", "2nd diagonal win detected"); 
      return 'O'; 
     } 
    } else 
     return ' '; 
    return ' '; 
} 

}

public class NewGame extends Activity implements OnTouchListener { 

DrawClass drawing; 
int movecounter = 0; 
int playercounter = 0; 
static { 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    drawing = new DrawClass(this); 
    //setting on touch events to correspond to this class ontouchlistener 
    drawing.setOnTouchListener(this); 

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 
    setContentView(drawing); 

} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 

    int tempx = (int) event.getX(); 
    int tempy = (int) event.getY(); 

    // calculating where the user touched the screen so as to fill the char 
    // array with the appropriate char 'x'or 'o' and then invalidate the 
    // drawing 
    for (int i = 0; i < 9; i++) { 
     if (drawing.rectangles[i].contains(tempx, tempy)) { 
      if (drawing.getArray(i) == ' ') { 
       if (playercounter % 2 == 0) 
        drawing.setArray(i, 'X'); 
       else 
        drawing.setArray(i, 'O'); 
       playercounter++; 
      } else if (drawing.getArray(i) != ' ') 
       Toast.makeText(getApplicationContext(), "DONT CHEAT", 
         Toast.LENGTH_SHORT).show(); 

      // CHECKINK FOR WINNER or DRAW 

      movecounter++; 
      if (movecounter >= 5) { 
       if (drawing.checkForWinner() == 'X') { 
        Toast.makeText(getApplicationContext(), "X WIN", 
          Toast.LENGTH_SHORT).show(); 
        finish(); 
       } 
       if (drawing.checkForWinner() == 'O') { 
        Toast.makeText(getApplicationContext(), "O WIN", 
          Toast.LENGTH_SHORT).show(); 
        finish(); 
       } 
       if (movecounter >= 9) { 
        Log.d("Entered Draw ", " ok "); 
        Toast.makeText(getApplicationContext(), 
          "IT'S A DRAW ", Toast.LENGTH_SHORT).show(); 
        finish(); 
       } 
      } 
      drawing.invalidate(); 
     } else 
      continue; 
    } 
    return false; 
} 

}

預先感謝您!

+0

你有沒有試過AsyncTask(http://developer.android.com/reference/android/os/AsyncTask.html),Handler(http://developer.android.com/reference/android/os/Handler.html )並閱讀本文(http://developer.android.com/guide/components/processes-and-threads.html) – JScoobyCed 2013-03-12 01:50:40

回答

0

Skipped 118 frames ! The applcation may be doing too much work on its main thread你在UI線程上做了很多工作。創建一個線程或使用asynctask來完成繁重的工作將解決這個錯誤。看看這個http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html,很好解釋。

+0

你好!問題是我不知道如何以及在我的代碼中實現線程或處理程序或AsyncTask的位置。我以前的嘗試是徒勞的。 – 2013-03-12 11:15:34

0

爲了幫助你解決線程和異步問題,你必須要更具體,但要找到性能問題的位置,你可以使用Android的profiling tools