Write a JAVA program that track two users as they play a game of chess: ? The bo
ID: 3561805 • Letter: W
Question
Write a JAVA program that track two users as they play a game of chess:
? The board will be displayed in the following format:
a b c d e f g h
8 br bn bb bq bk bb bn br 8
7 bp bp bp bp bp bp bp bp 7
6 -- -- -- -- -- -- -- -- 6
5 -- -- -- -- -- -- -- -- 5
4 -- -- -- -- -- -- -- -- 4
3 -- -- -- -- -- -- -- -- 3
2 wp wp wp wp wp wp wp wp 2
1 wr wn wb wq wk wb wn wr 1
a b c d e f g h
o Note that pieces will be represented by the color of the piece, then the type (p=pawn, n=knight,
b=bishop, r=rook, q=queen, k=king), with two dashes for an empty square)
? When you ask for a move, you should display
Explanation / Answer
class GameBoard { IPiece config[8][8]; init { createAndPlacePieces("Black"); createAndPlacePieces("White"); setTurn("Black"); } createAndPlacePieces(color) { //generate pieces using a factory method //for e.g. config[1][0] = PieceFactory("Pawn",color); } setTurn(color) { turn = color; } move(fromPt,toPt) { if(getPcAt(fromPt).color == turn) { toPtHasOppositeColorPiece = getPcAt(toPt) != null && getPcAt(toPt).color != turn; possiblePath = getPcAt(fromPt).generatePossiblePath(fromPt,toPt,toPtHasOppositeColorPiece); if(possiblePath != NULL) { traversePath(); changeTurn(); } } } } Interface IPiece { function generatePossiblePath(fromPt,toPt,toPtHasEnemy); } class PawnPiece implements IPiece{ function generatePossiblePath(fromPt,toPt,toPtHasEnemy) { return an array of points if such a path is possible else return null; } } class ElephantPiece implements IPiece {....}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.