วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

-Chess-




void setup()
{
  size(400, 400);
}
  int x,y,i,j;       //ประกาศตัวแปลเป็น Global variable เพื่อหัวทุกฟังก์ชันมองเห็นตัวแปลนี้
  int[][] Chess= {        //สร้างตัวแปรอาเรย์ 2 มิติ ชนิดจำนวนเต็ม และกำหนดค่า
    {
      1, 2, 3, 4, 5, 3, 2, 1
    }
    , {
      6, 6, 6, 6, 6, 6, 6, 6
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      6, 6, 6, 6, 6, 6, 6, 6
    }
    , {
      1, 2, 3, 4, 5, 3, 2, 1
    }
  };

void draw(){
drawTa();            //เรียกใช้ฟังกฺชัน drawTa( )
drawChess();      //เรียกใช้ฟังก์ชัน drawwChess( )

}

void drawTa(){   //ฟังก์ชันนี้จะวาดตาราง
  stroke(0);         //กำหนดสีให้กับเส้น
  strokeWeight(2);      //ใส่ความหนาให้เส้น
   y=0;
   for (i=0;i<Chess.length;i++) {    //เป็นการวนลูปในอะเรย์มิติที่1
                                                       //for( )คล้ายกับคำสั่ง while แต่ต่างกันตรงที่
                                                            การกำหนดเงื่อนไขดังนี้
                      for(กำหนดค่าให้ตัวแปล,เงื่อนไขในการวน, เพิ่มค่าให้ตัวแปลทุกครั้งที่มีการวนลูป)
                                                 
    x=0;
    for (j=0;Chess[0].length>j;j++)   //เป็นการวนลูปที่ใช้ในการรันอะเรย์ในมิติที่2 
เมื่อออกจากลูปนี้ก็จะกลับไปวนลูปแรกใหม่  
เป็นการเขียนแบบ ลูปซ้อนลูป
    {
      if ((j+i)%2==0) fill(139, 134, 130);   //เป็นการกำหนดเงื่อนไขการใส่สีของตาราง
      else fill(139, 69, 19);            
      rect(x, y, 100, 100);
      x=x+50;    
    }
    y=y+50;
  }
}
void drawChess(){    //ฟังก์ชันนี้จะใช้วาดตัวหมาก
  y=25;
  for (i=0;i<Chess.length;i++) {   //เป็นการวนลูปในอะเรย์มิติที่1 ใช้ในการตรวจสอบแถว
    x=25;
    for (j=0;j<Chess[0].length;j++)   {  //เป็นการวนลูปที่ใช้ในการรันอะเรย์ในมิติที่2 
ใช้ในการเช็คว่าค่าที่เก็บไว้ข้างต้นเป็นตัวหมากอะไรบ้าง
       if (y<200){
        fill(0);
        stroke(255, 0,0 );
      }
      else if (y>200)
      {
        fill(255);
        stroke(0, 255, 0);
      }

      strokeWeight(3);
      if (Chess[i][j]==1)        
      {rook(x, y);}            

      else if (Chess[i][j]==2)      
      {knight(x, y);}          

      else if (Chess[i][j]==3)        
      {bishop(x, y);}        

      else if (Chess[i][j]==4)      
      {queen(x, y); }        

      else if (Chess[i][j]==5)  
      {king(x, y);}          

      else if (Chess[i][j]==6)  
      {
        if (y<200)  
        {stroke(255);}  
        else if (y>200)  
        {stroke(0);}        
        strokeWeight(5);  
        Chip(x, y);  
      }
      x=x+50;
    }
    y=y+50;
  }
}

void rook(int x, int y)  //ฟังก์ชันที่ใช้วาดเรือ
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}          
  else if (y>200)          
  {fill(0);}        
   text("rook", x-12, y+3);
}


void knight(int x, int y)   //ฟังก์ชันที่ใช้วาดม้า
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}            
  else if (y>200)          
  {fill(0);}        
   text("knight", x-16, y+3);
}

void bishop(int x, int y)   //ฟังก์ชันที่ใช้วาดบิชอป
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}            
  else if (y>200)        
  {fill(0);}        
   text("bishop", x-17, y+3);
}

void queen(int x, int y)  //ฟังก์ชันที่ใช้วาดควีน
{
  ellipse(x, y, 45, 45);
  if (200>y)
  {fill(255);}
  else if (y>200)
  {fill(0);}
  text("queen", x-15, y+3);
}

void king(int x, int y)  //ฟังก์ชันที่ใช้วาดคิง
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}            
  else if (y>200)        
  {fill(0);}      
   text("king", x-10, y+3);
}

void Chip(int x, int y) //ฟังก์ชันที่ใช้วาดตัวเบี้ย
{
  ellipse(x, y, 40, 40);  
}


//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

ไม่มีความคิดเห็น:

แสดงความคิดเห็น