jdalbey

Flawed Board class

Oct 8th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.io.*;
  6. /**
  7.  * The class that makes the board.
  8.  *
  9.  * @author ------------------
  10.  * @version 2013-09-27
  11.  */
  12. public class TextMaxitBoard
  13. {
  14.     private int[][] values;
  15.     private int col;
  16.     private int row;
  17.     private String current = "  ";
  18.     private Random random = new Random();
  19.     private int scoreP = 0;
  20.     private int scoreC = 0;
  21.     private int moveCount = 0;
  22.     /**
  23.      * The method to create maxit boards if only a boardsize is indicated.
  24.      * @param boardSize How big the board is going to be
  25.      */
  26.     public TextMaxitBoard(int boardSize)
  27.     {
  28.         values = new int[boardSize][boardSize];
  29.         row = boardSize;
  30.         col = boardSize;
  31.         //Counts through the rows.
  32.         for(int countRow = 0; countRow < boardSize; countRow++)
  33.         {
  34.             //counts through the columns.
  35.             for(int countCol = 0; countCol < boardSize; countCol++)
  36.             {
  37.                 values[countCol][countRow] = (random.nextInt(30) + 1);
  38.             }
  39.         }
  40.     }
  41.     /**
  42.      * The method to create maxit boards if a board size and a file are indicated.
  43.      * @param boardSize How big the board will be
  44.      * @param fileName The name of the file that will be used to make the board
  45.      */
  46.     public TextMaxitBoard(int boardSize, String fileName) throws IOException
  47.     {
  48.         values = new int[boardSize][boardSize];
  49.         row = boardSize;
  50.         col = boardSize;
  51.         Scanner scanner = new Scanner(new File(fileName));
  52.         //counts through the rows
  53.         for(int placeRow = 0; placeRow < row; placeRow++)
  54.         {
  55.             //counts through the columns.
  56.             for (int placeCol = 0; placeCol < col; placeCol++)
  57.             {
  58.                 values[placeCol][placeRow] = scanner.nextInt();
  59.             }
  60.         }
  61.     }
  62.     /**
  63.      * Prints the board.
  64.      */
  65.     public void printBoard()
  66.     {
  67.         //Print out Score
  68.         System.out.println("Player: " + scoreP + "    Computer: " + scoreC);
  69.         //Print out a tab to start the column numbering.
  70.         System.out.print("\t");
  71.         //Printing the column numbering.
  72.         for(int place = 0; place < row; place++)
  73.         {
  74.             System.out.print((char) ( 'a' + place) + "\t");
  75.         }
  76.         System.out.print("\n");
  77.         //prints out the values of the board given the board size.
  78.         for (int place = 0; place < row; place++)
  79.         {
  80.             System.out.print((place) + "\t");
  81.             //Goes through the columns
  82.             for (int colCount = 0; colCount < col; colCount++)
  83.             {
  84.                 //If the space currently being printed is the last move
  85.                 if ((int)(current.charAt(0) -97) == colCount &&
  86.                     (int)(current.charAt(1) -48) == place)
  87.                 {
  88.                     System.out.print("** \t");
  89.                 }
  90.                 //If there is no number in this place.
  91.                 else if(values[colCount][place] == 0)
  92.                 {
  93.                     System.out.print(" \t");
  94.                 }
  95.                 else
  96.                 {
  97.                     System.out.print(values[colCount][place] + "\t");
  98.                 }
  99.             }
  100.             System.out.print("\n");
  101.         }      
  102.     }
  103.     /**
  104.      * Gets the value array.
  105.      * @return the array of values.
  106.      */
  107.     public int[][] getValues()
  108.     {
  109.         return values;
  110.     }
  111.     /**
  112.      * Gets the current place.
  113.      * @return the current place.
  114.      */
  115.     public String getCurrent()
  116.     {
  117.         return current;
  118.     }
  119.     /**
  120.      * Adds the score of the move and changes current to the new move.
  121.      * @param move The last move made.
  122.      * @return If the process worked.
  123.      */
  124.     public boolean giveMove(String move)
  125.     {
  126.         //Checks if starting move.
  127.         if(moveCount > 0)
  128.         {
  129.             //Checks for legality of move.
  130.             if(legalMoves().indexOf(move) == -1)
  131.             {
  132.                 return false;
  133.             }
  134.         }
  135.         current = move;
  136.         //If the move count is even then the player made the move.
  137.         if (moveCount % 2 == 0)
  138.         {
  139.             scoreP += values[(int)(current.charAt(0) -97)]
  140.                 [(int)(current.charAt(1) - 48)];
  141.         }
  142.         else
  143.         {
  144.             scoreC += values[(int)(current.charAt(0) -97)]
  145.                 [(int)(current.charAt(1) - 48)];
  146.         }
  147.         values[(int)(current.charAt(0) -97)][(int)(current.charAt(1) - 48)] = 0;
  148.         moveCount++;
  149.         return true;
  150.     }
  151.     /**
  152.      * Checks if the game is over
  153.      * @return if the game is true return true, if not return false.
  154.      */
  155.     public boolean isDone()
  156.     {    
  157.         return legalMoves().isEmpty();
  158.     }
  159.     /**
  160.      * Finds and returns all the legal moves
  161.      * @return An array full of the legal moves for the current position
  162.      */
  163.     public ArrayList<String> legalMoves()
  164.     {
  165.         int nextY = (int)(current.charAt(1) - 48);
  166.         ArrayList<String> moves = new ArrayList<String>();
  167.         //For all the values in the game.
  168.         for(int index = 0; index < values.length; index++)
  169.         {
  170.             //If they are in the same row
  171.             if(values[(int)current.charAt(0) - 97][index] != 0)
  172.             {
  173.                 moves.add(new String("" + current.charAt(0) + (char)(index + 48)));
  174.             }
  175.         }
  176.         //for all the values in the game
  177.         for(int index = 0; index < values.length; index++)
  178.         {
  179.             //if they are in the same column
  180.             if(values[index][(int)current.charAt(1) - 48] != 0)
  181.             {
  182.                 char newChar = (char)('a' + index);
  183.                 moves.add(new String("" + newChar + nextY));
  184.             }
  185.         }
  186.         return moves;
  187.     }
  188.     /**
  189.      * Gives human score
  190.      * @return score
  191.      */
  192.     public int getP()
  193.     {
  194.         return scoreP;
  195.     }
  196.     /**
  197.      * Gives computer score
  198.      * @return score
  199.      */
  200.     public int getC()
  201.     {
  202.         return scoreC;
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment