Skip to content

Commit

Permalink
Added game modes
Browse files Browse the repository at this point in the history
  • Loading branch information
adithya321 committed Jun 24, 2016
1 parent 6280579 commit 3dc2145
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 14 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
75 changes: 71 additions & 4 deletions app/src/main/java/com/zduo/dotsandboxes/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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};
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/zduo/dotsandboxes/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/zduo/dotsandboxes/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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());
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_new"
android:title="@string/action_new"
app:showAsAction="always" />
</menu>
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Dots and Boxes</string>

<string name="action_new">NEW GAME</string>
</resources>

0 comments on commit 3dc2145

Please sign in to comment.