Advertisement
kivaari

Untitled

Jan 6th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.67 KB | None | 0 0
  1. package go;
  2.  
  3. public class Game {
  4.     private Go_IO IOInterface;
  5.     private Board GameBoard;
  6.     private Player player1;
  7.     private Player player2;
  8.     private AnalysisData player1AnalysisData;
  9.     private AnalysisData player2AnalysisData;
  10.     private int currentTurn;
  11.     private boolean configured;
  12.     private int capturedStonesPlayer1;
  13.     private int capturedStonesPlayer2;
  14.     private Originator originator;
  15.     private Caretaker caretaker;
  16.     public static final int DEBUGLEVEL = 0;
  17.     public static final Class IOTYPE = Go_ConsoleIO.class;
  18.  
  19.  
  20.     // constructor
  21.     public Game() {
  22.         // create instance of Go_IO class, initialize other objects/variables
  23.         try {
  24.             IOInterface = Go_IOFactory.getIOFactoryInstance().create(IOTYPE);
  25.         } catch (GoIOInstanceException e) {
  26.             e.printStackTrace();
  27.         }
  28.         GameBoard = null;
  29.         player1 = null;
  30.         player2 = null;
  31.         player1AnalysisData = new AnalysisData();
  32.         player2AnalysisData = new AnalysisData();
  33.         currentTurn = 0;
  34.         configured = false;
  35.         capturedStonesPlayer1 = 0;
  36.         capturedStonesPlayer2 = 0;
  37.         originator = null;
  38.         caretaker = null;
  39.     }
  40.  
  41.     public static void main(String args[]){
  42.         Game game = new Game();
  43.  
  44.         game.startGame();
  45.     }
  46.  
  47.     // start this method from main to start the game
  48.     public void startGame() {
  49.         while (true) {
  50.             // print main menu and save menu choice in "menu"
  51.             int menu = IOInterface.getMenu();
  52.  
  53.             // start the game
  54.             if (menu == 1) {
  55.                 if (!configured) {
  56.                     getDefaultConfiguration();
  57.                     configured = true;
  58.                 }
  59.                 originator = new Originator();
  60.                 caretaker = new Caretaker();
  61.                 configured = false;
  62.                 gameLoop();
  63.             }
  64.             // configure the game
  65.             else if (menu == 2) {
  66.                 getConfiguration();
  67.                 configured = true;
  68.             }
  69.         }
  70.     }
  71.  
  72.     // get game configuration (dimension, human/ai player) from user
  73.     private void getConfiguration() {
  74.         // get board dimension
  75.         GameBoard = new Board(IOInterface.getDimension());
  76.         // initialize both players valid plays (all plays are valid in the beginning)
  77.         player1AnalysisData.initValidPlays(GameBoard.getDimension());
  78.         player2AnalysisData.initValidPlays(GameBoard.getDimension());
  79.         // pass board to IO
  80.         IOInterface.setBoard(GameBoard);
  81.  
  82.         // get players
  83.         int player;
  84.         // get player1 (player1 [black] starts the game)
  85.         player = IOInterface.getPlayer(1);
  86.         if (player == 1) {
  87.             // create human player
  88.             player1 = new Human(1);
  89.         }
  90.         else if (player == 2) {
  91.             // create AI player
  92.             player1 = new AI(1);
  93.         }
  94.         // get player2
  95.         player = IOInterface.getPlayer(2);
  96.         if (player == 1) {
  97.             // create human player
  98.             player2 = new Human(2);
  99.         }
  100.         else if (player == 2) {
  101.             // create AI player
  102.             player2 = new AI(2);
  103.         }
  104.  
  105.         // pass board and IO to both players
  106.         player1.setBoard(GameBoard);
  107.         player2.setBoard(GameBoard);
  108.         // pass IO to both players  ----> players can now get their own IO instance
  109.         //player1.setIO(IOInterface);
  110.         //player2.setIO(IOInterface);
  111.         // initialize variables
  112.         capturedStonesPlayer1 = 0;
  113.         capturedStonesPlayer2 = 0;
  114.         currentTurn = 0;
  115.     }
  116.  
  117.     // default configuration: dimension = 9, HUMAN vs. AI
  118.     private void getDefaultConfiguration() {
  119.         // set board dimension
  120.         final int DEFAULTDIMENSION = 9;
  121.         GameBoard = new Board(DEFAULTDIMENSION);
  122.         player1AnalysisData.initValidPlays(DEFAULTDIMENSION);
  123.         player2AnalysisData.initValidPlays(DEFAULTDIMENSION);
  124.         // pass board to IO
  125.         IOInterface.setBoard(GameBoard);
  126.  
  127.         // get starting player
  128.         int startingPlayer = IOInterface.getStartingPlayer();
  129.  
  130.         // set players
  131.         if (startingPlayer == 1) {
  132.             player1 = new Human(1);
  133.             player2 = new AI(2);
  134.         }
  135.         else {
  136.             player1 = new AI(1);
  137.             player2 = new Human(2);
  138.         }
  139.  
  140.         // pass board to both players
  141.         player1.setBoard(GameBoard);
  142.         player2.setBoard(GameBoard);
  143.         // pass IO to both players  ---> players can now get their own IO instance
  144.         //player1.setIO(IOInterface);
  145.         //player2.setIO(IOInterface);
  146.         // initialize variables
  147.         capturedStonesPlayer1 = 0;
  148.         capturedStonesPlayer2 = 0;
  149.         currentTurn = 0;
  150.     }
  151.  
  152.  
  153.     private void gameLoop() {
  154.         Boolean rungame = true;
  155.         int currentPlayer = 1;
  156.         save();
  157.  
  158.         while (rungame) {
  159.             if (DEBUGLEVEL == 1) {
  160.                 // debug();
  161.             }
  162.  
  163.             switch (currentPlayer) {
  164.                 case 1:
  165.                     IOInterface.printTurn(currentTurn);
  166.                     IOInterface.printCapturedStones(capturedStonesPlayer1, capturedStonesPlayer2);
  167.                     IOInterface.printBoard();
  168.                     currentPlayer = makeTurn(currentPlayer);
  169.                     break;
  170.                 case 2:
  171.                     IOInterface.printTurn(currentTurn);
  172.                     IOInterface.printCapturedStones(capturedStonesPlayer1, capturedStonesPlayer2);
  173.                     IOInterface.printBoard();
  174.                     currentPlayer = makeTurn(currentPlayer);
  175.                     break;
  176.                 case -3:
  177.                     // both passed
  178.                     endGame();
  179.                     rungame = false;
  180.                     break;
  181.                 case -2:
  182.                     // player2 forfeited
  183.                     endGameForfeit(2);
  184.                     rungame = false;
  185.                     break;
  186.                 case -1:
  187.                     // player1 forfeited
  188.                     endGameForfeit(1);
  189.                     rungame = false;
  190.                     break;
  191.             }
  192.         }
  193.  
  194.     }
  195.  
  196.     private int makeTurn(int player) {
  197.         currentTurn++;
  198.         // player1 turn
  199.         if (player == 1) {
  200.             // get player1Status
  201.             player1AnalysisData = player1.playTurn(player2AnalysisData.getValidPlays());
  202.  
  203.             switch (player1AnalysisData.getPlayerStatus()) {
  204.                 // normal turn
  205.                 case 0:
  206.                     capturedStonesPlayer1 += player1AnalysisData.getCapturedStones();
  207.                     save();
  208.                     return 2;
  209.                 // pass
  210.                 case 1:
  211.                     // if both passed
  212.                     if (player2AnalysisData.getPlayerStatus() == 1) {
  213.                         // end the game
  214.                         return -3;
  215.                     }
  216.                     save();
  217.                     return 2;
  218.                 // forfeit
  219.                 case 2:
  220.                     return -1;
  221.                 // undo
  222.                 case 3:
  223.                     // undo not allowed in turn 1 and 2
  224.                     if (currentTurn <= 2) {
  225.                         IOInterface.printUndoError();
  226.                         currentTurn--;  //
  227.                     }
  228.                     else {
  229.                         undo();
  230.                     }
  231.                     return 1;
  232.                 // no more valid plays
  233.                 case 4:
  234.                     IOInterface.printNoMoreValidPlaysMessage(1);
  235.                     // if both passed
  236.                     if (player2AnalysisData.getPlayerStatus() == 1) {
  237.                         // end the game
  238.                         return -3;
  239.                     }
  240.                     save();
  241.                     return 2;
  242.             }
  243.         }
  244.  
  245.         // player2 turn
  246.         else if (player == 2) {
  247.             // get player2Status
  248.             player2AnalysisData = player2.playTurn(player1AnalysisData.getValidPlays());
  249.  
  250.             switch (player2AnalysisData.getPlayerStatus()) {
  251.                 // normal turn
  252.                 case 0:
  253.                     capturedStonesPlayer2 += player2AnalysisData.getCapturedStones();
  254.                     save();
  255.                     return 1;
  256.                 // pass
  257.                 case 1:
  258.                     // if both passed
  259.                     if (player1AnalysisData.getPlayerStatus() == 1) {
  260.                         // end the game
  261.                         return -3;
  262.                     }
  263.                     save();
  264.                     return 1;
  265.                 // forfeit
  266.                 case 2:
  267.                     return -2;
  268.                 // undo
  269.                 case 3:
  270.                     // undo not allowed in turn 1 and 2
  271.                     if (currentTurn <= 2) {
  272.                         IOInterface.printUndoError();
  273.                         currentTurn--;
  274.                     }
  275.                     else {
  276.                         undo();
  277.                     }
  278.                     return 2;
  279.                 // no more valid plays
  280.                 case 4:
  281.                     IOInterface.printNoMoreValidPlaysMessage(2);
  282.                     // if both passed
  283.                     if (player2AnalysisData.getPlayerStatus() == 1) {
  284.                         // end the game
  285.                         return -3;
  286.                     }
  287.                     save();
  288.                     return 1;
  289.             }
  290.         }
  291.  
  292.         return 0;
  293.     }
  294.  
  295.     private void undo() {
  296.         Memento last = caretaker.getLastMemento();
  297.         originator.restoreFromMemento(last);
  298.         GameBoard.overwrite(last.getSavedBoard());
  299.         player1AnalysisData.setValidPlays(GameBoard.getValidPlays(2));
  300.         player2AnalysisData.setValidPlays(GameBoard.getValidPlays(1));
  301.         GameBoard.getValidPlays(2);
  302.         currentTurn = last.getSavedTurn();
  303.         capturedStonesPlayer1 = last.getSavedCapturedStonesForPlayer1();
  304.         capturedStonesPlayer2 = last.getSavedCapturedStonesForPlayer2();
  305.     }
  306.  
  307.     private void save() {
  308.         // make deep copy of the current GameBoard and save that copy instead of the current board
  309.         Board GameBoardCopy = new Board(GameBoard);
  310.         originator.set(GameBoardCopy, currentTurn, capturedStonesPlayer1, capturedStonesPlayer2);
  311.         Memento memento = originator.storeInMemento();
  312.         caretaker.addMemento(memento);
  313.     }
  314.  
  315.     private void endGame() {
  316.         double player1score = capturedStonesPlayer1;
  317.         double player2score = 6.5 + capturedStonesPlayer2;
  318.  
  319.         IOInterface.printEndGameMessage(player1score, player2score);
  320.  
  321.         IOInterface.printBackToMenuMessage();
  322.     }
  323.  
  324.     private void endGameForfeit(int player) {
  325.         IOInterface.printForfeitMessage(player);
  326.         IOInterface.printBackToMenuMessage();
  327.     }
  328.  
  329.     /* private void debug() {
  330.         System.err.print("savedMementos.size() = " + caretaker.savedMementosSize());
  331.         System.err.println();
  332.         System.err.print("Valid plays BLACK: ");
  333.         for (int play : GameBoard.getValidPlays(1)) {
  334.             System.err.print(play + " ");
  335.         }
  336.         System.err.println();
  337.         System.err.print("Valid plays WHITE: ");
  338.         for (int play : GameBoard.getValidPlays(2)) {
  339.             System.err.print(play + " ");
  340.         }
  341.         System.err.println();
  342.         System.err.print("Board Belegung: ");
  343.         for (int i = 0; i < GameBoard.getDimension()*GameBoard.getDimension(); i++) {
  344.             System.err.print(GameBoard.getStone(i).getColor() + " ");
  345.         }
  346.         System.err.println();
  347.     } */
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement