Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.io.*;
- /**
- * The class that makes the board.
- *
- * @author ------------------
- * @version 2013-09-27
- */
- public class TextMaxitBoard
- {
- private int[][] values;
- private int col;
- private int row;
- private String current = " ";
- private Random random = new Random();
- private int scoreP = 0;
- private int scoreC = 0;
- private int moveCount = 0;
- /**
- * The method to create maxit boards if only a boardsize is indicated.
- * @param boardSize How big the board is going to be
- */
- public TextMaxitBoard(int boardSize)
- {
- values = new int[boardSize][boardSize];
- row = boardSize;
- col = boardSize;
- //Counts through the rows.
- for(int countRow = 0; countRow < boardSize; countRow++)
- {
- //counts through the columns.
- for(int countCol = 0; countCol < boardSize; countCol++)
- {
- values[countCol][countRow] = (random.nextInt(30) + 1);
- }
- }
- }
- /**
- * The method to create maxit boards if a board size and a file are indicated.
- * @param boardSize How big the board will be
- * @param fileName The name of the file that will be used to make the board
- */
- public TextMaxitBoard(int boardSize, String fileName) throws IOException
- {
- values = new int[boardSize][boardSize];
- row = boardSize;
- col = boardSize;
- Scanner scanner = new Scanner(new File(fileName));
- //counts through the rows
- for(int placeRow = 0; placeRow < row; placeRow++)
- {
- //counts through the columns.
- for (int placeCol = 0; placeCol < col; placeCol++)
- {
- values[placeCol][placeRow] = scanner.nextInt();
- }
- }
- }
- /**
- * Prints the board.
- */
- public void printBoard()
- {
- //Print out Score
- System.out.println("Player: " + scoreP + " Computer: " + scoreC);
- //Print out a tab to start the column numbering.
- System.out.print("\t");
- //Printing the column numbering.
- for(int place = 0; place < row; place++)
- {
- System.out.print((char) ( 'a' + place) + "\t");
- }
- System.out.print("\n");
- //prints out the values of the board given the board size.
- for (int place = 0; place < row; place++)
- {
- System.out.print((place) + "\t");
- //Goes through the columns
- for (int colCount = 0; colCount < col; colCount++)
- {
- //If the space currently being printed is the last move
- if ((int)(current.charAt(0) -97) == colCount &&
- (int)(current.charAt(1) -48) == place)
- {
- System.out.print("** \t");
- }
- //If there is no number in this place.
- else if(values[colCount][place] == 0)
- {
- System.out.print(" \t");
- }
- else
- {
- System.out.print(values[colCount][place] + "\t");
- }
- }
- System.out.print("\n");
- }
- }
- /**
- * Gets the value array.
- * @return the array of values.
- */
- public int[][] getValues()
- {
- return values;
- }
- /**
- * Gets the current place.
- * @return the current place.
- */
- public String getCurrent()
- {
- return current;
- }
- /**
- * Adds the score of the move and changes current to the new move.
- * @param move The last move made.
- * @return If the process worked.
- */
- public boolean giveMove(String move)
- {
- //Checks if starting move.
- if(moveCount > 0)
- {
- //Checks for legality of move.
- if(legalMoves().indexOf(move) == -1)
- {
- return false;
- }
- }
- current = move;
- //If the move count is even then the player made the move.
- if (moveCount % 2 == 0)
- {
- scoreP += values[(int)(current.charAt(0) -97)]
- [(int)(current.charAt(1) - 48)];
- }
- else
- {
- scoreC += values[(int)(current.charAt(0) -97)]
- [(int)(current.charAt(1) - 48)];
- }
- values[(int)(current.charAt(0) -97)][(int)(current.charAt(1) - 48)] = 0;
- moveCount++;
- return true;
- }
- /**
- * Checks if the game is over
- * @return if the game is true return true, if not return false.
- */
- public boolean isDone()
- {
- return legalMoves().isEmpty();
- }
- /**
- * Finds and returns all the legal moves
- * @return An array full of the legal moves for the current position
- */
- public ArrayList<String> legalMoves()
- {
- int nextY = (int)(current.charAt(1) - 48);
- ArrayList<String> moves = new ArrayList<String>();
- //For all the values in the game.
- for(int index = 0; index < values.length; index++)
- {
- //If they are in the same row
- if(values[(int)current.charAt(0) - 97][index] != 0)
- {
- moves.add(new String("" + current.charAt(0) + (char)(index + 48)));
- }
- }
- //for all the values in the game
- for(int index = 0; index < values.length; index++)
- {
- //if they are in the same column
- if(values[index][(int)current.charAt(1) - 48] != 0)
- {
- char newChar = (char)('a' + index);
- moves.add(new String("" + newChar + nextY));
- }
- }
- return moves;
- }
- /**
- * Gives human score
- * @return score
- */
- public int getP()
- {
- return scoreP;
- }
- /**
- * Gives computer score
- * @return score
- */
- public int getC()
- {
- return scoreC;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment