1. import java.util.*;
  2. import javax.swing.JOptionPane;
  3. /**
  4. * Write a description of class GameBoardController here.
  5. *
  6. * @author (your name)
  7. * @version (a version number or a date)
  8. */
  9. public class GameBoardController
  10. {
  11. private GameBoard game ;
  12. private Scanner scan ;
  13. private Stack<GameBoard> undoStack;
  14. public GameBoardController(){
  15. game = new GameBoard();
  16. scan = new Scanner(System.in);
  17. undoStack = new Stack<GameBoard>();
  18. }
  19.  
  20. public void startGame(){
  21. game.populateGameCells();
  22.  
  23.  
  24.  
  25. game.printGameBoard();
  26. gameMenu();
  27. }
  28.  
  29. public void gameMenu(){
  30. char choice;
  31. int row;
  32. int row2;
  33. int finalCellRow;
  34. int holdingCellRow;
  35.  
  36. undoStack.push(new GameBoard(game)); // saving game state here <-----------------------
  37.  
  38.  
  39.  
  40.  
  41. System.out.println("What would you like to do?");
  42. System.out.println("A: Move from stack of cards to another stack of cards\n"
  43. + "B: Move Card from Stack to Holding Row\n"
  44. + "C: Move Card from Holding Row to Stack of Cards\n"
  45. + "D: Move Card from Stacks to Final Row\n"
  46. + "E: Move Card from Holding Row to Final Row\n"
  47. + "F: Undo");
  48.  
  49. choice = scan.next().toUpperCase().charAt(0);
  50.  
  51.  
  52. try{
  53. switch (choice) {
  54.  
  55. case 'A':
  56. System.out.println("Enter Row nubmer to get card from");
  57. row = scan.nextInt();
  58. System.out.println("Enter Row numer to place card on");
  59. row2 = scan.nextInt();
  60. game.rowToRow(row2, row);
  61. break;
  62. case 'B':
  63. System.out.println("Enter Row nubmer to get card from");
  64. row = scan.nextInt();
  65. System.out.println("Enter holding cell nubmer to place card on");
  66. row2 = scan.nextInt();
  67. game.rowToHoldingCell(row2, row);
  68. break;
  69. case 'C':
  70. System.out.println("Enter Holding Row nubmer to get card from");
  71. row = scan.nextInt();
  72. System.out.println("Enter Row of Stack of Card to put card on");
  73. row2 = scan.nextInt();
  74. game.holdingCellToRow(row2, row);
  75. break;
  76. case 'D':
  77. System.out.println("Enter Row nubmer to get card from");
  78. row = scan.nextInt();
  79. System.out.println("Enter final cell nubmer to place card on");
  80. row2 = scan.nextInt();
  81. game.rowToFinalCell(row2, row);
  82. break;
  83. case 'E':
  84. System.out.println("Enter Holding Row nubmer to get card from");
  85. row = scan.nextInt();
  86. System.out.println("Enter Final row number to put card on");
  87. row2 = scan.nextInt();
  88. game.holdingCellToFinalCell(row2, row);
  89. break;
  90. case 'F':
  91. undo();
  92. break;
  93.  
  94.  
  95. default:
  96. JOptionPane.showMessageDialog(null, "Letters are required!");
  97. break;
  98.  
  99. }
  100. }catch(InputMismatchException e){
  101. JOptionPane.showMessageDialog(null, "Numbers are required!");
  102. }
  103.  
  104.  
  105.  
  106. game.printGameBoard();
  107. gameMenu();
  108.  
  109.  
  110. }
  111.  
  112.  
  113. public void undo(){
  114. game = undoStack.pop(); // Changing game state here
  115.  
  116. }
  117.  
  118. }