Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Correct answer for this Java problem will get thumbs up and eternal thanks Problem Description Over the course of this semester you will write a

Correct answer for this Java problem will get thumbs up and eternal thanks

Problem Description

Over the course of this semester you will write a chess game database that will import chess games in PGN (http://www.saremba.de/chessgml/standards/pgn/pgn- complete.htm) format. As a first step you will write code to read PGN games and resolve board positions.

Solution Description

Write a class called PgnReader that contains the following public static methods:

tagValue takes two String arguments: a tag name and a String which contains a PGN-formatted game, and returns a String containing the value from the tag name tag pair in the PGN game text. If there is no tag name tag pair, return "NOT GIVEN" . finalPosition takes a single String argument which contains a PGN-formatted game and returns a String containing the games final position in Forsyth-

Edwards Notation (FEN (http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm#c16.1)).

Write a main method that reads the file named in the PgnReader s first command-line argument into a String and uses that String as the argument to each method above in order to print game information to the console. First, print the tag names and associated values for the core seven tags of the PGN standard: Event, Site, Date, Round, White, Black, Result. Then print a line reading Final Position: and a line displaying the final game position in FEN. Note that in this assignment we only care about the piece placement data, not the other elements of FEN such as active color or castling availability.

Each PGN file will contain a single game and you may assume that the PGN files are valid, and the move text contains only moves, no annotation text. Moves may end in check symbols (+) or strength judgements (!, ?).

As your program reads the moves in a game it will need to maintain the state of the board, which you should store in a 2-d array. You will also need to translate between the algebraic notation used to represent moves in PGN, and the internal representation you use for board state, e.g., array indices.

You may use this skeleton file which contains a completed main method and code to read a file and return its content as a String : PgnReader.java. This skeleton file also contains stubbed tagValue and finalPosition methods. A stubbed method is a method that returns a dummy type-correct value (if applicable) so that you can successfully compile code that uses the method. Stubbed methods are useful in incremental program development. You will want to write many helper methods.

image text in transcribed

Grading

There are 20 bonus points on this assignment.

50 points for correctly extracting tag values. 10 points for correctly finding final position of simple games or openings , scholars-mate.pgn 10 points for correctly finding final position of games that contain castling moves 10 points for correctly finding final position of games that contain pawn promotions 10 points for correctly finding final position of games that contain en passant pawn captures 10 points for correctly finding final position of games that contain moves requiring disambiguation of starting file or rank (but not both) to distinguish between two pieces that could make the same move 10 points for correctly finding final position of games that contain moves requiring disambiguation of starting file and rank to distinguish between two pieces that could make the same move 10 points for correctly finding final position of games that require knowledge of tactics (e.g., pinned pieces) to disambiguate to distinguish between two pieces that could make the same move

---------------------------------------------------------------------------------------Necessary files---------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------PgnReader.java---------------------------------------------------------------------------------------

import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class PgnReader { /** * Find the tagName tag pair in a PGN game and return its value. * * @see http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm * * @param tagName the name of the tag whose value you want * @param game a `String` containing the PGN text of a chess game * @return the value in the named tag pair */ public static String tagValue(String tagName, String game) { return "NOT GIVEN"; } /** * Play out the moves in game and return a String with the game's * final position in Forsyth-Edwards Notation (FEN). * * @see http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm#c16.1 * * @param game a `Strring` containing a PGN-formatted chess game or opening * @return the game's final position in FEN. */ public static String finalPosition(String game) { return ""; } /** * Reads the file named by path and returns its content as a String. * * @param path the relative or abolute path of the file to read * @return a String containing the content of the file */ public static String fileContent(String path) { Path file = Paths.get(path); StringBuilder sb = new StringBuilder(); try (BufferedReader reader = Files.newBufferedReader(file)) { String line = null; while ((line = reader.readLine()) != null) { // Add the that's removed by readline() sb.append(line + " "); } } catch (IOException e) { System.err.format("IOException: %s%n", e); System.exit(1); } return sb.toString(); } public static void main(String[] args) { String game = fileContent(args[0]); System.out.format("Event: %s%n", tagValue("Event", game)); System.out.format("Site: %s%n", tagValue("Site", game)); System.out.format("Date: %s%n", tagValue("Date", game)); System.out.format("Round: %s%n", tagValue("Round", game)); System.out.format("White: %s%n", tagValue("White", game)); System.out.format("Black: %s%n", tagValue("Black", game)); System.out.format("Result: %s%n", tagValue("Result", game)); System.out.println("Final Position:"); System.out.println(finalPosition(game)); } } 

---------------------------------------------------------------------------------------fegatello.pgn---------------------------------------------------------------------------------------

[Event "Fegatello Attack"]

1. e4 e5 2. Nf3 Nc6 3. Bc4 Nf6 4. Ng5 d5 5. exd5 Nxd5 6. Nxf7

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

Using the fegatello.pgn (/fall2017/hw1/fegatello.pgn), a shell session with your program would look like this: $ java PgnReader fegatello.pgn Event: Fegatello Attack Site: NOT GIVEN Date: NOT GIVEN Round: NOT GIVEN White: NOT GIVEN Black: NOT GIVEN Result: NOT GIVEN Final Position: r1bqkb1r/ppp2Npp/2n5/3np3/2B5/8/PPPP1PPP/RNBOK2R Domain Knowledge You don't need to know how to play chess, you only need to know how the pieces and pawns move and how to record chess moves. Use the following links for this purpose: Learn to Play Chess (http://www.chesscorner.com/tutorial/learn.htm) - You only need to read the information under "Rules of Chess." Chess Notation (http://www.chesscorner.com/tutorial/basicotationotate.htm) look at abbreviated algebraic notation. And, of course, you need to know the PGN Standard (http://www.saremba.de/chessgm/standards/pgn/pgn-complete.htm). You only need sections 2.3, 8.1-8.2.3.6, 16.1.3.1 and 16.1.4. PGN is simple, and you can learn it well enough by simply looking at example PGN games

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions