Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. package peg_Game;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class PegBoard {
  7.     private Hole[] board;
  8.  
  9.     public static void main( String[] argv ) throws FileNotFoundException, IOException {
  10.        
  11.         PegBoard test = new PegBoard( "peg_Game/board1.txt" );
  12.        
  13.        
  14.     }
  15.    
  16.     /** Represents the state of a peg board
  17.      *
  18.      * @param f the file where the state should be read from
  19.      */
  20.     public PegBoard (String f) throws FileNotFoundException, IOException {
  21.        
  22.         board = loadFromFile(new File (f));
  23.         System.out.println( coordToIndex( 1, 3 ) );
  24.     }
  25.  
  26.     /**
  27.      * @return the number of pegs currently left on the game board
  28.      */
  29.     public int getNumPegs () {
  30.         return 0;
  31.     }
  32.  
  33.     /** Determines the current state of a hole
  34.      * @param x is the x coordinate of the hole
  35.      * @param y is the y coordinate of the hole
  36.      * @return whether the hole has a peg in it or not
  37.      */
  38.     public boolean hasPeg (int x, int y) {
  39.         return true;
  40.     }
  41.  
  42.     /**  
  43.      * @param x
  44.      * @param y
  45.      * @return the hole object given a coordinate
  46.      */
  47.     public Hole findHole (int x, int y) {
  48.         return new Hole( 1, 1, false );
  49.     }
  50.  
  51.     /**
  52.      * @return an array of holes that have pegs in them
  53.      */
  54.     /*public Hole[] findPegs () {
  55.         return new Hole( 1, 1, false );
  56.     }*/
  57.  
  58.     /**
  59.      * @return an array of holes that do not have pegs in them
  60.      */
  61.     /*public Hole[] findEmptyHoles () {
  62.         return new Hole( 1, 1, false );
  63.     }*/
  64.  
  65.     /**
  66.      * @param from the hole that the jump is starting from, must have a peg
  67.      * @param to the hole that the peg will end up, must be empty
  68.      * @return true if the jump is valid, false if it is not
  69.      */
  70.     private boolean checkJump (Hole from, Hole to) {
  71.         return true;
  72.     }
  73.  
  74.     /**
  75.      * @param x
  76.      * @param y
  77.      * @return the array index of a hole given its coordinate
  78.      */
  79.     private int coordToIndex(int x, int y) {
  80.        
  81.         int arrayLength = board.length;
  82.         int rows = getRows( arrayLength );
  83.        
  84.         //Go through all the rows before the current one and add up all the indexes, to provide a base for the number.
  85.         int previousRows = 0;
  86.         for( int counter = 0; counter < y; counter++ ) {
  87.            
  88.             previousRows += rows - counter;
  89.            
  90.         }
  91.        
  92.         return previousRows + x;
  93.        
  94.     }
  95.  
  96.     /** Performs an actual jump on the game board, jumps must be valid
  97.      * @param from - the start location of the jump
  98.      * @param to - the finish location of the jump
  99.      * @return true if the jump succeeds, false if it fails
  100.      */
  101.     public boolean jump (Hole from, Hole to) {
  102.         return true;
  103.     }
  104.  
  105.     /** Parses a text file and return an array of holes which represent a game board, this method
  106.      *  is called in the constructor of this class
  107.      * @param file is the text file storing a game board
  108.      * @return an array of holes representing a starting board state
  109.      */
  110.     private Hole[] loadFromFile (File file) throws FileNotFoundException, IOException {
  111.    
  112.         //Load the file into the fileContents string.
  113.         String fileContents = "";
  114.         Scanner scan = new Scanner( file );
  115.         while( scan.hasNextLine() ) {
  116.            
  117.             fileContents += scan.nextLine();
  118.            
  119.         }
  120.        
  121.         //Setup a hole array to fit all the peg holes
  122.         Hole[] holeArray = new Hole[ fileContents.length() ];
  123.         int rows = getRows( fileContents.length() );
  124.        
  125.         //Start going one-by-one and finding the x,y coordinates of each peg.
  126.         int currentRow = 0;
  127.         int currentRowCounter = 0;
  128.         for( int counter = 0; counter < fileContents.length(); counter++ ) {
  129.            
  130.             int y = currentRow;
  131.             int x = currentRowCounter;
  132.             boolean isSet = ( fileContents.charAt( counter ) == '1' ? true : false );
  133.            
  134.             holeArray[ counter ] = new Hole( x, y, isSet );
  135.  
  136.             //move the peg along the imaginary board
  137.             if( currentRowCounter == rows - currentRow - 1 ) { //if true it would be the last peg in this row
  138.                 currentRow++; //move up a row
  139.                 currentRowCounter = 0; //reset to the first peg
  140.             } else {
  141.                 currentRowCounter++; //same row, next peg
  142.             }
  143.            
  144.             System.out.println( "Content: " + fileContents + " Rows: " + rows + "X: " + x + " Y: " + y );
  145.                        
  146.         }
  147.        
  148.         return holeArray;
  149.        
  150.     }
  151.  
  152.     /** Prints the current state of the board to the console, the printing format is the same
  153.      *  as the text file format
  154.      */
  155.     public void printBoard () {
  156.        
  157.     }
  158.  
  159.     /**
  160.      * Creates a deep copy of the current game board.
  161.      */
  162.     /*public PegBoard clone() {
  163.        
  164.     }*/
  165.    
  166.     /** Get the number of rows on the board from the number of pegs used. */
  167.     private int getRows( int numPegs ) {
  168.        
  169.         int perRow = 1;
  170.         int pegsLeft = numPegs;
  171.         int totalRows = 0;
  172.        
  173.         while( pegsLeft > 0 ) {
  174.            
  175.             int inRow = 0;
  176.             while( inRow < perRow ) {
  177.                
  178.                 inRow++;
  179.                 pegsLeft--;
  180.                
  181.             }
  182.            
  183.             totalRows++;
  184.             perRow++;
  185.            
  186.         }
  187.        
  188.         return totalRows;
  189.                
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement