วันพุธที่ 16 ธันวาคม พ.ศ. 2558

Assignment 2

int []check={0, 0, 0};
Acontroller c;
color []status = {color(255), color(255), color(255), color(255), color(255)};
int run = 0;
int countFrame = 0;
void setup() {
  size(700, 380);
  Amaker m = new Amaker (18, 10);
  Aplayer p = new Aplayer (m);
  Aviewer v = new Aviewer (m);
  c = new Acontroller(m, v, p);
}
void draw() {
  c.window();
  c.button();
  stroke(0);
}
class Amaker {
  int w, h, tile, wide, count;
  String [][] size;
  String [][][] frame;
  Amaker(int width_x, int height_y) {  
    w = width_x;
    h = height_y;
    tile = 0;
    count = 0;
    wide = 30;
    size = new String [h][w];
    frame = new String [100][h][w];
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        size[i][j] = "#";
      }
    }
  }

  void add_tile(int x, int y) {
    size[y][x] = "0";
    tile = tile+1;
  }
  void remove_tile(int x, int y) {
    size[y][x] = "#";
    tile = tile-1;
  }

  void remove_all_tile() {
    for (int y = 0; y<h; y++) {
      for (int x = 0; x<w; x++) {
        size[y][x] = "#";
        tile = 0;
      }
    }
  }

  void move_tile(int x1, int y1, int x2, int y2) {
    if (size[y1][x1] == "0") {
      size[y1][x1] = "#";
      size[y2][x2] = "1";
    }
  }
  void add_frame() {
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        frame[count][i][j] = size[i][j];
        if (size[i][j] == "1") {
          size[i][j] = "0";
        }
      }
    }
    count = count+1;
  }
  void remove_frame() {
    if (count <= 0) {
      count = 0;
    } else {
      count=count-1;
    }
    if (count>0) {
      for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
          size[i][j]=frame[count-1][i][j];
        }
      }
    }
  }
  int get_total_num_tile() {
    int num = 0;
    for (int y = 0; y<h; y++) {
      for (int x = 0; x<w; x++) {
        if (size[y][x] == "0" || size[y][x] == "1") {
          num = num+1;
        }
      }
    }
    return num;
  }
  int get_total_num_frame() {
    return count;
  }
  int get_width() {
    return w;
  }
  int get_height() {
    return h;
  }
  String [][] get_size() {
    return size;
  }
  int get_wide() {
    return wide;
  }
  String [][][] get_frame() {
    return frame;
  }
}
class Aviewer {
  int w, h, wide;
  String [][] size = new String [h][w];
  Amaker m;
  Aviewer(Amaker make) {
    m = make;
    w = m.get_width();
    h = m.get_height();
    size = m.get_size();
    wide = m.get_wide();
  }
  void display(int x, int y) {
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        if (size[i][j] == "0" || size[i][j] == "1") {
          fill(255);
        } else if (size[i][j] == "#") {
          fill(156, 145, 135);
        }
        rect(x+(j*wide), y+(i*wide), wide, wide);
      }
    }
  }
}
class Acontroller {
  int w, h, wide, x1, x2, y1, y2;
  int posX = 20;
  int posY = 20;
  int posText = 425;
  int move = 0;
  String [][] size = new String [w][h];
  int mode = 0;
  Amaker m;
  Aviewer v;
  Aplayer p;
  Acontroller(Amaker make, Aviewer view, Aplayer play) {
    m = make;
    v = view;
    p = play;
    w = make.get_width();
    h = make.get_height();
    size = m.get_size();
    wide = m.get_wide();
  }
  void mouse_position() {
    //add frame
    if (positionMouseClick(575, 625, 245, 295)) {
      m.add_frame();
    }
    //remove frame
    if (positionMouseClick(575, 625, 310, 360)) {
      m.remove_frame();
    }
    //remove tile
    if (mouseButton == LEFT && positionMouseClick(575, 625, 95, 135)) {
      m.remove_all_tile();
    }
    //play
    if (mousePressed && positionMouseClick(430, 470, 330, 370) && m.get_total_num_frame() != 0) {
      run = 1;
    }
    //stop
    if (mousePressed && positionMouseClick(500, 540, 330, 370)) {
      run = 0;
      countFrame = 0;
    }
  }
  void window() {
    background(100);
    textAlign(CENTER);
    for (int y = 0; y < m.get_height(); y++) {
      for (int x = 0; x < m.get_width(); x++) {
        if (mousePressed && positionMouseClick(575, 625, 20, 70)) {
          mode = 1;
          check[0] = 1;
          check[1] = 0;
          check[2] = 0;
        } else if (mousePressed && positionMouseClick(575, 625, 95, 135)) {
          check[1] = 1;
          check[0] = 0;
          check[2] = 0;
        } else if (mousePressed && positionMouseClick(575, 625, 150, 200)) {
          check[2] = 1;
          check[1] = 0;
          check[0] = 0;
        }

        for (int i=0; i<3; i++) {
          if (check[i] == 1) {
            status[i] = #00FF00;
          } else {
            status[i] = 255;
          }
        }
        //add tile
        if (mousePressed && mouseButton == LEFT && run == 0 && check[0] ==1  && positionMouseClick(575,625,20,70)) {
          m.add_tile(x, y);
        }
        //remove tile
        else if (mousePressed && mouseButton == RIGHT && run == 0 && check[0] ==1 && positionMouseClick(575,625,95,135)) {
          m.remove_tile(x, y);
        }
        //move tile
        if (mousePressed && mouseButton == LEFT && run == 0 && check[2] ==1  && positionMouseClick(575,625,150,200)) {
          if (move == 0 && size[y][x] == "0") {
            x1 = x;
            y1 = y;
            move = 1;
          }
          if (move == 1) {
            x2 = x;
            y2 = y;
            if ((abs(y2-y1) == 1 && x2 == x1) || (abs(x2-x1) == 1 && y2 == y1) && size[y][x] != "0" && size[y][x] != "1") {
              m.move_tile(x1, y1, x2, y2);
              move = 0;
            }
          }
        }
      }
    }
    if (mousePressed && positionMouseClick(380, 505, 450, 490)) {
      status[3] = #00FF00;
    } else status[3]= 255;
    if (mousePressed && positionMouseClick(505, 630, 450, 490)) {
      status[4] = #00FF00;
    } else status[4]= 255;
    if (run == 0) {
      v.display(posX, posY);
      frameRate(120);
    } else {
      p.play_frame(posX, posY);
      frameRate(5);
    }
    if (mode == 0) {
      textSize(30);
    }  
    fill(0);
    textSize(12);
    text("add\ntile", 650, 40);
    text("remove\ntile", 650, 105);
    text("move\ntile", 650, 170);
    text("add\nframe", 650, 265);
    text("remove\nframe", 650, 330);
    textSize(15);
    fill(0);
    text("Tile(s) : "+m.get_total_num_tile(), 60, 350);
    text("Frame : "+m.get_total_num_frame(), 250, 350);
  }
  void button() {
    noStroke();
    //play
    fill(255);
    ellipse(450, 350, 40, 40);
    fill(#00FF00);
    triangle(440, 340, 440, 360, 460, 350);
    //stop
    fill(255);
    ellipse(520, 350, 40, 40);
    fill(#FF0000);
    rect(510, 340, 20, 20);
    fill(status[0]);
    ellipse(600, 45, 50, 50);  //add tile
    fill(status[1]);
    ellipse(600, 110, 50, 50); //remove tile
    fill(status[2]);
    ellipse(600, 175, 50, 50); //move tile
    fill(status[3]);
    ellipse(600, 270, 50, 50); //add frame
    fill(status[4]);
    ellipse(600, 335, 50, 50); //remove frame
    fill(0);
    //add tile
    rect(592.5, 25, 15, 40);
    rect(580, 37.5, 40, 15);
    //remove tile
    quad(580,100,590,90,620,120,610,130);
    quad(590,130,580,120,610,90,620,100);
    //move tile
    triangle(585,160,595,160,585,170);
    triangle(605,160,615,160,615,170);
    triangle(615,180,615,190,605,190);
    triangle(595,190,585,190,585,180);
    stroke(5);
    line(590,165,610,185);
    line(610,165,590,185);
    //add frame
    rect(592.5, 250, 15, 40);
    rect(580, 262.5, 40, 15);
    //remove frame
    quad(580,325,590,315,620,345,610,355);
    quad(590,355,580,345,610,315,620,325);
  }
}
class Aplayer {
  int w, h, wide;
  int posX = 215;
  int posY = 75;
  String [][] size = new String [w][h];
  String [][][] frame = new String [100][w][h];
  Amaker m;
  Aplayer(Amaker make) {
    m = make;
    w = m.get_width();
    h = m.get_height();
    size = m.get_size();
    wide = m.get_wide();
    frame = m.get_frame();
  }
  void play_frame(int x, int y) {
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        if (frame[countFrame][i][j] == "0" || frame[countFrame][i][j] == "1") {
          fill(255);
        } else if (frame[countFrame][i][j] == "#") {
          fill(156, 145, 135);
        }
        rect(x+(j*wide), y+(i*wide), wide, wide);
      }
    }
    countFrame++;
    countFrame = countFrame%m.get_total_num_frame();
  }
}

boolean positionMouseClick(int morethanPosX, int lessthanPosX, int morethanPosY, int lessthanPosY) {
  boolean check = (mouseX >= morethanPosX && mouseX <= lessthanPosX && mouseY >= morethanPosY && mouseY <= lessthanPosY);
  return check;
}
void mousePressed() {
  c.mouse_position();
}

วันพุธที่ 25 พฤศจิกายน พ.ศ. 2558

Lab rasberry pi : class Student

class Student:
    def __init__(self,name,ID,score):
        self.name = name
        self.ID = ID
        self.score = score
    def get_score(self):
        return self.score
    def display(self):
        print('Name : ',self.name)
        print('ID : ',self.ID)
        print('Score : ',self.score)
    def set_score(self):
        return self.score
def setup():
    stu = [Student('A',123,69),
           Student('B',456,80),
           Student('C',789,18),
           Student('D',753,59),
           Student('E',951,27)]
    show_all_grade(stu)
   
def find_grade(stu):
    if(stu.get_score()>=80):
        return 'A'
    elif(stu.get_score()>=70 and stu.get_score()<80):
        return 'B'
    elif(stu.get_score()>=60 and stu.get_score()<70):
        return 'C'
    elif(stu.get_score()>=50 and stu.get_score()<60):
        return 'D'
    else:
        return 'F'

def count_grade(stu):
    n = 0
    i = 0
    s = stu[i].get_score()
    while(i<len(stu)):
        if(s>=80):
            n =n+1
        elif(s>=70 and s<80):
            n=n+1
        elif(s>=60 and s<70):
            n=n+1
        elif(s>=50 and s<60):
            n=n+1
        else:
            n=n+1
        i=i+1
    return n

def show_all_grade(stu):
   i = 0
   while(i<len(stu)):
       stu[i].display()
       print("Grade :",find_grade(stu[i]))
       print()
       i=i+1
setup()

วันอาทิตย์ที่ 25 ตุลาคม พ.ศ. 2558

Lab 6 : Multiply two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   multiply_matrix(matrix_1,matrix_2)
 
def multiply_matrix(matrix_1,matrix_2):
   i = 0
   i_i = 0
   k_i = 0
   while(i_i<len(matrix_1)):
      j = 0
      i_j = 0
      k_j = 0
      print("|",end="")
      while(i_j<len(matrix_2[i])):
         total_1=matrix_1[i+k_i][j]*matrix_2[i][j+k_j]
         total_2=matrix_1[i+k_i][j+1]*matrix_2[i+1][j+k_j]
         total_3=matrix_1[i+k_i][j+2]*matrix_2[i+2][j+k_j]
         print("",total_1+total_2+total_3,end="")
         k_j = k_j+1
         i_j = i_j+1
      print("|")
      k_i = k_i+1
      i_i = i_i+1
     
setup()

Lab 6 : Subtract two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   add_2(matrix_1,matrix_2)
 
def add_2(matrix_1,matrix_2):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j]-matrix_2[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Add two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   add_2(matrix_1,matrix_2)
 
def add_2(matrix_1,matrix_2):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j]+matrix_2[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Display the matrix

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   display_matrix(matrix_1)
 
def display_matrix(matrix_1):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Find indices of the room(s) with maximum number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   num = building_1[0][0]
   for i in range(0, len(building_1)):
      i_room = 0
      for i in range(0,len(building_1[i_floor])):
         if(building_1[i_floor][i_room] > num):
            num = building_1[i_floor][i_room]
         i_room = i_room+1
      i_floor = i_floor+1
   i_floor = 0
   for i in range(i_floor,len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         if(num==building_1[i_floor][i_room]):
            print("indices of the room(s) with maximum number of chairs :","[",i_floor,"][",i_room,"]")
         i_room = i_room+1
      i_floor = i_floor+1
     
setup()