2013年9月22日 星期日

第三周作業

1. 目標:畫出一個五子棋

int w = 700;   //可修改視窗寬度
int h = 700;    //可修改視窗高度
int l = 7;        // 可修改線的數量
int testW  = 0;   //儲存一格的寬度
int testH  = 0;   // 儲存一格的高度
int rectX, rectY;   // 設定多餘的邊界長度
void setup () {
  size (w, h);
}
void draw () {
  background (245, 177, 94);
  noFill();    //不填充顏色
  strokeWeight(6);        //修改線的寬度
  testW = w / ((l+1)*10);
  testH = h  / ((l+1)*10);  
  rectX = ( w - (testW * (l +1)*10) ) / 2 ;        // 計算多餘的寬度
  rectY = ( h - (testH * (l +1)*10) ) / 2 ;         // 計算多餘的長度
  rect(rectX  + testW * 10 / 2, rectY + testH *10 / 2, w - rectX * 2 - testW *10, h - rectY * 2 - testH *10 );     // 畫框框
  strokeWeight(1);
  for ( int i = 0 ; i < l ; i ++) {
    line ( rectX + testW * 10 / 2, rectY + testH * i * 10 + testH *10 / 2, w - rectX - testW * 10 / 2, rectY + testH * i *10 + testH *10 / 2);  
    line ( rectX + testW * i * 10 +  testW * 10 / 2, rectY + testH *10 / 2, rectX + testW * i * 10 +  testW * 10 / 2, h - rectY -testH *10 / 2 );
  }     // 利用for迴圈畫一個網狀的線
}











2. 利用for迴圈來顯示想要選擇的部分

int w = 700;
int h = 700;
int l = 7;
int testW  = 0;
int testH  = 0;
int rectX, rectY;
int newX, newY;   // 設定滑鼠所屬的位置
void setup () {
  size (w, h);
}
void draw () {
  background (245, 177, 94);
  noFill();
  strokeWeight(6);
  testW = w / ((l+1)*10);
  testH = h  / ((l+1)*10);
  rectX = ( w - (testW * (l +1)*10) ) / 2 ;
  rectY = ( h - (testH * (l +1)*10) ) / 2 ;
  rect(rectX  + testW * 10 / 2, rectY + testH *10 / 2, w - rectX * 2 - testW *10, h - rectY * 2 - testH *10 );
  strokeWeight(1);
  for ( int i = 0 ; i < l ; i ++) {
    line ( rectX + testW * 10 / 2, rectY + testH * i * 10 + testH *10 / 2, w - rectX - testW * 10 / 2, rectY + testH * i *10 + testH *10 / 2);
    line ( rectX + testW * i * 10 +  testW * 10 / 2, rectY + testH *10 / 2, rectX + testW * i * 10 +  testW * 10 / 2, h - rectY -testH *10 / 2 );
  }
  fill (255,255,255,128);
  strokeWeight(3);
  rect(rectX + newX * testW * 10 , rectY + newY * testH * 10 , testW *10, testH*10 ) ;    // 利用滑鼠所屬位置畫圖
}

void mouseMoved(){
   newX = (mouseX - rectX) / (testW * 10);   //計算所屬X位置
   newY = (mouseY - rectY) / (testH * 10);     //計算所屬Y位置
}   // 偵測滑鼠移動












3.最後讓棋子可以放上去


int w = 700;
int h = 700;
int l = 7;
int testW  = 0;
int testH  = 0;
int rectX, rectY;
int newX, newY;
int [][] array = new int [l+1][l+1];
int newColor = 1;
void setup () {
  size (w, h);
}
void draw () {
  background (245, 177, 94);
  noFill();
  strokeWeight(6);
  testW = w / ((l+1)*10);
  testH = h  / ((l+1)*10);
  rectX = ( w - (testW * (l +1)*10) ) / 2 ;
  rectY = ( h - (testH * (l +1)*10) ) / 2 ;
  rect(rectX  + testW * 10 / 2, rectY + testH *10 / 2, w - rectX * 2 - testW *10, h - rectY * 2 - testH *10 );
  strokeWeight(1);
  for ( int i = 0 ; i < l ; i ++) {
    line ( rectX + testW * 10 / 2, rectY + testH * i * 10 + testH *10 / 2, w - rectX - testW * 10 / 2, rectY + testH * i *10 + testH *10 / 2);
    line ( rectX + testW * i * 10 +  testW * 10 / 2, rectY + testH *10 / 2, rectX + testW * i * 10 +  testW * 10 / 2, h - rectY -testH *10 / 2 );
  }
  fill (255, 255, 255, 128);
  strokeWeight(3);
  rect(rectX + newX * testW * 10, rectY + newY * testH * 10, testW *10, testH*10 ) ;
  for (int i = 0 ; i < l +1; i++ ) {
    for (int j = 0 ; j < l +1; j++ ) {
      if (array[i][j] == 1) {
        fill(0);
        ellipse (rectX + i * testW *10  +  testW *10 /2, rectY+ j*testH *10+testH*10/2, testW *10, testH*10);
      }
      if (array[i][j] == 2) {
        fill(255);
        ellipse (rectX + i * testW *10  +  testW *10 /2, rectY+ j*testH *10+testH*10/2, testW *10, testH*10);
      }
    }
  }
}
void mousePressed() {
  if (array[newX][newY] == 0) {
    array[newX][newY] = newColor;
    if (newColor == 1)newColor = 2;  //修改目前棋子顏色
    else if (newColor == 2)newColor =1 ;   //修改目前棋子顏色
  } // 判斷該位置的棋子是否用過
}

void mouseMoved() {
  newX = (mouseX - rectX) / (testW * 10);
  newY = (mouseY - rectY) / (testH * 10);
  if ( newX > l) newX = l;
  if ( newY > l) newY =l;
}















結果:
可以隨著棋盤大小改變的棋子



































也可以改變線的數量





















附贈:
五子棋測試
隨著滑鼠移動的框框會跟著目前要下的棋子跟改顏色
當有任何顏色連成五子就會結束遊戲

int w = 800;
int h = 800;
int l = 25;
int testW  = 0;
int testH  = 0;
int rectX, rectY;
int newX, newY;
int [][] array = new int [l+1][l+1];
int newColor = 1;
int [] test = new int [8];
boolean game = true;
void setup () {
  size (w, h);
}
void draw () {
  background (245, 177, 94);
  noFill();
  stroke(0);
  strokeWeight(6);
  testW = w / ((l+1)*10);
  testH = h  / ((l+1)*10);
  rectX = ( w - (testW * (l +1)*10) ) / 2 ;
  rectY = ( h - (testH * (l +1)*10) ) / 2 ;
  rect(rectX  + testW * 10 / 2, rectY + testH *10 / 2, w - rectX * 2 - testW *10, h - rectY * 2 - testH *10 );
  strokeWeight(1);
  for ( int i = 0 ; i < l ; i ++) {
    line ( rectX + testW * 10 / 2, rectY + testH * i * 10 + testH *10 / 2, w - rectX - testW * 10 / 2, rectY + testH * i *10 + testH *10 / 2);
    line ( rectX + testW * i * 10 +  testW * 10 / 2, rectY + testH *10 / 2, rectX + testW * i * 10 +  testW * 10 / 2, h - rectY -testH *10 / 2 );
  }
  fill (255, 255, 255, 128);
  strokeWeight(3);
  if (newColor == 1) stroke(0);
  else if (newColor==2) stroke(255);
  rect(rectX + newX * testW * 10, rectY + newY * testH * 10, testW *10, testH*10 ) ;
  for (int i = 0 ; i < l +1; i++ ) {
    for (int j = 0 ; j < l +1; j++ ) {
      if (array[i][j] == 1) {
        fill(0);
        stroke(0);
        ellipse (rectX + i * testW *10  +  testW *10 /2, rectY+ j*testH *10+testH*10/2, testW *10, testH*10);
      }
      if (array[i][j] == 2) {
        fill(255);
        stroke(0);
        ellipse (rectX + i * testW *10  +  testW *10 /2, rectY+ j*testH *10+testH*10/2, testW *10, testH*10);
      }
    }
  }
}
void mousePressed() {
  if (game) {
    if (array[newX][newY] == 0) {
      array[newX][newY] = newColor;
      check (newX, newY, 0);
      if (newColor == 1)newColor = 2;
      else if (newColor == 2)newColor =1 ;
    }
    if (test[0]+test[7]>=4) {
      if (newColor == 2) {
        print("Black Win");
        game = !game;
      }
      else {
        print("White Win");
        game = !game;
      }
    }
    if (test[1]+test[6]>=4) {
      if (newColor == 2) {
        print("Black Win");
        game = !game;
      }
      else {
        print("White Win");
        game = !game;
      }
    }
    if (test[2]+test[5]>=4) {
      if (newColor == 2) {
        print("Black Win");
        game = !game;
      }
      else {
        print("White Win");
        game = !game;
      }
    }
    if (test[3]+test[4]>=4) {
      if (newColor == 2) {
        print("Black Win");
        game = !game;
      }
      else {
        print("White Win");
        game = !game;
      }
    }
    for (int i = 0 ; i < 8 ; i++) {
      test[i]=0;
    }
  }
}
void mouseMoved() {
  newX = (mouseX - rectX) / (testW * 10);
  newY = (mouseY - rectY) / (testH * 10);
  if ( newX > l) newX = l;
  if ( newY > l) newY =l;
}


void check (int x, int y, int z) {
  switch (z) {
  case 0 :
    if (x-1 >=0&&y-1>=0&&array[x-1][y-1] == newColor) {
      test[0]++;
      check(x-1, y-1, 1);
    }
    if (x-1 >=0&&array[x-1][y] == newColor) {
      test[1]++;
      check(x-1, y, 2);
    }
    if (x-1 >=0&&y+1<=l&&array[x-1][y+1] == newColor) {
      test[2]++;
      check(x-1, y+1, 3);
    }
    if (y-1>=0&&array[x][y-1] == newColor) {
      test[3]++;
      check(x, y-1, 4);
    }
    if (y+1<=l&&array[x][y+1] == newColor) {
      test[4]++;
      check(x, y+1, 5);
    }
    if (x+1<=l&&y-1>=0&&array[x+1][y-1] == newColor) {
      test[5]++;
      check(x+1, y-1, 6);
    }
    if (x+1<=l&&array[x+1][y] == newColor) {
      test[6]++;
      check(x+1, y, 7);
    }
    if (x+1<=l&&y+1<=l&&array[x+1][y+1] == newColor) {
      test[7]++;
      check(x+1, y+1, 8);
    }
    break;
  case 1 :
    if (x-1 >=0&&y-1>=0&&array[x-1][y-1] == newColor) {
      test[0]++;
      check(x-1, y-1, 1);
    }
    break;
  case 2 :
    if (x-1 >=0&&array[x-1][y] == newColor) {
      test[1]++;
      check(x-1, y, 2);
    }
    break;
  case 3 :
    if (x-1 >=0&&y+1<=l&&array[x-1][y+1] == newColor) {
      test[2]++;
      check(x-1, y+1, 3);
    }
    break;
  case 4 :
    if (y-1>=0&&array[x][y-1] == newColor) {
      test[3]++;
      check(x, y-1, 4);
    }
    break;
  case 5 :
    if (y+1<=l&&array[x][y+1] == newColor) {
      test[4]++;
      check(x, y+1, 5);
    }
    break;
  case 6 :
    if (x+1<=l&&y-1>=0&&array[x+1][y-1] == newColor) {
      test[5]++;
      check(x+1, y-1, 6);
    }
    break;
  case 7 :
    if (x+1<=l&&array[x+1][y] == newColor) {
      test[6]++;
      check(x+1, y, 7);
    }
    break;
  case 8 :
    if (x+1<=l&&y+1<=l&&array[x+1][y+1] == newColor) {
      test[7]++;
      check(x+1, y+1, 8);
    }
    break;
  }
}






沒有留言:

張貼留言