Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment is about chess and all the classes needed will be provided with the test codes to see if the codes are correct ALL

This assignment is about chess and all the classes needed will be provided with the test codes to see if the codes are correct

ALL THE CLASSES ARE PROVIDED BELOW

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess; import chess.Coordinate; /** * * @author Cheryl */ public class Assign1 {

/** * @param args the command line arguments */ public static void main(String[] args) { ChessBoard board = new ChessBoard(); System.out.println("Board:" + board.toString()); System.out.println("FEN:" + board.toFEN()); board.move ( new Coordinate("b2"), new Coordinate("b3") ); board.move ( new Coordinate("g8"), new Coordinate("f6") ); System.out.println("FEN:" + board.toFEN()); // Now construct a test board -- game already in play // Sample board = RN,,K,,R/,P,,,,,,/,,,,,,,,/,,,,,,,,/,Q,,,,,,/,,,,,,,,/pppppppp/rn,,k,,r/ Coordinate coordinates[] = new Coordinate[18]; Piece pieces[] = new Piece[18]; coordinates[0] = new Coordinate(0,0); pieces[0] = new Piece('R'); coordinates[1] = new Coordinate(1,0); pieces[1] = new Piece('N'); coordinates[2] = new Coordinate(4,0); pieces[2] = new Piece('K'); coordinates[3] = new Coordinate(7,0); pieces[3] = new Piece('R'); coordinates[4] = new Coordinate(1,1); pieces[4] = new Piece('P'); coordinates[5] = new Coordinate(1,4); pieces[5] = new Piece('Q'); coordinates[6] = new Coordinate(0,6); pieces[6] = new Piece('p'); coordinates[7] = new Coordinate(1,6); pieces[7] = new Piece('p'); coordinates[8] = new Coordinate(2,6); pieces[8] = new Piece('p'); coordinates[9] = new Coordinate(3,6); pieces[9] = new Piece('p'); coordinates[10] = new Coordinate(4,6); pieces[10] = new Piece('p'); coordinates[11] = new Coordinate(5,6); pieces[11] = new Piece('p'); coordinates[12] = new Coordinate(6,6); pieces[12] = new Piece('p'); coordinates[13] = new Coordinate(7,6); pieces[13] = new Piece('p'); coordinates[14] = new Coordinate(0,7); pieces[14] = new Piece('r'); coordinates[15] = new Coordinate(1,7); pieces[15] = new Piece('n'); coordinates[16] = new Coordinate(4,7); pieces[16] = new Piece('k'); coordinates[17] = new Coordinate(7,7); pieces[17] = new Piece('r'); board = new ChessBoard(coordinates, pieces); System.out.println("FEN:" + board.toFEN()); } }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess;

/** * * @author Cheryl */ public class ChessBoard { private Square board[][]; private ChessColour activeColour; private int fullMove; public ChessBoard() { board = new Square[8][8]; for (int c=0; c=0; r--) { for (int c=0; c=0; r--) { for (int c=0; c

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess;

/** * * @author Cheryl */ public enum ChessColour { WHITE, BLACK }

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* ** Good reference on ENUMs: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

*/ package chess;

/** * * @author Schramm */ public enum ChessPieces { PAWN ('P'), KNIGHT ('N'), BISHOP ('B'), ROOK ('R'), QUEEN ('Q'), KING ('K'); private final char shortName;

ChessPieces (char shortName) { this.shortName = shortName; } public char getShortName() { return this.shortName; } }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess;

/** * * @author Cheryl */ public class Coordinate { private int row; // 0 is the bottom horizontal row private int column; // 0 is the left vertical column public Coordinate(int column, int row) throws IndexOutOfBoundsException { if ( (column7)) throw new IndexOutOfBoundsException("column must be between 0 and 7,inclusive"); if ( (row7)) throw new IndexOutOfBoundsException("row must be between 0 and 7,inclusive"); this.row = row; this.column = column; } public Coordinate(char column, char row) throws IndexOutOfBoundsException { if ( (column'h')) throw new IndexOutOfBoundsException("column must be between a and h,inclusive"); if ( (row'8')) throw new IndexOutOfBoundsException("row must be between 1 and 8,inclusive"); this.column = column - 'a'; this.row = row - '1'; } public Coordinate(String coordinate) throws IndexOutOfBoundsException { if (coordinate.length() != 2) throw new IllegalArgumentException ("Coordinate is a 2-character string"); char column = coordinate.charAt(0); char row = coordinate.charAt(1); // this(x,y) except it must be the first statement! if ( (column'h')) throw new IndexOutOfBoundsException("x must be between a and h, inclusive"); if ( (row'8')) throw new IndexOutOfBoundsException("y must be between 1 and 8, inclusive"); this.column = column - 'a'; this.row = row - '1'; } public int getColumnNumber() { return this.column; } public int getRowNumber() { return this.row; } public char getColumn() { return (char)(column + 'a'); } public char getRow() { return (char)(row+'0'+1); } @Override public String toString() { return "("+getColumnNumber()+","+getRowNumber()+")"; } public String name() { return "" + getColumn()+ getRow() ; }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess; import chess.ChessColour;

/** * * @author Cheryl */ public class Piece { private ChessColour colour; private ChessPieces name; private char shortName; public Piece() {} public Piece(ChessColour colour, ChessPieces name) { this.colour = colour; this.name = name; this.shortName = name.getShortName(); if (colour == ChessColour.BLACK) this.shortName = Character.toLowerCase(this.shortName); } public Piece(char shortName) { this.shortName = shortName; char temp = Character.toUpperCase(shortName); switch (temp) { case 'P' : this.name = ChessPieces.PAWN; break; case 'N' : this.name = ChessPieces.KNIGHT; break; case 'B' : this.name = ChessPieces.BISHOP; break; case 'R' : this.name = ChessPieces.ROOK; break; case 'Q' : this.name = ChessPieces.QUEEN; break; case 'K' : this.name = ChessPieces.KING; break; default: throw new IllegalArgumentException ("shortname " + shortName + " is not valid"); } this.colour = (Character.isLowerCase(shortName)) ? ChessColour.BLACK : ChessColour.WHITE;

} public ChessColour getColour() { return this.colour; } public ChessPieces getName() { return this.name; } public char getShortName() { return this.shortName; } public String toString() { return colour + " " + name; } }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess;

/** * * @author Cheryl */ public class Square { private Coordinate coordinate; private Piece piece; public Square(Coordinate c) { this.coordinate = c; this.piece = null; // No piece is positioned on this square } public Square(Coordinate c, Piece p) { this( c ); this.piece = p; // A piece is positioned on this square } public char getColumn() { return this.coordinate.getColumn(); } public char getRow() { return this.coordinate.getRow(); } public int getColumnNumber() { return this.coordinate.getColumnNumber(); } public int getRowNumber() { return this.coordinate.getRowNumber(); } public Coordinate getCoordinate() { return this.coordinate; } public Piece getPiece() { return this.piece; } public boolean isOccupied() { return (this.piece != null); } // Returns previous piece, if the square is currently occupied. public Piece addPiece(Piece newPiece) { Piece previous = this.piece; this.piece = newPiece; return previous; } public Piece deletePiece() { Piece previous = this.piece; this.piece = null; return previous; } @Override public String toString() { String p = (this.piece == null) ? " " : piece.toString(); return ("Square"+coordinate.toString()+":"+p); } }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ANYTHING BELOW IS IS THE TEST CLASSES AND INSTRUCTIONS

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

import chess.ChessBoard; import chess.Coordinate; import chess.Piece; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*;

/** * * @author schr */ public class ChessBoardTest { private static ChessBoard testBoard; private static ChessBoard resetBoard; public ChessBoardTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { resetBoard = new ChessBoard(); Coordinate coordinates[] = new Coordinate[18]; Piece pieces[] = new Piece[18]; coordinates[0] = new Coordinate("a1"); pieces[0] = new Piece('R'); // White coordinates[1] = new Coordinate("b1"); pieces[1] = new Piece('N'); coordinates[2] = new Coordinate("e1"); pieces[2] = new Piece('K'); coordinates[3] = new Coordinate("h1"); pieces[3] = new Piece('R'); coordinates[4] = new Coordinate("b2"); pieces[4] = new Piece('P'); coordinates[5] = new Coordinate("b5"); pieces[5] = new Piece('Q'); coordinates[6] = new Coordinate("a7"); pieces[6] = new Piece('p'); coordinates[7] = new Coordinate("b7"); pieces[7] = new Piece('p'); coordinates[8] = new Coordinate("c7"); pieces[8] = new Piece('p'); coordinates[9] = new Coordinate("d7"); pieces[9] = new Piece('p'); coordinates[10] = new Coordinate("e7"); pieces[10] = new Piece('p'); coordinates[11] = new Coordinate("f7"); pieces[11] = new Piece('p'); coordinates[12] = new Coordinate("g7"); pieces[12] = new Piece('p'); coordinates[13] = new Coordinate("h7"); pieces[13] = new Piece('p'); coordinates[14] = new Coordinate("a8"); pieces[14] = new Piece('r'); coordinates[15] = new Coordinate("b8"); pieces[15] = new Piece('n'); coordinates[16] = new Coordinate("e8"); pieces[16] = new Piece('k'); coordinates[17] = new Coordinate("h8"); pieces[17] = new Piece('r'); // Black testBoard = new ChessBoard(coordinates, pieces); } @After public void tearDown() { }

@Test public void testReset() { // TBD? } @Test public void testPawns() { assertEquals(true, resetBoard.move( new Coordinate("d2"), new Coordinate("d4"))); // White Pawn - Move 2 forward assertEquals(true, resetBoard.move( new Coordinate("e7"), new Coordinate("e5"))); // Black Pawn =- Move 2 forward assertEquals(true, resetBoard.move( new Coordinate("d4"), new Coordinate("e5"))); // White Pawn takes Black Pawn assertEquals(true, resetBoard.move( new Coordinate("a7"), new Coordinate("a6"))); // Any legal move for Black to prepare for next test assertEquals(true, resetBoard.move( new Coordinate("b2"), new Coordinate("b3"))); // White Pawn - Move 1 forward assertEquals(true, resetBoard.move( new Coordinate("g7"), new Coordinate("g6"))); // Black Pawn =- Move 1 forward // In Part 1: This next test should pass (allowing a pawn to move 2 forward on 2nd move) // (So you need a Black move next) assertEquals(true, resetBoard.move( new Coordinate("b3"), new Coordinate("b5"))); // White Pawn - Move 2 forward (!FirstMove) // In Part 2: This next test should fail (restricting a pawn to moving 2 forward only on 1st move) // (So you need a White move next) //assertEquals(false, resetBoard.move( new Coordinate("b3"), new Coordinate("b5"))); // White Pawn - Move 2 forward //assertEquals(true, resetBoard.move( new Coordiante("e5"), new Coordinate("e6"))); // Any legal move for White to prepare next move assertEquals(false, resetBoard.move( new Coordinate("g6"), new Coordinate("g7"))); // Black Pawn - Move 1 backward (No) assertEquals(false, resetBoard.move( new Coordinate("g6"), new Coordinate("f6"))); // Black Pawn - Move 1 left (No) assertEquals(false, resetBoard.move( new Coordinate("g6"), new Coordinate("h6"))); // Black Pawn - Move 1 right (No)

} @Test public void testKings() { assertEquals(false, resetBoard.move( new Coordinate("e1"), new Coordinate("e2"))); // White King - 1 forward, occupied (No) // Notice: Using resetBoard! assertEquals(true, testBoard.move( new Coordinate("e1"), new Coordinate ("f1"))); // White King - Right assertEquals(true, testBoard.move ( new Coordinate("e8"), new Coordinate ("d8"))); // Black King - Left assertEquals(true, testBoard.move( new Coordinate("f1"), new Coordinate ("e1"))); // White King - Left assertEquals(true, testBoard.move ( new Coordinate("d8"), new Coordinate ("e8"))); // Black King - Right assertEquals(true, testBoard.move( new Coordinate("e1"), new Coordinate ("e2"))); // White King - Forward assertEquals(false, testBoard.move ( new Coordinate("e8"), new Coordinate ("e7"))); // Black King - Forward, Occupied (No) } @Test public void testKnights() { assertEquals(true, resetBoard.move( new Coordinate("b1"), new Coordinate ("c3"))); // White Knight - Hop forward, right assertEquals(true, resetBoard.move ( new Coordinate("g8"), new Coordinate ("h6"))); // Black Knight - Hop forward, right assertEquals(true, resetBoard.move( new Coordinate("c3"), new Coordinate ("b1"))); // White Knight - Hop backward, left assertEquals(true, resetBoard.move ( new Coordinate("h6"), new Coordinate ("g8"))); // Black Knight - Hop backward, left assertEquals(true, resetBoard.move( new Coordinate("b1"), new Coordinate ("a3"))); // White Knight - Hop forward, left assertEquals(true, resetBoard.move ( new Coordinate("g8"), new Coordinate ("f6"))); // Black Knight - Hop forward, left assertEquals(true, resetBoard.move( new Coordinate("a3"), new Coordinate ("b1"))); // White Knight - Hop backward, right assertEquals(true, resetBoard.move ( new Coordinate("f6"), new Coordinate ("g8"))); // Black Knight - Hop forward, right assertEquals(false,resetBoard.move( new Coordinate("b1"), new Coordinate ("b3"))); // White Knight - Hop forward (No) assertEquals(false, resetBoard.move ( new Coordinate("b1"), new Coordinate ("d1"))); // White Knight - Hop left (No) assertEquals(false, resetBoard.move ( new Coordinate("b1"), new Coordinate ("a2"))); // White Knight - Hop left-diagonal (No) assertEquals(false, resetBoard.move ( new Coordinate("b1"), new Coordinate ("a2"))); // White Knight - Hop left-diagonal (No)

} }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

import chess.Coordinate;

import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*;

/** * * @author Cheryl */ public class CoordinateTest { public CoordinateTest() { } // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // @Test public void testConstructorGood() { Coordinate c = new Coordinate(7,6); Assert.assertEquals(6, c.getRowNumber()); Assert.assertEquals(7, c.getColumnNumber()); } @Test public void testConstructorColumnTooHigh() { try { Coordinate c = new Coordinate(8,0); fail(); } catch (IndexOutOfBoundsException e) { /* Expected */ } } @Test public void testConstructorRowTooHigh() { try { Coordinate c = new Coordinate(0,8); fail(); } catch (IndexOutOfBoundsException e) { /* Expected */ } } @Test public void testCharConstructorGood() { Coordinate c = new Coordinate('h','7'); Assert.assertEquals('7', c.getRow()); Assert.assertEquals('h', c.getColumn()); Assert.assertEquals(6, c.getRowNumber()); Assert.assertEquals(7, c.getColumnNumber()); } @Test public void testCharConstructorColumnTooHigh() { try { Coordinate c = new Coordinate('i','1'); fail(); } catch (IndexOutOfBoundsException e) { /* Expected */ } } @Test public void testCharConstructorRowTooLow() { try { Coordinate c = new Coordinate('a','0'); fail(); } catch (IndexOutOfBoundsException e) { /* Expected */ } } @Test public void testStringConstructorGood() { Coordinate c = new Coordinate("a1"); Assert.assertEquals('a', c.getColumn()); Assert.assertEquals('1', c.getRow()); Assert.assertEquals(0, c.getColumnNumber()); Assert.assertEquals(0, c.getRowNumber()); } @Test public void testStringConstructorTooLong() { try { Coordinate c = new Coordinate("a0 "); fail(); } catch (IllegalArgumentException e) { /* Expected */ } } @Test public void testStringConstructorTooShort() { try { Coordinate c = new Coordinate("a"); fail(); } catch (IllegalArgumentException e) { /* Expected */ } } @Test public void testRowColumnLow () { Coordinate c = new Coordinate(0,0); Assert.assertEquals('a', c.getColumn()); Assert.assertEquals('1', c.getRow()); } @Test public void testRowColumnHigh () { Coordinate c = new Coordinate(7,7); Assert.assertEquals('h', c.getColumn()); Assert.assertEquals('8', c.getRow()); } @Test public void testName () { Coordinate c = new Coordinate(0,0); String s = c.name(); Assert.assertEquals("a1", s); } @Test public void testtoString () { Coordinate c = new Coordinate(0,0); String s = c.toString(); Assert.assertEquals("(0,0)", s); } }

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import chess.Coordinate; import chess.Piece; import chess.Square; import org.junit.Assert;

import org.junit.Test; import static org.junit.Assert.*;

/** * * @author Cheryl */ public class SquareTest { public SquareTest() { }

// TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // @Test public void testConstructorUnoccupied() { Square s = new Square ( new Coordinate(0,1) ); Assert.assertEquals(0, s.getColumnNumber()); Assert.assertEquals(1, s.getRowNumber()); Assert.assertEquals('a', s.getColumn()); Assert.assertEquals('2', s.getRow()); Assert.assertEquals(s.getPiece(), null); Assert.assertFalse("Square should be unoccupied", s.isOccupied()); } @Test public void testConstructorOccupied() { Piece p = new Piece('p'); Square s = new Square ( new Coordinate(0,1), p ); Assert.assertEquals(0, s.getColumnNumber()); Assert.assertEquals(1, s.getRowNumber()); Assert.assertEquals('a', s.getColumn()); Assert.assertEquals('2', s.getRow()); Assert.assertEquals(p, s.getPiece()); Assert.assertTrue("Square should be occupied", s.isOccupied()); } @Test public void testDeleteUnoccupied() { Square s = new Square ( new Coordinate(0,0) ); Piece p = s.deletePiece(); Assert.assertEquals(null, p); Assert.assertFalse( s.isOccupied() ); } @Test public void testDeleteOccupied() { Piece p = new Piece('p'); Square s = new Square ( new Coordinate(0,0), p ); Piece deletedPiece = s.deletePiece(); Assert.assertEquals(deletedPiece, p); Assert.assertFalse( s.isOccupied() ); } @Test public void testAddUnoccupied() { Square s = new Square ( new Coordinate(0,0) ); Piece p = new Piece('p'); Piece previousPiece = s.addPiece(p); Assert.assertEquals(null, previousPiece); Assert.assertEquals(s.getPiece(), p); Assert.assertTrue( s.isOccupied() ); } @Test public void testAddOccupied() { Piece p = new Piece('p'); Square s = new Square ( new Coordinate(0,0), p ); Piece secondPiece = new Piece('p'); Piece previousPiece = s.addPiece(secondPiece); Assert.assertEquals(p, previousPiece); Assert.assertEquals(s.getPiece(), secondPiece); Assert.assertTrue( s.isOccupied() ); } @Test public void testtoStringUnoccupied () { Square s = new Square ( new Coordinate(0,0) ); String str = s.toString(); Assert.assertEquals("Square(0,0): ", str); } @Test public void testtoStringOccupied () { Piece p = new Piece('p'); Square s = new Square ( new Coordinate(1,0), p ); String str = s.toString(); Assert.assertEquals("Square(1,0):"+p.toString(), str); } }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

THIS ARE THE INSTRUCTIONS FOR THE ASSIGNMENT

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

THANK YOU

SYSC2004 Winter 2018 Assignment 2 Objectives: To learn why we use inheritance To learn how we use inheritance If you did not successfully complete Assignment 1, a solution will be posted for use, after Assignment 1's deadline Background In Assignment 1, we finished off by building the ChessBoard class in which all of our pieces were placed on a board and a few moves were made. We will pick up from there by adding in the constraints for the proper movement of the pieces. Again, chess masters, keep it simple. Reference: From https://www.thespruce.com/rules-of-chess-611533 Chess uses stx pieces, each ofwhich moves in a specific way. All pieces share some common traits No piece is allowed to land on a square occupied by a friendly piece. If a piece lands on a square occupied by an enemy piece, that enemy is captured and removed from the board. With the exception of the knight, pieces are not allowed to jump over other pieces The rook usually looks like a small tower. It moves in a straight line horizontally or vertically for any number of squares. The bishop moves in a straight line diagonally for any mmber of squares. SYSC2004 Winter 2018 Assignment 2 Objectives: To learn why we use inheritance To learn how we use inheritance If you did not successfully complete Assignment 1, a solution will be posted for use, after Assignment 1's deadline Background In Assignment 1, we finished off by building the ChessBoard class in which all of our pieces were placed on a board and a few moves were made. We will pick up from there by adding in the constraints for the proper movement of the pieces. Again, chess masters, keep it simple. Reference: From https://www.thespruce.com/rules-of-chess-611533 Chess uses stx pieces, each ofwhich moves in a specific way. All pieces share some common traits No piece is allowed to land on a square occupied by a friendly piece. If a piece lands on a square occupied by an enemy piece, that enemy is captured and removed from the board. With the exception of the knight, pieces are not allowed to jump over other pieces The rook usually looks like a small tower. It moves in a straight line horizontally or vertically for any number of squares. The bishop moves in a straight line diagonally for any mmber of squares

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions