import dbg.*; import dbg.GamePackage.*; import org.omg.CORBA.*; class GameImpl extends GamePOA { /** * Contains a thread that checks periodically if both players * and the server are up and running, and takes appropriate action. */ protected Thread pingThread = null; public GameImpl (String testing) { System.out.println(testing); } public GameImpl(){ this(null,null); } public GameImpl(Player player1){ this(player1, null); } public GameImpl(Player player1, Player player2){ this.player1=player1; this.player2=player2; initialize(); } public void startPingThread(){ pingThread.start(); } public Player getPlayer1(){ return player1; } public void setPlayer1(Player player1){ this.player1=player1; } public void setPlayer2(Player player2){ this.player2=player2; // Let the opponents know of each other player1.setOpponent(player2); player2.setOpponent(player1); // Start game startPingThread(); player1.new_position(currentPosition); } public int getNumPlayers(){ if (player1!=null && player2!=null) return 2; if (player1!=null) return 1; if (player2!=null) return -1; //ERROR! return 0; } /** * Called to set all the pieces at one time, * typically called when a new game object is created * to continue an ongoing game from a failed server. */ public void setPosition(position p){ this.currentPosition = p; startPingThread(); } public void place_piece (short a, short b, color c) throws Not_allowed_to_place_now, Not_your_turn, Square_occupied { /* Update the board and send the new position to both players */ // Throw exceptions if state is not as wanted/expected if (currentPosition.whos_turn.value()!=color._white && currentPosition.whos_turn.value()!=color._black){ throw new Not_allowed_to_place_now("Sorry, playing is disabled for the moment!"); } if (currentPosition.whos_turn.value()!=c.value()){ throw new Not_your_turn(currentPosition.whos_turn, "Sorry, not your turn!"); } if (currentPosition.the_board[a][b].value()!=square._none){ throw new Square_occupied( (currentPosition.the_board[a][b].value() ==square._new_white) ? color.white : (currentPosition.the_board[a][b].value() ==square._new_black) ? color.black : null // Shouldn't happen!! ,"Square occupied!" ); } if (player1._non_existent() || player2._non_existent()) throw new Not_allowed_to_place_now("The other player has left!"); // Find out which square was clicked and update the board square clicked_square=null; if (c.value()==color._black){ currentPosition.the_board[a][b] = square.new_black; } else if (c.value()==color._white){ currentPosition.the_board[a][b] = square.new_white; }else{ throw new Not_your_turn("You don't have a valid piece color!", currentPosition.whos_turn, "Error placing piece: "+ "You don't have a valid piece color!"); } // Update whos turn it is if (currentPosition.whos_turn.value()==color._white){ currentPosition.whos_turn = color.black; }else if (currentPosition.whos_turn.value()==color._black){ currentPosition.whos_turn = color.white; }else { throw new Not_allowed_to_place_now("Sorry, playing is disabled for the moment!"); } // Check if any of the clients are lost square walkover_winner=null; if (player1==null){ // Not very flexible, but it's not possible to get the color of // Player 2 programatically: walkover_winner=square.new_black; player2.game_over(walkover_winner); return; }else if (player2==null){ // Not very flexible, but it's not possible to get the color of // Player 1 programatically: walkover_winner=square.new_white; player1.game_over(walkover_winner); return; } // Send the new position to the players player1.new_position(currentPosition); player2.new_position(currentPosition); if(winnerIsDefined(a, b)) { player1.game_over(currentPosition.the_board[a][b]); player2.game_over(currentPosition.the_board[a][b]); } } public void player_finished(Player player){ if (player2 != null){ if (player1._is_equivalent(player)){ player2.game_over(square.none); } if (player2._is_equivalent(player)){ player1.game_over(square.none); } } deactivate(); }//end of player_finished /* Protected methods */ protected void initialize(){ // Initialize the board square [][] board = new square[12][12]; for (int i=0; i < board.length; i++){ for (int j=0; j < board[i].length; j++){ board[i][j] = square.none; } } // White gets to start (as in chess) currentPosition = new position(color.white, board); // Set up the thread that checks if the players and the server is alive this.letMeLive = true; pingThread = new Thread(){ public void run(){ while(true){ checkIfPlayersAreAlive(); try{ sleep(5000); }catch(InterruptedException ie){} } }}; } protected void deactivate(){ //deactivate the object in the orb System.out.println("GameImpl: Deactivating object"); try{ org.omg.PortableServer.POA poa = _default_POA(); byte[] id = poa.servant_to_id(this); poa.deactivate_object(id); }catch(org.omg.PortableServer.POAPackage.ServantNotActive e){ System.out.println(e.getMessage()); e.printStackTrace(); }catch(org.omg.PortableServer.POAPackage.WrongPolicy e){ System.out.println(e.getMessage()); e.printStackTrace(); }catch(org.omg.PortableServer.POAPackage.ObjectNotActive e){ System.out.println(e.getMessage()); e.printStackTrace(); } // stopping pingThread. this.letMeLive = false; } protected void checkIfPlayersAreAlive(){ boolean p1alive = true; boolean p2alive = true; //Check if the players are alive // Need to move the check here, because letMeLive can't // be accessed from the inner anonymous Thread class that // calls this method if it isn't declared final. if (!letMeLive){ return; } try{ p1alive = !player1._non_existent(); }catch(Exception e){ p1alive = false; } try{ p2alive = !player2._non_existent(); }catch(Exception e){ p2alive = false; } // notify other player if (!p1alive){ System.out.println("GameImpl:player1 is dead!"); player2.game_over(square.none); deactivate(); }else if (!p2alive){ System.out.println("GameImpl:player2 is dead!"); player1.game_over(square.none); deactivate(); } } protected boolean winnerIsDefined(int i, int j) { square squareHelper = currentPosition.the_board[i][j]; //Check the X-axes: if ((findOutIf5(squareHelper, i, j, 1, 0) + findOutIf5(squareHelper, i, j, -1, 0) - 1) >= 5 ) { return true; } //Check Y-axes: if ((findOutIf5(squareHelper, i, j, 0, 1) + findOutIf5(squareHelper, i, j, 0, -1) - 1) >= 5) { return true; } //Check the diagonals: // (\) if ((findOutIf5(squareHelper, i, j, 1, 1) + findOutIf5(squareHelper, i, j, -1, -1) - 1) >= 5) { return true; } //(/) if ((findOutIf5(squareHelper, i, j, 1, -1) + findOutIf5(squareHelper, i, j, -1, 1) - 1) >= 5) { return true; } return false; } protected int findOutIf5(square squareHelper, int x, int y, int deltaLength, int deltaHeight) { int ant = 0; // Count until you reach borders of the board and until // there are no more pieces of the defined color: while(x > -1 && y > -1 && x < 12 && y < 12 && currentPosition.the_board[x][y] == squareHelper) { ant++; x += deltaLength; y += deltaHeight; } return ant; } /* Local variables */ protected position currentPosition; protected Player player1; //white player protected Player player2; //black player protected boolean letMeLive = false; }//end of class