Chess หมากรุกแบบ Java


Chess หมากรุกนะครับ

วันก่อนให้น้องๆ ที่เรียน 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;

}

 

 

}

คำสำคัญ (Tags): #chess java
หมายเลขบันทึก: 324279เขียนเมื่อ 31 ธันวาคม 2009 13:46 น. ()แก้ไขเมื่อ 5 มิถุนายน 2012 12:10 น. ()สัญญาอนุญาต: ครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า-อนุญาตแบบเดียวกันจำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท