Created
March 20, 2015 12:01
-
-
Save kvalv/ef2e84656a0b1e4cc55f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package oving7.sudoku; | |
| import java.util.Scanner; | |
| import java.util.Random; | |
| import java.util.Stack; | |
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.io.PrintWriter; | |
| import java.util.ArrayList; | |
| import oving7.sudoku.Board; | |
| public class SudokuGame { | |
| Scanner inputScan = new Scanner(System.in); | |
| SudokuBoard sudokuBoard = new SudokuBoard(game_getSudokuGame()); | |
| String inputString; | |
| static private ArrayList <String> gameMoves = new ArrayList <String>(); | |
| static private Stack <String> regretMoves = new Stack <String>(); | |
| private boolean play = true; | |
| void game_runGame() throws FileNotFoundException{ | |
| game_displayGameInfo(); | |
| while( play ){ | |
| sudokuBoard.printBoard(); | |
| inputString = inputScan.nextLine().toLowerCase(); | |
| switch ( inputString ) { | |
| case "save": | |
| game_saveGame(); | |
| System.out.println("game saved."); | |
| break; | |
| case "regret": { | |
| String regretMove = gameMoves.get( gameMoves.size() - 1 ); | |
| char[] toChar = Character.toChars( Integer.parseInt(regretMove.substring(1, 2)) + 97 /*UTF-16 has 97 for letter 'a' */ ); | |
| inputString = "-" + Integer.parseInt(regretMove.substring( 0, 1 ) ) + Character.toString(toChar[0]); | |
| game_addToRegretMoves( regretMove ); | |
| break; | |
| } | |
| case "forward": { | |
| if ( ! regretMoves.isEmpty() ) { | |
| String stringCopy = regretMoves.pop(); | |
| char[] toChar = Character.toChars( Integer.parseInt(stringCopy.substring(1, 2)) + 97 /*UTF-16 has 97 for letter 'a' */ ); | |
| inputString = stringCopy.substring(0, 1) + Character.toString(toChar[0]) + stringCopy.substring(2, 3); | |
| } else { | |
| System.out.println("no forward action possible"); | |
| } | |
| break; | |
| } | |
| case "quit": | |
| play = false; | |
| break; | |
| } | |
| if ( inputString.charAt(0) == '-' ) { | |
| int inputRow = Integer.parseInt( inputString.substring(1,2) ); | |
| int inputColumn = Character.getNumericValue( inputString.charAt(2) ) - 10; | |
| // updates the saveFile | |
| int entryToBeReplaced = 0; | |
| try { | |
| if ( sudokuBoard.getEntry(inputRow, inputColumn) != "." ) { | |
| entryToBeReplaced = Integer.parseInt(sudokuBoard.getEntry( inputRow, inputColumn ).substring(0, 1) ); | |
| int index = gameMoves.indexOf(Integer.toString(inputRow) + Integer.toString(inputColumn) + Integer.toString(entryToBeReplaced ) ); | |
| gameMoves.remove(index); | |
| } | |
| } catch (Exception e){ System.out.println("something went wrong");} | |
| sudokuBoard.sudoku_clearEntry( inputRow, inputColumn ); | |
| } else if ( inputString.length() == 3 ) { | |
| int inputRow = Integer.parseInt(inputString.substring(0, 1)); | |
| int inputColumn = Character.getNumericValue(inputString.charAt(1)) - 10; //API is being a fuck up here. remove 10. | |
| int inputValue = Integer.parseInt(inputString.substring(2,3)); | |
| sudokuBoard.sudoku_overwriteEntry(inputRow, inputColumn, inputValue); | |
| game_addToGameMoves(Integer.toString(inputRow) + Integer.toString(inputColumn) + Integer.toString(inputValue) ); | |
| } else { | |
| System.out.println("no furher action."); | |
| } | |
| sudokuBoard.checkAnyInconsistentNumber(9, 9); | |
| game_clearScreen(); | |
| } | |
| } | |
| static void game_addToGameMoves( String newMove ) { | |
| gameMoves.add( newMove ); | |
| } | |
| static void game_addToRegretMoves ( String newMove ) { | |
| regretMoves.push( newMove ); | |
| } | |
| static void game_saveGame () throws FileNotFoundException { | |
| File file = new File ("C:/Users/Mikael-pc/workspace/oop/src/oving7/sudoku/saveFile.txt"); | |
| PrintWriter printWriter = new PrintWriter ( file ); | |
| for ( int i = 0; i < gameMoves.size(); i++ ) { | |
| printWriter.println ( gameMoves.get(i) ); | |
| } | |
| printWriter.close(); | |
| System.out.println("done"); | |
| } | |
| void game_displayGameInfo(){ | |
| System.out.println("row = [NUM 1 - 9], column = [a - i] value = [NUM 1 - 9]"); | |
| System.out.println("Game is zero-indexed."); | |
| } | |
| String game_getSudokuGame(){ | |
| String[] gamePick = {".....2..38.273.45....6..87.9.8..5367..6...1..4513..9.8.84..3....79.512.62..8.....", | |
| ".68.257.3..........71..39..61.35.2...8.....4...3.64.95..76..58..........8.653.42.", | |
| ".....59.4.8..9.6.5..6....3..3.7.145...8.4.7...742.6.9..6....3..8.1.6..7.3.98.....", | |
| "...6...513....2..66...3..89..4.2.6...3.418.2...8.7.1..59..6...38..3....241...9..." | |
| }; | |
| Random randomVal = new Random(); | |
| return gamePick[randomVal.nextInt(4)]; | |
| } | |
| void game_clearScreen() { | |
| for ( int i = 0; i < 25; i++ ) { | |
| System.out.println("\n"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment