วันอาทิตย์ที่ 22 กันยายน พ.ศ. 2556

A2 Submit

ชื่อเกม : Egg
รูปแบบเกม : smooth & step

code รันได้บน Program Processing

การออกแบบฟังก์ชัน : http://com5630264.blogspot.com/2013/08/game-egg.html

Global Variable ที่ใช้ในโปรแกรม : http://com5630264.blogspot.com/2013/09/global-variable.html

เกม : (STEP) http://com5630264.blogspot.com/2013/09/game-egg_15.html
     : (SMOOTH) http://com5630264.blogspot.com/2013/09/game-egg-smooth.html

Global Variable ที่ใช้ในโปรแกรม

Smooth

float a = 250;
float b = 250;
float posX = a;
float posY = b;
float[] x1=new float[100];
float[] x2=new float[100];
float[] y1=new float[100];
float[] y2=new float[100];
int i=1;
int sum=0;
int miss1=0;
int miss2=0;
int take=0;

Step

int xSS=0;
float[] x1=new float[100];
float[] x2=new float[100];
float[] y1=new float[100];
float[] y2=new float[100];
int i=1;
int sum=0;
int miss1=0;
int miss2=0;
int count=0;
int take=0;

function ที่แต่ละคนรับผิดชอบ

นายชนานันน์ พงศ์กีรติกานต์ (56-010126-3004-3)
SetEgg() 
breakEgg(int x)

นางสาวสาริศา ปิ่นทอง (56-010126-3026-4)

drawTree()
drawChute()
drawBush()
drawChicken()

นางสาวอัญธิกา หนองบัว (56-010126-3028-1)

drawBody()
drawArms()

วันอาทิตย์ที่ 15 กันยายน พ.ศ. 2556

GAME : EGG (STEP)





GAME : EGG (SMOOTH)


วันอังคารที่ 10 กันยายน พ.ศ. 2556

BACKGROUND GAME : EGG


วันจันทร์ที่ 2 กันยายน พ.ศ. 2556

Factorial.

void setup() {
  print(fac(9)); //แสดงผลลัพธ์ factorial 9
}
int fac(int n) { //ฟังก์ชันสำหรับการหาผลลัพธ์ของจำนวน n!
  if (n==1) {
    return 1; //base case โดยการตรวจสอบว่า n=1 หรือไม่ โดยมีการ return ค่า 1
  }
  else {
    return n*fac(n-1); //recursive case โดยมีการ return ค่า n*fac(n-1) 
  }
}


out put
362880

G.C.D. & L.C.D.

void setup() {
  println(gcd(100, 64)); //แสดงผลลัพธ์ของ gcd ออกมาที่หน้าจอ และส่งค่า 100 กับ 64 ไปยังฟังก์ชัน gcd( )
  println(lcd(100, 64)); //แสดงผลลัพธ์ของ lcd ออกมาที่หน้าจอ และส่งค่า 100 กับ 64 ไปยังฟังก์ชัน lcd( )
}
int gcd(int x, int y) { //ฟังก์ชันมีการ return ค่าเป็นจำนวนเต็ม และมี parameter เป็น xและ y
  if (x==0) { 
    return y; //base case หาก x=0 ให้ return y
  }
  if (y==0) { 
    return x; //base case หาก y=0 ให้ return x
  }
  if (x>y) {
    return gcd(y, x%y); //recursive case หาก x>y ให้ return gcd(y, x%y)
  }
  else {
    return gcd (x, y%x); //recursive case ให้ return gcd(x, y%x)
  }
}
  int lcd (int x, int y) { //ฟังก์ชันมีการ return ค่าเป็นจำนวนเต็ม และมี parameter เป็น xและ y
  int lcd = (x*y)/gcd(x, y);
  return lcd;
}

out put
4
1600

วันอังคารที่ 27 สิงหาคม พ.ศ. 2556

Button.




void setup(){
  size(200, 200); //หน้าจอแสดงผลขนาด 200x200
  background(0); //พื้นหลังสีดำ
}

Button Name =new Button("Annt"); //ประกาศ object Name =new Button( ) ขึ้นมาเป็นButton (ซึ่งเป็นclassที่สร้างไว้) เป็นตัวแปรแบบ Local Variable
Button Hello = new Button ();

void draw(){
  Name.display(); //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนด object ที่ต้างการใช้ไว้ข้างหน้า method
  Hello.display();
}

void mousePressed(){
  Name.click(); //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนด object ที่ต้างการใช้ไว้ข้างหน้า method
  Hello.click(); 
}

class Button {
  String name;
  int x; //ประกาศ Attribute x ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
  int y; //ประกาศ Attribute y ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
  int z; //ประกาศ Attribute z ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
  Button (String n ){
    this.name=n;  
    this.x=50;
    this.y=50;
    this.z=50;
  }
  Button (){
    this.name="Hello";  
    this.x=120;
    this.y=50;
    this.z=40;
  }

  void display() { //method ที่ใช้ในการแสดงภาพขึ้นCanvas
    strokeWeight(3); 
    if (this.x==50){
      fill(255, 20, 147);
    }
    else{
      fill(0, 191, 255);
    }
    rect(this.x, this.y, this.z, this.z);
  }

  void click() {
    if (mouseX>=this.x&&mouseX<=this.x+this.z&&mouseY>=this.y&&mouseY<=this.y+this.z){
      println(this.name);
    }
  }

}

วันอังคารที่ 20 สิงหาคม พ.ศ. 2556

Palindrome & Reverse.

  void setup() {
  String s = "Computer"; //เป็นการประกาศตัวแปรชนิด String หรือข้อความ
  if (isPalindrome(s)) { 
    println(s + " is a palindrome"); //เป็นการแสดงค่าออกมาทางหน้าจอ
  } 
  else { 
    println(s + " is not a palindrome");
  } 
  print(reverseString("Computer"));
}

boolean isPalindrome(String s) {  //เป็นการสร้าง function โดยกำหนดให้มีการ return ค่าเป็น true หรือ false 
  String t=reverseString(s);
  int i, sum=0;
  for (i=0;i<s.length();i++) { //for(กำหนดค่าให้ตัวแปร,เงื่อนไขในการวน,เพิ่มค่าให้ตัวแปรทุกครั้งที่มีการวนลูป)
    if (t.charAt(i)==s.charAt(i)) {
      sum++;
    }
  }
  if (sum==s.length()) return true;
  else return false;
}
String reverseString(String A) {
  String B = "";
  int i;
  for (i=0;i<A.length();i++) { 
    B=B+(A.charAt(A.length()-1-i));
  }
  return B;
}

out put
Computer is not palindrome
retupmoC



Game&Watch : Egg


Nintendo Game & Watch EGG หรือ ที่เด็กไทยสมัยก่อนเรียกติดปากว่า เกมส์หมาป่ารับไข่ วิธีการเล่นก็ง่ายๆ คือ เราต้องบังคับตัวละครที่เป็นหมาป่าที่กำลังถือตระกร้าอยู่ให้รับไข่ที่จะค่อยๆตกลงมาจากเล้าไก่ทั้ง 4 ด้าน 

สร้างฟังก์ชัน BG() เพื่อเป็นการรวมฉากหลังภายในเกม โดยภายในฟังชันนี้มีการรวมไว้4ฟังก์ชัน  คือ
drawTree(); วาดต้นไม้
drawChute();วาดราง
drawBush();วาดพุ่มไม้
drawChicken();วาดไก่
ซึ่งทั้ง4ฟังก์ชันนี้ไม่มี parameters และการ return ค่า


สร้างฟังก์ชัน Body() เพื่อทำตัวหมาป่า ให้รับไข่ที่ร่วงลงมา
ภายในฟังชันนี้มีฟังก์ชัน drawArms (); วาดแขนของหมาป่าใช้รับไข่ การทำงานของฟังก์ชันแขนคือ ช่วยวาดแขนเมื่อเรากดปุ่มตามที่กำหนด ทำให้ไม่ต้องวาดซ้ำหลายรอบ ซึ่งฟังชันนี้มี parameter เอาไว้เก็บค่าที่ใช้ในคำสั่งวาดมือ quad


การใช้มือรับไข่ของหมาป่า ควบคุมโดยคีย์บอร์ด ปุ่ม Q คือซ้ายบน E คือขวาบน  Z คือซ้ายล่าง C คือขวาล่าง

สร้างฟังก์ชัน setEgg() เพื่อใช้ในการกำหนดตำแหน่งของไข่ไว้ก่อนตั้งแต่แรก
โดยการกำหนดตำแหน่งของไข่ จะทำโดยการ random

สร้างฟังก์ชัน breakEgg(int x) เพื่อทำไข่แตก เมื่อตัวหมาป่ารับไข่ไม่ได้ ซึ่งฟังก์ชันนี้มี parameter แต่ไม่มีการ return ค่า ค่าที่รับมาคือ ค่าในพิกัดแกน X และส่งค่าเข้าไปยังคำสั่งต่างๆภายในฟังก์ชัน





reference : http://www.youtube.com/watch?v=huPKkZlYJDY


class Fraction.

class Fraction { //สร้างClass ขึ้นมาเพื่อเก็บData & Method ทำให้สะดวกในการใช้งานต่อไปในภายหลัง
  int n; //ประกาศ Attribute n ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
  int d;
  Fraction (int a, int b) { //Constructor ใช้ในการกำหนด Object
    n = a; 
    d = b;
  }
  void add (int z) { //Method เกี่ยวกับการบวกเศษส่วน Overloading
    this.n = this.d*z+this.n;
  }
  String toString() {
    String s = this.n+"/"+this.d;
    return s;
  }
  void reciprocal() { //Method เกี่ยวกับการสลับเศษและส่วน
    int tmp;
    tmp = n;
    n = d;
    d = tmp;
  }
}
  void setup() {
    Fraction f = new Fraction (2, 7);
    f.add (3); //ใส่ค่า บวก 3
    println (f.toString());
  }

out put
23/7

วันอาทิตย์ที่ 18 สิงหาคม พ.ศ. 2556

Matrix Calculation.




void setup() {
  int[][] Mt1 = {  //เป็นการสร้างตัวแปร Array 2 มิติ ชนิดจำนวนเต็ม
    {
      1, 2, 3
    }
    , {
      3, 5, 1
    }
    , {
      5, 8, 4
    }
  };
  int[][] Mt2 = {
    {
      1, 3, 5
    }
    , {
      1, 6, 2
    }
    , {
      3, 4, 2
    }
  };
  int[][] sum = new int[3][3]; //เป็นการกำหนดค่าความกว้างของ Array แต่ละมิติโดยช่องแรกกำหนดความกว้างของ index และช่องที่  2 กำหนดว่าข้างในของแต่ละ index จะมีความกว้างเท่าไร
  int y=20, x;
  int i, j;
  for (i=0;i<Mt1.length;i++) { //for(กำหนดค่าให้ตัวแปร,เงื่อนไขในการวน,เพิ่มค่าให้ตัวแปทุกๆครั้งที่มีการวนลูป)
    x=10;
    for (j=0;j<Mt1[i].length;j++) {
      sum[i][j] = Mt1[i][j]+Mt2[i][j];
      fill(0);
      text(" "+sum[i][j],x,y); //คำสั่งแสดงข้อความโดยกำหนดดังนี้("ข้อความ"+ค่าที่ต้องการแสดง,ตำแน่งแกนx,ตำแหน่งแกนy)
      x=x+20;
    }
    y=y+20;
  }



วันศุกร์ที่ 16 สิงหาคม พ.ศ. 2556

Chess.




  void setup() {
  size(400, 400); //หน้าจอแสดงผลขนาด 400x400
  int x; //ประกาศตัวแปร x ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Global Variable
  int y; //ประกาศตัวแปร y ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Global Variable
  int i; //ประกาศตัวแปร i ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Global Variable
  int j; //ประกาศตัวแปร j ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Global Variable

//Array คือ รูปแบบการก็บข้อมูลหลายๆตัวที่มีชนิดของข้อมูลเหมือนกันไว้ด้วยกันเป็นชุด โดยเข้าถึงข้อมูลแต่ละตัวใน Array ได้โดยใช้ index เริ่มต้นที่0
  int[][] Chess= { //สร้างตัวแปร Array 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
    }
  };

  strokeWeight(2);
  for (y=0,i=0;Chess.length>i;i++,y+=50) { //for(กำหนดค่าให้ตัวแปร,เงื่อนไขในการวน, เพิ่มค่าให้ตัวแปรทุกครั้งที่มีการวนลูป)
    for (x=0,j=0;Chess[0].length>j;j++,x+=50) {
      if ((j+i)%2==0) { //เป็นการกำหนดเงื่อนไขการใส่สีของตาราง
        fill(255, 0, 0);
      }
      else {
        fill(0); //Black color
      }
      rect(x, y, 100, 100); //วาดสี่เหลี่ยมเป็นตาราง
    }
  }

  for (x=25,y=25,i=0;Chess.length>i;i++,y+=50) {
    for (x=25,j=0;Chess[i].length>j;j++,x+=50) {
      if (y<200) {
        fill(255, 20, 147);
        stroke(255, 255, 0);
      }
      else if (y>200) {
        fill(0, 255, 255);
        stroke(255, 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(255);
        }
        strokeWeight(5);
        prawn(x, y);
      }
    }
  }
}

void rook(int x, int y) { //ฟังก์ชันที่ใช้วาดเรือ
  ellipse(x, y, 45, 45);
  if (y<200) {
    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 (y<200) {
    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 (y<200) {
    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 (y<200) {
    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 (y<200) {
    fill(255);
  }
  else if (y>200) {
    fill(0);
  }
  text("king", x-10, y+3);
}
void prawn(int x, int y) { //ฟังก์ชันที่ใช้วาดตัวเบี้ย
  ellipse(x, y, 40, 40);
}

อ้างอิงอธิบายคำสั่งจาก  http://com5630264.blogspot.com/search/label/Lab%201
                               http://com5630264.blogspot.com/search/label/Lab%202
                               http://com5630264.blogspot.com/search/label/Lab%203

วันพุธที่ 14 สิงหาคม พ.ศ. 2556

Othello





int x=50; //ประกาศตัวแปร x=50 เป็นการประกาศแบบ Global Variable
int y=50; //ประกาศตัวแปร y=50 เป็นการประกาศแบบ Global Variable

void setup() {
  int i; //ประกาศตัวแปร i ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
  int j; //ประกาศตัวแปร j ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable

//Array คือ รูปแบบการก็บข้อมูลหลายๆตัวที่มีชนิดของข้อมูลเหมือนกันไว้ด้วยกันเป็นชุด โดยเข้าถึงข้อมูลแต่ละตัวใน Array ได้โดยใช้ index เริ่มต้นที่0
  int[][] Othello=new int[8][8] ; //สร้างตัวแปร Arra y2มิติ ชนิดจำนวนเต็ม และกำหนดค่า
  size(400, 400); //หน้าจอแสดงผลขนาด 400x400
  background(12, 144, 90); //ใส่สีเขียวให้กับพื้นหลัง
  for (i=0;i<Othello.length;i++)
  { 
    for (j=0;j<Othello[0].length;j++) //for(กำหนดค่าให้ตัวแปร,เงื่อนไขในการวน,เพิ่มค่าให้ตัวแปรทุกครั้งที่มีการวนลูป)
    {
      Othello[i][j]=int(random(0, 200));
    }
  }

  drawSchedule(); //เรียกใช้ ฟังก์ชัน drawSchedule();

  noStroke(); //ไม่มีเส้น
  for (i=0,  y=25;i<Othello.length;i++,y+=50) 
  {
    for (j=0, x=25;j<Othello[0].length;j++) 
    {
      if (Othello[i][j]%2==0) 
      {
        fill(0);
      }
      else 
      {
        fill(255); //white color
      }
      ellipse(x, y, 40, 40);
      x+=50;
    }
  }
}

void drawSchedule() { //สร้างฟังก์ชันวาดตารางบนกระดาน
  while (x<400) {
    strokeWeight(2);
    line(x, 0, x, 400);
    x=x+50; //x จะมีค่าเพิ่มขึ้น 50  ทุกๆครั้งที่มีการวนลูป
  }
  while (y<400) {
    line(0, y, 400, y);
    y=y+50; //y จะมีค่าเพิ่มขึ้น 50  ทุกๆครั้งที่มีการวนลูป
  }
}

อ้างอิงอธิบายคำสั่งจาก  http://com5630264.blogspot.com/search/label/Lab%201
                               http://com5630264.blogspot.com/search/label/Lab%202
                               http://com5630264.blogspot.com/search/label/Lab%203

วันอังคารที่ 13 สิงหาคม พ.ศ. 2556

Tic-tac-toe (OX)




int x = 100; //ประกาศตัวแปร x=100 เป็นการประกาศแบบ Global Variable
int y = 100; //ประกาศตัวแปร y=100 เป็นการประกาศแบบ Global Variable
//Array คือ รูปแบบการก็บข้อมูลหลายๆตัวที่มีชนิดของข้อมูลเหมือนกันไว้ด้วยกันเป็นชุด โดยเข้าถึงข้อมูลแต่ละตัวใน Array ได้โดยใช้ index เริ่มต้นที่0
int[][] ox= { //สร้างตัวแปร Array 2มิติ ชนิดจำนวนเต็ม และกำหนดค่า 
  {
    0, 1, 1
  }
  , {
    1, 1, 0
  }
  , {
    0, 0, 1
  }
};
int a = 0; 
int b = 0;
void setup() {
  size(300, 300); //หน้าจอแสดงผลขนาด 300x300
  background(0); //black color
  drawSchedule(); //เรียกใช้ ฟังก์ชัน drawSchedule();
  drawOX(); //เรียกใช้ ฟังก์ชัน OX();
}
void drawSchedule() { //สร้างฟังก์ชันในการวาดกระดาน
  while (x<300) { 
    stroke(255, 255, 0); //ใส่สีเหลืองให้กับเส้นวาดตาราง
    strokeWeight(10); //ใส่ความหนาของเส้นเท่ากับ 10
    line(x, 20, x, 280);
    x=x+100;  //x จะมีค่าเพิ่มขึ้น 100  ทุกๆครั้งที่มีการวนลูป
  }
  while (y<300) {
    line(20, y, 280, y);
    y=y+100; //y จะมีค่าเพิ่มขึ้น 100  ทุกๆครั้งที่มีการวนลูป
  }
}
void drawOX() { //สร้างฟังก์ชันในการวาดOX
  x = 50;
  y = 50;
  while (a<ox.length) {
    while (b<ox[a].length) {
      if (ox[a][b]==1) {
        strokeWeight(5);
        stroke(255, 20, 147); //เส้นสีชมพู
        line(x-20, y-20, x+20, y+20);
        line(x-20, y+20, x+20, y-20);
      }
      else if (ox[a][b]==0) {
        strokeWeight(5); //ใส่ความหนาของเส้นเท่ากับ 5
        stroke(0, 255, 255); //เส้นสีชมพู
        noFill(); //ไม่มีสี
        ellipse(x, y, 50, 50); //วาดวงกลม
      }
      b=b+1;
      x=x+100;
    }
    x=50;
    y=y+100;
    b=0;
    a=a+1;
  }
}

อ้างอิงอธิบายคำสั่งจาก  http://com5630264.blogspot.com/search/label/Lab%201
                               http://com5630264.blogspot.com/search/label/Lab%202
                               http://com5630264.blogspot.com/search/label/Lab%203