diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f0e819f..c5b12db 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,7 +8,9 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/app/src/main/java/com/zduo/dotsandboxes/MainActivity.java b/app/src/main/java/com/zduo/dotsandboxes/MainActivity.java index 05c7e17..27fae3a 100644 --- a/app/src/main/java/com/zduo/dotsandboxes/MainActivity.java +++ b/app/src/main/java/com/zduo/dotsandboxes/MainActivity.java @@ -4,6 +4,8 @@ import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.view.Menu; +import android.view.MenuItem; import android.widget.ImageView; import android.widget.TextView; @@ -18,7 +20,8 @@ public class MainActivity extends AppCompatActivity implements PlayersStateView { protected GameView gameView; - protected TextView player1state, player2state, player1occupying, player2occupying; + protected TextView player1name, player2name, player1state, player2state, player1occupying, + player2occupying; ImageView currentPlayerPointer; Player[] players; Integer[] playersOccupying = new Integer[]{0, 0}; @@ -32,17 +35,19 @@ protected void onCreate(Bundle savedInstanceState) { gameView = (GameView) findViewById(R.id.gameView); gameView.setPlayersState(this); + player1name = (TextView) findViewById(R.id.player1name); + player2name = (TextView) findViewById(R.id.player2name); player1state = (TextView) findViewById(R.id.player1state); player2state = (TextView) findViewById(R.id.player2state); player1occupying = (TextView) findViewById(R.id.player1occupying); player2occupying = (TextView) findViewById(R.id.player2occupying); currentPlayerPointer = (ImageView) findViewById(R.id.playerNowPointer); - startGame(); + players = new Player[]{new HumanPlayer("Human"), new RandomAIPlayer("Computer")}; + startGame(players); } - private void startGame() { - players = new Player[]{new HumanPlayer("Human"), new RandomAIPlayer("Computer")}; + private void startGame(Player[] players) { gameView.startGame(players); updateState(); } @@ -100,4 +105,66 @@ public void onClick(DialogInterface dialogInterface, int i) { } }); } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + + if (id == R.id.action_new) { + runOnUiThread(new Runnable() { + @Override + public void run() { + new AlertDialog.Builder(MainActivity.this) + .setTitle("Dots And Boxes") + .setMessage("New game versus") + .setPositiveButton("Computer", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + new AlertDialog.Builder(MainActivity.this) + .setTitle("Who goes first?") + .setPositiveButton("Computer", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + players = new Player[]{new RandomAIPlayer("Computer"), + new HumanPlayer("Human")}; + startGame(players); + + player1name.setText("Computer"); + player2name.setText("Human"); + } + }) + .setNegativeButton("Human", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + players = new Player[]{new HumanPlayer("Human"), + new RandomAIPlayer("Computer")}; + startGame(players); + + player1name.setText("Human"); + player2name.setText("Computer"); + } + }).show(); + } + }) + .setNeutralButton("Human", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + players = new Player[]{new HumanPlayer("Player 1"), new HumanPlayer("Player 2")}; + startGame(players); + + player1name.setText("Player 1"); + player2name.setText("Player 2"); + } + }).show(); + } + }); + } + + return super.onOptionsItemSelected(item); + } } diff --git a/app/src/main/java/com/zduo/dotsandboxes/model/Game.java b/app/src/main/java/com/zduo/dotsandboxes/model/Graph.java similarity index 98% rename from app/src/main/java/com/zduo/dotsandboxes/model/Game.java rename to app/src/main/java/com/zduo/dotsandboxes/model/Graph.java index b32b4f4..52d1408 100644 --- a/app/src/main/java/com/zduo/dotsandboxes/model/Game.java +++ b/app/src/main/java/com/zduo/dotsandboxes/model/Graph.java @@ -2,7 +2,7 @@ import java.util.Observable; -public class Game extends Observable { +public class Graph extends Observable { private Player[] players; private int currentPlayerIndex; private int width; @@ -12,7 +12,7 @@ public class Game extends Observable { private int[][] verticalLines; private Line latestLine; - public Game(int width, int height, Player[] players) { + public Graph(int width, int height, Player[] players) { this.width = width; this.height = height; this.players = players; diff --git a/app/src/main/java/com/zduo/dotsandboxes/model/Player.java b/app/src/main/java/com/zduo/dotsandboxes/model/Player.java index 9478bfe..d60bd28 100644 --- a/app/src/main/java/com/zduo/dotsandboxes/model/Player.java +++ b/app/src/main/java/com/zduo/dotsandboxes/model/Player.java @@ -2,7 +2,7 @@ public abstract class Player { protected final String name; - protected Game game; + protected Graph game; public Player(String name) { this.name = name; @@ -18,11 +18,11 @@ public static int indexIn(Player player, Player[] players) { public abstract Line move(); - public Game getGame() { + public Graph getGame() { return game; } - public void addToGame(Game game) { + public void addToGame(Graph game) { this.game = game; } diff --git a/app/src/main/java/com/zduo/dotsandboxes/view/GameView.java b/app/src/main/java/com/zduo/dotsandboxes/view/GameView.java index 814606e..213f4ee 100644 --- a/app/src/main/java/com/zduo/dotsandboxes/view/GameView.java +++ b/app/src/main/java/com/zduo/dotsandboxes/view/GameView.java @@ -5,12 +5,13 @@ import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; +import android.util.Log; import android.view.MotionEvent; import android.view.View; import com.zduo.dotsandboxes.R; import com.zduo.dotsandboxes.model.Direction; -import com.zduo.dotsandboxes.model.Game; +import com.zduo.dotsandboxes.model.Graph; import com.zduo.dotsandboxes.model.HumanPlayer; import com.zduo.dotsandboxes.model.Line; import com.zduo.dotsandboxes.model.Player; @@ -31,7 +32,7 @@ public class GameView extends View implements Observer { protected static final float add6 = (float) 9 / 824; protected final int[] playerColors; - protected Game game; + protected Graph game; protected Line move; protected Paint paint; protected PlayersStateView playersState; @@ -58,7 +59,7 @@ public void setPlayersState(PlayersStateView playersState) { } public void startGame(Player[] players) { - game = new Game(5, 5, players); + game = new Graph(5, 5, players); game.addObserver(this); new Thread() { @Override @@ -183,7 +184,11 @@ private void receiveInput(MotionEvent event) { else direction = Direction.VERTICAL; move = new Line(direction, a, b); - ((HumanPlayer) game.currentPlayer()).add(move); + try { + ((HumanPlayer) game.currentPlayer()).add(move); + } catch (Exception e) { + Log.e("GameView", e.toString()); + } } } diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml new file mode 100644 index 0000000..823f854 --- /dev/null +++ b/app/src/main/res/menu/main.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png index cde69bc..a13d1b3 100644 Binary files a/app/src/main/res/mipmap-hdpi/ic_launcher.png and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png index c133a0c..a1fa634 100644 Binary files a/app/src/main/res/mipmap-mdpi/ic_launcher.png and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png index bfa42f0..afaf5d8 100644 Binary files a/app/src/main/res/mipmap-xhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png index 324e72c..741f756 100644 Binary files a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png index aee44e1..97b1fb9 100644 Binary files a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 65cf55e..3e21ed9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,5 @@ Dots and Boxes + + NEW GAME