Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone tell me why is this code has following problems : What is the code for checking game is over ????????????????? initialization after game

Can someone tell me why is this code has following problems :

What is the code for checking game is over ?????????????????

initialization after game is complete ????

drawing a duplicate edge from neighboring should return false ??????????

Shared edges must appear in getDrawnEdgesAt of neighbor ???????

private boolean isInitialized = false; private HashMap scoreMap = new HashMap<>(); private HashMap gameGridMap = new HashMap<>(); private Player currPlayer; private int size;

@Override public void init(int size) { if(size < 2){ throw new DABException(); } for(int row = 0; row

@Override public int getSize() { if(!isInitialized){ throw new DABException(); } return size; }

@Override public Set getEdgesAt(int row, int col) { Set usedEdgesSet = new HashSet<>(); Box selectedBox = this.getSelectedBox(row, col); if(selectedBox ==null){ throw new DABException(); } //since this is a snapshot of the edges drawn we must create a new set //and add all the edges to it. usedEdgesSet.addAll(selectedBox.getDrawnEdges()); return usedEdgesSet; }

@Override public Player getOwnerAt(int row, int col) { Box selectedBox = this.getSelectedBox(row, col); return selectedBox.getOwner(); }

@Override public boolean drawEdge(int row, int col, Edge edge) { Box selectedBox = this.getSelectedBox(row, col); //find the adjacent box and draw the edge on opposite side Box adjBox =this.getAdjBox(row, col, edge); boolean isEdgeDrawn = false; boolean isAdjEdgeDrawn = false; //TODO-need to check if game is over too if(selectedBox == null || edge == null || !isInitialized ){ throw new DABException(); } //if the box is already owned then we dont want to draw an edge on it if(selectedBox.getOwner() == null){ isEdgeDrawn = selectedBox.drawEdge(edge, currPlayer); //if selected box is not on the outer edge of the grid if(adjBox != null ){ isAdjEdgeDrawn = adjBox.drawEdge(edge.opposite(), currPlayer); } //if the edge is drawn then next player should go unless player //scored if(selectedBox.getOwner()==null){ currPlayer = currPlayer.next(); }else{ this.addPointToPlayer(currPlayer); //if the adjacent box has an owner and there was a line drawn //on the last turn on that adjacent box then award another point if(adjBox.getOwner()!=null && isAdjEdgeDrawn){ this.addPointToPlayer(currPlayer); } } } return isEdgeDrawn; }

@Override public Map getScores() { if(!isInitialized){ throw new DABException(); } return scoreMap; }

@Override public Player getTurn() { if(!isInitialized){ throw new DABException(); } return currPlayer; } /** * Get the box on the edge side of the selected Box. * * if top edge.TOP chosen then get the box located above the selected box. * This returns null if the adjacent box sits outside of the grid. * * @param row the row within the grid; the top row of squares is 0. * @param col the column within the grid; the left column of squares is 0. * @return a Box that is located on chosen edge direction */ private Box getAdjBox(int row, int col, Edge edge){ Box adjBox = new Box(row, col); if(edge == Edge.BOTTOM){ adjBox = this.getSelectedBox(++row, col); }else if(edge == Edge.TOP){ adjBox = this.getSelectedBox(--row, col); }else if(edge == Edge.LEFT){ adjBox = this.getSelectedBox(row, --col); }else if(edge == Edge.RIGHT){ adjBox = this.getSelectedBox(row, ++col); } return adjBox; } /** * Gets the selected box from the Collection. * * This is a helper method for just searching the collection for the * box that was selected. * * @param row the row within the grid; the top row of squares is 0. * @param col the column within the grid; the left column of squares is 0. * @return the Box that contains the edge that was selected. */ private Box getSelectedBox(int row, int col){ Box selectedBox = gameGridMap.get(new Coordinate(row, col)); return selectedBox; } /** * Add a point to player. * * helper method for just adding a point to a player that scores. * * @param player player to add the point to * @return void because scoreBoard is a member variable, so it just modifies * the variable. */ private void addPointToPlayer(Player player){ //get the previous score of player in map Integer score= scoreMap.get(player);

//add one to that score scoreMap.put(player, ++score); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

_Can we afford to give our employees a pay raise?

Answered: 1 week ago