Skip to content

Commit

Permalink
Merge pull request #32 from DjLegolas/devel
Browse files Browse the repository at this point in the history
first task
  • Loading branch information
DjLegolas authored May 15, 2017
2 parents 8a1d7e6 + 4030dea commit 8bb28c2
Show file tree
Hide file tree
Showing 39 changed files with 2,747 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .idea/artifacts/Wordiada_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ConsoleUI/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: consoleui.ConsoleUI

4 changes: 0 additions & 4 deletions ConsoleUI/src/ViewScreen.java

This file was deleted.

181 changes: 181 additions & 0 deletions ConsoleUI/src/consoleui/ConsoleHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package consoleui;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import engine.Statistics;
import engine.Status;
import engine.Board.Point;

class ConsoleHandler {

static private int getInput(Scanner scanner) {
int input = 0;
boolean again;
do {
try {
again = false;
input = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Wrong input value. Expected a number.\nPlease try again:");
scanner.next();
again = true;
}
} while (again);
return input;
}

static int showMainMenu() {
Scanner scanner = new Scanner(System.in);
int selectedMenuItem;

System.out.println("Please select an option:");
System.out.println("1. Load game from xml.");
System.out.println("2. Start game.");
System.out.println("3. Show status.");
System.out.println("4. Make a move.");
System.out.println("5. Show statistics.");
System.out.println("6. Exit game.");

selectedMenuItem = getInput(scanner);
return selectedMenuItem;
}

static String getXML() {
Scanner scanner = new Scanner(System.in);

System.out.println("Please enter the path to the XML file:");
return scanner.nextLine();
}

static void showGameStatus(Status status, boolean needFullPrint) {
if (needFullPrint){
System.out.println("Game Status:\n");
}
printBoard(status.getBoard());
String line = "The number of cards ";
if (needFullPrint) {
line += "remaining ";
}
line += "in the Kupa: " + status.getLeftTiles();
System.out.println(line);
if (needFullPrint) {
System.out.println("Current player: " + status.getPlayerName() + "\n");
}
}

static void printBoard(char[][] board) {
int numOfRows = board.length;
int numOfCols = board.length;
int col, row;
StringBuilder line;
StringBuilder boarderLine = new StringBuilder(numOfRows > 9 ? "-----" : "---");
String tmpStr;
for (col = 0; col < numOfCols; col++) {
boarderLine.append("--");
}
System.out.println("Current Board:\n");
boardIndices(numOfCols, numOfRows);
System.out.println(boarderLine);
// print board
for (row = 0; row < numOfRows; row++) {
line = new StringBuilder(numOfRows > 9 && row < 9 ? " " : "");
tmpStr = (row + 1) + "|";
line.append(tmpStr);
for (col = 0; col < numOfCols; col++) {
tmpStr = board[row][col] + "|";
line.append(tmpStr);
}
line.append(row + 1);
System.out.println(line);
System.out.println(boarderLine);
}
boardIndices(numOfCols, numOfRows);
}

private static void boardIndices(int numOfCols, int numOfRows) {
int col;
StringBuilder line = new StringBuilder();
String tmpStr, lineStart = numOfRows > 9 ? " |" : " |";
line.append(lineStart);
if (numOfRows > 9) {
for (col = 1; col <= numOfCols; col++) {
line.append(col % 10 == 0 ? col / 10 + "|" : " |");
}
System.out.println(line);
}
line = new StringBuilder(lineStart);
for (col = 0; col < numOfCols; col++) {
tmpStr = (col + 1) % 10 + "|";
line.append(tmpStr);
}
System.out.println(line);
}

static List<int[]> getPoints(int numOfValues, boolean sizeWasTooShort, List<Point> unshown) {
Scanner scanner = new Scanner(System.in);
int unShownHalfSize = 1;
List<int[]> points = new ArrayList<>();
if (sizeWasTooShort) {
System.out.println("The number of coordinates is too short! Try again...\n");
}
System.out.println("Please enter "+ numOfValues + " of the available coordinates:");
for (Point p: unshown ){
System.out.print(p.printAsStr() + (unShownHalfSize % 10 != 0 ? " " : "\n"));
unShownHalfSize++;
}
System.out.println();
System.out.println("The format is: row col. example: 5 5");
System.out.println("");
for (int i = 0; i < numOfValues; i++) {
int point[] = {-1,-1};
point[0] = getInput(scanner);
point[1] = getInput(scanner);
points.add(point);
}
return points;
}

static String getWord(int tryNum, int maxTries) {
String word;
Scanner scanner = new Scanner(System.in);

System.out.println("Try #" + tryNum + " out of " + maxTries + ":");
System.out.println("If you can build a word from the letters above please write it:");
word = scanner.next();
return word.toUpperCase();
}

static void showStatistics(Statistics stats) {
System.out.println("Game Statistics:\n");
// number of turns
System.out.println("Turns played: " + stats.getNumOfTurns());
// total time played
long time = stats.getTime();
System.out.println("Time passed from game start: " + time / 60 + ":" + String.format("%02d", time % 60));
// cards left
int cardsLeft = stats.getLeftBoxTiles();
System.out.println("Number of cards left: " + cardsLeft);
for (Statistics.Letter letter: stats.getLetters()) {
System.out.println("\t" + letter.getLetter() + " - " + letter.getAmount() + "/" + cardsLeft);
}
// players
long totalWords = stats.getTotalWords();
for (Statistics.PlayerData player: stats.getPlayers()) {
System.out.println("Player " + player.getName());
System.out.println(" Score: " + player.getScore());
System.out.println(" Words:");
for (String word: player.getWords()) {
System.out.println("\t" + word + ": " + stats.getWordCount(word) + "/" + totalWords);
}
}
System.out.println();
}

static void printError(String title, String message) {
System.out.println(title + ":");
System.out.println(message);
System.out.println();
}
}
192 changes: 192 additions & 0 deletions ConsoleUI/src/consoleui/ConsoleUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package consoleui;

import java.util.List;
import engine.GameEngine;
import engine.Statistics;
import engine.exceptions.*;

public class ConsoleUI {
private static GameEngine engine = new GameEngine();
public static void main(String[] args) {
boolean wasEnded = false;
int selectedMenu;
while((selectedMenu = ConsoleHandler.showMainMenu()) != 6 && !wasEnded){
switch (selectedMenu) {
case 1:
getXml();
break;
case 2:
startGame();
break;
case 3:
if (isGameStarted()) {
ConsoleHandler.showGameStatus(engine.getStatus(), true);
}
break;
case 4:
if (isGameStarted()) {
wasEnded = playTurn();
}
break;
case 5:
if (isGameStarted()) {
Statistics stat = engine.getStatistics();
ConsoleHandler.showStatistics(stat);
}
break;
default:
System.out.println("Wrong menu number (need to bo 1-6).");
break;
}
}

System.out.println("---- Game ended ----");
if (engine.isStarted()) {
String winnerName;
if (selectedMenu == 6) {
winnerName = engine.getWinnerName(true);
}
else {
winnerName = engine.getWinnerName(false);
}

char[][] board = engine.getBoard();
ConsoleHandler.printBoard(board);
Statistics stat = engine.getStatistics();
ConsoleHandler.showStatistics(stat);
System.out.println("\n\n----------------------\nTHE WINNER IS: " + winnerName + "!");
}
}

private static void getXml() {
if (engine.isStarted()) {
System.out.println("The game was already started...\n" +
"Unable to load more XML files.\n");
return;
}

boolean needInput = true;
String pathToXml = null;
String message = "";
while (needInput) {
pathToXml = ConsoleHandler.getXML();
try {
engine.loadXml(pathToXml);
needInput = false;
} catch (WrongPathException e) {
message = "Invalid path to XML file.";
} catch (NotXmlFileException e) {
message = "The file \"" + pathToXml + "\" is not an XML file.";
} catch (DictionaryNotFoundException e) {
message = "Unable to use dictionary file \"" + e.getFileName() + "\"";
} catch (DuplicateLetterException e) {
message = "The letter " + e.getLetter() + " appears more than once.";
} catch (BoardSizeException e) {
message = "Expected size is between " + e.getMinSize() + " to " + e.getMaxSize() + ". Got: " + e.getSize();
} catch (NotValidXmlFileException e) {
message = "The XML file \"" + pathToXml + "\"\n" +
"does not contains the information for Wordiada game.";
} catch (WinTypeException e) {
message = "Winning type \"" + e.getWinType() + "\" is not currently supported.";
} catch (NotEnoughLettersException e) {
message = "There of cards is not enough to fill the board.\n" +
"The board needs " + e.getExpectedAmount() + " but there are only " + e.getCurrentAmount();
} catch (Exception e) {
message = "Error, " + e.getMessage();
}
if (needInput) {
ConsoleHandler.printError("XML load failed", message);
}
}
ConsoleHandler.showGameStatus(engine.getStatus(), false);
System.out.println("XML file \"" + pathToXml + "\" loaded successfully!\n");
}

private static void startGame(){
String errTitle = "Starting game failed";
if (!engine.isXmlLoaded()) {
ConsoleHandler.printError(errTitle, "No xml game file was loaded.\n" +
"Please select 1 first to load at least one xml file.\n");
}
else if (engine.isStarted()) {
ConsoleHandler.printError(errTitle, "The game was already started...\n" +
"Please DON'T use this option again.\n");
}
else {
try {
engine.startGame();
} catch (NumberOfPlayersException e) {
ConsoleHandler.printError(errTitle, "The number of players is incorrect.\n" +
"The minimum is " + e.getMinPlayers() + ", the maximum is " + e.getMaxPlayers() +
", but got " + e.getActualNumOfPlayers() + " players.\n");
return;
}
System.out.println("Game started! Have fun :>\n");
}
}

private static boolean playTurn() {

if(engine.isGameEnded()){
return true;
}
boolean listSizeTooShort = false, continueTrying = true, outOfBoarder;
List<int[]> points;
int diceValue = engine.getDiceValue(), tryNumber;
System.out.println("Dice value is " + diceValue);
do {
outOfBoarder = false;
points = ConsoleHandler.getPoints(diceValue, listSizeTooShort, engine.getUnShownPoints());
try {
listSizeTooShort = !engine.updateBoard(points);
} catch (OutOfBoardBoundariesException e) {
ConsoleHandler.printError("Error", "Some of the points you chose are out of boundaries!\n Try again.");
outOfBoarder = true;
}
} while(listSizeTooShort || outOfBoarder);
char[][] board = engine.getBoard();
ConsoleHandler.printBoard(board);
int maxTries = engine.getMaxRetries();
for (tryNumber = 1; continueTrying && tryNumber <= maxTries; tryNumber++) {
String word = ConsoleHandler.getWord(tryNumber, maxTries);
switch (engine.isWordValid(word, tryNumber)) {
case CORRECT:
System.out.println("The word " + word + " is correct!\n");
continueTrying = false;
break;
case WRONG:
System.out.println("Incorrect word!\n");
break;
case TRIES_DEPLETED:
System.out.println("\nNo more retries!");
continueTrying = false;
break;
case CHARS_NOT_PRESENT:
System.out.println("You wrote a word with unavailable characters...");
System.out.println("Please try again...\n");
tryNumber--;
break;
case WRONG_CANT_RETRY:
System.out.println("Incorrect word!\nNo more retries!");
System.out.println("Changing to next player...");
continueTrying = false;
break;
}
}
ConsoleHandler.showGameStatus(engine.getStatus(), true);
return false;
}

private static boolean isGameStarted() {
if (engine.isStarted()) {
return true;
}
System.out.println("The game wasn't started.\n" +
"Please select option 2 to start it" +
(!engine.isXmlLoaded() ? ", after loading at least one xml" : "") +
".\n");
return false;
}


}
3 changes: 0 additions & 3 deletions GameEngine/src/GameDataFromXml.java

This file was deleted.

Loading

0 comments on commit 8bb28c2

Please sign in to comment.