{"id":6981,"date":"2014-05-16T01:18:20","date_gmt":"2014-05-16T01:18:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/waiting-until-the-player-clicks-a-chess-piece-in-java-collection-of-common-programming-errors\/"},"modified":"2014-05-16T01:18:20","modified_gmt":"2014-05-16T01:18:20","slug":"waiting-until-the-player-clicks-a-chess-piece-in-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/waiting-until-the-player-clicks-a-chess-piece-in-java-collection-of-common-programming-errors\/","title":{"rendered":"Waiting until the player clicks a chess piece in Java-Collection of common programming errors"},"content":{"rendered":"<p>What is the proper way to implement the following code? I want to get the takeTurn() method to wait for the player to click on a button on the grid corresponding to the piece he wants to select. (<code>Button<\/code> objects have instance variables <code>int col, row<\/code> and <code>extend JButton<\/code>.)<\/p>\n<pre><code>int selectedCol, selectedRow;\n\nvoid takeTurn() {\n    System.out.print(name + \"'s turn. \");\n\n    \/\/ Get player to select a piece\n    selectedCol = -1;\n    selectedRow = -1;\n    while (selectedCol == -1 &amp;&amp; selectedRow == -1) {\n        try {\n            wait(500);\n        } catch (InterruptedException e) {\n\n        }\n    }\n    System.out.println(selectedCol + \" \" + selectedRow);\n}\n\n@Override\npublic void actionPerformed(ActionEvent e) {\n    Button b = (Button)e.getSource();\n    selectedCol = b.col;\n    selectedRow = b.row;\n}\n<\/code><\/pre>\n<p>Running gives <code>Exception in thread \"main\" java.lang.IllegalMonitorStateException<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the proper way to implement the following code? I want to get the takeTurn() method to wait for the player to click on a button on the grid corresponding to the piece he wants to select. (Button objects have instance variables int col, row and extend JButton.) int selectedCol, selectedRow; void takeTurn() { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6981","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6981"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6981\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}