วันก่อนให้น้องๆ ที่เรียน Advance Computer programming ได้ลองเขียน Chess หมากรุก บางคนเขียนได้บ้าง ไม่ได้ บ้างวันนี้เลย เอามาเฉลย เสียหน่อยนะครับ หวังว่าคงเป็นแนวทาง น้องคนไหนเข้ามาดูแล้วเขียนไม่เหมือนไม่ได้หมายความว่าผิดนะครับ แต่ถ้าเขียนแล้วรันได้ทำงานได้ แสดงว่าใช้ได้แล้วละ ความคิดมันไม่เหมือนกันจริงไหม อิอิ เหมือนบวกเลข 2 ตัวให้เท่ากับ 10 ไม่ได้หมายความว่า เอา 4+6 ได้วิธีเดียวจริงไหม ว่าแล้วก็ เริ่มเลยแล้วกัน
อันนี้เป็น Position Class
package Chess;
/**
* Description
* @author 50060113 Pisit Nakjai
* @since
* @note Microsoft Window 7
* @note JavaSun JDK 1.6
*/
public class Position {
//Atribute
private int row;
private int column;
/**
* Default Constructor
*/
public Position() {
this.row = 0;
this.column = 0;
}
/**
* Constructor
*/
public Position(int row, int column) {
this.row = row;
this.column = column;
}
/**
* Check row and column is equal this Position
* @param instanceOther
* @return true if row and column other opsition is equal row and column this position
* @return false if otherwise
* @test new Postion(4,5).equal(new Position(4,5)) = true;
* @test new Postion(4,1).equal(new Position(4,6)) = false;
* @test new Postion(1,5).equal(new Position()) = false;
*/
public boolean equals(Object instanceOther) {
if (instanceOther instanceof Position) {
Position insPos = (Position) instanceOther;
return (this.getRow() == insPos.getRow() && this.getColumn() == insPos.getColumn());
}
return false;
}
/**
* get the value of row
* @return the row
*/
public int getRow() {
return row;
}
/**
* Set the value into row
* @param row the row to set
*/
public void setRow(int row) {
this.row = row;
}
/**
* get the value column
* @return the column
*/
public int getColumn() {
return column;
}
/**
* set the value into column
* @param column the column to set
*/
public void setColumn(int column) {
this.column = column;
}
public String toString() {
return ("(" + this.row + "," + this.column + ")");
}
}
อันนี้เป้น King Class นะครับ
package Chess;
/**
* Description King Class Simple
* @author 50060113 Pisit Nakjai
* @since
* @note Microsoft Window 7
* @note JavaSun JDK 1.6
*/
public class King{
// Attribute
private String color;
private Position pos;
/**
* Default Constructor
*/
public King(){
this.color = "";
this.pos = new Position();
}
/**
* Constructor
*/
public King(String color,int row ,int column){
this.color = color;
this.pos = new Position(row,column);
}
public King(String color,Position instancePos){
this.color = color;
this.pos = instancePos;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the pos
*/
public Position getPos() {
return pos;
}
/**
* @param pos the pos to set
*/
public void setPos(Position pos) {
this.pos = pos;
}
}
ไม่มีความเห็น