Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package peg_Game;
- import java.io.*;
- import java.util.Scanner;
- public class PegBoard {
- private Hole[] board;
- public static void main( String[] argv ) throws FileNotFoundException, IOException {
- PegBoard test = new PegBoard( "peg_Game/board1.txt" );
- }
- /** Represents the state of a peg board
- *
- * @param f the file where the state should be read from
- */
- public PegBoard (String f) throws FileNotFoundException, IOException {
- board = loadFromFile(new File (f));
- System.out.println( coordToIndex( 1, 3 ) );
- }
- /**
- * @return the number of pegs currently left on the game board
- */
- public int getNumPegs () {
- return 0;
- }
- /** Determines the current state of a hole
- * @param x is the x coordinate of the hole
- * @param y is the y coordinate of the hole
- * @return whether the hole has a peg in it or not
- */
- public boolean hasPeg (int x, int y) {
- return true;
- }
- /**
- * @param x
- * @param y
- * @return the hole object given a coordinate
- */
- public Hole findHole (int x, int y) {
- return new Hole( 1, 1, false );
- }
- /**
- * @return an array of holes that have pegs in them
- */
- /*public Hole[] findPegs () {
- return new Hole( 1, 1, false );
- }*/
- /**
- * @return an array of holes that do not have pegs in them
- */
- /*public Hole[] findEmptyHoles () {
- return new Hole( 1, 1, false );
- }*/
- /**
- * @param from the hole that the jump is starting from, must have a peg
- * @param to the hole that the peg will end up, must be empty
- * @return true if the jump is valid, false if it is not
- */
- private boolean checkJump (Hole from, Hole to) {
- return true;
- }
- /**
- * @param x
- * @param y
- * @return the array index of a hole given its coordinate
- */
- private int coordToIndex(int x, int y) {
- int arrayLength = board.length;
- int rows = getRows( arrayLength );
- //Go through all the rows before the current one and add up all the indexes, to provide a base for the number.
- int previousRows = 0;
- for( int counter = 0; counter < y; counter++ ) {
- previousRows += rows - counter;
- }
- return previousRows + x;
- }
- /** Performs an actual jump on the game board, jumps must be valid
- * @param from - the start location of the jump
- * @param to - the finish location of the jump
- * @return true if the jump succeeds, false if it fails
- */
- public boolean jump (Hole from, Hole to) {
- return true;
- }
- /** Parses a text file and return an array of holes which represent a game board, this method
- * is called in the constructor of this class
- * @param file is the text file storing a game board
- * @return an array of holes representing a starting board state
- */
- private Hole[] loadFromFile (File file) throws FileNotFoundException, IOException {
- //Load the file into the fileContents string.
- String fileContents = "";
- Scanner scan = new Scanner( file );
- while( scan.hasNextLine() ) {
- fileContents += scan.nextLine();
- }
- //Setup a hole array to fit all the peg holes
- Hole[] holeArray = new Hole[ fileContents.length() ];
- int rows = getRows( fileContents.length() );
- //Start going one-by-one and finding the x,y coordinates of each peg.
- int currentRow = 0;
- int currentRowCounter = 0;
- for( int counter = 0; counter < fileContents.length(); counter++ ) {
- int y = currentRow;
- int x = currentRowCounter;
- boolean isSet = ( fileContents.charAt( counter ) == '1' ? true : false );
- holeArray[ counter ] = new Hole( x, y, isSet );
- //move the peg along the imaginary board
- if( currentRowCounter == rows - currentRow - 1 ) { //if true it would be the last peg in this row
- currentRow++; //move up a row
- currentRowCounter = 0; //reset to the first peg
- } else {
- currentRowCounter++; //same row, next peg
- }
- System.out.println( "Content: " + fileContents + " Rows: " + rows + "X: " + x + " Y: " + y );
- }
- return holeArray;
- }
- /** Prints the current state of the board to the console, the printing format is the same
- * as the text file format
- */
- public void printBoard () {
- }
- /**
- * Creates a deep copy of the current game board.
- */
- /*public PegBoard clone() {
- }*/
- /** Get the number of rows on the board from the number of pegs used. */
- private int getRows( int numPegs ) {
- int perRow = 1;
- int pegsLeft = numPegs;
- int totalRows = 0;
- while( pegsLeft > 0 ) {
- int inRow = 0;
- while( inRow < perRow ) {
- inRow++;
- pegsLeft--;
- }
- totalRows++;
- perRow++;
- }
- return totalRows;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement