วันศุกร์ที่ 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);
}
ป้ายกำกับ:
Lab 6.2
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น